Skip to content

Commit f373e64

Browse files
committed
Store LastUpdated as a duration, not a timestamp
The `lastUpdated` value is now the duration between timestamp of the last time the repo was updated and time the file was downloaded. This fixes the duration and it won't change over time.
1 parent e43b4e6 commit f373e64

4 files changed

Lines changed: 64 additions & 65 deletions

File tree

extensions/ql-vscode/src/remote-queries/gh-actions-api-client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ export async function getRepositoriesMetadata(credentials: Credentials, nwos: st
395395
const owner = node.owner.login;
396396
const name = node.name;
397397
const starCount = node.stargazerCount;
398-
const lastUpdated = new Date(node.updatedAt).getTime();
398+
const lastUpdated = Date.now() - new Date(node.updatedAt).getTime();
399399
metadata[`${owner}/${name}`] = {
400400
starCount, lastUpdated
401401
};

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

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import * as React from 'react';
2+
import { CalendarIcon } from '@primer/octicons-react';
3+
import styled from 'styled-components';
4+
5+
const Calendar = styled.span`
6+
flex-grow: 0;
7+
text-align: right;
8+
margin-right: 0;
9+
`;
10+
11+
const Duration = styled.span`
12+
text-align: left;
13+
width: 8em;
14+
margin-left: 0.5em;
15+
`;
16+
17+
type Props = { lastUpdated?: number };
18+
19+
const LastUpdated = ({ lastUpdated }: Props) => (
20+
Number.isFinite(lastUpdated) ? (
21+
<>
22+
<Calendar>
23+
<CalendarIcon size={16} />
24+
</Calendar>
25+
<Duration>
26+
{humanizeDuration(lastUpdated)}
27+
</Duration>
28+
</>
29+
) : (
30+
<></>
31+
)
32+
);
33+
34+
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import RepositoriesSearch from './RepositoriesSearch';
2323
import ActionButton from './ActionButton';
2424
import StarCount from './StarCount';
2525
import SortRepoFilter, { Sort, sorter } from './SortRepoFilter';
26-
import LastUpdated from './LasstUpdated';
26+
import LastUpdated from './LastUpdated';
2727

2828
const numOfReposInContractedMode = 10;
2929

0 commit comments

Comments
 (0)