Skip to content

Commit 75fa1f1

Browse files
committed
Fixes #39517 - Fix JS magic number usage
1 parent 61739ea commit 75fa1f1

16 files changed

Lines changed: 77 additions & 23 deletions

File tree

webpack/JobInvocationDetail/JobInvocationActions.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
CHANGE_ENABLED_RECURRING_LOGIC,
1313
GET_TASK,
1414
JOB_INVOCATION_KEY,
15+
POLLING_INTERVAL_MS,
1516
UPDATE_JOB,
1617
} from './JobInvocationConstants';
1718

@@ -32,7 +33,7 @@ export const getJobInvocation = url => dispatch => {
3233
response?.data?.error?.message ||
3334
'Error',
3435
}),
35-
1000
36+
POLLING_INTERVAL_MS
3637
);
3738

3839
dispatch(fetchData);

webpack/JobInvocationDetail/JobInvocationConstants.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ export const DIRECT_OPEN_HOST_LIMIT = 3;
2020
export const ALL_JOB_HOSTS = 'ALL_JOB_HOSTS';
2121
export const AWAITING_STATUS_FILTER = '(job_invocation.result = N/A)';
2222

23+
export const POLLING_INTERVAL_MS = 1000;
24+
export const AUTO_REFRESH_INTERVAL_MS = 5000;
25+
export const UNIX_TO_JS_TIMESTAMP_FACTOR = 1000;
26+
export const DEFAULT_CHART_LEGEND_WIDTH = 270;
27+
export const CLIPBOARD_COPIED_EXIT_DELAY_MS = 1500;
28+
export const CLIPBOARD_DEFAULT_EXIT_DELAY_MS = 600;
29+
2330
export const showTemplateInvocationUrl = (hostID, jobID) =>
2431
`/show_template_invocation_by_host/${hostID}/job_invocation/${jobID}`;
2532
export const LIST_TEMPLATE_INVOCATIONS = 'LIST_TEMPLATE_INVOCATIONS';

webpack/JobInvocationDetail/JobInvocationSystemStatusChart.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ import {
2323
global_palette_blue_300 as inProgressColor,
2424
global_palette_green_500 as successedColor,
2525
} from '@patternfly/react-tokens';
26-
import { STATUS_TITLES } from './JobInvocationConstants';
26+
import {
27+
STATUS_TITLES,
28+
DEFAULT_CHART_LEGEND_WIDTH,
29+
} from './JobInvocationConstants';
2730
import './JobInvocationDetail.scss';
2831

