diff options
author | Minteck <contact@minteck.org> | 2022-10-10 20:51:39 +0200 |
---|---|---|
committer | Minteck <contact@minteck.org> | 2022-10-10 20:51:39 +0200 |
commit | 108525534c28013cfe1897c30e4565f9893f3766 (patch) | |
tree | dd3e5132971f96ab5f05e7f3f8f6dbbf379a19bd /pages/stats.inc | |
parent | 2162eaa06f7e4764eb3dcfe130ec2c711d0c62ab (diff) | |
download | pluralconnect-108525534c28013cfe1897c30e4565f9893f3766.tar.gz pluralconnect-108525534c28013cfe1897c30e4565f9893f3766.tar.bz2 pluralconnect-108525534c28013cfe1897c30e4565f9893f3766.zip |
Update
Diffstat (limited to 'pages/stats.inc')
-rw-r--r-- | pages/stats.inc | 441 |
1 files changed, 441 insertions, 0 deletions
diff --git a/pages/stats.inc b/pages/stats.inc new file mode 100644 index 0000000..19d4453 --- /dev/null +++ b/pages/stats.inc @@ -0,0 +1,441 @@ +<?php + +require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/init.inc"; global $title; global $isLoggedIn; +require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/header.inc'; + +$switchesRaindrops = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/gdapd/switches.json"), true); +$switchesCloudburst = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/ynmuc/switches.json"), true); + +?> + +<script src="/assets/editor/chart.js"></script> + +<br> +<div class="container"> + <div id="page-content"> + <h2>Statistics</h2> + <?php + + $weeks = []; + $weeksRectified = []; + + $lastWeek = 0; + foreach ($switchesRaindrops as $switch) { + $week = date('YW', strtotime($switch["timestamp"])); + if ($lastWeek === 0) $lastWeek = $week; + + if (!isset($weeks[$week])) $weeks[$week] = [ + "cloudburst" => 0, + "raindrops" => 0, + "total" => 0, + "end" => strtotime($switch["timestamp"]), + "start" => null + ]; + + if ($week !== $lastWeek) { + if (isset($weeks[$lastWeek])) { + $weeks[$lastWeek]["start"] = strtotime($switch["timestamp"]); + $lastWeek = $week; + } + } + + $weeks[$week]["raindrops"]++; + $weeks[$week]["total"]++; + } + + $lastWeek = 0; + foreach ($switchesCloudburst as $switch) { + $week = date('YW', strtotime($switch["timestamp"])); + if ($lastWeek === 0) $lastWeek = $week; + + if (!isset($weeks[$week])) $weeks[$week] = [ + "cloudburst" => 0, + "raindrops" => 0, + "total" => 0, + "end" => strtotime($switch["timestamp"]), + "start" => null + ]; + + if ($week !== $lastWeek) { + if (isset($weeks[$lastWeek])) { + $weeks[$lastWeek]["start"] = strtotime($switch["timestamp"]); + $lastWeek = $week; + } + } + + $weeks[$week]["cloudburst"]++; + $weeks[$week]["total"]++; + } + + $index = 0; + foreach ($weeks as $id => $week) { + if ($index >= 20) continue; + $weeksRectified[$id] = $week; + $index++; + } + + $weeksRectified = array_filter(array_reverse($weeksRectified), function ($i) { + return $i["start"] > 1651363200; + }); + + $members = []; + $frontersMonth = []; + + $fronts = []; + $lastFrontIDs = []; + + foreach (array_reverse($switchesRaindrops) as $switch) { + foreach ($lastFrontIDs as $frontID) { + $fronts[$frontID]["end"] = strtotime($switch["timestamp"]); + } + $lastFrontIDs = []; + + $index = 0; foreach ($switch["members"] as $member) { + if (strtotime($switch["timestamp"]) - (time() - 86400*30) < 0) continue; + $fronts[$switch["id"] . ":" . $index] = [ + "member" => $member, + "start" => strtotime($switch["timestamp"]), + "end" => strtotime(date('c')), + "duration" => null + ]; + $lastFrontIDs[] = $switch["id"] . ":" . $index; + } $index++; + } + + foreach ($fronts as $id => $front) { + $fronts[$id]["duration"] = $front["end"] - $front["start"]; + + if (!($front["start"] - (time() - 86400*30) < 0)) { + if (!isset($members[$front["member"]])) $members[$front["member"]] = [ + "time" => 0, + "name" => getMemberWithoutSystem($front["member"])["display_name"], + "color" => getMemberWithoutSystem($front["member"])["color"], + ]; + $members[$front["member"]]["time"] += $fronts[$id]["duration"]; + } + } + + foreach ($fronts as $front) { + if (!isset($frontersMonth[date('Y-m', $front["start"])])) $frontersMonth[date('Y-m', $front["start"])] = []; + if (!isset($frontersMonth[date('Y-m', $front["start"])][$front["member"]])) $frontersMonth[date('Y-m', $front["start"])][$front["member"]] = 0; + $frontersMonth[date('Y-m', $front["start"])][$front["member"]] += $front["duration"]; + } + + $fronts = []; + $lastFrontIDs = []; + + foreach (array_reverse($switchesCloudburst) as $switch) { + foreach ($lastFrontIDs as $frontID) { + $fronts[$frontID]["end"] = strtotime($switch["timestamp"]); + } + $lastFrontIDs = []; + + $index = 0; foreach ($switch["members"] as $member) { + $fronts[$switch["id"] . ":" . $index] = [ + "member" => $member, + "start" => strtotime($switch["timestamp"]), + "end" => strtotime(date('c')), + "duration" => null + ]; + $lastFrontIDs[] = $switch["id"] . ":" . $index; + } $index++; + } + + foreach ($fronts as $id => $front) { + $fronts[$id]["duration"] = $front["end"] - $front["start"]; + + if (!($front["start"] - (time() - 86400*30) < 0)) { + if (!isset($members[$front["member"]])) $members[$front["member"]] = [ + "time" => 0, + "name" => getMemberWithoutSystem($front["member"])["display_name"], + "color" => getMemberWithoutSystem($front["member"])["color"], + ]; + $members[$front["member"]]["time"] += $fronts[$id]["duration"]; + } + } + + foreach ($fronts as $front) { + if (!isset($frontersMonth[date('Y-m', $front["start"])])) $frontersMonth[date('Y-m', $front["start"])] = []; + if (!isset($frontersMonth[date('Y-m', $front["start"])][$front["member"]])) $frontersMonth[date('Y-m', $front["start"])][$front["member"]] = 0; + $frontersMonth[date('Y-m', $front["start"])][$front["member"]] += $front["duration"]; + } + + $fronts = array_reverse($fronts); + uasort($members, function ($a, $b) { + return $b["time"] - $a["time"]; + }); + + $frontersMonthRectified = []; + foreach ($frontersMonth as $monthID => $month) { + $thisMonth = []; + + foreach ($month as $id => $time) { + $thisMonth[$id] = [ + "duration" => $time, + "name" => getMemberWithoutSystem($id)["display_name"], + "color" => getMemberWithoutSystem($id)["color"] + ]; + } + + $frontersMonthRectified[$monthID] = $thisMonth; + } + + uasort($frontersMonthRectified, function ($a, $b) use ($frontersMonthRectified) { + return strtotime(array_search($a, $frontersMonthRectified) . "-01") - strtotime(array_search($b, $frontersMonthRectified) . "-01"); + }); + + $frontersMonthMembers = []; + foreach ($frontersMonthRectified as $month => $fronters) { + foreach ($fronters as $fronter => $data) { + foreach (scoreOrderGlobal() as $member) { + if (!isset($frontersMonthMembers[$member["id"]])) $frontersMonthMembers[$member["id"]] = []; + } + } + + foreach ($frontersMonthMembers as $key => $member) { + $frontersMonthMembers[$key][$month] = $frontersMonthRectified[$month][$key] ?? [ + "duration" => 0 + ]; + /*$frontersMonthMembers[$key][$month] = array_map(function ($i) { + return $i["duration"]; + }, $frontersMonthMembers[$key][$month]);*/ + } + } + + ?> + + <h3>Switches per week</h3> + <canvas id="graph-00" style="width: 100%; height: 300px; max-height: 100%;"></canvas> + <script> + const ctx0 = document.getElementById('graph-00').getContext('2d'); + window.chart00 = [ + { + label: "Raindrops System", + data: JSON.parse(`<?= json_encode(array_map(function ($i) { + return $i["raindrops"]; + }, array_values($weeksRectified))) ?>`), + borderColor: "#e598ff" + }, + { + label: "Cloudburst System", + data: JSON.parse(`<?= json_encode(array_map(function ($i) { + return $i["cloudburst"]; + }, array_values($weeksRectified))) ?>`), + borderColor: "#98e0ff" + }, + { + label: "All systems", + data: JSON.parse(`<?= json_encode(array_map(function ($i) { + return $i["total"]; + }, array_values($weeksRectified))) ?>`), + borderColor: "rgba(160,255,153,0.5)", + borderDash: [5] + } + ]; + const graph0 = new Chart(ctx0, { + type: 'line', + data: { + labels: JSON.parse(`<?= json_encode(array_map(function ($i) { + return date("M d", $i["start"]) . " to " . date("M d", $i["end"]); + }, array_values($weeksRectified))) ?>`), + datasets: window.chart00 + }, + options: { + animation: { + duration: 0 + }, + scales: { + y: { + beginAtZero: true, + grid: { + color: "rgba(255,255,255,0.25)" + } + } + }, + plugins: { + legend: { + display: false + }, + tooltip: { + intersect: false + } + } + } + }); + </script> + + <br> + <h3>Time at front in the last 30 days</h3> + <canvas id="graph-01" style="width: 100%; height: 300px; max-height: 100%;"></canvas> + <script> + function toDuration(seconds, remainder) { + if (seconds > 60) { + if (seconds > 3600) { + if (seconds > 86400) { + if (remainder) { + return Math.floor(seconds / 86400) + " d. " + Math.floor((seconds / 3600) - Math.floor(seconds / 86400) * 24) + " hr. " + Math.floor((seconds - (Math.floor(seconds / 3600) * 3600)) / 60) + " min. " + Math.floor((seconds - (Math.floor(seconds / 3600) * 3600)) - (Math.floor((seconds - (Math.floor(seconds / 3600) * 3600)) / 60)) * 60) + " sec."; + } else { + return Math.round(seconds / 86400) + " d."; + } + } else { + if (remainder) { + return Math.floor(seconds / 3600) + " hr. " + Math.floor((seconds - (Math.floor(seconds / 3600) * 3600)) / 60) + " min. " + Math.floor((seconds - (Math.floor(seconds / 3600) * 3600)) - (Math.floor((seconds - (Math.floor(seconds / 3600) * 3600)) / 60)) * 60) + " sec."; + } else { + return Math.round(seconds / 3600) + " hr."; + } + } + } else { + if (remainder) { + return Math.round(seconds / 60) + " min. " + Math.floor((seconds - (Math.floor(seconds / 3600) * 3600)) - (Math.floor((seconds - (Math.floor(seconds / 3600) * 3600)) / 60)) * 60) + " sec."; + } else { + return Math.round(seconds / 60) + " min."; + } + } + } else { + return seconds + " sec."; + } + } + + const ctx1 = document.getElementById('graph-01').getContext('2d'); + window.graph01 = { + label: "Members", + data: JSON.parse(`<?= json_encode(array_map(function ($i) { + return $i["time"]; + }, array_values($members))) ?>`), + backgroundColor: JSON.parse(`<?= json_encode(array_map(function ($i) { + return "#" . $i["color"] . "33"; + }, array_values($members))) ?>`), + borderColor: JSON.parse(`<?= json_encode(array_map(function ($i) { + return "#" . $i["color"]; + }, array_values($members))) ?>`), + borderWidth: 1 + }; + const graph1 = new Chart(ctx1, { + type: 'bar', + data: { + labels: JSON.parse(`<?= json_encode(array_map(function ($i) { + return $i["name"]; + }, array_values($members))) ?>`), + datasets: [ + window.graph01 + ] + }, + options: { + animation: { + duration: 0 + }, + scales: { + y: { + beginAtZero: true, + ticks: { + callback: function(label) { + return toDuration(label); + } + } + }, + x: { + grid: { + color: "rgba(255,255,255,0.25)" + } + } + }, + elements: { + point:{ + radius: 0 + } + }, + plugins: { + legend: { + display: false + }, + tooltip: { + callbacks: { + label: function(tooltipItem) { + return toDuration(tooltipItem.raw, true); + } + } + } + } + } + }); + </script> + + <br> + <h3>Time at front per month</h3> + <canvas id="graph-02" style="width: 100%; height: 300px; max-height: 100%;"></canvas> + <script> + const ctx2 = document.getElementById('graph-02').getContext('2d'); + window.chart02 = JSON.parse(`<?= json_encode(array_values(array_map(function ($i) use ($frontersMonthMembers) { + $name = getMemberWithoutSystem(array_search($i, $frontersMonthMembers))["name"]; + $month = (int)implode("-", explode("-", array_search($i, $frontersMonthMembers))); + + $k = array_filter($i, function ($i) { + return isset($i["name"]) && isset($i["color"]); + }); + + if (count($k) > 0) { + return [ + "label" => $k[array_keys($k)[0]]["name"], + "data" => array_values(array_map(function ($j) use ($name, $month) { + return $name === "twilight" && $month < 202208 ? 0 : $j["duration"]; + }, $i)), + "borderColor" => "#" . $k[array_keys($k)[0]]["color"] + ]; + } else { + return [ + "label" => getMemberWithoutSystem(array_search($i, $frontersMonthMembers))["display_name"], + "data" => array_values(array_map(function ($j) { + return $j["duration"]; + }, $i)), + "borderColor" => "#" . getMemberWithoutSystem(array_search($i, $frontersMonthMembers))["color"] + ]; + } + }, $frontersMonthMembers))) ?>`); + console.log(JSON.parse(`<?= json_encode(array_map(function ($i) { + return date("M Y", strtotime($i . "-00T00:00:00.000Z")); + }, array_keys($frontersMonthRectified))) ?>`)); + const graph2 = new Chart(ctx2, { + type: 'line', + data: { + labels: JSON.parse(`<?= json_encode(array_map(function ($i) { + return date("M Y", strtotime($i . "-00T00:00:00.000Z")); + }, array_keys($frontersMonthRectified))) ?>`), + datasets: window.chart02 + }, + options: { + animation: { + duration: 0 + }, + scales: { + y: { + beginAtZero: true, + grid: { + color: "rgba(255,255,255,0.25)" + }, + ticks: { + callback: function(label) { + return toDuration(label); + } + } + } + }, + plugins: { + legend: { + display: false + }, + tooltip: { + intersect: false, + callbacks: { + label: function(tooltipItem) { + return tooltipItem.dataset.label + ": " + toDuration(tooltipItem.raw, true); + } + } + } + } + } + }); + </script> + </div> +</div> + +<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/footer.inc'; ?> |