Show traffic monthly summary

This commit is contained in:
Deon George 2021-03-12 21:08:40 +11:00
parent a301fa7fc0
commit fee4b5802e
No known key found for this signature in database
GPG Key ID: 7670E8DC27415254
6 changed files with 128 additions and 11 deletions

View File

@ -132,7 +132,6 @@ class BroadbandTraffic implements ShouldQueue
Log::info(sprintf('%s: Records Imported [%d] for [%s]',self::LOGKEY,$u,$this->aso->stats_lastupdate->format('Y-m-d')),['m'=>__METHOD__]); Log::info(sprintf('%s: Records Imported [%d] for [%s]',self::LOGKEY,$u,$this->aso->stats_lastupdate->format('Y-m-d')),['m'=>__METHOD__]);
if ($u) { if ($u) {
$this->aso->stats_lastupdate = $this->aso->stats_lastupdate->addDay(); $this->aso->stats_lastupdate = $this->aso->stats_lastupdate->addDay();
$this->aso->save(); $this->aso->save();

View File

@ -6,6 +6,8 @@ use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Leenooks\Carbon;
use App\Interfaces\{ServiceItem,ServiceUsage}; use App\Interfaces\{ServiceItem,ServiceUsage};
use App\Models\AdslSupplierPlan; use App\Models\AdslSupplierPlan;
use App\Models\Base\ServiceType; use App\Models\Base\ServiceType;
@ -26,6 +28,20 @@ class Adsl extends ServiceType implements ServiceItem,ServiceUsage
/** RELATIONSHIPS **/ /** 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 * The accounts that this user manages
* *
@ -104,15 +120,63 @@ class Adsl extends ServiceType implements ServiceItem,ServiceUsage
*/ */
public function usage(int $days=31): Collection public function usage(int $days=31): Collection
{ {
$maxdate = self::traffic() $maxdate = $this->usage_last_date();
->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__]); 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() return $this->traffic()
->where('date','<=',$maxdate) ->where('date','<=',$maxdate->date->format('Y-m-d'))
->where('date','>=',DB::raw(sprintf('date_sub(\'%s\',INTERVAL %s DAY)',$maxdate,$days))) ->where('date','>=',$maxdate->date->subDays($days)->format('Y-m-d'))
->get(); ->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; namespace App\Models\Service;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Leenooks\Carbon;
class AdslTraffic extends Model class AdslTraffic extends Model
{ {
protected $table = 'ab_service__adsl_traffic'; protected $table = 'ab_service__adsl_traffic';
public $timestamps = FALSE; public $timestamps = FALSE;
protected $dates = ['date']; protected $dates = ['date'];
private $traffic_end = 14;
public function broadband() public function broadband()
{ {
return $this->belongsTo(Adsl::class); 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));
}
}
} }

View File

@ -75,7 +75,9 @@
@endif @endif
@if ($o->hasUsage()) @if ($o->hasUsage())
<div class="tab-pane fade show" id="traffic" role="tabpanel"> <div class="tab-pane fade show" id="traffic" role="tabpanel">
@include('u.service.widgets.'.$o->stype.'.usagegraph',['o'=>$o->type]) @if ($o->type->usage(30)->count())
@include('u.service.widgets.'.$o->stype.'.usagegraph',['o'=>$o->type])
@endif
</div> </div>
@endif @endif
<div class="tab-pane fade" id="invoices" role="tabpanel"> <div class="tab-pane fade" id="invoices" role="tabpanel">

View File

@ -4,7 +4,29 @@
</div> </div>
<div class="card-body"> <div class="card-body">
<div id="graph"></div> <div class="row">
<div class="col-3">
<table class="table table-sm">
<thead>
<tr>
<th>Period</th>
<th class="text-right">Traffic</th>
</tr>
</thead>
<tbody>
@foreach ($o->usage_summary(6) as $key => $oo)
<tr>
<td>{{ $key }}</td>
<td class="text-right">{{ number_format($oo/1024,2) }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<div class="col-9">
<div id="graph"></div>
</div>
</div>
</div> </div>
</div> </div>
@ -15,6 +37,14 @@
@js('//code.highcharts.com/modules/exporting.js','highcharts-export','highcharts') @js('//code.highcharts.com/modules/exporting.js','highcharts-export','highcharts')
@js('//code.highcharts.com/modules/offline-exporting.js','highcharts-export-offline','highcharts-export') @js('//code.highcharts.com/modules/offline-exporting.js','highcharts-export-offline','highcharts-export')
<script> <script>
const timezone = new Date().getTimezoneOffset()
Highcharts.setOptions({
global: {
timezoneOffset: timezone
}
});
Highcharts.chart('graph', { Highcharts.chart('graph', {
chart: { chart: {
type: 'areaspline' type: 'areaspline'
@ -55,7 +85,7 @@
}, },
series: [{ series: [{
name: 'Traffic', name: 'Traffic',
data: {!! $o->usage(30)->map(function($item) { return ['x'=>$item->date->timestamp*1000,'y'=>$item->up_peak+$item->down_peak+$item->up_offpeak+$item->down_offpeak];}) !!} data: {!! $o->usage(30)->map(function($item) { return ['x'=>$item->date->timestamp*1000,'y'=>$item->total];}) !!}
}] }]
}); });
</script> </script>