From 35f80426ce083265ef469bf0d20b0ba219a37495 Mon Sep 17 00:00:00 2001 From: Zephyruso <127948745+Zephyruso@users.noreply.github.com> Date: Tue, 29 Aug 2023 17:45:07 +0800 Subject: [PATCH] fix: charts data slice (#17) * fix: charts data slice * feat: shrink the chart data array size down once a while --------- Co-authored-by: kunish --- src/pages/Overview.tsx | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/pages/Overview.tsx b/src/pages/Overview.tsx index 856cf96..b166f15 100644 --- a/src/pages/Overview.tsx +++ b/src/pages/Overview.tsx @@ -10,10 +10,13 @@ import { createEffect, createMemo, createSignal, + onCleanup, } from 'solid-js' import { secret, wsEndpointURL } from '~/signals' import type { Connection } from '~/types' +const CHART_MAX_XAXIS = 10 + const defaultChartOptions: ApexOptions = { chart: { toolbar: { show: false }, @@ -30,7 +33,11 @@ const defaultChartOptions: ApexOptions = { grid: { yaxis: { lines: { show: false } } }, stroke: { curve: 'smooth' }, tooltip: { enabled: false }, - xaxis: { labels: { show: false }, axisTicks: { show: false } }, + xaxis: { + range: CHART_MAX_XAXIS, + labels: { show: false }, + axisTicks: { show: false }, + }, yaxis: { labels: { style: { colors: 'gray' }, @@ -55,6 +62,18 @@ export default () => { [], ) const [memories, setMemories] = createSignal([]) + // https://github.com/apexcharts/apexcharts.js/blob/main/samples/source/line/realtime.xml + // TODO: need a better way + const preventLeakTimer = setInterval( + () => { + setTraffics((traffics) => traffics.slice(-CHART_MAX_XAXIS)) + setMemories((memo) => memo.slice(-CHART_MAX_XAXIS)) + }, + // we shrink the chart data array size down every 10 minutes + 10 * 60 * 1000, + ) + + onCleanup(() => clearInterval(preventLeakTimer)) const trafficWS = createReconnectingWS( `${wsEndpointURL()}/traffic?token=${secret()}}`, @@ -74,7 +93,7 @@ export default () => { const t = traffic() if (t) { - setTraffics((traffics) => [...traffics, t].slice(-10)) + setTraffics((traffics) => [...traffics, t]) } }) @@ -112,7 +131,7 @@ export default () => { const m = memory() if (m) { - setMemories((memories) => [...memories, m].slice(-10)) + setMemories((memories) => [...memories, m]) } })