@davra/ui-core 1.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/README.md +183 -0
  2. package/dist/style.css +1 -0
  3. package/dist/types/components/ComponentA.vue.d.ts +12 -0
  4. package/dist/types/components/ComponentB.vue.d.ts +2 -0
  5. package/dist/types/components/index.d.ts +3 -0
  6. package/dist/types/constants/MyConstants.d.ts +1 -0
  7. package/dist/types/constants/index.d.ts +2 -0
  8. package/dist/types/index.d.ts +10 -0
  9. package/dist/types/services/davraApi.d.ts +5 -0
  10. package/dist/types/services/devicesCountersService.d.ts +10 -0
  11. package/dist/types/services/devicesCountersService.test.d.ts +1 -0
  12. package/dist/types/services/devicesService.d.ts +18 -0
  13. package/dist/types/services/devicesService.test.d.ts +1 -0
  14. package/dist/types/services/index.d.ts +10 -0
  15. package/dist/types/services/labelsService.d.ts +8 -0
  16. package/dist/types/services/labelsService.test.d.ts +1 -0
  17. package/dist/types/services/metricsCountersService.d.ts +5 -0
  18. package/dist/types/services/metricsCountersService.test.d.ts +1 -0
  19. package/dist/types/services/metricsService.d.ts +6 -0
  20. package/dist/types/services/metricsService.test.d.ts +1 -0
  21. package/dist/types/services/timeseriesService.d.ts +5 -0
  22. package/dist/types/services/timeseriesService.test.d.ts +1 -0
  23. package/dist/types/services/twinTypesService.d.ts +5 -0
  24. package/dist/types/services/twinTypesService.test.d.ts +1 -0
  25. package/dist/types/services/twinsCountersService.d.ts +6 -0
  26. package/dist/types/services/twinsCountersService.test.d.ts +1 -0
  27. package/dist/types/services/twinsService.d.ts +22 -0
  28. package/dist/types/services/twinsService.test.d.ts +1 -0
  29. package/dist/types/services/userSessionService.d.ts +12 -0
  30. package/dist/types/services/userSessionService.test.d.ts +1 -0
  31. package/dist/types/stores/alertMessages.d.ts +19 -0
  32. package/dist/types/stores/alertMessages.test.d.ts +1 -0
  33. package/dist/types/stores/devices.d.ts +42 -0
  34. package/dist/types/stores/devices.test.d.ts +1 -0
  35. package/dist/types/stores/index.d.ts +7 -0
  36. package/dist/types/stores/labels.d.ts +20 -0
  37. package/dist/types/stores/labels.test.d.ts +1 -0
  38. package/dist/types/stores/metrics.d.ts +39 -0
  39. package/dist/types/stores/metrics.test.d.ts +1 -0
  40. package/dist/types/stores/twinTypes.d.ts +27 -0
  41. package/dist/types/stores/twinTypes.test.d.ts +1 -0
  42. package/dist/types/stores/twins.d.ts +42 -0
  43. package/dist/types/stores/twins.test.d.ts +1 -0
  44. package/dist/types/stores/userSession.d.ts +24 -0
  45. package/dist/types/stores/userSession.test.d.ts +1 -0
  46. package/dist/types/types.d.ts +175 -0
  47. package/dist/types/utils/MyUtil.d.ts +5 -0
  48. package/dist/types/utils/index.d.ts +2 -0
  49. package/dist/ui-core.es.js +1982 -0
  50. package/package.json +46 -0
  51. package/src/assets/fonts/myfont.woff +0 -0
  52. package/src/assets/main.scss +17 -0
  53. package/src/auto-imports.d.ts +200 -0
  54. package/src/components/ComponentA.vue +13 -0
  55. package/src/components/ComponentB.vue +26 -0
  56. package/src/components/index.ts +7 -0
  57. package/src/constants/MyConstants.ts +1 -0
  58. package/src/constants/index.ts +5 -0
  59. package/src/env.d.ts +8 -0
  60. package/src/index.ts +23 -0
  61. package/src/services/davraApi.ts +18 -0
  62. package/src/services/devicesCountersService.test.ts +209 -0
  63. package/src/services/devicesCountersService.ts +117 -0
  64. package/src/services/devicesService.test.ts +207 -0
  65. package/src/services/devicesService.ts +110 -0
  66. package/src/services/index.ts +22 -0
  67. package/src/services/labelsService.test.ts +124 -0
  68. package/src/services/labelsService.ts +71 -0
  69. package/src/services/metricsCountersService.test.ts +44 -0
  70. package/src/services/metricsCountersService.ts +24 -0
  71. package/src/services/metricsService.test.ts +97 -0
  72. package/src/services/metricsService.ts +54 -0
  73. package/src/services/timeseriesService.test.ts +86 -0
  74. package/src/services/timeseriesService.ts +24 -0
  75. package/src/services/twinTypesService.test.ts +74 -0
  76. package/src/services/twinTypesService.ts +24 -0
  77. package/src/services/twinsCountersService.test.ts +72 -0
  78. package/src/services/twinsCountersService.ts +40 -0
  79. package/src/services/twinsService.test.ts +228 -0
  80. package/src/services/twinsService.ts +137 -0
  81. package/src/services/userSessionService.test.ts +74 -0
  82. package/src/services/userSessionService.ts +82 -0
  83. package/src/stores/alertMessages.test.ts +27 -0
  84. package/src/stores/alertMessages.ts +26 -0
  85. package/src/stores/devices.test.ts +149 -0
  86. package/src/stores/devices.ts +78 -0
  87. package/src/stores/index.ts +12 -0
  88. package/src/stores/labels.test.ts +72 -0
  89. package/src/stores/labels.ts +39 -0
  90. package/src/stores/metrics.test.ts +116 -0
  91. package/src/stores/metrics.ts +71 -0
  92. package/src/stores/twinTypes.test.ts +71 -0
  93. package/src/stores/twinTypes.ts +36 -0
  94. package/src/stores/twins.test.ts +148 -0
  95. package/src/stores/twins.ts +78 -0
  96. package/src/stores/userSession.test.ts +107 -0
  97. package/src/stores/userSession.ts +57 -0
  98. package/src/types.ts +173 -0
  99. package/src/utils/MyUtil.ts +7 -0
  100. package/src/utils/index.ts +5 -0
