-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.php
144 lines (129 loc) · 3.85 KB
/
stats.php
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
<?php
namespace arnoson\KirbyStats;
use DateTimeImmutable;
use Kirby\Toolkit\A;
function path(string $slug = null) {
if (!$slug) {
return;
}
$path = str_replace('+', '/', $slug);
return $path === 'home' ? '/' : "/$path";
}
function statsView(array $data) {
$now = new DateTimeImmutable();
$page = $data['page'] ?? null;
$labels = ['page' => Helpers::pathTitle($page)];
$urls = [];
foreach (['day', 'week', 'month', 'year'] as $name) {
$slug = Interval::slug(Interval::fromName($name), $now);
$urls[$name] = "stats/$name/$slug" . ($page ? "/page/$page" : '');
}
foreach (['today', '7-days', '30-days'] as $name) {
$urls[$name] = "stats/$name" . ($page ? "/page/$page" : '');
}
return [
'component' => 'k-stats-main-view',
'props' => [
'stats' => $data['stats'],
'labels' => A::merge($labels, $data['labels']),
'urls' => A::merge($urls, $data['urls']),
'page' => $page,
],
];
}
function statsViewLatest(string $range, string $page = null) {
$to = (new DateTimeImmutable())->modify('tomorrow');
[$modifier, $label, $interval] = match ($range) {
'today' => ['-1 day', 'Today', Interval::HOUR],
'7-days' => ['-7 days', 'Last 7 days', Interval::DAY],
'30-days' => ['-30 days', 'Last 30 days', Interval::DAY],
};
$from = $to->modify($modifier);
if ($range === 'today') {
// Overwrite statsViewInterval's date label.
$labels = ['date' => $label];
return statsViewInterval(Interval::DAY, $from, null, $labels);
}
return statsView([
'stats' => (new KirbyStats())->data($interval, $from, $to, path($page)),
'page' => "stats/$range/page/{{slug}}",
'urls' => ['page' => "stats/$range/page/{{slug}}"],
'labels' => ['date' => $label],
'page' => $page,
]);
}
function statsViewInterval(
int $interval,
DateTimeImmutable $date,
$page = null,
$labels = []
) {
$stats = new KirbyStats();
$now = new DateTimeImmutable();
$current = Interval::startOf($interval, $date);
$last = Interval::startOfLast($interval, $date);
$next = Interval::startOfNext($interval, $date);
$hasLast = $date > $stats->getFirstTime();
$hasNext = $next < $now;
$format = $interval === 'month' ? 'Y-m' : 'Y-m-d';
$path = $page ? ($page === 'home' ? '/' : "/$page") : null;
$from = $current;
$to = Interval::endOf($interval, $date);
$intervalName = Interval::name($interval);
$dataInterval = match ($interval) {
Interval::DAY => Interval::HOUR,
Interval::YEAR => Interval::MONTH,
default => Interval::DAY,
};
return statsView([
'stats' => $stats->data($dataInterval, $from, $to, $path),
'labels' => A::merge(
['date' => Interval::label($interval, $current)],
$labels
),
'urls' => [
'page' => "stats/$intervalName/{$current->format($format)}/page/{{slug}}",
'last' => $hasLast
? "stats/$intervalName/{$last->format($format)}"
: null,
'next' => $hasNext
? "stats/$intervalName/{$next->format($format)}"
: null,
],
]);
}
return fn() => [
'label' => 'Stats',
'icon' => 'chart',
'menu' => true,
'link' => 'stats',
'views' => [
[
'pattern' => 'stats',
'action' => fn() => statsViewLatest('today'),
],
[
'pattern' => 'stats/(:any)/page/(:any)',
'action' => fn($range, $page) => statsViewLatest($range, $page),
],
[
'pattern' => 'stats/(:any)',
'action' => fn($range) => statsViewLatest($range),
],
[
'pattern' => 'stats/(:any)/(:any)/page/(:any)',
'action' => fn($interval, $date, $page) => statsViewInterval(
Interval::fromName($interval),
new DateTimeImmutable($date),
$page
),
],
[
'pattern' => 'stats/(:any)/(:any)',
'action' => fn($interval, $date) => statsViewInterval(
Interval::fromName($interval),
new DateTimeImmutable($date)
),
],
],
];