Show traffic monthly summary
This commit is contained in:
parent
a301fa7fc0
commit
fee4b5802e
@ -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__]);
|
||||
|
||||
if ($u) {
|
||||
|
||||
$this->aso->stats_lastupdate = $this->aso->stats_lastupdate->addDay();
|
||||
$this->aso->save();
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
@ -75,7 +75,9 @@
|
||||
@endif
|
||||
@if ($o->hasUsage())
|
||||
<div class="tab-pane fade show" id="traffic" role="tabpanel">
|
||||
@if ($o->type->usage(30)->count())
|
||||
@include('u.service.widgets.'.$o->stype.'.usagegraph',['o'=>$o->type])
|
||||
@endif
|
||||
</div>
|
||||
@endif
|
||||
<div class="tab-pane fade" id="invoices" role="tabpanel">
|
||||
|
@ -4,8 +4,30 @@
|
||||
</div>
|
||||
|
||||
<div class="card-body">
|
||||
<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>
|
||||
|
||||
@section('page-scripts')
|
||||
@ -15,6 +37,14 @@
|
||||
@js('//code.highcharts.com/modules/exporting.js','highcharts-export','highcharts')
|
||||
@js('//code.highcharts.com/modules/offline-exporting.js','highcharts-export-offline','highcharts-export')
|
||||
<script>
|
||||
const timezone = new Date().getTimezoneOffset()
|
||||
|
||||
Highcharts.setOptions({
|
||||
global: {
|
||||
timezoneOffset: timezone
|
||||
}
|
||||
});
|
||||
|
||||
Highcharts.chart('graph', {
|
||||
chart: {
|
||||
type: 'areaspline'
|
||||
@ -55,7 +85,7 @@
|
||||
},
|
||||
series: [{
|
||||
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>
|
||||
|
Loading…
Reference in New Issue
Block a user