forked from monitoror/monitoror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.ts
485 lines (417 loc) · 14.5 KB
/
store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import axios from 'axios'
import {now} from 'lodash-es'
import {Md5 as md5} from 'ts-md5/dist/md5'
import Vue from 'vue'
import Vuex, {StoreOptions} from 'vuex'
import DISPLAYABLE_SUBTILE_STATUS from '@/constants/displayableSubtileStatus'
import Task from '@/classes/task'
import ConfigErrorId from '@/enums/configErrorId'
import Route from '@/enums/route'
import TaskInterval from '@/enums/taskInterval'
import TaskType from '@/enums/taskType'
import Theme from '@/enums/theme'
import TileStatus from '@/enums/tileStatus'
import getQueryParamValue from '@/helpers/getQueryParamValue'
import getSubTilePreviousOrStatus from '@/helpers/getSubTilePreviousOrStatus'
import mostImportantStatus from '@/helpers/mostImportantStatus'
import Config from '@/interfaces/config'
import ConfigBag from '@/interfaces/configBag'
import ConfigError from '@/interfaces/configError'
import ConfigMetadata from '@/interfaces/configMetadata'
import Info from '@/interfaces/info'
import TaskOptions from '@/interfaces/taskOptions'
import TileConfig from '@/interfaces/tileConfig'
import TileState from '@/interfaces/tileState'
Vue.use(Vuex)
const API_BASE_PATH = '/api/v1'
export const DEFAULT_CONFIG_NAME = 'default'
const INFO_URL = '/info'
const QUERY_PARAM_KEYS = {
API_BASE_URL: 'apiBaseUrl',
CONFIG: 'config',
THEME: 'theme',
}
export interface RootState {
appVersion: string | undefined,
configVersion: string | undefined,
columns: number,
zoom: number,
tiles: TileConfig[],
tilesState: { [key: string]: TileState },
tasks: Task[],
errors: ConfigError[],
online: boolean,
now: Date,
lastRefreshDate: Date,
configList: ConfigMetadata[],
}
const store: StoreOptions<RootState> = {
state: {
appVersion: undefined,
configVersion: undefined,
columns: 4,
zoom: 1,
tiles: [],
tilesState: {},
tasks: [],
errors: [],
online: true,
now: new Date(),
lastRefreshDate: new Date(),
configList: [],
},
getters: {
apiBaseUrl(): string {
const defaultApiBaseUrl = window.location.origin + window.location.pathname
let apiBaseUrl = getQueryParamValue(QUERY_PARAM_KEYS.API_BASE_URL, defaultApiBaseUrl) as string
apiBaseUrl = apiBaseUrl.replace(/\/+$/, '')
return apiBaseUrl
},
configParam(): string {
return getQueryParamValue(QUERY_PARAM_KEYS.CONFIG, DEFAULT_CONFIG_NAME) as string
},
configProxyUrl(state, getters): string {
return `${getters.apiBaseUrl}${API_BASE_PATH}/configs`
},
proxyfiedConfigUrl(state, getters): string | undefined {
const urlEncodedConfigParam = encodeURIComponent(getters.configParam)
return `${getters.configProxyUrl}/${urlEncodedConfigParam}`
},
currentRoute(): Route | undefined {
const queryHash = window.location.hash.replace(/^#/, '')
let route
if (Object.values(Route).includes(queryHash.toLowerCase() as Route)) {
route = queryHash.toLowerCase() as Route
}
return route
},
theme(): Theme {
let theme = Theme.Default
const queryTheme = getQueryParamValue(QUERY_PARAM_KEYS.THEME, theme) as string
if (Object.values(Theme).includes(queryTheme.toUpperCase() as Theme)) {
theme = queryTheme.toUpperCase() as Theme
}
return theme
},
tileStateKeys(state): string[] {
const tileStateKeys: string[] = []
state.tiles.forEach((tile: TileConfig) => {
tileStateKeys.push(tile.stateKey)
// Add group subTiles stateKeys
if (tile.tiles) {
tile.tiles.forEach((subTile) => {
tileStateKeys.push(subTile.stateKey)
})
}
})
return tileStateKeys
},
taskIds(state): string[] {
return state.tasks.map((task: Task) => task.id)
},
loadedTilesCount(state): number {
const loadedTilesCount = Object.keys(state.tilesState).length
return loadedTilesCount
},
loadableTilesCount(state): number {
const loadableTilesStateKeys: string[] = []
function addLoadableTileStateKey(stateKey: string) {
if (loadableTilesStateKeys.includes(stateKey)) {
return
}
loadableTilesStateKeys.push(stateKey)
}
state.tiles.forEach((tile) => {
if (tile.url) {
addLoadableTileStateKey(tile.stateKey)
}
if (tile.tiles) {
addLoadableTileStateKey(tile.stateKey)
tile.tiles.forEach((subTile) => {
addLoadableTileStateKey(subTile.stateKey)
})
}
})
const loadableTilesCount = loadableTilesStateKeys.length
return loadableTilesCount
},
loadingProgress(state, getters): number {
const loadingProgress = getters.loadedTilesCount / getters.loadableTilesCount
if (!loadingProgress) {
return 0
}
return loadingProgress
},
hasUnknownDefaultConfigError(state, getters): boolean {
if (state.errors.length === 0) {
return false
}
const error = state.errors[0]
const isDefaultConfig = getters.configParam === DEFAULT_CONFIG_NAME
const isUnknownNamedConfigError = error.id === ConfigErrorId.UnknownNamedConfig
return isDefaultConfig && isUnknownNamedConfigError
},
isNewUser(state, getters): boolean {
if (!getters.hasUnknownDefaultConfigError) {
return false
}
const error = state.errors[0]
const hasNamedConfig = error.data.expected !== undefined
return !hasNamedConfig
},
shouldShowWelcomePage(state, getters): boolean {
const welcomePageRoutes = [
Route.Welcome,
Route.ChooseConfiguration,
]
const isOnWelcomePageRoute = welcomePageRoutes.includes(getters.currentRoute)
return isOnWelcomePageRoute || getters.isNewUser || getters.hasUnknownDefaultConfigError
},
shouldInit(state, getters): boolean {
const isOnChooseConfigurationPageRoute = Route.ChooseConfiguration === getters.currentRoute
return !isOnChooseConfigurationPageRoute
},
},
mutations: {
setAppVersion(state, payload: string): void {
state.appVersion = payload
},
setConfig(state, payload: Config): void {
state.configVersion = payload.version
state.columns = payload.columns
if (payload.zoom !== undefined) {
state.zoom = payload.zoom
}
state.tiles = payload.tiles
},
setConfigList(state, payload: ConfigMetadata[]): void {
state.configList = payload
},
setErrors(state, payload: ConfigError[]): void {
state.errors = payload
},
setTileState(state, payload: { tileStateKey: string, tileState: TileState }): void {
if (!state.tilesState.hasOwnProperty(payload.tileStateKey)) {
Vue.set(state.tilesState, payload.tileStateKey, payload.tileState)
} else {
state.tilesState[payload.tileStateKey] = payload.tileState
}
},
setOnline(state, payload: boolean): void {
state.online = payload
},
setTasks(state, payload: Task[]): void {
state.tasks = payload
},
addTask(state, payload: Task): void {
state.tasks.push(payload)
},
setLastRefreshDate(state, payload: Date): void {
state.lastRefreshDate = payload
},
setNow(state, payload: Date): void {
state.now = payload
},
},
actions: {
async autoUpdate({commit, state, getters}) {
const infoUrl = getters.apiBaseUrl + API_BASE_PATH + INFO_URL
return axios.get(infoUrl)
.then((response) => {
const info: Info = response.data
if (state.appVersion === undefined) {
commit('setAppVersion', info.version)
return
}
if (info.version !== state.appVersion) {
window.location.reload()
}
})
},
async fetchConfigList({commit, getters}) {
return axios.get(getters.configProxyUrl)
.then((response) => {
const configList: ConfigMetadata[] = response.data.map((configMetadata: ConfigMetadata) => {
let uiUrl = `?${QUERY_PARAM_KEYS.CONFIG}=${configMetadata.name}`
if (getters.theme !== Theme.Default) {
uiUrl += `&${QUERY_PARAM_KEYS.THEME}=${getters.theme.toLowerCase()}`
}
configMetadata.uiUrl = uiUrl
return configMetadata
}).sort((configMetadataA: ConfigMetadata, configMetadataB: ConfigMetadata) => {
return configMetadataA.name.localeCompare(configMetadataB.name)
})
commit('setConfigList', configList)
})
},
async fetchConfiguration({commit, state, getters, dispatch}) {
const hydrateTile = (tile: TileConfig, groupTile?: TileConfig) => {
// Create a identifier based on tile configuration
tile.stateKey = tile.type + '_' + md5.hashStr(JSON.stringify(tile))
if (tile.url) {
// Prefix URL with api base URL
tile.url = getters.apiBaseUrl + tile.url
// Create a task for this tile
dispatch('createRefreshTileTask', {tile, groupTile})
}
// Set stateKey on group subTiles
if (tile.tiles) {
tile.tiles = tile.tiles.map((subTile) => hydrateTile(subTile, tile))
}
return tile
}
commit('setLastRefreshDate', new Date())
return axios.get(getters.proxyfiedConfigUrl)
.then((response) => {
const configBag: ConfigBag = response.data
// Kill old refreshTile tasks
state.tasks
.filter((task) => task.type === TaskType.RefreshTile && !getters.tileStateKeys.includes(task.id))
.map((task) => task.kill())
if (configBag.errors !== undefined) {
commit('setErrors', configBag.errors)
} else {
commit('setErrors', [])
if (configBag.config !== undefined) {
configBag.config.tiles = configBag.config.tiles.map((tile) => hydrateTile(tile))
commit('setConfig', configBag.config)
}
}
})
},
createRefreshTileTask({dispatch}, {tile, groupTile}: { tile: TileConfig, groupTile?: TileConfig }) {
dispatch('addTask', {
id: tile.stateKey,
type: TaskType.RefreshTile,
executor: async () => {
await dispatch('refreshTile', tile)
if (groupTile !== undefined) {
await dispatch('refreshGroup', groupTile)
}
},
interval: 10 * TaskInterval.Second,
initialDelay: Math.random() * (tile.initialMaxDelay || 0),
})
},
async refreshTile({commit}, tile: TileConfig) {
if (!tile.url) {
return Promise.resolve()
}
return axios.get(tile.url)
.then(async (response) => {
const tileState = response.data
commit('setTileState', {tileStateKey: tile.stateKey, tileState})
})
},
refreshGroup({state, commit}, groupTile: TileConfig) {
if (!groupTile.tiles) {
return
}
const groupSubTilesState = groupTile.tiles
.map((subTile) => subTile.stateKey)
.map((subTileStateKey) => state.tilesState[subTileStateKey])
const groupStatus = groupSubTilesState.reduce((worstSubTileStatus, subTileState) => {
const subTileStatus = getSubTilePreviousOrStatus(subTileState)
return mostImportantStatus(worstSubTileStatus, subTileStatus)
}, TileStatus.Unknown)
const groupNonDisplayedSubTiles = groupSubTilesState.filter((subTileState) => {
const subTileStatus = getSubTilePreviousOrStatus(subTileState)
return !DISPLAYABLE_SUBTILE_STATUS.includes(subTileStatus)
})
const groupMessage = `${groupNonDisplayedSubTiles.length} / ${groupTile.tiles.length}`
const groupState = {
status: groupStatus,
message: groupMessage,
}
commit('setTileState', {tileStateKey: groupTile.stateKey, tileState: groupState})
},
updateNetworkState({commit}) {
commit('setOnline', navigator.onLine)
},
addTask({getters, commit}, taskOptions: TaskOptions) {
// Avoid adding multiple task with the same ID
if (getters.taskIds.includes(taskOptions.id)) {
return
}
commit('addTask', new Task(taskOptions))
},
runTasks({state, dispatch}) {
const nowTime = now()
const shouldRunTask = (task: Task) => !task.isDone() && !task.isRunning() && task.time <= nowTime
const taskToRun = state.tasks.filter(shouldRunTask)
Promise.all(taskToRun.map((task: Task) => {
return task.run()
})).then(() => {
dispatch('updateTasks')
})
},
updateTasks({commit, state}) {
const {taskList, hasChanged} = state.tasks.reduce(
(previousValue: { taskList: Task[], hasChanged: boolean }, task: Task) => {
// Remove dead tasks from task list
if (task.isDead()) {
previousValue.hasChanged = true
return previousValue
}
// Update outdated recurring tasks
if (task.isDone()) {
task.prepareNextRun()
}
previousValue.taskList.push(task)
return previousValue
},
{taskList: [], hasChanged: false},
)
if (hasChanged) {
commit('setTasks', taskList)
}
},
killAllTasks({commit, state}) {
state.tasks.map((task: Task) => task.kill())
commit('setTasks', [])
},
init({state, commit, dispatch}) {
// Run auto-update each minute
dispatch('addTask', {
id: 'autoUpdate',
type: TaskType.Root,
executor: async () => {
await dispatch('autoUpdate')
},
interval: 1 * TaskInterval.Minute,
})
// Fetch configuration each minute
dispatch('addTask', {
id: 'fetchConfiguration',
type: TaskType.Root,
executor: async () => {
await dispatch('fetchConfiguration')
},
interval: 1 * TaskInterval.Minute,
retryOnFailInterval: 5 * TaskInterval.Second,
onFailedCallback: () => {
// When offline, we know why we cannot get a response from the Core
if (!state.online) {
return
}
const configCannotBeFetch: ConfigError = {
id: ConfigErrorId.CannotBeFetched,
message: 'Configuration cannot be fetch from Monitoror Core',
data: {},
}
commit('setErrors', [configCannotBeFetch])
},
})
// Update "now" each second
dispatch('addTask', {
id: 'updateNow',
type: TaskType.Root,
executor: async () => {
commit('setNow', new Date())
},
interval: 1 * TaskInterval.Second,
})
},
},
}
export default new Vuex.Store(store)