@@ -0,0 +1,149 @@
1
+ import { createPinia, setActivePinia } from 'pinia'
2
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
3
+ import { useDevicesStore } from './devices'
4
+ import devicesService from '~/services/devicesService'
5
+ import type { Device } from '~/types'
6
+ vi.mock('~/services/davraApi')
7
+
8
+ describe('Devices Store', () => {
9
+ beforeEach(() => {
10
+ // creates a fresh pinia and make it active so it's automatically picked
11
+ // up by any useStore() call without having to pass it to it:
12
+ // `useStore(pinia)`
13
+ setActivePinia(createPinia())
14
+ })
15
+
16
+ it('should fetch devices by UUIDs and update the collection', async () => {
17
+ const store = useDevicesStore()
18
+
19
+ // Mock the DevicesService
20
+ const mockedDevice = { UUID: '123', name: 'Mocked device' } as Device
21
+ const mockedDevicesRequest = { records: [mockedDevice], totalRecords: 1 }
22
+ const mockedGetDeviceByUUIDs = vi.spyOn(devicesService, 'getDeviceByUUIDs').mockResolvedValue(mockedDevicesRequest)
23
+
24
+ // Call the store method
25
+ await store.pullDevicesByUUIDs(['123'], false)
26
+
27
+ // Check the result
28
+ expect(mockedGetDeviceByUUIDs).toHaveBeenCalledWith(['123'])
29
+ expect(store.devicesCollection).toEqual({ 123: mockedDevice })
30
+ })
31
+
32
+ it('pullDevicesByUUIDs should fetch devices by UUIDs and update the collection and purge the previous records', async () => {
33
+ const store = useDevicesStore()
34
+ store.devicesCollection = { qwe124: { UUID: 'qwe124', name: 'Mocked device' } as Device }
35
+ // Mock the DevicesService
36
+ const mockedDevice = { UUID: '123', name: 'Mocked device' } as Device
37
+ const mockedDevicesRequest = { records: [mockedDevice], totalRecords: 1 }
38
+ const mockedGetDeviceByUUIDs = vi.spyOn(devicesService, 'getDeviceByUUIDs').mockResolvedValue(mockedDevicesRequest)
39
+
40
+ // Call the store method
41
+ const result = await store.pullDevicesByUUIDs(['qwe124', '123'], true)
42
+
43
+ // Check the result
44
+ expect(mockedGetDeviceByUUIDs).toHaveBeenCalledWith(['qwe124', '123'])
45
+ expect(store.devicesCollection).toEqual({ 123: mockedDevice })
46
+ expect(result).toEqual(mockedDevicesRequest)
47
+ })
48
+
49
+ it('pullDevicesByUUIDs should return empty records when the api throw an error', async () => {
50
+ const store = useDevicesStore()
51
+ store.devicesCollection = { qwe124: { UUID: 'qwe124', name: 'Mocked device' } as Device }
52
+ // Mock the DevicesService
53
+ const mockedGetDeviceByUUIDs = vi.spyOn(devicesService, 'getDeviceByUUIDs').mockRejectedValue(new Error('ERROR'))
54
+
55
+ // Call the store method
56
+ await store.pullDevicesByUUIDs(['qwe124', '123'], true)
57
+
58
+ // Check the result
59
+ expect(mockedGetDeviceByUUIDs).toHaveBeenCalledWith(['qwe124', '123'])
60
+ expect(store.devicesCollection).toEqual({})
61
+ })
62
+
63
+ it('pullDevices should fetch devices and update the collection', async () => {
64
+ const store = useDevicesStore()
65
+
66
+ // Mock the DevicesService
67
+ const mockedDevice = { UUID: '123', name: 'Mocked device' } as Device
68
+ const mockedDevicesRequest = { totalRecords: 1, records: [mockedDevice] }
69
+ const mockedGetDevices = vi.spyOn(devicesService, 'getDevices').mockResolvedValue(mockedDevicesRequest)
70
+
71
+ // Call the store method
72
+ const result = await store.pullDevices(0, 10, '', '')
73
+
74
+ // Check the result
75
+ expect(mockedGetDevices).toHaveBeenCalledWith(0, 10, '', '')
76
+ expect(store.devicesCollection).toEqual({ 123: mockedDevice })
77
+ expect(result).toEqual(mockedDevicesRequest)
78
+ })
79
+
80
+ it('pullDevices should return empty records when the api throw an error', async () => {
81
+ const store = useDevicesStore()
82
+
83
+ // Mock the DevicesService
84
+ const mockedGetDevices = vi.spyOn(devicesService, 'getDevices').mockRejectedValue(new Error('ERROR'))
85
+
86
+ // Call the store method
87
+ const result = await store.pullDevices(0, 10, '', '')
88
+
89
+ // Check the result
90
+ expect(mockedGetDevices).toHaveBeenCalledWith(0, 10, '', '')
91
+ expect(store.devicesCollection).toEqual({})
92
+ expect(result).toEqual({
93
+ totalRecords: 0,
94
+ records: [],
95
+ })
96
+ })
97
+
98
+ it('getDeviceFromUUID should fetch devices and update the collection', async () => {
99
+ const store = useDevicesStore()
100
+
101
+ // Mock the DevicesService
102
+ const mockedDevice = { UUID: '123', name: 'Mocked device' } as Device
103
+ const mockedGetDevices = vi.spyOn(devicesService, 'getDeviceByUUID').mockResolvedValue(mockedDevice)
104
+
105
+ // Call the store method
106
+ const result = await store.getDeviceFromUUID('123')
107
+
108
+ // Check the result
109
+ expect(mockedGetDevices).toHaveBeenCalledWith('123')
110
+ expect(store.devicesCollection).toEqual({ 123: mockedDevice })
111
+ expect(result).toEqual(mockedDevice)
112
+ })
113
+
114
+ it('getDeviceFromUUID should return the device if it is already in the cache', async () => {
115
+ const store = useDevicesStore()
116
+ store.devicesCollection = { 123: { UUID: '123', name: 'Mocked device' } as Device }
117
+
118
+ // Mock the DevicesService
119
+ const mockedDevice = { UUID: '123', name: 'Mocked device' } as Device
120
+ const mockedGetDevices = vi.spyOn(devicesService, 'getDeviceByUUID').mockResolvedValue(mockedDevice)
121
+
122
+ // Call the store method
123
+ const result = await store.getDeviceFromUUID('123')
124
+
125
+ // Check the result
126
+ expect(mockedGetDevices).not.toHaveBeenCalled()
127
+ expect(store.devicesCollection).toEqual({ 123: mockedDevice })
128
+ expect(result).toEqual(mockedDevice)
129
+ })
130
+
131
+ it('getDeviceFromUUID should return empty records when the api throw an error', async () => {
132
+ const store = useDevicesStore()
133
+
134
+ // Mock the DevicesService
135
+ const mockedGetDevices = vi.spyOn(devicesService, 'getDeviceByUUID').mockRejectedValue(new Error('ERROR'))
136
+
137
+ // Call the store method
138
+ const result = await store.getDeviceFromUUID('123')
139
+
140
+ // Check the result
141
+ expect(mockedGetDevices).toHaveBeenCalledWith('123')
142
+ expect(store.devicesCollection).toEqual({ })
143
+ expect(result).toEqual(null)
144
+ })
145
+
146
+ afterEach(() => {
147
+ vi.restoreAllMocks()
148
+ })
149
+ })
@@ -0,0 +1,78 @@
1
+ import { acceptHMRUpdate, defineStore } from 'pinia'
2
+ import type { Ref } from 'vue'
3
+ import DevicesService from '~/services/devicesService'
4
+ import type { Device } from '~/types'
5
+
6
+ export const useDevicesStore = defineStore('devices', () => {
7
+ const devicesCollection: Ref<{ [UUID: string]: Device }> = ref({})
8
+
9
+ const pullDevicesByUUIDs = async (uuids: string[], forceClear = false) => {
10
+ if (forceClear) {
11
+ uuids.forEach((uuid) => {
12
+ delete devicesCollection.value[uuid]
13
+ })
14
+ }
15
+ try {
16
+ const devicesRequest = await DevicesService.getDeviceByUUIDs(uuids)
17
+ devicesRequest.records.forEach((device: Device) => {
18
+ // may need to sanitize the devices
19
+ devicesCollection.value[device.UUID] = device
20
+ })
21
+ return devicesRequest
22
+ }
23
+ catch (err) {
24
+ return {
25
+ totalRecords: 0,
26
+ records: [],
27
+ }
28
+ }
29
+ }
30
+
31
+ const pullDevices = async (start: number, limit: number, sort: string, filters: string) => {
32
+ try {
33
+ const devicesRequest = await DevicesService.getDevices(start, limit, sort, filters)
34
+ devicesRequest.records.forEach((device: Device) => {
35
+ // may need to sanitize the devices
36
+ devicesCollection.value[device.UUID] = device
37
+ })
38
+ return devicesRequest
39
+ }
40
+ catch (err) {
41
+ return {
42
+ totalRecords: 0,
43
+ records: [],
44
+ }
45
+ }
46
+ }
47
+
48
+ const getDeviceFromUUID = async (uuid: string, fromCache?: boolean) => {
49
+ const deviceCache = devicesCollection.value[uuid]
50
+ if (deviceCache) {
51
+ return deviceCache
52
+ }
53
+ else if (!fromCache) {
54
+ try {
55
+ const device = await DevicesService.getDeviceByUUID(uuid)
56
+ if (device) {
57
+ devicesCollection.value[device.UUID] = device
58
+ return device
59
+ }
60
+ }
61
+ catch (err) {
62
+ return null
63
+ }
64
+ }
65
+ else {
66
+ return null
67
+ }
68
+ }
69
+
70
+ return {
71
+ pullDevicesByUUIDs,
72
+ pullDevices,
73
+ devicesCollection,
74
+ getDeviceFromUUID,
75
+ }
76
+ })
77
+ if (import.meta.hot)
78
+ import.meta.hot.accept(acceptHMRUpdate(useDevicesStore, import.meta.hot))
@@ -0,0 +1,12 @@
1
+
2
+ export { useAlertMessagesStore } from './alertMessages'
3
+
4
+ export { useUserSessionStore } from './userSession'
5
+
6
+ export { useDevicesStore } from './devices'
7
+ export { useTwinsStore } from './twins'
8
+ export { useLabelsStore } from './labels'
9
+ export { useTwinTypesStore } from './twinTypes'
10
+ export { useMetricsStore } from './metrics'
11
+
12
+
@@ -0,0 +1,72 @@
1
+ import { createPinia, setActivePinia } from 'pinia'
2
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
3
+ import { useLabelsStore } from './labels'
4
+ import { useTwinTypesStore } from './twinTypes'
5
+ import labelsService from '~/services/labelsService'
6
+
7
+ describe('Labels Store', () => {
8
+ beforeEach(() => {
9
+ // creates a fresh pinia and make it active so it's automatically picked
10
+ // up by any useStore() call without having to pass it to it:
11
+ // `useStore(pinia)`
12
+ vi.mock('~/services/davraApi')
13
+ setActivePinia(createPinia())
14
+ })
15
+
16
+ afterEach(() => {
17
+ vi.restoreAllMocks()
18
+ })
19
+
20
+ it('init store with empty setting', () => {
21
+ const labelsStore = useLabelsStore()
22
+ expect(labelsStore.labels).toEqual([])
23
+ })
24
+
25
+ it('pullLabels to an empty store', async () => {
26
+ const mockLabels = ['label1', 'label2']
27
+
28
+ const labelStore = useLabelsStore()
29
+
30
+ const spyGetLabels = vi.spyOn(labelsService, 'getLabels').mockResolvedValue(mockLabels)
31
+
32
+ await labelStore.pullLabels()
33
+
34
+ expect(spyGetLabels).toHaveBeenCalledOnce()
35
+
36
+ expect(labelStore.labels).toEqual(mockLabels)
37
+ })
38
+
39
+ it('updateLabels to an empty store', async () => {
40
+ const mockLabels = { label3: 'hi' }
41
+ const mockTwinTypes = { _id: '5fbd141b5d07a511c71dec1c', name: 'twinType1', description: 'sample description', labels: {}, customAttributes: {}, tenantId: 'ruban', UUID: '33b4de5a-800e-4314-9c2b-bcb3d30f37e8', created: 1606226971844, owner: '04fdbec2a4a7477b856c63fc128f3ca8' }
42
+
43
+ const labelStore = useLabelsStore()
44
+ const twinStores = useTwinTypesStore()
45
+ twinStores.twinTypes = [mockTwinTypes]
46
+ const spyGetLabels = vi.spyOn(labelsService, 'getLabels').mockResolvedValue(['label3'])
47
+ const spyCreateLabels = vi.spyOn(labelsService, 'createLabel').mockResolvedValue(true)
48
+
49
+ await labelStore.updateLabels(mockLabels)
50
+
51
+ expect(spyGetLabels).toHaveBeenCalledOnce()
52
+ expect(spyCreateLabels).toHaveBeenLastCalledWith(mockLabels, [mockTwinTypes.name])
53
+ })
54
+
55
+ it('handle an error without crashing', async () => {
56
+ const mockTwinTypes = { _id: '5fbd141b5d07a511c71dec1c', name: 'twinType1', description: 'sample description', labels: {}, customAttributes: {}, tenantId: 'ruban', UUID: '33b4de5a-800e-4314-9c2b-bcb3d30f37e8', created: 1606226971844, owner: '04fdbec2a4a7477b856c63fc128f3ca8' }
57
+
58
+ const twinStores = useTwinTypesStore()
59
+ twinStores.twinTypes = [mockTwinTypes]
60
+ const labelStore = useLabelsStore()
61
+ const mError = new Error('Unable to retrieve twintypes')
62
+ const spyGetLabels = vi.spyOn(labelsService, 'getLabels').mockRejectedValueOnce(mError)
63
+ const spyCreateLabel = vi.spyOn(labelsService, 'createLabel').mockRejectedValueOnce(mError)
64
+ await labelStore.pullLabels()
65
+ expect(spyGetLabels).toHaveBeenCalledOnce()
66
+
67
+ await labelStore.updateLabels({ test: 'qwe' })
68
+ expect(spyCreateLabel).toHaveBeenCalledOnce()
69
+
70
+ expect(labelStore.labels).toEqual([])
71
+ })
72
+ })
@@ -0,0 +1,39 @@
1
+ import { acceptHMRUpdate, defineStore, storeToRefs } from 'pinia'
2
+ import type { Ref } from 'vue'
3
+ import { useTwinTypesStore } from './twinTypes'
4
+ import LabelsService from '~/services/labelsService'
5
+ export const useLabelsStore = defineStore('labels', () => {
6
+ const labels: Ref<string[]> = ref([])
7
+
8
+ const pullLabels = async () => {
9
+ try {
10
+ const labelsRequest = await LabelsService.getLabels()
11
+ labels.value = labelsRequest
12
+ }
13
+ catch {
14
+
15
+ }
16
+ }
17
+
18
+ const updateLabels = async (labels: { [key: string]: string }) => {
19
+ const { twinTypeNames } = storeToRefs(useTwinTypesStore())
20
+ try {
21
+ await LabelsService.createLabel(labels, twinTypeNames.value)
22
+ await pullLabels()
23
+ }
24
+ catch {
25
+
26
+ }
27
+ }
28
+
29
+ // init store
30
+ pullLabels()
31
+
32
+ return {
33
+ labels,
34
+ pullLabels,
35
+ updateLabels,
36
+ }
37
+ })
38
+ if (import.meta.hot)
39
+ import.meta.hot.accept(acceptHMRUpdate(useLabelsStore, import.meta.hot))
@@ -0,0 +1,116 @@
1
+ import { createPinia, setActivePinia } from 'pinia'
2
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
3
+ import { useMetricsStore } from './metrics'
4
+ import MetricsService from '~/services/metricsService'
5
+ import MetricCountersService from '~/services/metricsCountersService'
6
+ import type { Metric } from '~/types'
7
+
8
+ describe('Labels Store', () => {
9
+ beforeEach(() => {
10
+ // creates a fresh pinia and make it active so it's automatically picked
11
+ // up by any useStore() call without having to pass it to it:
12
+ // `useStore(pinia)`
13
+ setActivePinia(createPinia())
14
+ })
15
+
16
+ afterEach(() => {
17
+ })
18
+
19
+ it('init store with empty setting', () => {
20
+ const metricsStore = useMetricsStore()
21
+ expect(metricsStore.metrics).toEqual([])
22
+ })
23
+
24
+ it('pullMetrics to an empty store', async () => {
25
+ const mockMetrics = [{
26
+ name: '43040_100',
27
+ label: 'Memory',
28
+ },
29
+ {
30
+ name: '43040_101',
31
+ label: 'CPU',
32
+ }]
33
+
34
+ const metricsStore = useMetricsStore()
35
+
36
+ const spyGetLabels = vi.spyOn(MetricsService, 'getMetrics').mockResolvedValue(mockMetrics)
37
+
38
+ await metricsStore.pullMetrics()
39
+
40
+ expect(spyGetLabels).toHaveBeenCalledOnce()
41
+
42
+ expect(metricsStore.metrics).toEqual(mockMetrics)
43
+ })
44
+
45
+ it('pullMetricsCounters to an empty store', async () => {
46
+ const mockMetricsCounter = [{
47
+ name: '43040_100',
48
+ count: 111,
49
+ metadata: {},
50
+ values: [],
51
+ },
52
+ {
53
+ name: '43040_101',
54
+ count: 121,
55
+ metadata: {},
56
+ values: [],
57
+
58
+ }]
59
+
60
+ const spyGetLabelsCounter = vi.spyOn(MetricCountersService, 'getMetricsCounters').mockResolvedValue(mockMetricsCounter)
61
+
62
+ const metricsStore = useMetricsStore()
63
+
64
+ await metricsStore.pullMetricsCounters()
65
+
66
+ expect(spyGetLabelsCounter).toHaveBeenCalledTimes(1)
67
+
68
+ expect(metricsStore.metricsCounters).toEqual(mockMetricsCounter)
69
+ })
70
+ it('pullMetricsCounters with an API ERROR', async () => {
71
+ const spyGetLabelsCounter = vi.spyOn(MetricCountersService, 'getMetricsCounters').mockRejectedValue(new Error('Error!'))
72
+
73
+ const metricsStore = useMetricsStore()
74
+
75
+ await metricsStore.pullMetricsCounters()
76
+
77
+ expect(spyGetLabelsCounter).toHaveBeenCalledTimes(1)
78
+
79
+ expect(metricsStore.metricsCounters).toEqual([])
80
+ })
81
+
82
+ it('saveMetric to an empty store', async () => {
83
+ const mockMetric: Metric = {
84
+ name: 'test',
85
+ label: 'test1',
86
+ }
87
+ const spyGetMetric = vi.spyOn(MetricsService, 'getMetrics').mockResolvedValue([])
88
+ const spyCreateMetric = vi.spyOn(MetricsService, 'postMetric').mockResolvedValue({})
89
+ const spyGetLabelsCounter = vi.spyOn(MetricCountersService, 'getMetricsCounters').mockResolvedValue([])
90
+
91
+ const metricsStore = useMetricsStore()
92
+
93
+ await metricsStore.saveMetric(mockMetric)
94
+
95
+ expect(spyGetMetric).toHaveBeenCalledTimes(2)
96
+ expect(spyGetLabelsCounter).toHaveBeenCalledTimes(1)
97
+ expect(spyCreateMetric).toHaveBeenLastCalledWith(mockMetric)
98
+ })
99
+ it('saveMetric to with an API ERROR', async () => {
100
+ const mockMetric: Metric = {
101
+ name: 'test',
102
+ label: 'test1',
103
+ }
104
+ const spyGetMetric = vi.spyOn(MetricsService, 'getMetrics').mockResolvedValue([])
105
+ const spyCreateMetric = vi.spyOn(MetricsService, 'postMetric').mockRejectedValue(new Error('API ERROR!'))
106
+ const spyGetLabelsCounter = vi.spyOn(MetricCountersService, 'getMetricsCounters').mockResolvedValue([])
107
+
108
+ const metricsStore = useMetricsStore()
109
+
110
+ expect(await metricsStore.saveMetric(mockMetric)).toEqual(false)
111
+
112
+ expect(spyGetMetric).toHaveBeenCalledTimes(2)
113
+ expect(spyGetLabelsCounter).toHaveBeenCalledTimes(1)
114
+ expect(spyCreateMetric).toHaveBeenLastCalledWith(mockMetric)
115
+ })
116
+ })
@@ -0,0 +1,71 @@
1
+ import { acceptHMRUpdate, defineStore } from 'pinia'
2
+ import type { Ref } from 'vue'
3
+ import MetricsService from '~/services/metricsService'
4
+ import MetricCountersService from '~/services/metricsCountersService'
5
+ import type { Metric, MetricCounter } from '~/types'
6
+ export const useMetricsStore = defineStore('metrics', () => {
7
+ const metrics: Ref<Metric[]> = ref([])
8
+ const metricsCounters: Ref<MetricCounter[]> = ref([])
9
+
10
+ const getMetricCounter: ComputedRef<(metricName: string) => MetricCounter | undefined > = computed(() => {
11
+ return (metricName: string) => {
12
+ return metricsCounters.value?.find((metricCounter: MetricCounter) => metricCounter.name === metricName)
13
+ }
14
+ })
15
+
16
+ const metricsArr = computed(() => metrics.value.map(m => ({ title: `${m.label} ${m.name}`, label: m.label, value: m.name })))
17
+
18
+ const pullMetrics = async (ifEmpty?: boolean) => {
19
+ if (ifEmpty && metrics.value.length)
20
+ return
21
+
22
+ try {
23
+ const metricsRequest = await MetricsService.getMetrics()
24
+ metrics.value = metricsRequest
25
+ }
26
+ catch (err: any) {
27
+ useAlertMessagesStore().setSnackbarMessage(err, 'error')
28
+ }
29
+ }
30
+
31
+ const pullMetricsCounters = async () => {
32
+ try {
33
+ const metricsRequest = await MetricCountersService.getMetricsCounters()
34
+ metricsCounters.value = metricsRequest
35
+ }
36
+ catch (err: any) {
37
+ useAlertMessagesStore().setSnackbarMessage(err, 'error')
38
+ }
39
+ }
40
+
41
+ const saveMetric = async (metric: Metric) => {
42
+ let res = false
43
+ try {
44
+ await MetricsService.postMetric(metric)
45
+ res = true
46
+ useAlertMessagesStore().setSnackbarMessage(`Metric ${metric.name} successfully saved`, 'success')
47
+ }
48
+ catch (err: any) {
49
+ useAlertMessagesStore().setSnackbarMessage(err, 'error')
50
+ }
51
+ await pullMetrics()
52
+ await pullMetricsCounters()
53
+
54
+ return res
55
+ }
56
+
57
+ // init store
58
+ pullMetrics()
59
+
60
+ return {
61
+ metrics,
62
+ metricsCounters,
63
+ pullMetrics,
64
+ pullMetricsCounters,
65
+ getMetricCounter,
66
+ saveMetric,
67
+ metricsArr,
68
+ }
69
+ })
70
+ if (import.meta.hot)
71
+ import.meta.hot.accept(acceptHMRUpdate(useMetricsStore, import.meta.hot))
@@ -0,0 +1,71 @@
1
+ import { createPinia, setActivePinia } from 'pinia'
2
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
3
+ import { useTwinTypesStore } from './twinTypes'
4
+ import twinTypesService from '~/services/twinTypesService'
5
+ vi.mock('~/services/davraApi')
6
+
7
+ describe('Twintypes Store', () => {
8
+ beforeEach(() => {
9
+ // creates a fresh pinia and make it active so it's automatically picked
10
+ // up by any useStore() call without having to pass it to it:
11
+ // `useStore(pinia)`
12
+ setActivePinia(createPinia())
13
+ vi.spyOn(twinTypesService, 'getTwinTypes').mockResolvedValue([])
14
+ })
15
+
16
+ afterEach(() => {
17
+ vi.restoreAllMocks()
18
+ })
19
+
20
+ it('init store with empty setting', () => {
21
+ const twintypeStore = useTwinTypesStore()
22
+ expect(twintypeStore.twinTypes).toEqual([])
23
+ })
24
+
25
+ it('pullTwinTypes to an empty store', async () => {
26
+ const mockTwinTypes = [{
27
+ _id: '5fb7fdd919ed592dd321d767',
28
+ tenantId: 'ruban',
29
+ UUID: 'e8cc8e7d-900b-49da-b3df-43dceb101690',
30
+ created: 1605893593664,
31
+ owner: '451e5a4868b3fd5bda206fc82e436ac3',
32
+ name: 'stateful_incident',
33
+ description: 'Default type for stateful incidents. Do not delete.',
34
+ customAttributes: {},
35
+ },
36
+ {
37
+ _id: '5fbd141f5d07a570541dec20',
38
+ name: 'testTypes',
39
+ description: 'sample description',
40
+ labels: {},
41
+ customAttributes: {
42
+ testAttribute: 'x',
43
+ },
44
+ tenantId: 'ruban',
45
+ UUID: 'de68d333-7cd6-45ff-bfb4-d7d55b99eec5',
46
+ created: 1606226975987,
47
+ owner: '451e5a4868b3fd5bda206fc82e436ac3',
48
+ }]
49
+
50
+ const labelStore = useTwinTypesStore()
51
+
52
+ const spyGetTwinTypes = vi.spyOn(twinTypesService, 'getTwinTypes').mockResolvedValue(mockTwinTypes)
53
+
54
+ await labelStore.pullTwinTypes()
55
+
56
+ expect(spyGetTwinTypes).toHaveBeenCalledOnce()
57
+
58
+ expect(labelStore.twinTypes).toEqual(mockTwinTypes)
59
+ expect(labelStore.twinTypeNames).toEqual(['stateful_incident', 'testTypes'])
60
+ })
61
+
62
+ it('handle an error without crashing', async () => {
63
+ const labelStore = useTwinTypesStore()
64
+ const mError = new Error('Unable to retrieve twintypes')
65
+ const spyGetTwinTypes = vi.spyOn(twinTypesService, 'getTwinTypes').mockRejectedValueOnce(mError)
66
+ await labelStore.pullTwinTypes()
67
+ expect(spyGetTwinTypes).toHaveBeenCalledOnce()
68
+
69
+ expect(labelStore.twinTypes).toEqual([])
70
+ })
71
+ })
@@ -0,0 +1,36 @@
1
+ import { acceptHMRUpdate, defineStore } from 'pinia'
2
+ import type { Ref } from 'vue'
3
+ import TwinTypesService from '~/services/twinTypesService'
4
+ import type { TwinType } from '~/types'
5
+
6
+ export const useTwinTypesStore = defineStore('twinTypes', () => {
7
+ const twinTypes: Ref<TwinType[]> = ref([])
8
+
9
+ const twinTypeNames = computed(() => {
10
+ return twinTypes.value.map(twinType => twinType.name)
11
+ })
12
+ const twinTypeSelectArray = computed(() => {
13
+ return twinTypes.value.map((twintype: any) => ({ value: twintype.UUID, label: twintype.name }))
14
+ })
15
+
16
+ const pullTwinTypes = async () => {
17
+ try {
18
+ const twinTypesRequest = await TwinTypesService.getTwinTypes()
19
+ twinTypes.value = twinTypesRequest
20
+ }
21
+ catch {
22
+ }
23
+ }
24
+
25
+ // init store
26
+ pullTwinTypes()
27
+
28
+ return {
29
+ twinTypes,
30
+ pullTwinTypes,
31
+ twinTypeNames,
32
+ twinTypeSelectArray,
33
+ }
34
+ })
35
+ if (import.meta.hot)
36
+ import.meta.hot.accept(acceptHMRUpdate(useTwinTypesStore, import.meta.hot))