Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schedule exceptions #12778

Merged
merged 9 commits into from
Sep 15, 2022
4 changes: 4 additions & 0 deletions awx/ui/src/components/DetailList/DetailList.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ export default styled(DetailList)`
--column-count: 3;
}
`}
& + & {
margin-top: 20px;
}
`;
36 changes: 30 additions & 6 deletions awx/ui/src/components/Schedule/ScheduleDetail/FrequencyDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ const Label = styled.div`
font-weight: var(--pf-global--FontWeight--bold);
`;

export default function FrequencyDetails({ type, label, options, timezone }) {
export default function FrequencyDetails({
type,
label,
options,
timezone,
isException,
}) {
const getRunEveryLabel = () => {
const { interval } = options;
switch (type) {
Expand Down Expand Up @@ -77,22 +83,33 @@ export default function FrequencyDetails({ type, label, options, timezone }) {
6: t`Sunday`,
};

const prefix = isException ? `exception-${type}` : `frequency-${type}`;

return (
<div>
<Label>{label}</Label>
<DetailList gutter="sm">
<Detail label={t`Run every`} value={getRunEveryLabel()} />
<Detail
label={isException ? t`Skip every` : t`Run every`}
value={getRunEveryLabel()}
dataCy={`${prefix}-run-every`}
/>
{type === 'week' ? (
<Detail
label={t`On days`}
value={options.daysOfWeek
.sort(sortWeekday)
.map((d) => weekdays[d.weekday])
.join(', ')}
dataCy={`${prefix}-days-of-week`}
/>
) : null}
<RunOnDetail type={type} options={options} />
<Detail label={t`End`} value={getEndValue(type, options, timezone)} />
<RunOnDetail type={type} options={options} prefix={prefix} />
<Detail
label={t`End`}
value={getEndValue(type, options, timezone)}
dataCy={`${prefix}-end`}
/>
</DetailList>
</div>
);
Expand All @@ -104,11 +121,15 @@ function sortWeekday(a, b) {
return a.weekday - b.weekday;
}

function RunOnDetail({ type, options }) {
function RunOnDetail({ type, options, prefix }) {
if (type === 'month') {
if (options.runOn === 'day') {
return (
<Detail label={t`Run on`} value={t`Day ${options.runOnDayNumber}`} />
<Detail
label={t`Run on`}
value={t`Day ${options.runOnDayNumber}`}
dataCy={`${prefix}-run-on-day`}
/>
);
}
const dayOfWeek = options.runOnTheDay;
Expand All @@ -129,6 +150,7 @@ function RunOnDetail({ type, options }) {
/>
)
}
dataCy={`${prefix}-run-on-day`}
/>
);
}
Expand All @@ -152,6 +174,7 @@ function RunOnDetail({ type, options }) {
<Detail
label={t`Run on`}
value={`${months[options.runOnTheMonth]} ${options.runOnDayMonth}`}
dataCy={`${prefix}-run-on-day`}
/>
);
}
Expand Down Expand Up @@ -186,6 +209,7 @@ function RunOnDetail({ type, options }) {
/>
)
}
dataCy={`${prefix}-run-on-day`}
/>
);
}
Expand Down
111 changes: 89 additions & 22 deletions awx/ui/src/components/Schedule/ScheduleDetail/ScheduleDetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'styled-components/macro';
import React, { useCallback, useEffect } from 'react';
import { Link, useHistory, useLocation } from 'react-router-dom';
import styled from 'styled-components';

import { t } from '@lingui/macro';
import { Chip, Divider, Title, Button } from '@patternfly/react-core';
import { Schedule } from 'types';
Expand Down Expand Up @@ -60,6 +59,10 @@ const FrequencyDetailsContainer = styled.div`
padding-bottom: var(--pf-global--spacer--md);
border-bottom: 1px solid var(--pf-global--palette--black-300);
}

