Skip to content

Commit

Permalink
fix: charts data slice (#17)
Browse files Browse the repository at this point in the history
* fix: charts data slice

* feat: shrink the chart data array size down once a while

---------

Co-authored-by: kunish <kunish.butt@gmail.com>
  • Loading branch information
Zephyruso and kunish authored Aug 29, 2023
1 parent 36cc05f commit 35f8042
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/pages/Overview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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' },
Expand All @@ -55,6 +62,18 @@ export default () => {
[],
)
const [memories, setMemories] = createSignal<number[]>([])
// 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()}}`,
Expand All @@ -74,7 +93,7 @@ export default () => {
const t = traffic()

if (t) {
setTraffics((traffics) => [...traffics, t].slice(-10))
setTraffics((traffics) => [...traffics, t])
}
})

Expand Down Expand Up @@ -112,7 +131,7 @@ export default () => {
const m = memory()

if (m) {
setMemories((memories) => [...memories, m].slice(-10))
setMemories((memories) => [...memories, m])
}
})

Expand Down

0 comments on commit 35f8042

Please sign in to comment.