refactor: elapsed time utilities

This commit is contained in:
Rhys Arkins 2021-04-17 09:26:28 +02:00
parent e36384a80c
commit 4fb024e51b
4 changed files with 18 additions and 14 deletions

View file

@ -1,6 +1,7 @@
import { logger } from '../../logger'; import { logger } from '../../logger';
import { ExternalHostError } from '../../types/errors/external-host-error'; import { ExternalHostError } from '../../types/errors/external-host-error';
import { clone } from '../../util/clone'; import { clone } from '../../util/clone';
import { getElapsedMinutes } from '../../util/date';
import { Http } from '../../util/http'; import { Http } from '../../util/http';
import type { GetReleasesConfig, Release, ReleaseResult } from '../types'; import type { GetReleasesConfig, Release, ReleaseResult } from '../types';
import { id } from './common'; import { id } from './common';
@ -43,10 +44,7 @@ interface JenkinsPluginsVersionsResponse {
} }
function hasCacheExpired(cache: JenkinsCache<JenkinsCacheTypes>): boolean { function hasCacheExpired(cache: JenkinsCache<JenkinsCacheTypes>): boolean {
const minutesElapsed = Math.floor( return getElapsedMinutes(cache.lastSync) >= cache.cacheTimeMin;
(new Date().getTime() - cache.lastSync.getTime()) / (60 * 1000)
);
return minutesElapsed >= cache.cacheTimeMin;
} }
async function updateJenkinsCache( async function updateJenkinsCache(

View file

@ -1,5 +1,6 @@
import { logger } from '../../logger'; import { logger } from '../../logger';
import { ExternalHostError } from '../../types/errors/external-host-error'; import { ExternalHostError } from '../../types/errors/external-host-error';
import { getElapsedMinutes } from '../../util/date';
import { Http } from '../../util/http'; import { Http } from '../../util/http';
import type { ReleaseResult } from '../types'; import type { ReleaseResult } from '../types';
import { id } from './common'; import { id } from './common';
@ -88,10 +89,7 @@ async function updateRubyGemsVersions(): Promise<void> {
} }
function isDataStale(): boolean { function isDataStale(): boolean {
const minutesElapsed = Math.floor( return getElapsedMinutes(lastSync) >= 5;
(new Date().getTime() - lastSync.getTime()) / (60 * 1000)
);
return minutesElapsed >= 5;
} }
let updateRubyGemsVersionsPromise: Promise<void> | undefined; let updateRubyGemsVersionsPromise: Promise<void> | undefined;

12
lib/util/date.ts Normal file
View file

@ -0,0 +1,12 @@
const ONE_MINUTE_MS = 60 * 1000;
const ONE_DAY_MS = 24 * 60 * ONE_MINUTE_MS;
export function getElapsedDays(timestamp: string): number {
return Math.floor(
(new Date().getTime() - new Date(timestamp).getTime()) / ONE_DAY_MS
);
}
export function getElapsedMinutes(date: Date): number {
return Math.floor((new Date().getTime() - date.getTime()) / ONE_MINUTE_MS);
}

View file

@ -18,6 +18,7 @@ import { getAdditionalFiles } from '../../manager/npm/post-update';
import { Pr, platform } from '../../platform'; import { Pr, platform } from '../../platform';
import { BranchStatus, PrState } from '../../types'; import { BranchStatus, PrState } from '../../types';
import { ExternalHostError } from '../../types/errors/external-host-error'; import { ExternalHostError } from '../../types/errors/external-host-error';
import { getElapsedDays } from '../../util/date';
import { emojify } from '../../util/emoji'; import { emojify } from '../../util/emoji';
import { import {
checkoutBranch, checkoutBranch,
@ -213,14 +214,9 @@ export async function processBranch(
// both a stabilityDays setting and a releaseTimestamp // both a stabilityDays setting and a releaseTimestamp
config.stabilityStatus = BranchStatus.green; config.stabilityStatus = BranchStatus.green;
// Default to 'success' but set 'pending' if any update is pending // Default to 'success' but set 'pending' if any update is pending
const oneDay = 24 * 60 * 60 * 1000;
for (const upgrade of config.upgrades) { for (const upgrade of config.upgrades) {
if (upgrade.stabilityDays && upgrade.releaseTimestamp) { if (upgrade.stabilityDays && upgrade.releaseTimestamp) {
const daysElapsed = Math.floor( const daysElapsed = getElapsedDays(upgrade.releaseTimestamp);
(new Date().getTime() -
new Date(upgrade.releaseTimestamp).getTime()) /
oneDay
);
if ( if (
!dependencyDashboardCheck && !dependencyDashboardCheck &&
daysElapsed < upgrade.stabilityDays daysElapsed < upgrade.stabilityDays