master
Dirk Heilig 2024-09-09 19:33:21 +02:00
parent f61c21fbad
commit 5f563be51b
1 changed files with 7 additions and 4 deletions

View File

@ -72,17 +72,17 @@ $mqtt->onMessage(function ($message) use (
} }
$globalCounter++; $globalCounter++;
$stats["messages"][] = time(); $stats["messages"][] = time();
if (!precheck($message)) { if (!precheck($message)) {
$stats["errors"][] = time(); $stats["errors"][] = time();
return; return;
} }
$stats["metrics"][] = time(); $stats["metrics"][] = time();
$payload = (array) json_decode($message->payload, true); $payload = (array) json_decode($message->payload, true);
$payload["timestamp"] = time();
if (!isset($payload["valid_seconds"])) { if (!isset($payload["valid_seconds"])) {
$payload["valid_seconds"] = 60; $payload["valid_seconds"] = 60;
} }
$payload["timestamp"] = time();
$data[] = $payload; $data[] = $payload;
$news = true; $news = true;
}); });
@ -177,6 +177,9 @@ function precheck(Message $message): bool
} }
} }
if (!isset($payload["valid_seconds"])) {
$payload["valid_seconds"] = 60;
}
if (!is_numeric($payload["valid_seconds"])) { if (!is_numeric($payload["valid_seconds"])) {
error("valid_seconds must be a number"); error("valid_seconds must be a number");
return false; return false;
@ -326,10 +329,10 @@ function filter(): void
$data = array_filter($data, function ($entry) { $data = array_filter($data, function ($entry) {
return $entry["timestamp"] > time() - $entry["valid_seconds"]; return $entry["timestamp"] > time() - $entry["valid_seconds"];
}); });
// remove stats older than 24h // remove stats older than 48h
foreach ($stats as $key => $values) { foreach ($stats as $key => $values) {
$stats[$key] = array_filter($values, function ($v) { $stats[$key] = array_filter($values, function ($v) {
return $v > time() - 24 * 60 * 60; return $v > time() - 48 * 60 * 60;
}); });
} }
} }