v2.1.29 - Remove SQLite support, hardcode PostgreSQL backend, fix admin API to use first_seen, add pgweb systemd service
This commit is contained in:
+20
-3
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
/**
|
||||
* admin2/api/chart.php — Standalone ASCII chart endpoint.
|
||||
* admin/api/chart.php — Standalone ASCII chart endpoint.
|
||||
*
|
||||
* Returns a plain-text ASCII X-bar chart of event counts over time.
|
||||
* Works in both the browser (injected into a <div>) and the terminal:
|
||||
@@ -10,8 +10,22 @@
|
||||
* curl http://localhost:8088/api/chart.php?range=month
|
||||
* curl http://localhost:8088/api/chart.php?range=year
|
||||
*
|
||||
* This endpoint is read-only (a fixed aggregate COUNT query with the only
|
||||
* user input being the `range` selector, which is validated against a
|
||||
* whitelist). It is safe to expose publicly and is served without auth at:
|
||||
*
|
||||
* https://<domain>/relay/api/chart.php?range=day
|
||||
*
|
||||
* It is also reachable (behind Basic Auth) from the admin UI at:
|
||||
*
|
||||
* https://<domain>/relay/admin/api/chart.php?range=day
|
||||
*
|
||||
* Caching: the hour chart is never cached (live). Day/month/year are
|
||||
* cached to file with TTLs to avoid expensive queries on every request.
|
||||
*
|
||||
* NOTE: Uses first_seen (not created_at) for binning, because some events
|
||||
* have corrupted created_at timestamps (far-future values) that would
|
||||
* place them outside the visible chart range.
|
||||
*/
|
||||
|
||||
require_once __DIR__ . '/../lib/helpers.php';
|
||||
@@ -59,9 +73,12 @@ try {
|
||||
// bin index 0 = oldest, bin index (num_bins-1) = newest
|
||||
$base_bin = (int)floor($epoch / $bin_size);
|
||||
|
||||
$sql = "SELECT FLOOR(created_at / {$bin_size})::BIGINT - {$base_bin} AS bin, COUNT(*) AS cnt
|
||||
// Use first_seen (not created_at) for binning, because some events
|
||||
// have corrupted created_at timestamps (far-future values) that
|
||||
// would place them outside the visible chart range.
|
||||
$sql = "SELECT FLOOR(first_seen / {$bin_size})::BIGINT - {$base_bin} AS bin, COUNT(*) AS cnt
|
||||
FROM events
|
||||
WHERE created_at >= {$epoch}
|
||||
WHERE first_seen >= {$epoch}
|
||||
GROUP BY bin
|
||||
ORDER BY bin";
|
||||
$rows = $pdo->query($sql)->fetchAll();
|
||||
|
||||
Reference in New Issue
Block a user