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

mendersoftware / gui / 1336732209

18 Jun 2024 08:59AM UTC coverage: 83.434% (-16.5%) from 99.965%
1336732209

Pull #4443

gitlab-ci

mzedel
feat: added notification about changes to the device offline threshold

Ticket: MEN-7288
Changelog: Title
Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #4443: MEN-7288 - feat/threshold migration

4493 of 6427 branches covered (69.91%)

33 of 35 new or added lines in 8 files covered. (94.29%)

1680 existing lines in 163 files now uncovered.

8547 of 10244 relevant lines covered (83.43%)

151.26 hits per line

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

86.57
/src/js/actions/appActions.js
1
// Copyright 2019 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
import moment from 'moment';
15
import Cookies from 'universal-cookie';
16

17
import GeneralApi from '../api/general-api';
18
import { getSessionInfo, getToken } from '../auth';
19
import {
20
  SET_ENVIRONMENT_DATA,
21
  SET_FEATURES,
22
  SET_FIRST_LOGIN_AFTER_SIGNUP,
23
  SET_OFFLINE_THRESHOLD,
24
  SET_SEARCH_STATE,
25
  SET_SNACKBAR,
26
  SET_VERSION_INFORMATION,
27
  TIMEOUTS
28
} from '../constants/appConstants';
29
import { DEPLOYMENT_STATES } from '../constants/deploymentConstants';
30
import { DEVICE_STATES, timeUnits } from '../constants/deviceConstants';
31
import { onboardingSteps } from '../constants/onboardingConstants';
32
import { SET_TOOLTIPS_STATE, SUCCESSFULLY_LOGGED_IN } from '../constants/userConstants';
33
import { deepCompare, extractErrorMessage, preformatWithRequestID, stringToBoolean } from '../helpers';
34
import { getFeatures, getIsEnterprise, getOfflineThresholdSettings, getUserCapabilities, getUserSettings as getUserSettingsSelector } from '../selectors';
35
import { getOnboardingComponentFor } from '../utils/onboardingmanager';
36
import { getDeploymentsByStatus } from './deploymentActions';
37
import {
38
  getDeviceAttributes,
39
  getDeviceById,
40
  getDeviceLimit,
41
  getDevicesByStatus,
42
  getDynamicGroups,
43
  getGroups,
44
  searchDevices,
45
  setDeviceListState
46
} from './deviceActions';
47
import { getOnboardingState, setDemoArtifactPort, setOnboardingComplete } from './onboardingActions';
48
import { getIntegrations, getUserOrganization } from './organizationActions';
49
import { getReleases } from './releaseActions';
50
import { getGlobalSettings, getRoles, getUserSettings, saveGlobalSettings, saveUserSettings, setShowStartupNotification } from './userActions';
51

52
const cookies = new Cookies();
184✔
53

54
export const commonErrorFallback = 'Please check your connection.';
184✔
55
export const commonErrorHandler = (err, errorContext, dispatch, fallback, mightBeAuthRelated = false) => {
184✔
56
  const errMsg = extractErrorMessage(err, fallback);
6✔
57
  if (mightBeAuthRelated || getToken()) {
6!
58
    dispatch(setSnackbar(preformatWithRequestID(err.response, `${errorContext} ${errMsg}`), null, 'Copy to clipboard'));
6✔
59
  }
60
  return Promise.reject(err);
6✔
61
};
62

63
const getComparisonCompatibleVersion = version => (isNaN(version.charAt(0)) && version !== 'next' ? 'master' : version);
184✔
64

