• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / gui / 1315496247

03 Jun 2024 07:49AM UTC coverage: 83.437% (-16.5%) from 99.964%
1315496247

Pull #4434

gitlab-ci

mzedel
chore: aligned snapshots with updated mui version

Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #4434: chore: Bump the mui group with 3 updates

4476 of 6391 branches covered (70.04%)

8488 of 10173 relevant lines covered (83.44%)

140.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.8
/src/js/components/deployments/deployments.js
1
// Copyright 2015 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
/* eslint-disable react-hooks/exhaustive-deps */
16
import React, { useCallback, useEffect, useRef, useState } from 'react';
17
import { useDispatch, useSelector } from 'react-redux';
18
import { Link, useNavigate } from 'react-router-dom';
19

20
import { Button, Tab, Tabs } from '@mui/material';
21

22
import { setSnackbar } from '../../actions/appActions';
23
import { abortDeployment, setDeploymentsState } from '../../actions/deploymentActions';
24
import { getDynamicGroups, getGroups } from '../../actions/deviceActions';
25
import { advanceOnboarding } from '../../actions/onboardingActions';
26
import { DEPLOYMENT_ROUTES, DEPLOYMENT_STATES, listDefaultsByState } from '../../constants/deploymentConstants';
27
import { ALL_DEVICES } from '../../constants/deviceConstants';
28
import { onboardingSteps } from '../../constants/onboardingConstants';
29
import { getISOStringBoundaries } from '../../helpers';
30
import { getDevicesById, getGroupsByIdWithoutUngrouped, getIsEnterprise, getOnboardingState, getReleasesById, getUserCapabilities } from '../../selectors';
31
import { useLocationParams } from '../../utils/liststatehook';
32
import { getOnboardingComponentFor } from '../../utils/onboardingmanager';
33
import useWindowSize from '../../utils/resizehook';
34
import CreateDeployment from './createdeployment';
35
import Progress from './inprogressdeployments';
36
import Past from './pastdeployments';
37
import Report from './report';
38
import Scheduled from './scheduleddeployments';
39

40
const routes = {
6✔
41
  [DEPLOYMENT_ROUTES.active.key]: {
42
    ...DEPLOYMENT_ROUTES.active,
43
    component: Progress
44
  },
45
  [DEPLOYMENT_ROUTES.scheduled.key]: {
46
    ...DEPLOYMENT_ROUTES.scheduled,
47
    component: Scheduled
48
  },
49
  [DEPLOYMENT_ROUTES.finished.key]: {
50
    ...DEPLOYMENT_ROUTES.finished,
51
    component: Past
52
  }
53
};
54

55
export const defaultRefreshDeploymentsLength = 30000;
6✔
56

