From fee4b5802e1cefb91aa3212d7b670cd7bb0c6cec Mon Sep 17 00:00:00 2001 From: Deon George Date: Fri, 12 Mar 2021 21:08:40 +1100 Subject: [PATCH] Show traffic monthly summary --- app/Jobs/BroadbandTraffic.php | 1 - app/Models/Service/Adsl.php | 76 +++++++++++++++++-- app/Models/Service/AdslTraffic.php | 22 ++++++ .../backend/adminlte/u/service/home.blade.php | 4 +- .../widgets/broadband/details.blade.php | 2 +- .../widgets/broadband/usagegraph.blade.php | 34 ++++++++- 6 files changed, 128 insertions(+), 11 deletions(-) diff --git a/app/Jobs/BroadbandTraffic.php b/app/Jobs/BroadbandTraffic.php index f206f38..18ef625 100644 --- a/app/Jobs/BroadbandTraffic.php +++ b/app/Jobs/BroadbandTraffic.php @@ -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(); diff --git a/app/Models/Service/Adsl.php b/app/Models/Service/Adsl.php index 76d3b93..31a6f23 100644 --- a/app/Models/Service/Adsl.php +++ b/app/Models/Service/Adsl.php @@ -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; + } } diff --git a/app/Models/Service/AdslTraffic.php b/app/Models/Service/AdslTraffic.php index f21136e..7fc8ba0 100644 --- a/app/Models/Service/AdslTraffic.php +++ b/app/Models/Service/AdslTraffic.php @@ -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)); + } + } } \ No newline at end of file diff --git a/resources/views/theme/backend/adminlte/u/service/home.blade.php b/resources/views/theme/backend/adminlte/u/service/home.blade.php index 2cbf723..d482f9d 100644 --- a/resources/views/theme/backend/adminlte/u/service/home.blade.php +++ b/resources/views/theme/backend/adminlte/u/service/home.blade.php @@ -75,7 +75,9 @@ @endif @if ($o->hasUsage())
- @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
@endif
diff --git a/resources/views/theme/backend/adminlte/u/service/widgets/broadband/details.blade.php b/resources/views/theme/backend/adminlte/u/service/widgets/broadband/details.blade.php index 979fd5f..6758be0 100644 --- a/resources/views/theme/backend/adminlte/u/service/widgets/broadband/details.blade.php +++ b/resources/views/theme/backend/adminlte/u/service/widgets/broadband/details.blade.php @@ -71,4 +71,4 @@
- \ No newline at end of file + diff --git a/resources/views/theme/backend/adminlte/u/service/widgets/broadband/usagegraph.blade.php b/resources/views/theme/backend/adminlte/u/service/widgets/broadband/usagegraph.blade.php index b102ba5..07dbd2a 100644 --- a/resources/views/theme/backend/adminlte/u/service/widgets/broadband/usagegraph.blade.php +++ b/resources/views/theme/backend/adminlte/u/service/widgets/broadband/usagegraph.blade.php @@ -4,7 +4,29 @@
-
+
+
+ + + + + + + + + @foreach ($o->usage_summary(6) as $key => $oo) + + + + + @endforeach + +
PeriodTraffic
{{ $key }}{{ number_format($oo/1024,2) }}
+
+
+
+
+
@@ -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')