65
const featureFlags = [
184✔
66
  'hasAuditlogs',
67
  'hasMultitenancy',
68
  'hasDeltaProgress',
69
  'hasDeviceConfig',
70
  'hasDeviceConnect',
71
  'hasReporting',
72
  'hasMonitor',
73
  'hasMultiTenantAccess',
74
  'isEnterprise'
75
];
76
export const parseEnvironmentInfo = () => (dispatch, getState) => {
184✔
77
  const state = getState();
15✔
78
  let onboardingComplete = state.onboarding.complete || !!JSON.parse(window.localStorage.getItem('onboardingComplete') ?? 'false');
15✔
79
  let demoArtifactPort = 85;
15✔
80
  let environmentData = {};
15✔
81
  let environmentFeatures = {};
15✔
82
  let versionInfo = {};
15✔
83
  if (mender_environment) {
15!
84
    const {
85
      features = {},
×
86
      demoArtifactPort: port,
87
      disableOnboarding,
88
      hostAddress,
89
      hostedAnnouncement,
90
      integrationVersion,
91
      isDemoMode,
92
      menderVersion,
93
      menderArtifactVersion,
94
      metaMenderVersion,
95
      recaptchaSiteKey,
96
      services = {},
×
97
      stripeAPIKey,
98
      trackerCode
99
    } = mender_environment;
15✔
100
    onboardingComplete = stringToBoolean(features.isEnterprise) || stringToBoolean(disableOnboarding) || onboardingComplete;
15✔
101
    demoArtifactPort = port || demoArtifactPort;
15✔
102
    environmentData = {
15✔
103
      hostedAnnouncement: hostedAnnouncement || state.app.hostedAnnouncement,
30✔
104
      hostAddress: hostAddress || state.app.hostAddress,
30✔
105
      recaptchaSiteKey: recaptchaSiteKey || state.app.recaptchaSiteKey,
30✔
106
      stripeAPIKey: stripeAPIKey || state.app.stripeAPIKey,
30✔
107
      trackerCode: trackerCode || state.app.trackerCode
30✔
108
    };
109
    environmentFeatures = {
15✔
110
      ...featureFlags.reduce((accu, flag) => ({ ...accu, [flag]: stringToBoolean(features[flag]) }), {}),
135✔
111
      // the check in features is purely kept as a local override, it shouldn't become relevant for production again
112
      isHosted: features.isHosted || window.location.hostname.includes('hosted.mender.io'),
30✔
113
      isDemoMode: stringToBoolean(isDemoMode || features.isDemoMode)
30✔
114
    };
115
    versionInfo = {
15✔
116
      docs: isNaN(integrationVersion.charAt(0)) ? '' : integrationVersion.split('.').slice(0, 2).join('.'),
15!
117
      remainder: {
118
        Integration: getComparisonCompatibleVersion(integrationVersion),
119
        'Mender-Client': getComparisonCompatibleVersion(menderVersion),
120
        'Mender-Artifact': menderArtifactVersion,
121
        'Meta-Mender': metaMenderVersion,
122
        Deployments: services.deploymentsVersion,
123
        Deviceauth: services.deviceauthVersion,
124
        Inventory: services.inventoryVersion,
125
        GUI: services.guiVersion
126
      }
127
    };
128
  }
129
  return Promise.all([
15✔
130
    dispatch({ type: SUCCESSFULLY_LOGGED_IN, value: getSessionInfo() }),
131
    dispatch(setOnboardingComplete(onboardingComplete)),
132
    dispatch(setDemoArtifactPort(demoArtifactPort)),
133
    dispatch({ type: SET_FEATURES, value: environmentFeatures }),
134
    dispatch({ type: SET_VERSION_INFORMATION, docsVersion: versionInfo.docs, value: versionInfo.remainder }),
135
    dispatch({ type: SET_ENVIRONMENT_DATA, value: environmentData }),
136
    dispatch(getLatestReleaseInfo())
137
  ]);
138
};
139

140
const maybeAddOnboardingTasks = ({ devicesByStatus, dispatch, onboardingState, tasks }) => {
184✔
141
  if (!onboardingState.showTips || onboardingState.complete) {
9!
142
    return tasks;
9✔
143
  }
UNCOV
144
  const welcomeTip = getOnboardingComponentFor(onboardingSteps.ONBOARDING_START, {
×
145
    progress: onboardingState.progress,
146
    complete: onboardingState.complete,
147
    showTips: onboardingState.showTips
148
  });
UNCOV
149
  if (welcomeTip) {
×
UNCOV
150
    tasks.push(dispatch(setSnackbar('open', TIMEOUTS.refreshDefault, '', welcomeTip, () => {}, true)));
×
151
  }
152
  // try to retrieve full device details for onboarding devices to ensure ips etc. are available
153
  // we only load the first few/ 20 devices, as it is possible the onboarding is left dangling
154
  // and a lot of devices are present and we don't want to flood the backend for this
UNCOV
155
  return devicesByStatus[DEVICE_STATES.accepted].deviceIds.reduce((accu, id) => {
×
UNCOV
156
    accu.push(dispatch(getDeviceById(id)));
×
UNCOV
157
    return accu;
×
158
  }, tasks);
159
};
160

