-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(18161): Change the persistence of the metrics to a local sto…
…re (#243) * feat(18161): add a store for the metrics * refactor(18161): replace the deprecated localStorage with the new met… * test(18161): add tests * chore(18077): little bit of cleaning
- Loading branch information
Showing
4 changed files
with
204 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
hivemq-edge/src/frontend/src/modules/Metrics/hooks/useMetricsStore.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { expect } from 'vitest' | ||
import { act, renderHook } from '@testing-library/react' | ||
|
||
import useMetricsStore, { | ||
MetricDefinitionSpec, | ||
MetricDefinitionStore, | ||
} from '@/modules/Metrics/hooks/useMetricsStore.ts' | ||
import { ChartType } from '@/modules/Metrics/types.ts' | ||
|
||
describe('useMetricsStore', () => { | ||
beforeEach(() => { | ||
const { result } = renderHook<MetricDefinitionStore, unknown>(useMetricsStore) | ||
act(() => { | ||
result.current.reset() | ||
}) | ||
}) | ||
|
||
it('should start with an empty store', async () => { | ||
const { result } = renderHook<MetricDefinitionStore, unknown>(useMetricsStore) | ||
const { metrics } = result.current | ||
|
||
expect(metrics).toHaveLength(0) | ||
}) | ||
|
||
it('should add a metric to the store', async () => { | ||
const { result } = renderHook<MetricDefinitionStore, unknown>(useMetricsStore) | ||
const { metrics } = result.current | ||
|
||
expect(metrics).toHaveLength(0) | ||
|
||
act(() => { | ||
const { addMetrics } = result.current | ||
addMetrics('nodeId', 'metricNAme', ChartType.SAMPLE) | ||
}) | ||
|
||
expect(result.current.metrics).toHaveLength(1) | ||
}) | ||
|
||
it('should remove a metric to the store', async () => { | ||
const { result } = renderHook<MetricDefinitionStore, unknown>(useMetricsStore) | ||
const { metrics } = result.current | ||
|
||
expect(metrics).toHaveLength(0) | ||
|
||
act(() => { | ||
const { addMetrics } = result.current | ||
addMetrics('nodeId', 'metricName1', ChartType.SAMPLE) | ||
addMetrics('nodeId', 'metricName2', ChartType.SAMPLE) | ||
addMetrics('anotherNodeId', 'metricName1', ChartType.SAMPLE) | ||
}) | ||
|
||
expect(result.current.metrics).toHaveLength(3) | ||
|
||
act(() => { | ||
const { removeMetrics } = result.current | ||
removeMetrics('nodeId', 'metricName1') | ||
}) | ||
|
||
expect(result.current.metrics).toStrictEqual<MetricDefinitionSpec[]>([ | ||
{ | ||
chart: ChartType.SAMPLE, | ||
metrics: 'metricName2', | ||
source: 'nodeId', | ||
}, | ||
{ | ||
chart: ChartType.SAMPLE, | ||
metrics: 'metricName1', | ||
source: 'anotherNodeId', | ||
}, | ||
]) | ||
}) | ||
|
||
it('should return all the metrics for a node', async () => { | ||
const { result } = renderHook<MetricDefinitionStore, unknown>(useMetricsStore) | ||
const { metrics } = result.current | ||
|
||
expect(metrics).toHaveLength(0) | ||
|
||
act(() => { | ||
const { addMetrics } = result.current | ||
addMetrics('nodeId', 'metricName1', ChartType.SAMPLE) | ||
addMetrics('nodeId', 'metricName2', ChartType.SAMPLE) | ||
addMetrics('anotherNodeId', 'metricName1', ChartType.SAMPLE) | ||
}) | ||
|
||
expect(result.current.metrics).toHaveLength(3) | ||
|
||
act(() => { | ||
const { getMetricsFor } = result.current | ||
const res = getMetricsFor('nodeId') | ||
expect(res).toStrictEqual<MetricDefinitionSpec[]>([ | ||
{ | ||
chart: ChartType.SAMPLE, | ||
metrics: 'metricName1', | ||
source: 'nodeId', | ||
}, | ||
{ | ||
chart: ChartType.SAMPLE, | ||
metrics: 'metricName2', | ||
source: 'nodeId', | ||
}, | ||
]) | ||
}) | ||
}) | ||
|
||
it('should reset the store', async () => { | ||
const { result } = renderHook<MetricDefinitionStore, unknown>(useMetricsStore) | ||
const { metrics } = result.current | ||
|
||
expect(metrics).toHaveLength(0) | ||
|
||
act(() => { | ||
const { addMetrics } = result.current | ||
addMetrics('nodeId', 'metricName1', ChartType.SAMPLE) | ||
addMetrics('nodeId', 'metricName2', ChartType.SAMPLE) | ||
addMetrics('anotherNodeId', 'metricName1', ChartType.SAMPLE) | ||
}) | ||
|
||
expect(result.current.metrics).toHaveLength(3) | ||
|
||
act(() => { | ||
const { reset } = result.current | ||
reset() | ||
}) | ||
|
||
expect(result.current.metrics).toHaveLength(0) | ||
}) | ||
}) |
48 changes: 48 additions & 0 deletions
48
hivemq-edge/src/frontend/src/modules/Metrics/hooks/useMetricsStore.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { create } from 'zustand' | ||
import { persist, createJSONStorage } from 'zustand/middleware' | ||
|
||
import { ChartType } from '../types.ts' | ||
|
||
export interface MetricDefinitionSpec { | ||
source: string | ||
metrics: string | ||
chart?: ChartType | ||
} | ||
|
||
export interface MetricDefinitionStore { | ||
metrics: MetricDefinitionSpec[] | ||
reset: () => void | ||
addMetrics: (source: string, metrics: string, chart?: ChartType) => void | ||
removeMetrics: (source: string, metrics: string) => void | ||
getMetricsFor: (source: string) => MetricDefinitionSpec[] | ||
} | ||
|
||
const useMetricsStore = create<MetricDefinitionStore>()( | ||
persist( | ||
(set, get) => ({ | ||
metrics: [], | ||
reset: () => { | ||
set({ metrics: [] }) | ||
}, | ||
addMetrics: (source: string, metrics, chart) => { | ||
set({ | ||
metrics: [...get().metrics, { source: source, metrics: metrics, chart: chart }], | ||
}) | ||
}, | ||
removeMetrics: (source: string, metrics: string) => { | ||
set({ | ||
metrics: get().metrics.filter((e) => e.source !== source || e.metrics !== metrics), | ||
}) | ||
}, | ||
getMetricsFor: (source: string) => { | ||
return get().metrics.filter((m) => m.source === source) | ||
}, | ||
}), | ||
{ | ||
name: 'edge.observability', | ||
storage: createJSONStorage(() => localStorage), | ||
} | ||
) | ||
) | ||
|
||
export default useMetricsStore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters