Skip to content

Commit 9d26304

Browse files
committed
Extract time functions
Create the `time.ts` module as a place to put fime functions. Move two time functions there and create tests for them. The `humanizeUnit` function now uses ECMAscript apis. This ensures that pluralization happens appropriately. Also, fix a small bug in the results view to enure `repository` is correctly pluralized.
1 parent f73bda4 commit 9d26304

File tree

5 files changed

+154
-48
lines changed

5 files changed

+154
-48
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Contains an assortment of helper functions for working with time, dates, and durations.
3+
*/
4+
5+
6+
const durationFormatter = new Intl.RelativeTimeFormat('en', {
7+
numeric: 'auto',
8+
});
9+
10+
// All these are approximate, specifically months and years
11+
const MINUTE_IN_MILLIS = 1000 * 60;
12+
const HOUR_IN_MILLIS = 60 * MINUTE_IN_MILLIS;
13+
const DAY_IN_MILLIS = 24 * HOUR_IN_MILLIS;
14+
const MONTH_IN_MILLIS = 30 * DAY_IN_MILLIS;
15+
const YEAR_IN_MILLIS = 365 * DAY_IN_MILLIS;
16+
17+
export function humanizeDuration(diffInMs?: number) {
18+
if (diffInMs === undefined) {
19+
return '';
20+
}
21+
22+
if (Math.abs(diffInMs) < HOUR_IN_MILLIS) {
23+
return durationFormatter.format(Math.floor(diffInMs / MINUTE_IN_MILLIS), 'minute');
24+
} else if (Math.abs(diffInMs) < DAY_IN_MILLIS) {
25+
return durationFormatter.format(Math.floor(diffInMs / HOUR_IN_MILLIS), 'hour');
26+
} else if (Math.abs(diffInMs) < MONTH_IN_MILLIS) {
27+
return durationFormatter.format(Math.floor(diffInMs / DAY_IN_MILLIS), 'day');
28+
} else if (Math.abs(diffInMs) < YEAR_IN_MILLIS) {
29+
return durationFormatter.format(Math.floor(diffInMs / MONTH_IN_MILLIS), 'month');
30+
} else {
31+
return durationFormatter.format(Math.floor(diffInMs / YEAR_IN_MILLIS), 'year');
32+
}
33+
}
34+
35+
function createFormatter(unit: string) {
36+
return Intl.NumberFormat('en-US', {
37+
style: 'unit',
38+
unit,
39+
unitDisplay: 'long'
40+
});
41+
}
42+
43+
export function humanizeUnit(diffInMs?: number): string {
44+
// assume a blank or empty string is a zero
45+
// assume anything less than 0 is a zero
46+
if (!diffInMs || diffInMs < MINUTE_IN_MILLIS) {
47+
return 'Less than a minute';
48+
}
49+
let unit: string;
50+
let unitDiff: number;
51+
if (diffInMs < HOUR_IN_MILLIS) {
52+
unit = 'minute';
53+
unitDiff = Math.floor(diffInMs / MINUTE_IN_MILLIS);
54+
} else if (diffInMs < DAY_IN_MILLIS) {
55+
unit = 'hour';
56+
unitDiff = Math.floor(diffInMs / HOUR_IN_MILLIS);
57+
} else if (diffInMs < MONTH_IN_MILLIS) {
58+
unit = 'day';
59+
unitDiff = Math.floor(diffInMs / DAY_IN_MILLIS);
60+
} else if (diffInMs < YEAR_IN_MILLIS) {
61+
unit = 'month';
62+
unitDiff = Math.floor(diffInMs / MONTH_IN_MILLIS);
63+
} else {
64+
unit = 'year';
65+
unitDiff = Math.floor(diffInMs / YEAR_IN_MILLIS);
66+
}
67+
68+
return createFormatter(unit).format(unitDiff);
69+
}

extensions/ql-vscode/src/remote-queries/remote-queries-interface.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { URLSearchParams } from 'url';
2727
import { SHOW_QUERY_TEXT_MSG } from '../query-history';
2828
import { AnalysesResultsManager } from './analyses-results-manager';
2929
import { AnalysisResults } from './shared/analysis-result';
30+
import { humanizeUnit } from '../pure/time';
3031