161
const interpretAppData = () => (dispatch, getState) => {
184✔
162
  const state = getState();
9✔
163
  let { columnSelection = [], trackingConsentGiven: hasTrackingEnabled, tooltips = {} } = getUserSettingsSelector(state);
9!
164
  let settings = {};
9✔
165
  if (cookies.get('_ga') && typeof hasTrackingEnabled === 'undefined') {
9!
UNCOV
166
    settings.trackingConsentGiven = true;
×
167
  }
168
  let tasks = [
9✔
UNCOV
169
    dispatch(setDeviceListState({ selectedAttributes: columnSelection.map(column => ({ attribute: column.key, scope: column.scope })) })),
×
170
    dispatch({ type: SET_TOOLTIPS_STATE, value: tooltips }), // tooltips read state is primarily trusted from the redux store, except on app init - here user settings are the reference
171
    dispatch(saveUserSettings(settings))
172
  ];
173
  tasks = maybeAddOnboardingTasks({ devicesByStatus: state.devices.byStatus, dispatch, tasks, onboardingState: state.onboarding });
9✔
174

175
  const { canManageUsers } = getUserCapabilities(getState());
9✔
176
  const { interval, intervalUnit } = getOfflineThresholdSettings(getState());
9✔
177
  if (canManageUsers && intervalUnit && intervalUnit !== timeUnits.days) {
9✔
178
    const duration = moment.duration(interval, intervalUnit);
5✔
179
    const days = duration.asDays();
5✔
180
    if (days < 1) {
5✔
181
      tasks.push(Promise.resolve(setTimeout(() => dispatch(setShowStartupNotification(true)), TIMEOUTS.fiveSeconds)));
3✔
182
    } else {
183
      const roundedDays = Math.max(1, Math.round(days));
2✔
184
      tasks.push(dispatch(saveGlobalSettings({ offlineThreshold: { interval: roundedDays, intervalUnit: timeUnits.days } })));
2✔
185
    }
186
  }
187

188
  // the following is used as a migration and initialization of the stored identity attribute
189
  // changing the default device attribute to the first non-deviceId attribute, unless a stored
190
  // id attribute setting exists
191
  const identityOptions = state.devices.filteringAttributes.identityAttributes.filter(attribute => !['id', 'Device ID', 'status'].includes(attribute));
11✔
192
  const { id_attribute } = state.users.globalSettings;
9✔
193
  if (!id_attribute && identityOptions.length) {
9✔
194
    tasks.push(dispatch(saveGlobalSettings({ id_attribute: { attribute: identityOptions[0], scope: 'identity' } })));
3✔
195
  } else if (typeof id_attribute === 'string') {
6!
UNCOV
196
    let attribute = id_attribute;
×
UNCOV
197
    if (attribute === 'Device ID') {
×
UNCOV
198
      attribute = 'id';
×
199
    }
UNCOV
200
    tasks.push(dispatch(saveGlobalSettings({ id_attribute: { attribute, scope: 'identity' } })));
×
201
  }
202
  return Promise.all(tasks);
9✔
203
};
204