2932
const JobInvocationSystemStatusChart = ({
@@ -52,7 +55,7 @@ const JobInvocationSystemStatusChart = ({
5255
return '0';
5356
};
5457
const chartSize = 105;
55-
const [legendWidth, setLegendWidth] = useState(270);
58+
const [legendWidth, setLegendWidth] = useState(DEFAULT_CHART_LEGEND_WIDTH);
5659

5760
// Calculates chart legend width based on its content
5861
useEffect(() => {

webpack/JobInvocationDetail/TemplateInvocation.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import {
1111
showTemplateInvocationUrl,
1212
templateInvocationPageUrl,
1313
GET_TEMPLATE_INVOCATION,
14+
CLIPBOARD_COPIED_EXIT_DELAY_MS,
15+
CLIPBOARD_DEFAULT_EXIT_DELAY_MS,
16+
AUTO_REFRESH_INTERVAL_MS,
1417
} from './JobInvocationConstants';
1518
import {
1619
selectTemplateInvocationStatus,
@@ -41,7 +44,11 @@ const CopyToClipboard = ({ fullOutput }) => {
4144
textId="code-content"
4245
aria-label="Copy to clipboard"
4346
onClick={e => onClick(e, fullOutput)}
44-
exitDelay={copied ? 1500 : 600}
47+
exitDelay={
48+
copied
49+
? CLIPBOARD_COPIED_EXIT_DELAY_MS
50+
: CLIPBOARD_DEFAULT_EXIT_DELAY_MS
51+
}
4552
maxWidth="110px"
4653
variant="plain"
4754
onTooltipHidden={() => setCopied(false)}
@@ -111,7 +118,7 @@ export const TemplateInvocation = ({
111118
} else if (intervalRef.current) {
112119
clearInterval(intervalRef.current);
113120
}
114-
}, 5000);
121+
}, AUTO_REFRESH_INTERVAL_MS);
115122
}
116123

117124
return () => {

webpack/JobInvocationDetail/TemplateInvocationComponents/OutputCodeBlock.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import React, {
77
import PropTypes from 'prop-types';
88
import { Button } from '@patternfly/react-core';
99
import { translate as __ } from 'foremanReact/common/I18n';
10+
import { UNIX_TO_JS_TIMESTAMP_FACTOR } from '../JobInvocationConstants';
1011

1112
export const OutputCodeBlock = ({ code, showOutputType, scrollElement }) => {
1213
let lineCounter = 0;
@@ -122,7 +123,9 @@ export const OutputCodeBlock = ({ code, showOutputType, scrollElement }) => {
122123
<div key={index} className={`line ${line.output_type}`}>
123124
<span
124125
className="counter"
125-
title={new Date(line.timestamp * 1000).toISOString()}
126+
title={new Date(
127+
line.timestamp * UNIX_TO_JS_TIMESTAMP_FACTOR
128+
).toISOString()}
126129
>
127130
{lineCounter.toString().padStart(4, '\u00A0')}:{' '}
128131
</span>

webpack/JobWizard/JobWizard.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
WIZARD_TITLES,
1919
SCHEDULE_TYPES,
2020
initialScheduleState,
21+
STARTS_ERROR_CHECK_INTERVAL_MS,
2122
} from './JobWizardConstants';
2223
import {
2324
selectTemplateError,
@@ -237,7 +238,10 @@ export const JobWizard = ({ rerunData }) => {
237238
}
238239
};
239240
updateStartsError();
240-
const interval = setInterval(updateStartsError, 5000);
241+
const interval = setInterval(
242+
updateStartsError,
243+
STARTS_ERROR_CHECK_INTERVAL_MS
244+
);
241245

242246
return () => {
243247
interval && clearInterval(interval);

webpack/JobWizard/JobWizardConstants.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,12 @@ export const HOST_IDS = 'HOST_IDS';
7878
export const REX_FEATURE = 'REX_FEATURE';
7979

8080
export const JOB_API_KEY = 'JOB_API_KEY';
81+
82+
export const MINUTES_IN_HOUR = 60;
83+
export const DAYS_IN_WEEK = 7;
84+
export const SUNDAY_BASE_YEAR = 2017;
85+
export const DEFAULT_MINUTE_OPTIONS = [0, 15, 30, 45];
86+
export const MS_PER_MINUTE = 60000;
87+
export const STARTS_ERROR_CHECK_INTERVAL_MS = 5000;
88+
export const DATE_PADDING_SLICE = -2;
89+
export const DEBOUNCE_INPUT_MS = 1000;

webpack/JobWizard/steps/Schedule/RepeatHour.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ import {
1313
} from '@patternfly/react-core/deprecated';
1414
import { translate as __ } from 'foremanReact/common/I18n';
1515
import { helpLabel } from '../form/FormHelpers';
16+
import {
17+
MINUTES_IN_HOUR,
18+
DEFAULT_MINUTE_OPTIONS,
19+
} from '../../JobWizardConstants';
1620

1721
export const RepeatHour = ({ repeatData, setRepeatData }) => {
1822
const isValidMinute = newMinute =>
1923
Number.isInteger(parseInt(newMinute, 10)) &&
2024
newMinute >= 0 &&
21-
newMinute < 60;
25+
newMinute < MINUTES_IN_HOUR;
2226

2327
const { minute } = repeatData;
2428
useEffect(() => {
@@ -27,7 +31,7 @@ export const RepeatHour = ({ repeatData, setRepeatData }) => {
2731
}
2832
}, [minute, setRepeatData]);
2933
const [minuteOpen, setMinuteOpen] = useState(false);
30-
const [options, setOptions] = useState([0, 15, 30, 45]);
34+
const [options, setOptions] = useState(DEFAULT_MINUTE_OPTIONS);
3135
const [isAlertOpen, setIsAlertOpen] = useState(false);
3236
return (
3337
<FormGroup

webpack/JobWizard/steps/Schedule/RepeatWeek.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import { FormGroup, Checkbox } from '@patternfly/react-core';
44
import { translate as __, documentLocale } from 'foremanReact/common/I18n';
55
import { RepeatDaily } from './RepeatDaily';
66
import { noop } from '../../../helpers';
7+
import { SUNDAY_BASE_YEAR, DAYS_IN_WEEK } from '../../JobWizardConstants';
78

89
export const getWeekDays = () => {
910
const locale = documentLocale().replace(/-/g, '_');
10-
const baseDate = new Date(Date.UTC(2017, 0, 1)); // just a Sunday
11+
const baseDate = new Date(Date.UTC(SUNDAY_BASE_YEAR, 0, 1)); // just a Sunday
1112
const weekDays = [];
1213
const formatOptions = { weekday: 'short', timeZone: 'UTC' };
13-
for (let i = 0; i < 7; i++) {
14+
for (let i = 0; i < DAYS_IN_WEEK; i++) {
1415
try {
1516
weekDays.push(baseDate.toLocaleDateString(locale, formatOptions));
1617
} catch {

webpack/JobWizard/steps/Schedule/ScheduleRecurring.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
import { ExclamationCircleIcon } from '@patternfly/react-icons';
1414
import { translate as __ } from 'foremanReact/common/I18n';
1515
import { RepeatOn } from './RepeatOn';
16-
import { SCHEDULE_TYPES } from '../../JobWizardConstants';
16+
import { SCHEDULE_TYPES, MS_PER_MINUTE } from '../../JobWizardConstants';
1717
import { PurposeField } from './PurposeField';
1818
import { DateTimePicker } from '../form/DateTimePicker';
1919
import { WizardTitle } from '../form/WizardTitle';
@@ -114,7 +114,7 @@ export const ScheduleRecurring = ({
114114
setScheduleValue(current => ({
115115
...current,
116116
startsAt: new Date(
117-
new Date().getTime() + 60000
117+
new Date().getTime() + MS_PER_MINUTE
118118
).toISOString(), // 1 minute in the future
119119
isFuture: true,
120120
}))

0 commit comments

Comments
 (0)