From 11595abc934d951cdd24db4b5873deaef98f5820 Mon Sep 17 00:00:00 2001 From: Jari Kolehmainen Date: Tue, 12 Jan 2021 12:01:06 +0200 Subject: [PATCH] Fix fill the gaps logic in normalize metrics (#1940) * fix fill the gaps logic in normalize metrics Signed-off-by: Jari Kolehmainen * fix Signed-off-by: Jari Kolehmainen * fix Signed-off-by: Jari Kolehmainen --- src/renderer/api/endpoints/metrics.api.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/renderer/api/endpoints/metrics.api.ts b/src/renderer/api/endpoints/metrics.api.ts index a530c68506..9c3ee74adc 100644 --- a/src/renderer/api/endpoints/metrics.api.ts +++ b/src/renderer/api/endpoints/metrics.api.ts @@ -78,10 +78,24 @@ export function normalizeMetrics(metrics: IMetrics, frames = 60): IMetrics { result.forEach(res => { if (!res.values || !res.values.length) return; + let now = moment().startOf("minute").subtract(1, "minute").unix(); + let timestamp = res.values[0][0]; + + while (timestamp <= now) { + timestamp = moment.unix(timestamp).add(1, "minute").unix(); + + if (!res.values.find((value) => value[0] === timestamp)) { + res.values.push([timestamp, "0"]); + } + } + while (res.values.length < frames) { const timestamp = moment.unix(res.values[0][0]).subtract(1, "minute").unix(); - res.values.unshift([timestamp, "0"]); + if (!res.values.find((value) => value[0] === timestamp)) { + res.values.unshift([timestamp, "0"]); + } + now = timestamp; } }); }