& + & {
margin-top: calc(var(--pf-global--spacer--lg) * -1);
}
`;

function ScheduleDetail({ hasDaysToKeepField, schedule, surveyConfig }) {
Expand Down Expand Up @@ -161,10 +164,14 @@ function ScheduleDetail({ hasDaysToKeepField, schedule, surveyConfig }) {
month: t`Month`,
year: t`Year`,
};
const { frequency, frequencyOptions } = parseRuleObj(schedule);
const { frequency, frequencyOptions, exceptionFrequency, exceptionOptions } =
parseRuleObj(schedule);
const repeatFrequency = frequency.length
? frequency.map((f) => frequencies[f]).join(', ')
: t`None (Run Once)`;
const exceptionRepeatFrequency = exceptionFrequency.length
? exceptionFrequency.map((f) => frequencies[f]).join(', ')
: t`None (Run Once)`;

const {
ask_credential_on_launch,
Expand Down Expand Up @@ -271,43 +278,84 @@ function ScheduleDetail({ hasDaysToKeepField, schedule, surveyConfig }) {
isDisabled={isDisabled}
/>
<DetailList gutter="sm">
<Detail label={t`Name`} value={name} />
<Detail label={t`Description`} value={description} />
<Detail label={t`Name`} value={name} dataCy="schedule-name" />
<Detail
label={t`Description`}
value={description}
dataCy="schedule-description"
/>
<Detail
label={t`First Run`}
value={formatDateString(dtstart, timezone)}
dataCy="schedule-first-run"
/>
<Detail
label={t`Next Run`}
value={formatDateString(next_run, timezone)}
dataCy="schedule-next-run"
/>
<Detail label={t`Last Run`} value={formatDateString(dtend, timezone)} />
<Detail
label={t`Local Time Zone`}
value={timezone}
helpText={helpText.localTimeZone(config)}
dataCy="schedule-timezone"
/>
<Detail
label={t`Repeat Frequency`}
value={repeatFrequency}
dataCy="schedule-repeat-frequency"
/>
<Detail
label={t`Exception Frequency`}
value={exceptionRepeatFrequency}
dataCy="schedule-exception-frequency"
/>
<Detail label={t`Repeat Frequency`} value={repeatFrequency} />
</DetailList>
{frequency.length ? (
<FrequencyDetailsContainer>
<p>
<strong>{t`Frequency Details`}</strong>
</p>
{frequency.map((freq) => (
<FrequencyDetails
key={freq}
type={freq}
label={frequencies[freq]}
options={frequencyOptions[freq]}
timezone={timezone}
/>
))}
<div ouia-component-id="schedule-frequency-details">
<p>
<strong>{t`Frequency Details`}</strong>
</p>
{frequency.map((freq) => (
<FrequencyDetails
key={freq}
type={freq}
label={frequencies[freq]}
options={frequencyOptions[freq]}
timezone={timezone}
/>
))}
</div>
</FrequencyDetailsContainer>
) : null}
{exceptionFrequency.length ? (
<FrequencyDetailsContainer>
<div ouia-component-id="schedule-exception-details">
<p css="border-top: 0">
<strong>{t`Frequency Exception Details`}</strong>
</p>
{exceptionFrequency.map((freq) => (
<FrequencyDetails
key={freq}
type={freq}
label={frequencies[freq]}
options={exceptionOptions[freq]}
timezone={timezone}
isException
/>
))}
</div>
</FrequencyDetailsContainer>
) : null}
<DetailList gutter="sm">
{hasDaysToKeepField ? (
<Detail label={t`Days of Data to Keep`} value={daysToKeep} />
<Detail
label={t`Days of Data to Keep`}
value={daysToKeep}
dataCy="schedule-days-to-keep"
/>
) : null}
<ScheduleOccurrences preview={preview} tz={timezone} />
<UserDateDetail
Expand All @@ -327,7 +375,11 @@ function ScheduleDetail({ hasDaysToKeepField, schedule, surveyConfig }) {
<PromptDivider />
<PromptDetailList>
{ask_job_type_on_launch && (
<Detail label={t`Job Type`} value={job_type} />
<Detail
label={t`Job Type`}
value={job_type}
dataCy="shedule-job-type"
/>
)}
{showInventoryDetail && (
<Detail
Expand All @@ -347,19 +399,31 @@ function ScheduleDetail({ hasDaysToKeepField, schedule, surveyConfig }) {
' '
)
}
dataCy="schedule-inventory"
/>
)}
{ask_verbosity_on_launch && (
<Detail label={t`Verbosity`} value={VERBOSITY()[verbosity]} />
<Detail
label={t`Verbosity`}
value={VERBOSITY()[verbosity]}
dataCy="schedule-verbosity"
/>
)}
{ask_scm_branch_on_launch && (
<Detail label={t`Source Control Branch`} value={scm_branch} />
<Detail
label={t`Source Control Branch`}
value={scm_branch}
dataCy="schedule-scm-branch"
/>
)}
{ask_limit_on_launch && (
<Detail label={t`Limit`} value={limit} dataCy="schedule-limit" />
)}
{ask_limit_on_launch && <Detail label={t`Limit`} value={limit} />}
{showDiffModeDetail && (
<Detail
label={t`Show Changes`}
value={diff_mode ? t`On` : t`Off`}
dataCy="schedule-show-changes"
/>
)}
{showCredentialsDetail && (
Expand All @@ -382,6 +446,7 @@ function ScheduleDetail({ hasDaysToKeepField, schedule, surveyConfig }) {
))}
</ChipGroup>
}
dataCy="schedule-credentials"
/>
)}
{showTagsDetail && (
Expand All @@ -405,6 +470,7 @@ function ScheduleDetail({ hasDaysToKeepField, schedule, surveyConfig }) {
))}
</ChipGroup>
}
dataCy="schedule-job-tags"
/>
)}
{showSkipTagsDetail && (
Expand All @@ -428,6 +494,7 @@ function ScheduleDetail({ hasDaysToKeepField, schedule, surveyConfig }) {
))}
</ChipGroup>
}
dataCy="schedule-skip-tags"
/>
)}
{showVariablesDetail && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const Checkbox = styled(_Checkbox)`
}
`;

const FrequencyDetailSubform = ({ frequency, prefix }) => {
const FrequencyDetailSubform = ({ frequency, prefix, isException }) => {
const id = prefix.replace('.', '-');
const [runOnDayMonth] = useField({
name: `${prefix}.runOnDayMonth`,
Expand Down Expand Up @@ -220,7 +220,7 @@ const FrequencyDetailSubform = ({ frequency, prefix }) => {
validated={
!intervalMeta.touched || !intervalMeta.error ? 'default' : 'error'
}
label={t`Run every`}
label={isException ? t`Skip every` : t`Run every`}
>
<div css="display: flex">
<TextInput
Expand Down
26 changes: 26 additions & 0 deletions awx/ui/src/components/Schedule/shared/ScheduleForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import ScheduleFormFields from './ScheduleFormFields';
import UnsupportedScheduleForm from './UnsupportedScheduleForm';
import parseRuleObj, { UnsupportedRRuleError } from './parseRuleObj';
import buildRuleObj from './buildRuleObj';
import buildRuleSet from './buildRuleSet';

const NUM_DAYS_PER_FREQUENCY = {
week: 7,
Expand Down Expand Up @@ -411,6 +412,10 @@ function ScheduleForm({
}
});

if (values.exceptionFrequency.length > 0 && !scheduleHasInstances(values)) {
errors.exceptionFrequency = t`This schedule has no occurrences due to the selected exceptions.`;
}

return errors;
};

Expand Down Expand Up @@ -518,3 +523,24 @@ ScheduleForm.defaultProps = {
};

export default ScheduleForm;

function scheduleHasInstances(values) {
let rangeToCheck = 1;
values.frequency.forEach((freq) => {
if (NUM_DAYS_PER_FREQUENCY[freq] > rangeToCheck) {
rangeToCheck = NUM_DAYS_PER_FREQUENCY[freq];
}
});

const ruleSet = buildRuleSet(values, true);
const startDate = DateTime.fromISO(values.startDate);
const endDate = startDate.plus({ days: rangeToCheck });
const instances = ruleSet.between(
startDate.toJSDate(),
endDate.toJSDate(),
true,
(date, i) => i === 0
);

return instances.length > 0;
}
Loading