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

mendersoftware / gui / 897326496

pending completion
897326496

Pull #3752

gitlab-ci

mzedel
chore(e2e): made use of shared timeout & login checking values to remove code duplication

Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #3752: chore(e2e-tests): slightly simplified log in test + separated log out test

4395 of 6392 branches covered (68.76%)

8060 of 9780 relevant lines covered (82.41%)

126.17 hits per line

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

78.89
/src/js/components/settings/artifactgeneration.js
1
// Copyright 2022 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 React, { useEffect, useMemo, useRef, useState } from 'react';
15
import { connect } from 'react-redux';
16

17
// material ui
18
import { InfoOutlined as InfoOutlinedIcon } from '@mui/icons-material';
19
import { Checkbox, FormControlLabel, TextField, Typography, formControlLabelClasses, textFieldClasses } from '@mui/material';
20
import { makeStyles } from 'tss-react/mui';
21

22
import DeltaIcon from '../../../assets/img/deltaicon.svg';
23
import { getDeploymentsConfig, saveDeltaDeploymentsConfig } from '../../actions/deploymentActions';
24
import { TIMEOUTS } from '../../constants/appConstants';
25
import { getTenantCapabilities } from '../../selectors';
26
import { useDebounce } from '../../utils/debouncehook';
27
import EnterpriseNotification from '../common/enterpriseNotification';
28
import InfoText from '../common/infotext';
29

30
const useStyles = makeStyles()(theme => ({
6✔
31
  deviceLimitBar: { backgroundColor: theme.palette.grey[500], margin: '15px 0' },
32
  wrapper: {
33
    backgroundColor: theme.palette.background.lightgrey,
34
    display: 'flex',
35
    flexDirection: 'column',
36
    marginTop: theme.spacing(6),
37
    padding: theme.spacing(2),
38
    paddingTop: 0,
39
    '&>h5': { marginTop: 0, marginBottom: 0 },
40
    '.flexbox>span': { alignSelf: 'flex-end' },
41
    [`.${textFieldClasses.root}`]: { maxWidth: 200, minWidth: 100 },
42
    [`.${formControlLabelClasses.root}`]: { marginTop: theme.spacing() }
43
  }
44
}));
45

46
const numberFields = {
6✔
47
  compressionLevel: { key: 'compressionLevel', title: 'Compression level' },
48
  sourceWindow: { key: 'sourceWindow', title: 'Source window size' },
49
  inputWindow: { key: 'inputWindow', title: 'Input window size' },
50
  duplicatesWindow: { key: 'duplicatesWindow', title: 'Compression duplicates window' },
51
  instructionBuffer: { key: 'instructionBuffer', title: 'Instruction buffer size' }
52
};
53

54
const NumberInputLimited = ({ limit, onChange, value: propsValue, ...remainder }) => {
6✔
55
  const [value, setValue] = useState(propsValue);
12✔
56
  const debouncedValue = useDebounce(value, TIMEOUTS.oneSecond);
12✔
57
  const { default: defaultValue, max, min } = limit;
12✔
58

59
  useEffect(() => {
12✔
60
    const minimum = Math.max(min, debouncedValue);
6✔
61
    const allowedValue = Math.min(max ?? minimum, minimum);
6✔
62
    if (allowedValue !== debouncedValue) {
6!
63
      setValue(allowedValue);
6✔
64
      return;
6✔
65
    }
66
    onChange(allowedValue);
×
67
  }, [debouncedValue]);
68

69
  return (
12✔
70
    <TextField
71
      inputProps={{ step: 1, type: 'numeric', pattern: '[0-9]*', autoComplete: 'off' }}
72
      InputLabelProps={{ shrink: true }}
73
      error={min || max ? min > value || value > max : false}
41✔
74
      value={value}
75
      onChange={({ target: { value } }) => setValue(Number(value) || 0)}
×
76
      helperText={defaultValue !== undefined ? `Defaults to: ${defaultValue}` : null}
12✔
77
      {...remainder}
78
    />
79
  );
80
};
81

