From ad52bb15a32f2237a8e9fdbd87364e4cdc13bd98 Mon Sep 17 00:00:00 2001 From: Kekskurse Date: Thu, 21 Jan 2021 16:05:26 +0100 Subject: [PATCH] Traffiy Type --- app/Console/Commands/CalculateTraffic.php | 15 +++++++++- app/Http/Controllers/ReportController.php | 6 ++-- app/Models/Traffic.php | 6 ++-- .../2021_01_21_145341_traffix_typ.php | 30 +++++++++++++++++++ resources/views/dashboard/index.blade.php | 18 +++++++++-- routes/web.php | 2 +- 6 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 database/migrations/2021_01_21_145341_traffix_typ.php diff --git a/app/Console/Commands/CalculateTraffic.php b/app/Console/Commands/CalculateTraffic.php index 2adbae3..7fda765 100644 --- a/app/Console/Commands/CalculateTraffic.php +++ b/app/Console/Commands/CalculateTraffic.php @@ -43,7 +43,7 @@ class CalculateTraffic extends Command $result = []; $res = DB::select("SELECT * FROM access"); foreach($res as $r) { - $key = $r->tenant."-".$r->gallery."-".$r->year."-".$r->month."-".$r->day."-".$r->hour; + $key = $r->tenant."-".$r->gallery."-".$r->year."-".$r->month."-".$r->day."-".$r->hour."-".$r->typ; if(isset($result[$key])) { $result[$key] = $result[$key] + $r->size; } else { @@ -64,6 +64,7 @@ class CalculateTraffic extends Command ->where("month", "=", (int)$split[3]) ->where("day", "=", (int)$split[4]) ->where("hour", "=", (int)$split[5]) + ->where("typ", "=", $this->getTyp($split[6])) ->first(); } else { $traffic = Traffic::query() @@ -73,6 +74,7 @@ class CalculateTraffic extends Command ->where("month", "=", (int)$split[3]) ->where("day", "=", (int)$split[4]) ->where("hour", "=", (int)$split[5]) + ->where("typ", "=", $this->getTyp($split[6])) ->first(); } @@ -84,6 +86,7 @@ class CalculateTraffic extends Command $traffic->month =$split[3]; $traffic->day = $split[4]; $traffic->hour = $split[5]; + $traffic->typ = $this->getTyp($split[6]); $traffic->date = $split[2]."-".$split[3]."-".$split[4]; } if($traffic->gallery == "") { @@ -98,4 +101,14 @@ class CalculateTraffic extends Command } } } + + private function getTyp($typ) { + if($typ == "Access") { + return "s3"; + } + if($typ == "Cache") { + return "cache"; + } + return "generic"; + } } diff --git a/app/Http/Controllers/ReportController.php b/app/Http/Controllers/ReportController.php index 3a4da80..15f6b42 100644 --- a/app/Http/Controllers/ReportController.php +++ b/app/Http/Controllers/ReportController.php @@ -46,11 +46,11 @@ class ReportController extends BaseController return $res; } - public function traficReport($year, $month) { + public function trafficReport($year, $month) { $reportData = DB::select("SELECT * FROM traffic WHERE tenant = ".(int)session("current_tenant_id")." AND `year` = ".(int)$year." AND `month` = ".(int)$month.";"); - $report = "year;month;day;hour;gallery;traffic\n"; + $report = "year;month;day;hour;gallery;typ;traffic\n"; foreach ($reportData as $reportLine) { - $report .= $reportLine->year.";".$reportLine->month.";".$reportLine->day.";".$reportLine->hour.";".$reportLine->gallery.";".$reportLine->traffic."\n"; + $report .= $reportLine->year.";".$reportLine->month.";".$reportLine->day.";".$reportLine->hour.";".$reportLine->gallery.";".$reportLine->typ.";".$reportLine->traffic."\n"; } $res = new Response(); diff --git a/app/Models/Traffic.php b/app/Models/Traffic.php index f90275e..f2cc7e6 100644 --- a/app/Models/Traffic.php +++ b/app/Models/Traffic.php @@ -47,7 +47,7 @@ class Traffic extends Authenticatable ]; public static function getLastDays(int $days = 7) { - $sql = "SELECT year, month, day, SUM(traffic) AS `traffic` FROM traffic WHERE tenant = ".session("current_tenant_id")." GROUP BY year, month, day ORDER BY year, month, day DESC LIMIT ".$days.";"; + $sql = "SELECT year, month, day, typ, SUM(traffic) AS `traffic` FROM traffic WHERE tenant = ".session("current_tenant_id")." GROUP BY year, month, day, typ ORDER BY year, month, day DESC LIMIT ".$days.";"; $res = DB::select($sql); $now = new \DateTime( $days." days ago", new \DateTimeZone('UTC')); @@ -56,10 +56,10 @@ class Traffic extends Authenticatable $result = []; foreach( $period as $day) { $key = $day->format('Y-m-d'); - $result[$day->format('Y-m-d')] = 0; + $result[$day->format('Y-m-d')] = ["cache" => 0, "s3" => 0]; foreach($res as $r) { if($r->year == $day->format('Y') && $r->month == $day->format('m') && $r->day == $day->format('d')) { - $result[$day->format('Y-m-d')] = $r->traffic / 1000 / 1000; + $result[$day->format('Y-m-d')][$r->typ] = $r->traffic / 1000 / 1000; } } diff --git a/database/migrations/2021_01_21_145341_traffix_typ.php b/database/migrations/2021_01_21_145341_traffix_typ.php new file mode 100644 index 0000000..39989d8 --- /dev/null +++ b/database/migrations/2021_01_21_145341_traffix_typ.php @@ -0,0 +1,30 @@ +enum("typ", ["cache", "s3", "generic"])->default("generic"); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +} diff --git a/resources/views/dashboard/index.blade.php b/resources/views/dashboard/index.blade.php index 3380729..87c158d 100644 --- a/resources/views/dashboard/index.blade.php +++ b/resources/views/dashboard/index.blade.php @@ -50,7 +50,7 @@ @endforeach], datasets: [ { - label: "Traffic per Day", + label: "Cache Traffic per Day", fillColor: "rgba(220,220,220,0.2)", strokeColor: "rgba(220,220,220,1)", pointColor: "rgba(220,220,220,1)", @@ -59,7 +59,21 @@ pointHighlightStroke: "rgba(220,220,220,1)", data: [ @foreach($lastDaysTreffic as $traffic) - {{ $traffic }}, + {{ $traffic["cache"] }}, + @endforeach + ] + }, + { + label: "S3 Traffic per Day", + fillColor: "rgba(220,220,220,0.2)", + strokeColor: "rgba(220,220,220,1)", + pointColor: "rgba(220,220,220,1)", + pointStrokeColor: "#fff", + pointHighlightFill: "#fff", + pointHighlightStroke: "rgba(220,220,220,1)", + data: [ + @foreach($lastDaysTreffic as $traffic) + {{ $traffic["s3"] }}, @endforeach ] } diff --git a/routes/web.php b/routes/web.php index 839a4d5..4246855 100644 --- a/routes/web.php +++ b/routes/web.php @@ -51,7 +51,7 @@ Route::middleware([\App\Http\Middleware\TenanMiddleware::class])->group(function Route::get("/s/watermark/delete", [\App\Http\Controllers\TenantController::class, "deleteWatermark"]); Route::get("s/reports", [\App\Http\Controllers\ReportController::class, "showReportList"]); Route::get("s/reports/{year}/{month}/space", [\App\Http\Controllers\ReportController::class, "spaceReport"]); - Route::get("s/reports/{year}/{month}/traffic", [\App\Http\Controllers\ReportController::class, "traficReport"]); + Route::get("s/reports/{year}/{month}/traffic", [\App\Http\Controllers\ReportController::class, "trafficReport"]); }); Route::get("/{name}", [\App\Http\Controllers\PublicController::class, 'listGalleriesView'])->middleware([\App\Http\Middleware\TenanMiddleware::class]);