From a5e0a3f2c53063bb17c7c488a1caa0c03351fe16 Mon Sep 17 00:00:00 2001 From: Dirk Heilig Date: Mon, 23 Sep 2024 14:19:01 +0200 Subject: [PATCH] filter old date before they fade out --- mqtt2prom/run | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/mqtt2prom/run b/mqtt2prom/run index 52e6991..b1ba4d5 100755 --- a/mqtt2prom/run +++ b/mqtt2prom/run @@ -374,6 +374,34 @@ function filter(): void return $v > time() - 60 * 60 * 24 * 14; }); } + // remove data points with the same name and labels, keep the newest + $filterEntries = []; + foreach ($data as $key => $entry) { + $sort = $entry["labels"] ?? []; + ksort($sort); + $sort[] = $entry["name"]; + $hash = md5(strval(json_encode($sort))); + if (!isset($filterEntries[$hash])) { + $filterEntries[$hash] = [ + "key" => $key, + "timestamp" => $entry["timestamp"], + ]; + continue; + } + if ($filterEntries[$hash]["timestamp"] < $entry["timestamp"]) { + $filterEntries[$hash] = [ + "key" => $key, + "timestamp" => $entry["timestamp"], + ]; + } + } + $keysToKeep = array_map(function ($v) { + return $v["key"]; + }, $filterEntries); + $data = array_filter((array) $data, function ($key) use ($keysToKeep) { + return in_array($key, $keysToKeep); + }); + $data = array_values($data); } function getEnvWithDefaultStr(string $key, string $default): string