-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
alerts.ts
788 lines (722 loc) · 24.5 KB
/
alerts.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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
// Copyright 2018 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
/**
* Alerts is a collection of selectors which determine if there are any Alerts
* to display based on the current redux state.
*/
import _ from "lodash";
import moment from "moment-timezone";
import { createSelector } from "reselect";
import { Store, Dispatch, Action, AnyAction } from "redux";
import { ThunkAction } from "redux-thunk";
import { LocalSetting } from "./localsettings";
import {
VERSION_DISMISSED_KEY,
INSTRUCTIONS_BOX_COLLAPSED_KEY,
saveUIData,
loadUIData,
isInFlight,
UIDataState,
UIDataStatus,
} from "./uiData";
import {
refreshCluster,
refreshNodes,
refreshVersion,
refreshHealth,
refreshSettings,
} from "./apiReducers";
import {
singleVersionSelector,
numNodesByVersionsTagSelector,
numNodesByVersionsSelector,
} from "src/redux/nodes";
import { AdminUIState, AppDispatch } from "./state";
import * as docsURL from "src/util/docs";
import { getDataFromServer } from "../util/dataFromServer";
import {
selectClusterSettings,
selectClusterSettingVersion,
} from "./clusterSettings";
import { longToInt } from "src/util/fixLong";
export enum AlertLevel {
NOTIFICATION,
WARNING,
CRITICAL,
SUCCESS,
}
export interface AlertInfo {
// Alert Level, which determines visual qualities such as icon and coloring.
level: AlertLevel;
// Title to display with the alert.
title: string;
// The text of this alert.
text?: string;
// Optional hypertext link to be followed when clicking alert.
link?: string;
}
export interface Alert extends AlertInfo {
// ThunkAction which will result in this alert being dismissed. This
// function will be dispatched to the redux store when the alert is dismissed.
dismiss: ThunkAction<Promise<void>, AdminUIState, void, AnyAction>;
// Makes alert to be positioned in the top right corner of the screen instead of
// stretching to full width.
showAsAlert?: boolean;
autoClose?: boolean;
closable?: boolean;
autoCloseTimeout?: number;
}
const localSettingsSelector = (state: AdminUIState) => state.localSettings;
// Clusterviz Instruction Box collapsed
export const instructionsBoxCollapsedSetting = new LocalSetting(
INSTRUCTIONS_BOX_COLLAPSED_KEY,
localSettingsSelector,
false,
);
const instructionsBoxCollapsedPersistentLoadedSelector = createSelector(
(state: AdminUIState) => state.uiData,
(uiData): boolean =>
uiData &&
_.has(uiData, INSTRUCTIONS_BOX_COLLAPSED_KEY) &&
uiData[INSTRUCTIONS_BOX_COLLAPSED_KEY].status === UIDataStatus.VALID,
);
const instructionsBoxCollapsedPersistentSelector = createSelector(
(state: AdminUIState) => state.uiData,
(uiData): boolean =>
uiData &&
_.has(uiData, INSTRUCTIONS_BOX_COLLAPSED_KEY) &&
uiData[INSTRUCTIONS_BOX_COLLAPSED_KEY].status === UIDataStatus.VALID &&
uiData[INSTRUCTIONS_BOX_COLLAPSED_KEY].data,
);
export const instructionsBoxCollapsedSelector = createSelector(
instructionsBoxCollapsedPersistentLoadedSelector,
instructionsBoxCollapsedPersistentSelector,
instructionsBoxCollapsedSetting.selector,
(persistentLoaded, persistentCollapsed, localSettingCollapsed): boolean => {
if (persistentLoaded) {
return persistentCollapsed;
}
return localSettingCollapsed;
},
);
export function setInstructionsBoxCollapsed(collapsed: boolean) {
return (dispatch: AppDispatch) => {
dispatch(instructionsBoxCollapsedSetting.set(collapsed));
dispatch(
saveUIData({
key: INSTRUCTIONS_BOX_COLLAPSED_KEY,
value: collapsed,
}),
);
};
}
////////////////////////////////////////
// Version mismatch.
////////////////////////////////////////
export const staggeredVersionDismissedSetting = new LocalSetting(
"staggered_version_dismissed",
localSettingsSelector,
false,
);
/**
* Warning when multiple versions of CockroachDB are detected on the cluster.
* This excludes decommissioned nodes.
*/
export const staggeredVersionWarningSelector = createSelector(
numNodesByVersionsTagSelector,
staggeredVersionDismissedSetting.selector,
(versionsMap, versionMismatchDismissed): Alert => {
if (versionMismatchDismissed) {
return undefined;
}
if (!versionsMap || versionsMap.size < 2) {
return undefined;
}
const versionsText = Array.from(versionsMap)
.map(([k, v]) => (v === 1 ? `1 node on ${k}` : `${v} nodes on ${k}`))
.join(", ")
.concat(". ");
return {
level: AlertLevel.WARNING,
title: "Multiple versions of CockroachDB are running on this cluster.",
text:
"Listed versions: " +
versionsText +
`You can see a list of all nodes and their versions below.
This may be part of a normal rolling upgrade process, but should be investigated
if unexpected.`,
dismiss: (dispatch: AppDispatch) => {
dispatch(staggeredVersionDismissedSetting.set(true));
return Promise.resolve();
},
};
},
);
// A boolean that indicates whether the server has yet been checked for a
// persistent dismissal of this notification.
// TODO(mrtracy): Refactor so that we can distinguish "never loaded" from
// "loaded, doesn't exist on server" without a separate selector.
const newVersionDismissedPersistentLoadedSelector = createSelector(
(state: AdminUIState) => state.uiData,
uiData => uiData && _.has(uiData, VERSION_DISMISSED_KEY),
);
const newVersionDismissedPersistentSelector = createSelector(
(state: AdminUIState) => state.uiData,
uiData => {
return (
(uiData &&
uiData[VERSION_DISMISSED_KEY] &&
uiData[VERSION_DISMISSED_KEY].data &&
moment(uiData[VERSION_DISMISSED_KEY].data)) ||
moment(0)
);
},
);
export const newVersionDismissedLocalSetting = new LocalSetting(
"new_version_dismissed",
localSettingsSelector,
moment(0),
);
export const newerVersionsSelector = (state: AdminUIState) =>
state.cachedData.version.valid ? state.cachedData.version.data : null;
/**
* Notification when a new version of CockroachDB is available.
*/
export const newVersionNotificationSelector = createSelector(
newerVersionsSelector,
newVersionDismissedPersistentLoadedSelector,
newVersionDismissedPersistentSelector,
newVersionDismissedLocalSetting.selector,
(
newerVersions,
newVersionDismissedPersistentLoaded,
newVersionDismissedPersistent,
newVersionDismissedLocal,
): Alert => {
// Check if there are new versions available.
if (
!newerVersions ||
!newerVersions.details ||
newerVersions.details.length === 0
) {
return undefined;
}
// Check local dismissal. Local dismissal is valid for one day.
const yesterday = moment().subtract(1, "day");
if (
newVersionDismissedLocal.isAfter &&
newVersionDismissedLocal.isAfter(yesterday)
) {
return undefined;
}
// Check persistent dismissal, also valid for one day.
if (
!newVersionDismissedPersistentLoaded ||
!newVersionDismissedPersistent ||
newVersionDismissedPersistent.isAfter(yesterday)
) {
return undefined;
}
return {
level: AlertLevel.NOTIFICATION,
title: "New Version Available",
text: "A new version of CockroachDB is available.",
link: docsURL.upgradeCockroachVersion,
dismiss: (dispatch: any) => {
const dismissedAt = moment();
// Dismiss locally.
dispatch(newVersionDismissedLocalSetting.set(dismissedAt));
// Dismiss persistently.
return dispatch(
saveUIData({
key: VERSION_DISMISSED_KEY,
value: dismissedAt.valueOf(),
}),
);
},
};
},
);
export const disconnectedDismissedLocalSetting = new LocalSetting(
"disconnected_dismissed",
localSettingsSelector,
moment(0),
);
/**
* Notification when the Admin UI is disconnected from the cluster.
*/
export const disconnectedAlertSelector = createSelector(
(state: AdminUIState) => state.cachedData.health,
disconnectedDismissedLocalSetting.selector,
(health, disconnectedDismissed): Alert => {
if (!health || !health.lastError) {
return undefined;
}
// Allow local dismissal for one minute.
const dismissedMaxTime = moment().subtract(1, "m");
if (disconnectedDismissed.isAfter(dismissedMaxTime)) {
return undefined;
}
return {
level: AlertLevel.CRITICAL,
title:
"We're currently having some trouble fetching updated data. If this persists, it might be a good idea to check your network connection to the CockroachDB cluster.",
dismiss: (dispatch: Dispatch<Action>) => {
dispatch(disconnectedDismissedLocalSetting.set(moment()));
return Promise.resolve();
},
};
},
);
export const emailSubscriptionAlertLocalSetting = new LocalSetting(
"email_subscription_alert",
localSettingsSelector,
false,
);
export const emailSubscriptionAlertSelector = createSelector(
emailSubscriptionAlertLocalSetting.selector,
(emailSubscriptionAlert): Alert => {
if (!emailSubscriptionAlert) {
return undefined;
}
return {
level: AlertLevel.SUCCESS,
title: "You successfully signed up for CockroachDB release notes",
showAsAlert: true,
autoClose: true,
closable: false,
dismiss: (dispatch: Dispatch<Action>) => {
dispatch(emailSubscriptionAlertLocalSetting.set(false));
return Promise.resolve();
},
};
},
);
type CreateStatementDiagnosticsAlertPayload = {
show: boolean;
status?: "SUCCESS" | "FAILED";
};
export const createStatementDiagnosticsAlertLocalSetting = new LocalSetting<
AdminUIState,
CreateStatementDiagnosticsAlertPayload
>("create_stmnt_diagnostics_alert", localSettingsSelector, { show: false });
export const createStatementDiagnosticsAlertSelector = createSelector(
createStatementDiagnosticsAlertLocalSetting.selector,
(createStatementDiagnosticsAlert): Alert => {
if (
!createStatementDiagnosticsAlert ||
!createStatementDiagnosticsAlert.show
) {
return undefined;
}
const { status } = createStatementDiagnosticsAlert;
if (status === "FAILED") {
return {
level: AlertLevel.CRITICAL,
title: "There was an error activating statement diagnostics",
text: "Please try activating again. If the problem continues please reach out to customer support.",
showAsAlert: true,
dismiss: (dispatch: Dispatch<Action>) => {
dispatch(
createStatementDiagnosticsAlertLocalSetting.set({ show: false }),
);
return Promise.resolve();
},
};
}
return {
level: AlertLevel.SUCCESS,
title: "Statement diagnostics were successfully activated",
showAsAlert: true,
autoClose: true,
closable: false,
dismiss: (dispatch: Dispatch<Action>) => {
dispatch(
createStatementDiagnosticsAlertLocalSetting.set({ show: false }),
);
return Promise.resolve();
},
};
},
);
type CancelStatementDiagnosticsAlertPayload = {
show: boolean;
status?: "SUCCESS" | "FAILED";
};
export const cancelStatementDiagnosticsAlertLocalSetting = new LocalSetting<
AdminUIState,
CancelStatementDiagnosticsAlertPayload
>("cancel_stmnt_diagnostics_alert", localSettingsSelector, { show: false });
export const cancelStatementDiagnosticsAlertSelector = createSelector(
cancelStatementDiagnosticsAlertLocalSetting.selector,
(cancelStatementDiagnosticsAlert): Alert => {
if (
!cancelStatementDiagnosticsAlert ||
!cancelStatementDiagnosticsAlert.show
) {
return undefined;
}
const { status } = cancelStatementDiagnosticsAlert;
if (status === "FAILED") {
return {
level: AlertLevel.CRITICAL,
title: "There was an error cancelling statement diagnostics",
text: "Please try cancelling the statement diagnostic again. If the problem continues please reach out to customer support.",
showAsAlert: true,
dismiss: (dispatch: Dispatch<Action>) => {
dispatch(
cancelStatementDiagnosticsAlertLocalSetting.set({ show: false }),
);
return Promise.resolve();
},
};
}
return {
level: AlertLevel.SUCCESS,
title: "Statement diagnostics were successfully cancelled",
showAsAlert: true,
autoClose: true,
closable: false,
dismiss: (dispatch: Dispatch<Action>) => {
dispatch(
cancelStatementDiagnosticsAlertLocalSetting.set({ show: false }),
);
return Promise.resolve();
},
};
},
);
type TerminateSessionAlertPayload = {
show: boolean;
status?: "SUCCESS" | "FAILED";
};
export const terminateSessionAlertLocalSetting = new LocalSetting<
AdminUIState,
TerminateSessionAlertPayload
>("terminate_session_alert", localSettingsSelector, { show: false });
export const terminateSessionAlertSelector = createSelector(
terminateSessionAlertLocalSetting.selector,
(terminateSessionAlert): Alert => {
if (!terminateSessionAlert || !terminateSessionAlert.show) {
return undefined;
}
const { status } = terminateSessionAlert;
if (status === "FAILED") {
return {
level: AlertLevel.CRITICAL,
title: "There was an error cancelling the session.",
text: "Please try cancelling again. If the problem continues please reach out to customer support.",
showAsAlert: true,
dismiss: (dispatch: Dispatch<Action>) => {
dispatch(terminateSessionAlertLocalSetting.set({ show: false }));
return Promise.resolve();
},
};
}
return {
level: AlertLevel.SUCCESS,
title: "Session cancelled.",
showAsAlert: true,
autoClose: true,
closable: false,
dismiss: (dispatch: Dispatch<Action>) => {
dispatch(terminateSessionAlertLocalSetting.set({ show: false }));
return Promise.resolve();
},
};
},
);
type TerminateQueryAlertPayload = {
show: boolean;
status?: "SUCCESS" | "FAILED";
};
export const terminateQueryAlertLocalSetting = new LocalSetting<
AdminUIState,
TerminateQueryAlertPayload
>("terminate_query_alert", localSettingsSelector, { show: false });
export const terminateQueryAlertSelector = createSelector(
terminateQueryAlertLocalSetting.selector,
(terminateQueryAlert): Alert => {
if (!terminateQueryAlert || !terminateQueryAlert.show) {
return undefined;
}
const { status } = terminateQueryAlert;
if (status === "FAILED") {
return {
level: AlertLevel.CRITICAL,
title: "There was an error cancelling the statement.",
text: "Please try cancelling again. If the problem continues please reach out to customer support.",
showAsAlert: true,
dismiss: (dispatch: Dispatch<Action>) => {
dispatch(terminateQueryAlertLocalSetting.set({ show: false }));
return Promise.resolve();
},
};
}
return {
level: AlertLevel.SUCCESS,
title: "Statement cancelled.",
showAsAlert: true,
autoClose: true,
closable: false,
dismiss: (dispatch: Dispatch<Action>) => {
dispatch(terminateQueryAlertLocalSetting.set({ show: false }));
return Promise.resolve();
},
};
},
);
/**
* Notification for when the cluster.preserve_downgrade_option has been set for
* too long of a duration (48hrs) as part of a version upgrade.
*/
export const clusterPreserveDowngradeOptionDismissedSetting = new LocalSetting(
"cluster_preserve_downgrade_option_dismissed",
localSettingsSelector,
false,
);
export const clusterPreserveDowngradeOptionOvertimeSelector = createSelector(
selectClusterSettings,
clusterPreserveDowngradeOptionDismissedSetting.selector,
(settings, notificationDismissed): Alert => {
if (notificationDismissed || !settings) {
return undefined;
}
const clusterPreserveDowngradeOption =
settings["cluster.preserve_downgrade_option"];
const value = clusterPreserveDowngradeOption?.value;
const lastUpdated = clusterPreserveDowngradeOption?.last_updated;
if (!value || !lastUpdated) {
return undefined;
}
const lastUpdatedTime = moment.unix(longToInt(lastUpdated.seconds));
const diff = moment.duration(moment().diff(lastUpdatedTime)).asHours();
if (diff <= 0) {
return undefined;
}
return {
level: AlertLevel.WARNING,
title: `Cluster setting cluster.preserve_downgrade_option has been set for ${diff.toFixed(
1,
)} hours`,
text: `You can see a list of all nodes and their versions below.
Once all cluster nodes have been upgraded, and you have validated the stability and performance of
your workload on the new version, you must reset the cluster.preserve_downgrade_option cluster
setting with the following command:
RESET CLUSTER SETTING cluster.preserve_downgrade_option;`,
dismiss: (dispatch: AppDispatch) => {
dispatch(clusterPreserveDowngradeOptionDismissedSetting.set(true));
return Promise.resolve();
},
};
},
);
////////////////////////////////////////
// Upgrade not finalized.
////////////////////////////////////////
export const upgradeNotFinalizedDismissedSetting = new LocalSetting(
"upgrade_not_finalized_dismissed",
localSettingsSelector,
false,
);
/**
* Warning when all the nodes are running on the new version, but the cluster is not finalized.
*/
export const upgradeNotFinalizedWarningSelector = createSelector(
selectClusterSettings,
numNodesByVersionsSelector,
selectClusterSettingVersion,
upgradeNotFinalizedDismissedSetting.selector,
(
settings,
versionsMap,
clusterVersion,
upgradeNotFinalizedDismissed,
): Alert => {
if (upgradeNotFinalizedDismissed) {
return undefined;
}
// Don't show this warning if nodes are on different versions, since there is
// already an alert for that (staggeredVersionWarningSelector).
if (!versionsMap || versionsMap.size !== 1 || !clusterVersion) {
return undefined;
}
// Don't show this warning if cluster.preserve_downgrade_option is set,
// because it's expected for the upgrade not be finalized on that case and there is
// an alert for that (clusterPreserveDowngradeOptionOvertimeSelector)
const clusterPreserveDowngradeOption =
settings["cluster.preserve_downgrade_option"];
const value = clusterPreserveDowngradeOption?.value;
const lastUpdated = clusterPreserveDowngradeOption?.last_updated;
if (value && lastUpdated) {
return undefined;
}
const nodesVersion = versionsMap.keys().next().value;
// Prod: node version is 23.1 and cluster version is 23.1.
// Dev: node version is 23.1 and cluster version is 23.1-2.
if (clusterVersion.startsWith(nodesVersion)) {
return undefined;
}
return {
level: AlertLevel.WARNING,
title: "Upgrade not finalized.",
text: `All nodes are running on version ${nodesVersion}, but the cluster is on version ${clusterVersion}.`,
dismiss: (dispatch: AppDispatch) => {
dispatch(upgradeNotFinalizedDismissedSetting.set(true));
return Promise.resolve();
},
};
},
);
/**
* Selector which returns an array of all active alerts which should be
* displayed in the overview list page, these should be non-critical alerts.
*/
export const overviewListAlertsSelector = createSelector(
staggeredVersionWarningSelector,
clusterPreserveDowngradeOptionOvertimeSelector,
upgradeNotFinalizedWarningSelector,
(...alerts: Alert[]): Alert[] => {
return _.without(alerts, null, undefined);
},
);
/**
* Selector which returns an array of all active alerts which should be
* displayed in the alerts panel, which is embedded within the cluster overview
* page; currently, this includes all non-critical alerts.
*/
export const panelAlertsSelector = createSelector(
newVersionNotificationSelector,
staggeredVersionWarningSelector,
(...alerts: Alert[]): Alert[] => {
return _.without(alerts, null, undefined);
},
);
/**
* dataFromServerAlertSelector returns an alert when the
* `dataFromServer` window variable is unset. This variable is retrieved
* prior to page render and contains some base metadata about the
* backing CRDB node.
*/
export const dataFromServerAlertSelector = createSelector(
() => {},
(): Alert => {
if (_.isEmpty(getDataFromServer())) {
return {
level: AlertLevel.CRITICAL,
title: "There was an error retrieving base DB Console configuration.",
text: "Please try refreshing the page. If the problem continues please reach out to customer support.",
showAsAlert: true,
dismiss: (_: Dispatch<Action>) => {
// Dismiss is a no-op here because the alert component itself
// is dismissable by default and because we do want to keep
// showing it if the metadata continues to be missing.
return Promise.resolve();
},
};
} else {
return null;
}
},
);
/**
* Selector which returns an array of all active alerts which should be
* displayed as a banner, which appears at the top of the page and overlaps
* content in recognition of the severity of the alert; currently, this includes
* all critical-level alerts.
*/
export const bannerAlertsSelector = createSelector(
disconnectedAlertSelector,
emailSubscriptionAlertSelector,
createStatementDiagnosticsAlertSelector,
cancelStatementDiagnosticsAlertSelector,
terminateSessionAlertSelector,
terminateQueryAlertSelector,
dataFromServerAlertSelector,
(...alerts: Alert[]): Alert[] => {
return _.without(alerts, null, undefined);
},
);
/**
* This function, when supplied with a redux store, generates a callback that
* attempts to populate missing information that has not yet been loaded from
* the cluster that is needed to show certain alerts. This returned function is
* intended to be attached to the store as a subscriber.
*/
export function alertDataSync(store: Store<AdminUIState>) {
const dispatch = store.dispatch as AppDispatch;
// Memoizers to prevent unnecessary dispatches of alertDataSync if store
// hasn't changed in an interesting way.
let lastUIData: UIDataState;
return () => {
const state: AdminUIState = store.getState();
// Always refresh health.
dispatch(refreshHealth());
const { Insecure } = getDataFromServer();
// We should not send out requests to the endpoints below if
// the user has not successfully logged in since the requests
// will always return with a 401 error.
// Insecure mode is an exception, where login state is irrelevant.
if (!Insecure) {
if (
!state.login ||
!state.login.loggedInUser ||
state.login.loggedInUser == ``
) {
return;
}
}
// Load persistent settings which have not yet been loaded.
const uiData = state.uiData;
if (uiData !== lastUIData) {
lastUIData = uiData;
const keysToMaybeLoad = [
VERSION_DISMISSED_KEY,
INSTRUCTIONS_BOX_COLLAPSED_KEY,
];
const keysToLoad = _.filter(keysToMaybeLoad, key => {
return !(_.has(uiData, key) || isInFlight(state, key));
});
if (keysToLoad) {
dispatch(loadUIData(...keysToLoad));
}
}
// Load Cluster ID once at startup.
const cluster = state.cachedData.cluster;
if (cluster && !cluster.data && !cluster.inFlight && !cluster.valid) {
dispatch(refreshCluster());
}
// Load Nodes initially if it has not yet been loaded.
const nodes = state.cachedData.nodes;
if (nodes && !nodes.data && !nodes.inFlight && !nodes.valid) {
dispatch(refreshNodes());
}
// Load settings if not loaded
const settings = state.cachedData.settings;
if (settings && !settings.data && !settings.inFlight && !settings.valid) {
dispatch(refreshSettings());
}
// Load potential new versions from CockroachDB cluster. This is the
// complicating factor of this function, since the call requires the cluster
// ID and node statuses being loaded first and thus cannot simply run at
// startup.
const currentVersion = singleVersionSelector(state);
if (_.isNil(newerVersionsSelector(state))) {
if (cluster.data && cluster.data.cluster_id && currentVersion) {
dispatch(
refreshVersion({
clusterID: cluster.data.cluster_id,
buildtag: currentVersion,
}),
);
}
}
};
}