Added usage graph (ADSL), improved logging for usage collection (ADSL)

This commit is contained in:
Deon George
2021-02-18 00:22:50 +11:00
parent 338296982b
commit a301fa7fc0
11 changed files with 258 additions and 115 deletions

View File

@@ -29,7 +29,13 @@ class AdslSupplier extends Model
/** METHODS **/
public function traffic_mismatch(Carbon $date): Collection
/**
* Return the traffic records, that were not matched to a service.
*
* @param Carbon $date
* @return Collection
*/
public function trafficMismatch(Carbon $date): Collection
{
return AdslTraffic::where('date',$date->format('Y-m-d'))
->where('supplier_id',$this->id)

View File

@@ -28,6 +28,8 @@ class Product extends Model
protected $with = ['descriptions'];
/* RELATIONS */
public function descriptions()
{
return $this->hasMany(ProductTranslate::class);
@@ -48,6 +50,8 @@ class Product extends Model
return $this->morphTo(null,'model','prod_plugin_data');
}
/* ATTRIBUTES */
/**
* Get the service category (from the product)
*
@@ -161,6 +165,17 @@ class Product extends Model
return Arr::get($this->price_array,sprintf('%s.1.price_setup',$this->price_recurr_default))*1.1;
}
/**
* Return if this product captures usage data
*
* @return bool
*/
public function hasUsage(): bool
{
// @todo This should be configured in the DB
return in_array($this->model, ['App\Models\Product\Adsl']);
}
public function scopeActive()
{
return $this->where('active',TRUE);

View File

@@ -228,6 +228,8 @@ class Service extends Model
],
];
/* RELATIONS */
/**
* Account the service belongs to
*
@@ -335,7 +337,7 @@ class Service extends Model
return $this->morphTo(null,'model','id','service_id');
}
/** SCOPES **/
/* SCOPES */
/**
* Only query active categories
@@ -384,7 +386,7 @@ class Service extends Model
return $query->where('id','like','%'.$term.'%');
}
/** ATTRIBUTES **/
/* ATTRIBUTES */
/**
* Name of the account for this service
@@ -846,7 +848,7 @@ class Service extends Model
return sprintf('<a href="/u/service/%s">%s</a>',$this->id,$this->service_id);
}
/** SETTERS **/
/* SETTERS */
public function setDateOrigAttribute($value)
{
@@ -858,7 +860,7 @@ class Service extends Model
$this->attributes['date_last'] = $value->timestamp;
}
/** FUNCTIONS **/
/* FUNCTIONS */
// The action methods will return: NULL for no progress|FALSE for a failed status|next stage name.
@@ -1140,6 +1142,16 @@ class Service extends Model
});
}
/**
* Does this service have traffic data to be graphed
*
* @return bool
*/
public function hasUsage(): bool
{
return $this->product->hasUsage();
}
/**
* Determine if a service is active. It is active, if active=1, or the order_status is not in inactive_status[]
*

View File

@@ -2,16 +2,19 @@
namespace App\Models\Service;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Support\Collection;
use App\Interfaces\ServiceItem;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Interfaces\{ServiceItem,ServiceUsage};
use App\Models\AdslSupplierPlan;
use App\Models\Base\ServiceType;
use App\Models\Service;
use App\Traits\NextKey;
class Adsl extends ServiceType implements ServiceItem
class Adsl extends ServiceType implements ServiceItem,ServiceUsage
{
private const LOGKEY = 'MSA';
use NextKey;
const RECORD_ID = 'service__adsl';
@@ -30,6 +33,7 @@ class Adsl extends ServiceType implements ServiceItem
*/
public function traffic()
{
// @todo Need to include site_id in this relation
return $this->hasMany(AdslTraffic::class,'ab_service_adsl_id');
}
@@ -91,4 +95,24 @@ class Adsl extends ServiceType implements ServiceItem
{
return $this->service_contract_date AND $this->service_contract_date->addMonths($this->contract_term)->isFuture();
}
}
/**
* Return service usage data
*
* @param int $days
* @return Collection
*/
public function usage(int $days=31): Collection
{
$maxdate = self::traffic()
->select(DB::raw('max(date) as max'))
->pluck('max')->pop();
Log::debug(sprintf('%s:Getting Usage data for [%d] days from [%s]',self::LOGKEY,$days,$maxdate),['m'=>__METHOD__]);
return $this->traffic()
->where('date','<=',$maxdate)
->where('date','>=',DB::raw(sprintf('date_sub(\'%s\',INTERVAL %s DAY)',$maxdate,$days)))
->get();
}
}

View File

@@ -8,6 +8,7 @@ class AdslTraffic extends Model
{
protected $table = 'ab_service__adsl_traffic';
public $timestamps = FALSE;
protected $dates = ['date'];
public function broadband()
{