205
const retrieveAppData = () => (dispatch, getState) => {
184✔
206
  let tasks = [
9✔
207
    dispatch(parseEnvironmentInfo()),
208
    dispatch(getUserSettings()),
209
    dispatch(getGlobalSettings()),
210
    dispatch(getDeviceAttributes()),
211
    dispatch(getDeploymentsByStatus(DEPLOYMENT_STATES.finished, undefined, undefined, undefined, undefined, undefined, undefined, false)),
212
    dispatch(getDeploymentsByStatus(DEPLOYMENT_STATES.inprogress)),
213
    dispatch(getDevicesByStatus(DEVICE_STATES.accepted)),
214
    dispatch(getDevicesByStatus(DEVICE_STATES.pending)),
215
    dispatch(getDevicesByStatus(DEVICE_STATES.preauth)),
216
    dispatch(getDevicesByStatus(DEVICE_STATES.rejected)),
217
    dispatch(getDynamicGroups()),
218
    dispatch(getGroups()),
219
    dispatch(getIntegrations()),
220
    dispatch(getReleases()),
221
    dispatch(getDeviceLimit()),
222
    dispatch(getRoles()),
223
    dispatch(setFirstLoginAfterSignup(stringToBoolean(cookies.get('firstLoginAfterSignup'))))
224
  ];
225
  const { hasMultitenancy, isHosted } = getFeatures(getState());
9✔
226
  const multitenancy = hasMultitenancy || isHosted || getIsEnterprise(getState());
9✔
227
  if (multitenancy) {
9✔
228
    tasks.push(dispatch(getUserOrganization()));
8✔
229
  }
230
  return Promise.all(tasks);
9✔
231
};
232

233
export const initializeAppData = () => dispatch =>
184✔
234
  dispatch(retrieveAppData())
9✔
235
    .then(() => dispatch(interpretAppData()))
9✔
236
    // this is allowed to fail if no user information are available
UNCOV
237
    .catch(err => console.log(extractErrorMessage(err)))
×
238
    .then(() => dispatch(getOnboardingState()));
9✔
239

240
/*
241
  General
242
*/
243
export const setSnackbar = (message, autoHideDuration, action, children, onClick, onClose) => dispatch =>
184✔
244
  dispatch({
133✔
245
    type: SET_SNACKBAR,
246
    snackbar: {
247
      open: message ? true : false,
133✔
248
      message,
249
      maxWidth: '900px',
250
      autoHideDuration,
251
      action,
252
      children,
253
      onClick,
254
      onClose
255
    }
256
  });
257

258
export const setFirstLoginAfterSignup = firstLoginAfterSignup => dispatch => {
184✔
259
  cookies.set('firstLoginAfterSignup', !!firstLoginAfterSignup, { maxAge: 60, path: '/', domain: '.mender.io', sameSite: false });
13✔
260
  dispatch({ type: SET_FIRST_LOGIN_AFTER_SIGNUP, firstLoginAfterSignup: !!firstLoginAfterSignup });
13✔
261
};
262

263
const dateFunctionMap = {
184✔
264
  getDays: 'getDate',
265
  setDays: 'setDate'
266
};
267
export const setOfflineThreshold = () => (dispatch, getState) => {
184✔
268
  const { interval, intervalUnit } = getOfflineThresholdSettings(getState());
25✔
269
  const today = new Date();
25✔
270
  const intervalName = `${intervalUnit.charAt(0).toUpperCase()}${intervalUnit.substring(1)}`;
25✔
271
  const setter = dateFunctionMap[`set${intervalName}`] ?? `set${intervalName}`;
25✔
272
  const getter = dateFunctionMap[`get${intervalName}`] ?? `get${intervalName}`;
25✔
273
  today[setter](today[getter]() - interval);
25✔
274
  let value;
275
  try {
25✔
276
    value = today.toISOString();
25✔
277
  } catch {
UNCOV
278
    return Promise.resolve(dispatch(setSnackbar('There was an error saving the offline threshold, please check your settings.')));
×
279
  }
280
  return Promise.resolve(dispatch({ type: SET_OFFLINE_THRESHOLD, value }));
25✔
281
};
282

283
export const setVersionInfo = info => (dispatch, getState) =>
184✔
284
  Promise.resolve(
2✔
285
    dispatch({
286
      type: SET_VERSION_INFORMATION,
287
      docsVersion: getState().app.docsVersion,
288
      value: {
289
        ...getState().app.versionInformation,
290
        ...info
291
      }
292
    })
293
  );
294

295
const versionRegex = new RegExp(/\d+\.\d+/);
184✔
296
const getLatestRelease = thing => {
184✔
297
  const latestKey = Object.keys(thing)
16✔
298
    .filter(key => versionRegex.test(key))
40✔
299
    .sort()
300
    .reverse()[0];
301
  return thing[latestKey];
16✔
302
};
303