82
export const ArtifactGenerationSettings = ({ deltaConfig, deltaEnabled, deltaLimits, getDeploymentsConfig, isEnterprise, saveDeltaDeploymentsConfig }) => {
6✔
83
  const [timeoutValue, setTimeoutValue] = useState(deltaConfig.timeout);
2✔
84
  const [disableChecksum, setDisableChecksum] = useState(deltaConfig.disableChecksum);
2✔
85
  const [disableDecompression, setDisableDecompression] = useState(deltaConfig.disableChecksum);
2✔
86
  const [compressionLevel, setCompressionLevel] = useState(deltaConfig.compressionLevel);
2✔
87
  const [sourceWindow, setSourceWindow] = useState(deltaConfig.sourceWindow);
2✔
88
  const [inputWindow, setInputWindow] = useState(deltaConfig.inputWindow);
2✔
89
  const [duplicatesWindow, setDuplicatesWindow] = useState(deltaConfig.duplicatesWindow);
2✔
90
  const [instructionBuffer, setInstructionBuffer] = useState(deltaConfig.instructionBuffer);
2✔
91
  const [isInitialized, setIsInitialized] = useState(false);
2✔
92
  const timer = useRef(null);
2✔
93

94
  const { classes } = useStyles();
2✔
95

96
  useEffect(() => {
2✔
97
    if (deltaConfig.timeout === -1) {
1!
98
      return;
×
99
    }
100
    const { timeout, duplicatesWindow, compressionLevel, disableChecksum, disableDecompression, inputWindow, instructionBuffer, sourceWindow } = deltaConfig;
1✔
101
    setDisableChecksum(disableChecksum);
1✔
102
    setDisableDecompression(disableDecompression);
1✔
103
    setCompressionLevel(compressionLevel);
1✔
104
    setTimeoutValue(timeout);
1✔
105
    setSourceWindow(sourceWindow);
1✔
106
    setInputWindow(inputWindow);
1✔
107
    setDuplicatesWindow(duplicatesWindow);
1✔
108
    setInstructionBuffer(instructionBuffer);
1✔
109
    setTimeout(() => setIsInitialized(true), 0);
1✔
110
  }, [JSON.stringify(deltaConfig), JSON.stringify(deltaLimits)]);
111

112
  useEffect(() => {
2✔
113
    getDeploymentsConfig();
1✔
114
  }, []);
115

116
  useEffect(() => {
2✔
117
    if (!isInitialized) {
1!
118
      return;
1✔
119
    }
120
    clearTimeout(timer.current);
×
121
    timer.current = setTimeout(
×
122
      () =>
123
        saveDeltaDeploymentsConfig({
×
124
          timeout: timeoutValue,
125
          duplicatesWindow,
126
          compressionLevel,
127
          disableChecksum,
128
          disableDecompression,
129
          inputWindow,
130
          instructionBuffer,
131
          sourceWindow
132
        }),
133
      TIMEOUTS.twoSeconds
134
    );
135
    return () => {
×
136
      clearTimeout(timer.current);
×
137
    };
138
  }, [compressionLevel, disableChecksum, disableDecompression, duplicatesWindow, inputWindow, instructionBuffer, sourceWindow, timeoutValue]);
139

140
  const numberInputs = useMemo(() => {
2✔
141
    return [
1✔
142
      { ...numberFields.compressionLevel, setter: setCompressionLevel, value: compressionLevel, ...deltaLimits.compressionLevel },
143
      { ...numberFields.sourceWindow, setter: setSourceWindow, value: sourceWindow, ...deltaLimits.sourceWindow },
144
      { ...numberFields.inputWindow, setter: setInputWindow, value: inputWindow, ...deltaLimits.inputWindow },
145
      { ...numberFields.duplicatesWindow, setter: setDuplicatesWindow, value: duplicatesWindow, ...deltaLimits.duplicatesWindow },
146
      { ...numberFields.instructionBuffer, setter: setInstructionBuffer, value: instructionBuffer, ...deltaLimits.instructionBuffer }
147
    ];
148
  }, [
149
    compressionLevel,
150
    setCompressionLevel,
151
    setSourceWindow,
152
    sourceWindow,
153
    inputWindow,
154
    setInputWindow,
155
    setDuplicatesWindow,
156
    duplicatesWindow,
157
    setInstructionBuffer,
158
    instructionBuffer,
159
    JSON.stringify(deltaLimits)
160
  ]);
161

162
  return (
2✔
163
    <div className={`flexbox column ${classes.wrapper}`}>
164
      <div className="flexbox center-aligned">
165
        <DeltaIcon />
166
        <h5 className="margin-left-small">Delta artifacts generation configuration</h5>
167
      </div>
168
      {deltaEnabled && isInitialized ? (
6✔
169
        <div className="margin-small margin-top-none">
170
          <div className="flexbox">
171
            <NumberInputLimited
172
              limit={{ default: deltaLimits.timeout.default, min: deltaLimits.timeout.min, max: deltaLimits.timeout.max }}
173
              label="Timeout"
174
              onChange={setTimeoutValue}
175
              value={timeoutValue}
176
            />
177
            <span className="margin-left-small muted slightly-smaller">seconds</span>
178
          </div>
179
          <Typography className="margin-top-small" display="block" variant="caption">
180
            xDelta3 arguments
181
          </Typography>
182
          <div className="flexbox column margin-left">
183
            <FormControlLabel
184
              control={<Checkbox color="primary" checked={disableChecksum} onChange={({ target: { checked } }) => setDisableChecksum(checked)} size="small" />}
×
185
              label="Disable checksum"
186
            />
187
            <FormControlLabel
188
              control={
189
                <Checkbox
190
                  color="primary"
191
                  checked={disableDecompression}
192
                  onChange={({ target: { checked } }) => setDisableDecompression(checked)}
×
193
                  size="small"
194
                />
195
              }
196
              label="Disable external decompression"
197
            />
198
            {numberInputs.map(({ default: defaultValue, key, setter, title, value, min = 0, max }) => (
1✔
199
              <NumberInputLimited key={key} limit={{ default: defaultValue, max, min }} label={title} value={value} onChange={setter} />
5✔
200
            ))}
201
          </div>
202
        </div>
203
      ) : isEnterprise ? (
1!
204
        <InfoText>
205
          <InfoOutlinedIcon style={{ fontSize: '14px', margin: '0 4px 4px 0', verticalAlign: 'middle' }} />
206
          Automatic delta artifacts generation is not enabled in your account. If you want to start using this feature,{' '}
207
          <a href="mailto:contact@mender.io" target="_blank" rel="noopener noreferrer">
208
            contact our team
209
          </a>
210
          .
211
        </InfoText>
212
      ) : (
213
        <EnterpriseNotification
214
          isEnterprise={isEnterprise}
215
          benefit="automatic delta artifacts generation to minimize data transfer and improve the update delivery"
216
        />
217
      )}
218
    </div>
219
  );
220
};
221

222
const actionCreators = { getDeploymentsConfig, saveDeltaDeploymentsConfig };
6✔
223

224
const mapStateToProps = state => {
6✔
225
  const { binaryDelta = {}, binaryDeltaLimits = {}, hasDelta } = state.deployments.config ?? {};
1!
226
  const { isEnterprise } = getTenantCapabilities(state);
1✔
227
  return {
1✔
228
    deltaConfig: binaryDelta,
229
    deltaEnabled: hasDelta,
230
    deltaLimits: binaryDeltaLimits,
231
    isEnterprise
232
  };
233
};
234

235
export default connect(mapStateToProps, actionCreators)(ArtifactGenerationSettings);
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