Skip to content

Commit

Permalink
Host Ansible jobs - Readable cron line (#510)
Browse files Browse the repository at this point in the history
* Fixes #34460 - Host Ansible jobs - human readable cron line

Convert the ansible cronline into a human readable format
  • Loading branch information
stejskalleos authored Feb 16, 2022
1 parent 9a2ca77 commit a2fb46f
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,23 @@ export const useCancelMutation = (resourceName, resourceId) =>
},
],
});

export const readableCron = (cron = '') => {
if (cron.match(/(\d+ \* \* \* \*)/)) {
return 'hourly';
}

if (cron.match(/(\d+ \d+ \* \* \*)/)) {
return 'daily';
}

if (cron.match(/(\d+ \d+ \* \* \d+)/)) {
return 'weekly';
}

if (cron.match(/(\d+ \d+ \d+ \* \*)/)) {
return 'monthly';
}

return 'custom';
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Pagination from 'foremanReact/components/Pagination';

import { decodeId } from '../../../../globalIdHelper';
import withLoading from '../../../withLoading';
import { readableCron } from './JobsTabHelper';

const PreviousJobsTable = ({ history, totalCount, jobs, pagination }) => {
const columns = [
Expand Down Expand Up @@ -61,7 +62,7 @@ const PreviousJobsTable = ({ history, totalCount, jobs, pagination }) => {
<Td>
<RelativeDateTime date={job.startAt} />
</Td>
<Td>{job.recurringLogic.cronLine}</Td>
<Td>{readableCron(job.recurringLogic.cronLine)}</Td>
</Tr>
))}
</Tbody>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
Td,
} from '@patternfly/react-table';

import { useCancelMutation } from './JobsTabHelper';
import { useCancelMutation, readableCron } from './JobsTabHelper';
import withLoading from '../../../withLoading';
import { decodeId } from '../../../../globalIdHelper';

Expand Down Expand Up @@ -74,7 +74,7 @@ const RecurringJobsTable = ({ jobs, resourceName, resourceId }) => {
{job.description}
</a>
</Td>
<Td>{job.recurringLogic.cronLine}</Td>
<Td>{readableCron(job.recurringLogic.cronLine)}</Td>
<Td>
<RelativeDateTime date={job.startAt} />
</Td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import * as toasts from '../../../../../toastHelper';

import { toCron } from '../NewRecurringJobHelper';
import { readableCron } from '../JobsTabHelper';

import {
tick,
Expand Down Expand Up @@ -49,8 +50,10 @@ describe('JobsTab', () => {
.map(element => expect(element).toBeInTheDocument());
expect(screen.getByText('Scheduled recurring jobs')).toBeInTheDocument();
expect(screen.getByText('Previously executed jobs')).toBeInTheDocument();
expect(screen.getByText(toCron(futureDate, 'weekly'))).toBeInTheDocument();
expect(screen.getByText('54 10 15 * *')).toBeInTheDocument();
expect(
screen.getByText(readableCron(toCron(futureDate, 'weekly')))
).toBeInTheDocument();
expect(screen.getByText('monthly')).toBeInTheDocument();
});
it('should show empty state', async () => {
render(
Expand Down Expand Up @@ -129,7 +132,9 @@ describe('JobsTab', () => {
message: 'Ansible job was successfully created.',
});
await waitFor(tick);
expect(screen.getByText(toCron(futureDate, 'weekly'))).toBeInTheDocument();
expect(
screen.getByText(readableCron(toCron(futureDate, 'weekly')))
).toBeInTheDocument();
expect(screen.getByText('in 3 days')).toBeInTheDocument();
expect(
screen.queryByText('No config job for Ansible roles scheduled')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { readableCron } from '../JobsTabHelper';

describe('JobTabsHelper', () => {
it('readableCron', () => {
expect(readableCron('01 * * * *')).toBe('hourly');
expect(readableCron('01 01 * * *')).toBe('daily');
expect(readableCron('01 01 * * 01')).toBe('weekly');
expect(readableCron('01 01 01 * *')).toBe('monthly');
expect(readableCron()).toBe('custom');
});
});
7 changes: 5 additions & 2 deletions webpack/routes/HostgroupJobs/__test__/HostgroupJobs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
} from '../../../testHelper';

import { toCron } from '../../../components/AnsibleHostDetail/components/JobsTab/NewRecurringJobHelper';
import { readableCron } from '../../../components/AnsibleHostDetail/components/JobsTab/JobsTabHelper';

const TestComponent = withRedux(withRouter(withMockedProvider(HostgroupJobs)));

Expand All @@ -46,7 +47,7 @@ describe('HostgroupJobs', () => {
.map(element => expect(element).toBeInTheDocument());
expect(screen.getByText('Scheduled recurring jobs')).toBeInTheDocument();
expect(screen.getByText('Previously executed jobs')).toBeInTheDocument();
expect(screen.getByText('54 10 15 * *')).toBeInTheDocument();
expect(screen.getByText(readableCron('54 10 15 * *'))).toBeInTheDocument();
});
it('should show empty state', async () => {
render(
Expand Down Expand Up @@ -103,7 +104,9 @@ describe('HostgroupJobs', () => {
type: 'success',
message: 'Ansible job was successfully created.',
});
expect(screen.getByText(toCron(futureDate, 'weekly'))).toBeInTheDocument();
expect(
screen.getByText(readableCron(toCron(futureDate, 'weekly')))
).toBeInTheDocument();
expect(screen.getByText('in 3 days')).toBeInTheDocument();
expect(
screen.queryByText('No config job for Ansible roles scheduled')
Expand Down

0 comments on commit a2fb46f

Please sign in to comment.