304
const repoKeyMap = {
184✔
305
  integration: 'Integration',
306
  mender: 'Mender-Client',
307
  'mender-artifact': 'Mender-Artifact'
308
};
309

310
const deductSaasState = (latestRelease, guiTags, saasReleases) => {
184✔
311
  const latestGuiTag = guiTags.length ? guiTags[0].name : '';
8!
312
  const latestSaasRelease = latestGuiTag.startsWith('saas-v') ? { date: latestGuiTag.split('-v')[1].replaceAll('.', '-'), tag: latestGuiTag } : saasReleases[0];
8!
313
  return latestSaasRelease.date > latestRelease.release_date ? latestSaasRelease.tag : latestRelease.release;
8!
314
};
315

316
export const getLatestReleaseInfo = () => (dispatch, getState) => {
184✔
317
  if (!getState().app.features.isHosted) {
19✔
318
    return Promise.resolve();
11✔
319
  }
320
  return Promise.all([GeneralApi.get('/versions.json'), GeneralApi.get('/tags.json')])
8✔
321
    .catch(err => {
UNCOV
322
      console.log('init error:', extractErrorMessage(err));
×
UNCOV
323
      return Promise.resolve([{ data: {} }, { data: [] }]);
×
324
    })
325
    .then(([{ data }, { data: guiTags }]) => {
326
      if (!guiTags.length) {
8!
UNCOV
327
        return Promise.resolve();
×
328
      }
329
      const { releases, saas } = data;
8✔
330
      const latestRelease = getLatestRelease(getLatestRelease(releases));
8✔
331
      const { latestRepos, latestVersions } = latestRelease.repos.reduce(
8✔
332
        (accu, item) => {
333
          if (repoKeyMap[item.name]) {
40✔
334
            accu.latestVersions[repoKeyMap[item.name]] = getComparisonCompatibleVersion(item.version);
24✔
335
          }
336
          accu.latestRepos[item.name] = getComparisonCompatibleVersion(item.version);
40✔
337
          return accu;
40✔
338
        },
339
        { latestVersions: { ...getState().app.versionInformation }, latestRepos: {} }
340
      );
341
      const info = deductSaasState(latestRelease, guiTags, saas);
8✔
342
      return Promise.resolve(
8✔
343
        dispatch({
344
          type: SET_VERSION_INFORMATION,
345
          docsVersion: getState().app.docsVersion,
346
          value: {
347
            ...latestVersions,
348
            backend: info,
349
            GUI: info,
350
            latestRelease: {
351
              releaseDate: latestRelease.release_date,
352
              repos: latestRepos
353
            }
354
          }
355
        })
356
      );
357
    });
358
};
359

360
export const setSearchState = searchState => (dispatch, getState) => {
184✔
361
  const currentState = getState().app.searchState;
4✔
362
  let nextState = {
4✔
363
    ...currentState,
364
    ...searchState,
365
    sort: {
366
      ...currentState.sort,
367
      ...searchState.sort
368
    }
369
  };
370
  let tasks = [];
4✔
371
  // eslint-disable-next-line no-unused-vars
372
  const { isSearching: currentSearching, deviceIds: currentDevices, searchTotal: currentTotal, ...currentRequestState } = currentState;
4✔
373
  // eslint-disable-next-line no-unused-vars
374
  const { isSearching: nextSearching, deviceIds: nextDevices, searchTotal: nextTotal, ...nextRequestState } = nextState;
4✔
375
  if (nextRequestState.searchTerm && !deepCompare(currentRequestState, nextRequestState)) {
4✔
376
    nextState.isSearching = true;
2✔
377
    tasks.push(
2✔
378
      dispatch(searchDevices(nextState))
379
        .then(results => {
380
          const searchResult = results[results.length - 1];
2✔
381
          return dispatch(setSearchState({ ...searchResult, isSearching: false }));
2✔
382
        })
UNCOV
383
        .catch(() => dispatch(setSearchState({ isSearching: false, searchTotal: 0 })))
×
384
    );
385
  }
386
  tasks.push(dispatch({ type: SET_SEARCH_STATE, state: nextState }));
4✔
387
  return Promise.all(tasks);
4✔
388
};
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