summaryrefslogtreecommitdiff
path: root/pages/stats.inc
blob: 75b9e9ddc1dd914ef3eb56f8a44bb778bea5127f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php

require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/init.inc"; global $title; global $isLoggedIn; global $lang; global $pages;
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>
    </div>
</div>

<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/footer.inc'; ?>