Skip to content

Commit

Permalink
refactor: elapsed time utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Apr 17, 2021
1 parent e36384a commit 4fb024e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
6 changes: 2 additions & 4 deletions lib/datasource/jenkins-plugins/get.ts
@@ -1,6 +1,7 @@
import { logger } from '../../logger';
import { ExternalHostError } from '../../types/errors/external-host-error';
import { clone } from '../../util/clone';
import { getElapsedMinutes } from '../../util/date';
import { Http } from '../../util/http';
import type { GetReleasesConfig, Release, ReleaseResult } from '../types';
import { id } from './common';
Expand Down Expand Up @@ -43,10 +44,7 @@ interface JenkinsPluginsVersionsResponse {
}

function hasCacheExpired(cache: JenkinsCache<JenkinsCacheTypes>): boolean {
const minutesElapsed = Math.floor(
(new Date().getTime() - cache.lastSync.getTime()) / (60 * 1000)
);
return minutesElapsed >= cache.cacheTimeMin;
return getElapsedMinutes(cache.lastSync) >= cache.cacheTimeMin;
}

async function updateJenkinsCache(
Expand Down
6 changes: 2 additions & 4 deletions lib/datasource/rubygems/get-rubygems-org.ts
@@ -1,5 +1,6 @@
import { logger } from '../../logger';
import { ExternalHostError } from '../../types/errors/external-host-error';
import { getElapsedMinutes } from '../../util/date';
import { Http } from '../../util/http';
import type { ReleaseResult } from '../types';
import { id } from './common';
Expand Down Expand Up @@ -88,10 +89,7 @@ async function updateRubyGemsVersions(): Promise<void> {
}

function isDataStale(): boolean {
const minutesElapsed = Math.floor(
(new Date().getTime() - lastSync.getTime()) / (60 * 1000)
);
return minutesElapsed >= 5;
return getElapsedMinutes(lastSync) >= 5;
}

let updateRubyGemsVersionsPromise: Promise<void> | undefined;
Expand Down
12 changes: 12 additions & 0 deletions lib/util/date.ts
@@ -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);
}
8 changes: 2 additions & 6 deletions lib/workers/branch/index.ts
Expand Up @@ -18,6 +18,7 @@ import { getAdditionalFiles } from '../../manager/npm/post-update';
import { Pr, platform } from '../../platform';
import { BranchStatus, PrState } from '../../types';
import { ExternalHostError } from '../../types/errors/external-host-error';
import { getElapsedDays } from '../../util/date';
import { emojify } from '../../util/emoji';
import {
checkoutBranch,
Expand Down Expand Up @@ -213,14 +214,9 @@ export async function processBranch(
// both a stabilityDays setting and a releaseTimestamp
config.stabilityStatus = BranchStatus.green;
// Default to 'success' but set 'pending' if any update is pending
const oneDay = 24 * 60 * 60 * 1000;
for (const upgrade of config.upgrades) {
if (upgrade.stabilityDays && upgrade.releaseTimestamp) {
const daysElapsed = Math.floor(
(new Date().getTime() -
new Date(upgrade.releaseTimestamp).getTime()) /
oneDay
);
const daysElapsed = getElapsedDays(upgrade.releaseTimestamp);
if (
!dependencyDashboardCheck &&
daysElapsed < upgrade.stabilityDays
Expand Down

0 comments on commit 4fb024e

Please sign in to comment.