57
export const Deployments = () => {
6✔
58
  const groupsById = useSelector(getGroupsByIdWithoutUngrouped);
141✔
59
  const devicesById = useSelector(getDevicesById);
140✔
60
  const isEnterprise = useSelector(getIsEnterprise);
140✔
61
  const onboardingState = useSelector(getOnboardingState);
140✔
62
  const pastCount = useSelector(state => state.deployments.byStatus.finished.total);
444✔
63
  const releases = useSelector(getReleasesById);
140✔
64
  const selectionState = useSelector(state => state.deployments.selectionState);
444✔
65
  const userCapabilities = useSelector(getUserCapabilities);
140✔
66
  const dispatch = useDispatch();
140✔
67

68
  const [deploymentObject, setDeploymentObject] = useState({});
140✔
69
  // eslint-disable-next-line no-unused-vars
70
  const size = useWindowSize();
140✔
71
  const tabsRef = useRef();
140✔
72
  const isInitialized = useRef(false);
140✔
73
  const deploymentObjInitialized = useRef(false);
140✔
74
  const navigate = useNavigate();
140✔
75
  const { reportType, showCreationDialog: createDialog, showReportDialog: reportDialog, state } = selectionState.general;
140✔
76
  const { canDeploy, canReadReleases } = userCapabilities;
140✔
77

78
  const [date] = useState(getISOStringBoundaries(new Date()));
140✔
79
  const { start: today, end: tonight } = date;
140✔
80

81
  const [locationParams, setLocationParams] = useLocationParams('deployments', { today, tonight, defaults: listDefaultsByState });
140✔
82

83
  useEffect(() => {
140✔
84
    if (!isInitialized.current) {
61✔
85
      return;
6✔
86
    }
87
    setLocationParams({ deploymentObject, pageState: selectionState });
55✔
88
  }, [
89
    JSON.stringify(deploymentObject),
90
    selectionState.selectedId,
91
    selectionState.general.state,
92
    selectionState.general.showCreationDialog,
93
    selectionState.general.showReportDialog,
94
    selectionState.general.reportType,
95
    selectionState[DEPLOYMENT_STATES.finished].endDate,
96
    selectionState[DEPLOYMENT_STATES.finished].search,
97
    selectionState[DEPLOYMENT_STATES.finished].startDate,
98
    selectionState[DEPLOYMENT_STATES.finished].page,
99
    selectionState[DEPLOYMENT_STATES.finished].perPage,
100
    selectionState[DEPLOYMENT_STATES.finished].type,
101
    selectionState[DEPLOYMENT_STATES.inprogress].page,
102
    selectionState[DEPLOYMENT_STATES.inprogress].perPage,
103
    selectionState[DEPLOYMENT_STATES.pending].page,
104
    selectionState[DEPLOYMENT_STATES.pending].perPage,
105
    setLocationParams
106
  ]);
107

108
  useEffect(() => {
140✔
109
    dispatch(getGroups());
6✔
110
    if (isEnterprise) {
6✔
111
      dispatch(getDynamicGroups());
2✔
112
    }
113
    const { deploymentObject = {}, id: selectedId = [], ...remainder } = locationParams;
6!
114
    const { devices: selectedDevices = [], release: releaseName } = deploymentObject;
6✔
115
    const release = releaseName ? { ...(releases[releaseName] ?? { name: releaseName }) } : undefined;
6!
116
    const devices = selectedDevices.length ? selectedDevices.map(device => ({ ...device, ...devicesById[device.id] })) : [];
6!
117
    setDeploymentObject({ devices, release, releaseSelectionLocked: !!release });
6✔
118
    dispatch(setDeploymentsState({ selectedId: selectedId[0], ...remainder }));
6✔
119
    isInitialized.current = true;
6✔
120
  }, [dispatch, isEnterprise]);
121

122
  useEffect(() => {
140✔
123
    if (Object.keys(deploymentObject).length > 0) {
60✔
124
      // render create deployment dialog when the deployment object is initialized
125
      // otherwise CreateDeployment.setDeploymentSettings will remove device id
126
      deploymentObjInitialized.current = true;
51✔
127
    }
128
  }, [deploymentObject]);
129

130
  const retryDeployment = (deployment, deploymentDeviceIds) => {
140✔
131
    const { artifact_name, name, update_control_map = {} } = deployment;
×
132
    const release = releases[artifact_name];
×
133
    const enterpriseSettings = isEnterprise
×
134
      ? {
135
          phases: [{ batch_size: 100, start_ts: undefined, delay: 0 }],
136
          update_control_map: { states: update_control_map.states || {} }
×
137
        }
138
      : {};
139
    const targetDevicesConfig = name === ALL_DEVICES || groupsById[name] ? { group: name } : { devices: [devicesById[name]] };
×
140
    const deploymentObject = {
×
141
      deploymentDeviceIds,
142
      release,
143
      deploymentDeviceCount: deploymentDeviceIds.length,
144
      ...targetDevicesConfig,
145
      ...enterpriseSettings
146
    };
147
    setDeploymentObject(deploymentObject);
×
148
    dispatch(setDeploymentsState({ general: { showCreationDialog: true, showReportDialog: false } }));
×
149
  };
150

151
  const onScheduleSubmit = () => {
140✔
152
    dispatch(setDeploymentsState({ general: { showCreationDialog: false, showReportDialog: false } }));
2✔
153
    setDeploymentObject({});
2✔
154
    // successfully retrieved new deployment
155
    if (routes.active.key !== state) {
2✔
156
      navigate(routes.active.route);
1✔
157
      changeTab(undefined, routes.active.key);
1✔
158
    }
159
  };
160

161
  const onAbortDeployment = id =>
140✔
162
    dispatch(abortDeployment(id)).then(() => {
×
163
      dispatch(setDeploymentsState({ general: { showCreationDialog: false, showReportDialog: false } }));
×
164
      return Promise.resolve();
×
165
    });
166

167
  const changeTab = (_, tabIndex) => {
140✔
168
    dispatch(setDeploymentsState({ general: { state: tabIndex } }));
6✔
169
    dispatch(setSnackbar(''));
6✔
170
    if (pastCount && !onboardingState.complete) {
6✔
171
      dispatch(advanceOnboarding(onboardingSteps.DEPLOYMENTS_PAST));
2✔
172
    }
173
  };
174

175
  const showReport = (reportType, selectedId) => {
140✔
176
    if (!onboardingState.complete) {
1!
177
      dispatch(advanceOnboarding(onboardingSteps.DEPLOYMENTS_INPROGRESS));
1✔
178
    }
179
    dispatch(setDeploymentsState({ general: { reportType, showCreationDialog: false, showReportDialog: true }, selectedId }));
1✔
180
  };
181

182
  const closeReport = () => dispatch(setDeploymentsState({ general: { reportType: undefined, showReportDialog: false }, selectedId: undefined }));
140✔
183

184
  const onCreationDismiss = () => {
140✔
185
    dispatch(setDeploymentsState({ general: { showCreationDialog: false } }));
1✔
186
    setDeploymentObject({});
1✔
187
  };
188

189
  const onCreationShow = () => dispatch(setDeploymentsState({ general: { showCreationDialog: true } }));
140✔
190

191
  const setDeploymentSettings = useCallback(change => setDeploymentObject(current => ({ ...current, ...change })), []);
140✔
192

193
  let onboardingComponent = null;
140✔
194
  // the pastCount prop is needed to trigger the rerender as the change in past deployments would otherwise not be noticed on this view
195
  if (pastCount && tabsRef.current && !reportDialog) {
140✔
196
    const tabs = tabsRef.current.getElementsByClassName('MuiTab-root');
89✔
197
    const finishedTab = tabs[tabs.length - 1];
89✔
198
    onboardingComponent = getOnboardingComponentFor(onboardingSteps.DEPLOYMENTS_PAST, onboardingState, {
89✔
199
      anchor: {
200
        left: tabsRef.current.offsetLeft + tabsRef.current.offsetWidth - finishedTab.offsetWidth / 2,
201
        top: tabsRef.current.parentElement.offsetTop + finishedTab.offsetHeight
202
      }
203
    });
204
  }
205

206
  const ComponentToShow = routes[state].component;
140✔
207
  return (
140✔
208
    <>
209
      <div className="margin-left-small margin-top" style={{ maxWidth: '80vw' }}>
210
        <div className="flexbox space-between">
211
          <Tabs value={state} onChange={changeTab} ref={tabsRef}>
212
            {Object.values(routes).map(route => (
213
              <Tab component={Link} key={route.route} label={route.title} to={route.route} value={route.key} />
420✔
214
            ))}
215
          </Tabs>
216
          {canDeploy && canReadReleases && (
420✔
217
            <Button color="secondary" variant="contained" onClick={onCreationShow} style={{ height: '100%' }}>
218
              Create a deployment
219
            </Button>
220
          )}
221
        </div>
222
        <ComponentToShow abort={onAbortDeployment} createClick={onCreationShow} openReport={showReport} isShowingDetails={reportDialog} />
223
      </div>
224
      {reportDialog && <Report abort={onAbortDeployment} onClose={closeReport} retry={retryDeployment} type={reportType} />}
142✔
225
      {createDialog && deploymentObjInitialized.current && (
282✔
226
        <CreateDeployment
227
          onDismiss={onCreationDismiss}
228
          deploymentObject={deploymentObject}
229
          onScheduleSubmit={onScheduleSubmit}
230
          setDeploymentSettings={setDeploymentSettings}
231
        />
232
      )}
233
      {!reportDialog && onboardingComponent}
278✔
234
    </>
235
  );
236
};
237

238
export default Deployments;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc