From b8a9ae6608386f231966e4f3a50d0b0a41e5e8bd Mon Sep 17 00:00:00 2001 From: Dominik Hebeler <dominik@suma-ev.de> Date: Fri, 3 Jun 2022 12:40:11 +0200 Subject: [PATCH] fixed cache --- .../app/Http/Controllers/AdminInterface.php | 51 ++++++++++++++----- metager/resources/js/admin/count.js | 5 +- metager/resources/views/admin/count.blade.php | 2 +- 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/metager/app/Http/Controllers/AdminInterface.php b/metager/app/Http/Controllers/AdminInterface.php index f038bf55b..144133dfd 100644 --- a/metager/app/Http/Controllers/AdminInterface.php +++ b/metager/app/Http/Controllers/AdminInterface.php @@ -20,9 +20,11 @@ class AdminInterface extends Controller if ($request->input('out', 'web') === "web") { $days = $request->input("days", 28); + $interface = $request->input('interface', 'all'); return view('admin.count') ->with('title', 'Suchanfragen - MetaGer') ->with('days', $days) + ->with('interface', $interface) ->with('css', [mix('/css/count/style.css')]) ->with('darkcss', [mix('/css/count/dark.css')]) ->with('js', [ @@ -39,11 +41,12 @@ class AdminInterface extends Controller abort(404); } + $interface = $request->input('interface', 'all'); + $year = $date->format("Y"); $month = $date->format("m"); $day = $date->format("d"); - $cache_key = "admin_count_total_${year}_${month}_${day}"; - Cache::forget($cache_key); + $cache_key = "admin_count_total_${interface}_${year}_${month}_${day}"; $total_count = Cache::get($cache_key); if ($total_count === null) { @@ -58,13 +61,19 @@ class AdminInterface extends Controller abort(404); } - $total_count = $connection->table("logs")->count('*'); + $total_count = $connection->table("logs"); + if ($interface !== "all") { + $total_count = $total_count->where("interface", "=", $interface); + } + $total_count = $total_count->count('*'); } finally { $connection->disconnect(); } // No Cache for today if ($date->isToday()) { Cache::put($cache_key, $total_count, now()->addWeek()); + } else { + Cache::put($cache_key, $total_count, now()->addMinutes(5)); } } @@ -82,31 +91,44 @@ class AdminInterface extends Controller public function getCountDataUntil(Request $request) { $date = $request->input('date', ''); - $date = DateTime::createFromFormat("Y-m-d", $date); + $date = Carbon::createFromFormat("Y-m-d", $date); if ($date === false) { abort(404); } + $interface = $request->input('interface', 'all'); + $year = $date->format("Y"); $month = $date->format("m"); $day = $date->format("d"); $time = now()->format("H:i:s"); + $cache_key = "admin_count_until_${interface}_${year}_${month}_${day}"; + $total_count = Cache::get($cache_key); - $database_file = \storage_path("logs/metager/$year/$month/$day.sqlite"); - if (!\file_exists($database_file)) { - abort(404); - } + if ($total_count === null) { - $connection = new SQLiteConnection(new PDO("sqlite:$database_file", null, null, [PDO::SQLITE_ATTR_OPEN_FLAGS => PDO::SQLITE_OPEN_READONLY])); - try { - if (!$connection->getSchemaBuilder()->hasTable("logs")) { + $database_file = \storage_path("logs/metager/$year/$month/$day.sqlite"); + if (!\file_exists($database_file)) { abort(404); } - $total_count = $connection->table("logs")->whereTime("time", "<", $time)->count(); - } finally { - $connection->disconnect(); + $connection = new SQLiteConnection(new PDO("sqlite:$database_file", null, null, [PDO::SQLITE_ATTR_OPEN_FLAGS => PDO::SQLITE_OPEN_READONLY])); + try { + if (!$connection->getSchemaBuilder()->hasTable("logs")) { + abort(404); + } + + $total_count = $connection->table("logs")->whereTime("time", "<", $time); + if ($interface !== "all") { + $total_count = $total_count->where("interface", "=", $interface); + } + $total_count = $total_count->count('*'); + } finally { + $connection->disconnect(); + } + + Cache::put($cache_key, $total_count, now()->addMinutes(5)); } $result = [ "status" => 200, @@ -116,6 +138,7 @@ class AdminInterface extends Controller "total" => $total_count, ] ]; + return \response()->json($result); } diff --git a/metager/resources/js/admin/count.js b/metager/resources/js/admin/count.js index 2210ad252..731c1c317 100644 --- a/metager/resources/js/admin/count.js +++ b/metager/resources/js/admin/count.js @@ -1,6 +1,7 @@ let parallel_fetches = 8; let data = []; +let lang = document.getElementById("data-table").dataset.interface; let chart = null; load(); @@ -108,7 +109,7 @@ function loadTotals(parallel) { let days_ago = parseInt(element.parentNode.dataset.days_ago) if (fetches.length < parallel) { - fetches.push(fetch('/admin/count/count-data-total?date=' + date) + fetches.push(fetch('/admin/count/count-data-total?date=' + date + '&interface=' + lang) .then(response => response.json()) .then(response => { total_requests = parseInt(response.data.total); @@ -141,7 +142,7 @@ function loadSameTimes(parallel) { let days_ago = parseInt(element.parentNode.dataset.days_ago) if (fetches.length < parallel) { - fetches.push(fetch('/admin/count/count-data-until?date=' + date) + fetches.push(fetch('/admin/count/count-data-until?date=' + date + '&interface=' + lang) .then(response => response.json()) .then(response => { let total_requests = parseInt(response.data.total) diff --git a/metager/resources/views/admin/count.blade.php b/metager/resources/views/admin/count.blade.php index d876e570a..4668535cd 100644 --- a/metager/resources/views/admin/count.blade.php +++ b/metager/resources/views/admin/count.blade.php @@ -8,7 +8,7 @@ </div> <p class="record">Am <span class="record-date loading"></span> zur gleichen Zeit <span class="record-same-time text-info loading"></span> - insgesamt <span class="record-total text-danger loading"></span></p> <p class="total-median">Mittelwert der letzten <span class="median-days loading"></span> Tage: <span class="median-value loading"></span></p> -<table class="table table-striped"> +<table id="data-table" class="table table-striped" data-interface="{{ $interface }}"> <caption> <form method="GET" style="display: flex; align-items: center;"> <div class="form-group" style="max-width: 100px; margin-right: 8px;"> -- GitLab