Show traffic monthly summary

This commit is contained in:
Deon George
2021-03-12 21:08:40 +11:00
parent a301fa7fc0
commit fee4b5802e
6 changed files with 128 additions and 11 deletions

View File

@@ -6,6 +6,8 @@ use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Leenooks\Carbon;
use App\Interfaces\{ServiceItem,ServiceUsage};
use App\Models\AdslSupplierPlan;
use App\Models\Base\ServiceType;
@@ -26,6 +28,20 @@ class Adsl extends ServiceType implements ServiceItem,ServiceUsage
/** RELATIONSHIPS **/
/**
* The suppliers product
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function product()
{
return $this
->hasOne(AdslSupplierPlan::class,'id','adsl_supplier_plan_id')
->withDefault(function() {
$o = new AdslSupplierPlan;
});
}
/**
* The accounts that this user manages
*
@@ -104,15 +120,63 @@ class Adsl extends ServiceType implements ServiceItem,ServiceUsage
*/
public function usage(int $days=31): Collection
{
$maxdate = self::traffic()
->select(DB::raw('max(date) as max'))
->pluck('max')->pop();
$maxdate = $this->usage_last_date();
Log::debug(sprintf('%s:Getting Usage data for [%d] days from [%s]',self::LOGKEY,$days,$maxdate),['m'=>__METHOD__]);
if (! $maxdate)
return collect();
Log::debug(sprintf('%s:Getting Usage data for [%d] days from [%s]',self::LOGKEY,$days,$maxdate->date->format('Y-m-d')),['m'=>__METHOD__]);
return $this->traffic()
->where('date','<=',$maxdate)
->where('date','>=',DB::raw(sprintf('date_sub(\'%s\',INTERVAL %s DAY)',$maxdate,$days)))
->where('date','<=',$maxdate->date->format('Y-m-d'))
->where('date','>=',$maxdate->date->subDays($days)->format('Y-m-d'))
->get();
}
/**
* Find the last date any traffic was recorded for a service
*
* @return AdslTraffic
*/
private function usage_last_date(): AdslTraffic
{
return $this->traffic
->sortBy('date')
->last();
}
public function usage_summary(int $months=2): Collection
{
$maxdate = $this->usage_last_date();
if (! $maxdate)
return collect();
Log::debug(sprintf('%s:Getting Usage data for [%d] months from [%s]',self::LOGKEY,$months,$maxdate),['m'=>__METHOD__]);
// Go back an extra month;
$start = $maxdate->date->subMonths($months);
// If we are before the 15th
if ($start->day < 15) {
$start = Carbon::createFromFormat('Y-m-d',$start->subMonth->format('Y-m-').'15');
} else {
$start = $start->subDays($start->day-15);
}
Log::debug(sprintf('%s:Getting Usage data from [%s]',self::LOGKEY,$start->format('Y-m-d')),['m'=>__METHOD__]);
$result = collect();
foreach ($this->traffic()
->where('date','>=',$start->format('Y-m-d'))
->where('date','<=',$maxdate->date->format('Y-m-d'))
->get()->groupBy(function($item) {
return sprintf('%s-%s',$item->trafficMonthStart->format('M-d'),$item->trafficMonthEnd->format('M-d'));
}) as $key => $o) {
$result->put($key,$o->sum('total'));
}
return $result;
}
}

View File

@@ -3,15 +3,37 @@
namespace App\Models\Service;
use Illuminate\Database\Eloquent\Model;
use Leenooks\Carbon;
class AdslTraffic extends Model
{
protected $table = 'ab_service__adsl_traffic';
public $timestamps = FALSE;
protected $dates = ['date'];
private $traffic_end = 14;
public function broadband()
{
return $this->belongsTo(Adsl::class);
}
public function getTotalAttribute() {
return $this->up_peak+$this->down_peak+$this->up_offpeak+$this->down_offpeak;
}
public function getTrafficMonthEndAttribute() {
if ($this->date->day > $this->traffic_end) {
return Carbon::createFromFormat('Y-m-d',$this->date->addMonth()->format('Y-m-').$this->traffic_end);
} else {
return Carbon::createFromFormat('Y-m-d',$this->date->format('Y-m-').$this->traffic_end);
}
}
public function getTrafficMonthStartAttribute() {
if ($this->date->day > $this->traffic_end) {
return Carbon::createFromFormat('Y-m-d',$this->date->format('Y-m-').($this->traffic_end+1));
} else {
return Carbon::createFromFormat('Y-m-d',$this->date->subMonth()->format('Y-m-').($this->traffic_end+1));
}
}
}