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

Fix: Refresh schedule durations not pluralized sufficiently #3268

Merged
merged 2 commits into from
Jan 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions client/app/components/queries/ScheduleDialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Select from 'antd/lib/select';
import Radio from 'antd/lib/radio';
import { capitalize, clone, isEqual } from 'lodash';
import moment from 'moment';
import { secondsToInterval, IntervalEnum, localizeTime } from '@/filters';
import { secondsToInterval, durationHumanize, pluralize, IntervalEnum, localizeTime } from '@/filters';

import './ScheduleDialog.css';

Expand Down Expand Up @@ -182,9 +182,9 @@ class ScheduleDialog extends React.Component {
<Select value={seconds} onChange={this.setInterval} {...selectProps}>
<Option value={null} key="never">Never</Option>
{Object.keys(this.intervals).map(int => (
<OptGroup label={capitalize(int)} key={int}>
<OptGroup label={capitalize(pluralize(int))} key={int}>
{this.intervals[int].map(([cnt, secs]) => (
<Option value={secs} key={cnt}>{cnt} {int}</Option>
<Option value={secs} key={cnt}>{durationHumanize(secs)}</Option>
))}
</OptGroup>
))}
Expand Down
10 changes: 6 additions & 4 deletions client/app/components/queries/SchedulePhrase.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { react2angular } from 'react2angular';
import React from 'react';
import PropTypes from 'prop-types';
import Tooltip from 'antd/lib/tooltip';
import { localizeTime, secondsToInterval } from '@/filters';
import { localizeTime, durationHumanize } from '@/filters';

import './ScheduleDialog.css';

Expand All @@ -23,9 +23,11 @@ class SchedulePhrase extends React.Component {
if (!seconds) {
return ['Never'];
}
const { count, interval } = secondsToInterval(seconds);
const short = `Every ${count} ${interval}`;
let full = `Refreshes every ${count} ${interval}`;
const humanized = durationHumanize(seconds, {
omitSingleValueNumber: true,
});
const short = `Every ${humanized}`;
let full = `Refreshes every ${humanized}`;

const { time, day_of_week: dayOfWeek } = this.props.schedule;
if (time) {
Expand Down
59 changes: 29 additions & 30 deletions client/app/filters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { capitalize as _capitalize, isEmpty } from 'lodash';

export const IntervalEnum = {
NEVER: 'Never',
MINUTES: 'minute(s)',
HOURS: 'hour(s)',
DAYS: 'day(s)',
WEEKS: 'week(s)',
SECONDS: 'second',
MINUTES: 'minute',
HOURS: 'hour',
DAYS: 'day',
WEEKS: 'week',
};

export function localizeTime(time) {
Expand All @@ -19,12 +20,16 @@ export function localizeTime(time) {
.format('HH:mm');
}

export function secondsToInterval(seconds) {
if (!seconds) {
export function secondsToInterval(count) {
if (!count) {
return { interval: IntervalEnum.NEVER };
}
let interval = IntervalEnum.MINUTES;
let count = seconds / 60;

let interval = IntervalEnum.SECONDS;
if (count >= 60) {
count /= 60;
interval = IntervalEnum.MINUTES;
}
if (count >= 60) {
count /= 60;
interval = IntervalEnum.HOURS;
Expand Down Expand Up @@ -61,29 +66,23 @@ export function intervalToSeconds(count, interval) {
return intervalInSeconds * count;
}

export function durationHumanize(duration) {
let humanized = '';

if (duration === undefined || duration === null) {
humanized = '-';
} else if (duration < 60) {
const seconds = Math.round(duration);
humanized = `${seconds} seconds`;
} else if (duration > 3600 * 24) {
const days = Math.round(parseFloat(duration) / 60.0 / 60.0 / 24.0);
humanized = `${days} days`;
} else if (duration === 3600) {
humanized = '1 hour';
} else if (duration >= 3600) {
const hours = Math.round(parseFloat(duration) / 60.0 / 60.0);
humanized = `${hours} hours`;
} else if (duration === 60) {
humanized = '1 minute';
} else {
const minutes = Math.round(parseFloat(duration) / 60.0);
humanized = `${minutes} minutes`;
export function pluralize(text, count) {
const should = count !== 1;
return text + (should ? 's' : '');
}

export function durationHumanize(duration, options = {}) {
if (!duration) {
return '-';
}
let ret = '';
const { interval, count } = secondsToInterval(duration);
const rounded = Math.round(count);
if (rounded !== 1 || !options.omitSingleValueNumber) {
ret = `${rounded} `;
}
return humanized;
ret += pluralize(interval, rounded);
return ret;
}

export function toHuman(text) {
Expand Down