]> git.arvados.org - arvados.git/blob - services/workbench2/src/websocket/websocket.ts
23083: Add throttles to websocket-triggered data explorer refreshes
[arvados.git] / services / workbench2 / src / websocket / websocket.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { RootStore } from "store/store";
6 import { AuthService } from "services/auth-service/auth-service";
7 import { Config } from "common/config";
8 import { WebSocketService } from "./websocket-service";
9 import { ResourceEventMessage } from "./resource-event-message";
10 import { ResourceKind } from "models/resource";
11 import { loadProcess } from "store/process-panel/process-panel-actions";
12 import { getProcess, getSubprocesses } from "store/processes/process";
13 import { LogEventType } from "models/log";
14 import { subprocessPanelActions } from "store/subprocess-panel/subprocess-panel-actions";
15 import { projectPanelDataActions } from "store/project-panel/project-panel-action-bind";
16 import { getProjectPanelCurrentUuid } from "store/project-panel/project-panel";
17 import { allProcessesPanelActions } from "store/all-processes-panel/all-processes-panel-action";
18 import { loadCollection } from "store/workbench/workbench-actions";
19 import { matchAllProcessesRoute, matchProjectRoute, matchProcessRoute } from "routes/routes";
20 import { throttle } from "lodash";
21
22 type ThrottleSet = {
23     subProcess: () => void;
24     allProcesses: () => void;
25     project: () => void;
26 };
27
28 const THROTTLE_INTERVAL = 15000;
29
30 export const initWebSocket = (config: Config, authService: AuthService, store: RootStore) => {
31     if (config.websocketUrl) {
32         const webSocketService = WebSocketService.getInstance();
33
34         // Throttles must be constructed once
35         const throttleSet: ThrottleSet = {
36             subProcess: throttle(() => {
37                 store.dispatch(subprocessPanelActions.REQUEST_ITEMS(false, true));
38             }, THROTTLE_INTERVAL),
39             allProcesses: throttle(() => {
40                 store.dispatch(allProcessesPanelActions.REQUEST_ITEMS(false, true));
41             }, THROTTLE_INTERVAL),
42             project: throttle(() => {
43                 store.dispatch(projectPanelDataActions.REQUEST_ITEMS(false, true));
44             }, THROTTLE_INTERVAL),
45         };
46
47         webSocketService.setMessageListener(messageListener(store, throttleSet));
48         webSocketService.connect(config.websocketUrl, authService);
49     } else {
50         console.warn("WARNING: Websocket ExternalURL is not set on the cluster config.");
51     }
52 };
53
54 const messageListener = (store: RootStore, throttles: ThrottleSet) => (message: ResourceEventMessage) => {
55     if (message.eventType === LogEventType.CREATE || message.eventType === LogEventType.UPDATE) {
56         const state = store.getState();
57         const location = state.router.location ? state.router.location.pathname : "";
58
59         switch (message.objectKind) {
60             case ResourceKind.COLLECTION:
61                 const currentCollection = state.collectionPanel.item;
62                 if (currentCollection && currentCollection.uuid === message.objectUuid) {
63                     store.dispatch(loadCollection(message.objectUuid));
64                 }
65                 return;
66             case ResourceKind.CONTAINER_REQUEST:
67                 if (matchProcessRoute(location)) {
68                     if (state.processPanel.containerRequestUuid === message.objectUuid) {
69                         store.dispatch(loadProcess(message.objectUuid));
70                     }
71                     const proc = getProcess(state.processPanel.containerRequestUuid)(state.resources);
72                     if (proc && proc.container && proc.container.uuid === message.properties["new_attributes"]["requesting_container_uuid"]) {
73                         throttles.subProcess();
74                         return;
75                     }
76                 }
77             // fall through, this will happen for container requests as well.
78             case ResourceKind.CONTAINER:
79                 if (matchProcessRoute(location)) {
80                     // refresh only if this is a subprocess of the currently displayed process.
81                     const subproc = getSubprocesses(state.processPanel.containerRequestUuid)(state.resources);
82                     for (const sb of subproc) {
83                         if (sb.containerRequest.uuid === message.objectUuid || (sb.container && sb.container.uuid === message.objectUuid)) {
84                             throttles.subProcess();
85                             break;
86                         }
87                     }
88                 }
89                 if (matchAllProcessesRoute(location)) {
90                     throttles.allProcesses();
91                 }
92                 if (matchProjectRoute(location) && message.objectOwnerUuid === getProjectPanelCurrentUuid(state)) {
93                     throttles.project();
94                 }
95                 return;
96             default:
97                 return;
98         }
99     }
100 };