3132
export class RemoteQueriesInterfaceManager {
3233
private panel: WebviewPanel | undefined;
@@ -249,23 +250,7 @@ export class RemoteQueriesInterfaceManager {
249250

250251
private getDuration(startTime: number, endTime: number): string {
251252
const diffInMs = startTime - endTime;
252-
return this.formatDuration(diffInMs);
253-
}
254-
255-
private formatDuration(ms: number): string {
256-
const seconds = ms / 1000;
257-
const minutes = seconds / 60;
258-
const hours = minutes / 60;
259-
const days = hours / 24;
260-
if (days > 1) {
261-
return `${days.toFixed(2)} days`;
262-
} else if (hours > 1) {
263-
return `${hours.toFixed(2)} hours`;
264-
} else if (minutes > 1) {
265-
return `${minutes.toFixed(2)} minutes`;
266-
} else {
267-
return `${seconds.toFixed(2)} seconds`;
268-
}
253+
return humanizeUnit(diffInMs);
269254
}
270255

271256
private formatDate = (millis: number): string => {

extensions/ql-vscode/src/remote-queries/view/LastUpdated.tsx

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import * as React from 'react';
22
import { RepoPushIcon } from '@primer/octicons-react';
33
import styled from 'styled-components';
44

5+
import { humanizeDuration } from '../../pure/time';
6+
57
const IconContainer = styled.span`
68
flex-grow: 0;
79
text-align: right;
@@ -23,7 +25,7 @@ const LastUpdated = ({ lastUpdated }: Props) => (
2325
<RepoPushIcon size={16} />
2426
</IconContainer>
2527
<Duration>
26-
{humanizeDuration(lastUpdated)}
28+
{humanizeDuration(lastUpdated && -lastUpdated)}
2729
</Duration>
2830
</>
2931
) : (
@@ -32,31 +34,3 @@ const LastUpdated = ({ lastUpdated }: Props) => (
3234
);
3335

3436
export default LastUpdated;
35-
36-
const formatter = new Intl.RelativeTimeFormat('en', {
37-
numeric: 'auto'
38-
});
39-
40-
// All these are approximate, specifically months and years
41-
const MINUTE_IN_MILLIS = 1000 * 60;
42-
const HOUR_IN_MILLIS = 60 * MINUTE_IN_MILLIS;
43-
const DAY_IN_MILLIS = 24 * HOUR_IN_MILLIS;
44-
const MONTH_IN_MILLIS = 30 * DAY_IN_MILLIS;
45-
const YEAR_IN_MILLIS = 365 * DAY_IN_MILLIS;
46-
47-
function humanizeDuration(diff?: number) {
48-
if (!diff) {
49-
return '';
50-
}
51-
if (diff < HOUR_IN_MILLIS) {
52-
return formatter.format(- Math.floor(diff / MINUTE_IN_MILLIS), 'minute');
53-
} else if (diff < DAY_IN_MILLIS) {
54-
return formatter.format(- Math.floor(diff / HOUR_IN_MILLIS), 'hour');
55-
} else if (diff < MONTH_IN_MILLIS) {
56-
return formatter.format(- Math.floor(diff / DAY_IN_MILLIS), 'day');
57-
} else if (diff < YEAR_IN_MILLIS) {
58-
return formatter.format(- Math.floor(diff / MONTH_IN_MILLIS), 'month');
59-
} else {
60-
return formatter.format(- Math.floor(diff / YEAR_IN_MILLIS), 'year');
61-
}
62-
}

extensions/ql-vscode/src/remote-queries/view/RemoteQueries.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,19 @@ const openQueryTextVirtualFile = (queryResult: RemoteQueryResult) => {
7171
});
7272
};
7373

74+
function createResultsDescription(queryResult: RemoteQueryResult) {
75+
return `${queryResult.totalResultCount} results from running against ${queryResult.totalRepositoryCount
76+
} ${queryResult.totalRepositoryCount === 1 ? 'repository' : 'repositories'
77+
} (${queryResult.executionDuration}), ${queryResult.executionTimestamp}`;
78+
}
79+
7480
const sumAnalysesResults = (analysesResults: AnalysisResults[]) =>
7581
analysesResults.reduce((acc, curr) => acc + getAnalysisResultCount(curr), 0);
7682

7783
const QueryInfo = (queryResult: RemoteQueryResult) => (
7884
<>
7985
<VerticalSpace size={1} />
80-
{queryResult.totalResultCount} results from running against {queryResult.totalRepositoryCount} repositories
81-
({queryResult.executionDuration}), {queryResult.executionTimestamp}
86+
{createResultsDescription(queryResult)}
8287
<VerticalSpace size={1} />
8388
<span>
8489
<a className="vscode-codeql__query-info-link" href="#" onClick={() => openQueryFile(queryResult)}>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { expect } from 'chai';
2+
import 'mocha';
3+
4+
import { humanizeDuration, humanizeUnit } from '../../src/pure/time';
5+
6+
describe('Time', () => {
7+
it('should return a humanized unit', () => {
8+
expect(humanizeUnit(undefined)).to.eq('Less than a minute');
9+
expect(humanizeUnit(0)).to.eq('Less than a minute');
10+
expect(humanizeUnit(-1)).to.eq('Less than a minute');
11+
expect(humanizeUnit(1000 * 60 - 1)).to.eq('Less than a minute');
12+
expect(humanizeUnit(1000 * 60)).to.eq('1 minute');
13+
expect(humanizeUnit(1000 * 60 * 2 - 1)).to.eq('1 minute');
14+
expect(humanizeUnit(1000 * 60 * 2)).to.eq('2 minutes');
15+
expect(humanizeUnit(1000 * 60 * 60)).to.eq('1 hour');
16+
expect(humanizeUnit(1000 * 60 * 60 * 2)).to.eq('2 hours');
17+
expect(humanizeUnit(1000 * 60 * 60 * 24)).to.eq('1 day');
18+
expect(humanizeUnit(1000 * 60 * 60 * 24 * 2)).to.eq('2 days');
19+
20+
// assume every month has 30 days
21+
expect(humanizeUnit(1000 * 60 * 60 * 24 * 30)).to.eq('1 month');
22+
expect(humanizeUnit(1000 * 60 * 60 * 24 * 30 * 2)).to.eq('2 months');
23+
expect(humanizeUnit(1000 * 60 * 60 * 24 * 30 * 12)).to.eq('12 months');
24+
25+
// assume every year has 365 days
26+
expect(humanizeUnit(1000 * 60 * 60 * 24 * 365)).to.eq('1 year');
27+
expect(humanizeUnit(1000 * 60 * 60 * 24 * 365 * 2)).to.eq('2 years');
28+
});
29+
30+
it('should return a humanized duration positive', () => {
31+
expect(humanizeDuration(undefined)).to.eq('');
32+
expect(humanizeDuration(0)).to.eq('this minute');
33+
expect(humanizeDuration(1)).to.eq('this minute');
34+
expect(humanizeDuration(1000 * 60 - 1)).to.eq('this minute');
35+
expect(humanizeDuration(1000 * 60)).to.eq('in 1 minute');
36+
expect(humanizeDuration(1000 * 60 * 2 - 1)).to.eq('in 1 minute');
37+
expect(humanizeDuration(1000 * 60 * 2)).to.eq('in 2 minutes');
38+
expect(humanizeDuration(1000 * 60 * 60)).to.eq('in 1 hour');
39+
expect(humanizeDuration(1000 * 60 * 60 * 2)).to.eq('in 2 hours');
40+
expect(humanizeDuration(1000 * 60 * 60 * 24)).to.eq('tomorrow');
41+
expect(humanizeDuration(1000 * 60 * 60 * 24 * 2)).to.eq('in 2 days');
42+
43+
// assume every month has 30 days
44+
expect(humanizeDuration(1000 * 60 * 60 * 24 * 30)).to.eq('next month');
45+
expect(humanizeDuration(1000 * 60 * 60 * 24 * 30 * 2)).to.eq('in 2 months');
46+
expect(humanizeDuration(1000 * 60 * 60 * 24 * 30 * 12)).to.eq('in 12 months');
47+
48+
// assume every year has 365 days
49+
expect(humanizeDuration(1000 * 60 * 60 * 24 * 365)).to.eq('next year');
50+
expect(humanizeDuration(1000 * 60 * 60 * 24 * 365 * 2)).to.eq('in 2 years');
51+
});
52+
53+
it('should return a humanized duration negative', () => {
54+
expect(humanizeDuration(-1)).to.eq('1 minute ago');
55+
expect(humanizeDuration(-1000 * 60)).to.eq('1 minute ago');
56+
expect(humanizeDuration(-1000 * 60 - 1)).to.eq('2 minutes ago');
57+
expect(humanizeDuration(-1000 * 60 * 2)).to.eq('2 minutes ago');
58+
expect(humanizeDuration(-1000 * 60 * 2 - 1)).to.eq('3 minutes ago');
59+
expect(humanizeDuration(-1000 * 60 * 60)).to.eq('1 hour ago');
60+
expect(humanizeDuration(-1000 * 60 * 60 * 2)).to.eq('2 hours ago');
61+
expect(humanizeDuration(-1000 * 60 * 60 * 24)).to.eq('yesterday');
62+
expect(humanizeDuration(-1000 * 60 * 60 * 24 * 2)).to.eq('2 days ago');
63+
64+
// assume every month has 30 days
65+
expect(humanizeDuration(-1000 * 60 * 60 * 24 * 30)).to.eq('last month');
66+
expect(humanizeDuration(-1000 * 60 * 60 * 24 * 30 * 2)).to.eq('2 months ago');
67+
expect(humanizeDuration(-1000 * 60 * 60 * 24 * 30 * 12)).to.eq('12 months ago');
68+
69+
// assume every year has 365 days
70+
expect(humanizeDuration(-1000 * 60 * 60 * 24 * 365)).to.eq('last year');
71+
expect(humanizeDuration(-1000 * 60 * 60 * 24 * 365 * 2)).to.eq('2 years ago');
72+
});
73+
});

0 commit comments

Comments
 (0)