Skip to content

Commit cd6e042

Browse files
committed
Fix metricts filters, fix #68
1 parent e01110a commit cd6e042

7 files changed

Lines changed: 40 additions & 23 deletions

File tree

assets/js/scripts.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ const time_switcher = (callback) => {
559559
time_buttons.forEach(btn => btn.classList.remove('active'));
560560
button.classList.add('active');
561561

562-
metrics_active_filter = parseInt(button.dataset.tab, 10);
562+
metrics_active_filter = button.dataset.tab;
563563
callback();
564564
});
565565
});
@@ -607,7 +607,7 @@ const fetch_metrics = (callback) => {
607607
} else {
608608
document.getElementById('alerts').innerHTML = `Server responded with status ${request.status}`;
609609
}
610-
}, {points: metrics_active_filter}, false);
610+
}, {filter: metrics_active_filter}, false);
611611
};
612612

613613
const init_metrics = (render_charts, chart_config) => {

config.dist.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
'listview' => 'table', // table/tree - default key list view.
150150
'panelrefresh' => 30, // In seconds, refresh interval for panels - default 30
151151
'metricsrefresh' => 60, // In seconds, refresh interval for metrics - default 60
152-
'metricstab' => 1440, // Default tab in metrics, 60 - Last hour, 1440 - Last day, 10080 - Last week, 43200 - Last month - default 1440
152+
'metricstab' => '1d', // Default tab in metrics, 1h - Last hour, 1d - Last day, 1w - Last week, 1m - Last month - default 1d
153153
'hash' => 'pca', // Any random string to secure a metrics DB file.
154154
'metricsdir' => __DIR__.'/tmp/metrics', // Directory for metrics DB files.
155155
'twigcache' => __DIR__.'/tmp/twig', // Directory for Twig cache files.

src/Dashboards/Memcached/MemcachedMetrics.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,15 +248,22 @@ private function insertMetrics(array $metrics): void {
248248
* @return array<int, array<string, mixed>>
249249
*/
250250
private function fetchRecentMetrics(): array {
251-
$max_data_points_to_return = Http::post('points', Config::get('metricstab', 1440));
251+
$filter = Http::post('filter', Config::get('metricstab', '1d'));
252252

253-
$stmt = $this->pdo->prepare('SELECT * FROM metrics ORDER BY id DESC LIMIT :limit');
254-
$stmt->bindValue(':limit', $max_data_points_to_return, PDO::PARAM_INT);
255-
$stmt->execute();
253+
$seconds = match ($filter) {
254+
'1h' => 3600,
255+
'1w' => 604800,
256+
'1m' => 2592000,
257+
default => 86400,
258+
};
259+
260+
$time_ago = time() - $seconds;
256261

257-
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
262+
$stmt = $this->pdo->prepare('SELECT * FROM metrics WHERE timestamp >= :time_ago ORDER BY id DESC');
263+
$stmt->bindValue(':time_ago', $time_ago, PDO::PARAM_INT);
264+
$stmt->execute();
258265

259-
return array_reverse($results);
266+
return array_reverse($stmt->fetchAll(PDO::FETCH_ASSOC));
260267
}
261268

262269
/**

src/Dashboards/Redis/RedisMetrics.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,19 @@ private function insertMetrics(array $metrics): void {
158158
* @return array<int, array<string, mixed>>
159159
*/
160160
private function fetchRecentMetrics(): array {
161-
$max_data_points_to_return = Http::post('points', Config::get('metricstab', 1440));
161+
$filter = Http::post('filter', Config::get('metricstab', '1d'));
162162

163-
$stmt = $this->pdo->prepare('SELECT * FROM metrics ORDER BY id DESC LIMIT :limit');
164-
$stmt->bindValue(':limit', $max_data_points_to_return, PDO::PARAM_INT);
163+
$seconds = match ($filter) {
164+
'1h' => 3600,
165+
'1w' => 604800,
166+
'1m' => 2592000,
167+
default => 86400,
168+
};
169+
170+
$time_ago = time() - $seconds;
171+
172+
$stmt = $this->pdo->prepare('SELECT * FROM metrics WHERE timestamp >= :time_ago ORDER BY id DESC');
173+
$stmt->bindValue(':time_ago', $time_ago, PDO::PARAM_INT);
165174
$stmt->execute();
166175

167176
return array_reverse($stmt->fetchAll(PDO::FETCH_ASSOC));

templates/dashboards/memcached/metrics.twig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{{ include('components/tabs.twig', {
22
tabs: true,
33
pills: true,
4-
selected: config('metricstab', 1440),
4+
selected: config('metricstab', '1d'),
55
links: {
6-
60: 'Last hour',
7-
1440: 'Last day',
8-
10080: 'Last week',
9-
43200: 'Last month',
6+
'1h': 'Last hour',
7+
'1d': 'Last day',
8+
'1w': 'Last week',
9+
'1m': 'Last month',
1010
},
1111
}) }}
1212

templates/dashboards/redis/metrics.twig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{{ include('components/tabs.twig', {
22
tabs: true,
33
pills: true,
4-
selected: config('metricstab', 1440),
4+
selected: config('metricstab', '1d'),
55
links: {
6-
60: 'Last hour',
7-
1440: 'Last day',
8-
10080: 'Last week',
9-
43200: 'Last month',
6+
'1h': 'Last hour',
7+
'1d': 'Last day',
8+
'1w': 'Last week',
9+
'1m': 'Last month',
1010
},
1111
}) }}
1212

templates/layout.twig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
3131
const panels_refresh_interval = {{ config('panelrefresh', 30) * 1000 }};
3232
const ajax_panels = {{ ajax_panels is defined and ajax_panels == true ? 'true' : 'false' }};
33-
let metrics_active_filter = {{ config('metricstab', 1440) }};
33+
const metrics_refresh_interval = {{ config('metricsrefresh', 30) * 1000 }};
34+
let metrics_active_filter = '{{ config('metricstab', '1d') }}';
3435
const decimalsep = '{{ config('decimalsep', ',') }}';
3536
const thousandssep = '{{ config('thousandssep', ' ') }}';
3637
</script>

0 commit comments

Comments
 (0)