Extracted duration function and related tests

Signed-off-by: Andre Wanlin <awanlin@rapidrtc.com>
This commit is contained in:
Andre Wanlin
2021-11-03 09:28:34 -05:00
parent a9b7481cd3
commit 7468647b37
4 changed files with 96 additions and 63 deletions
@@ -242,23 +242,3 @@ describe('getBuildStateComponent', () => {
});
});
});
describe('getBuildDuration', () => {
describe('getBuildDuration with undefined startTime and valid finishTime', () => {
it('should return empty result', () => {
const finishTime = new Date('2021-10-15T11:00:00.0000000Z');
const result = getBuildDuration(undefined, finishTime);
expect(result).toEqual('');
});
});
describe('getBuildDuration with undefined startTime and undefined finishTime', () => {
it('should return empty result', () => {
const result = getBuildDuration(undefined, undefined);
expect(result).toEqual('');
});
});
});
@@ -20,7 +20,6 @@ import {
BuildStatus,
RepoBuild,
} from '@backstage/plugin-azure-devops-common';
import { DateTime, Interval } from 'luxon'
import {
Link,
ResponseErrorPanel,
@@ -35,8 +34,9 @@ import {
} from '@backstage/core-components';
import { AzurePipelinesIcon } from '../AzurePipelinesIcon';
import { DateTime } from 'luxon';
import React from 'react';
import humanizeDuration from 'humanize-duration';
import { getDurationFromDates } from '../../utils/getDurationFromDates';
export const getBuildResultComponent = (result: number | undefined) => {
switch (result) {
@@ -115,46 +115,6 @@ export const getBuildStateComponent = (
}
};
export const getBuildDuration = (
startTime?: Date,
finishTime?: Date,
): string => {
if (!startTime || (!startTime && !finishTime)) {
return '';
}
const start = DateTime.fromISO(startTime.toString());
const finish = finishTime
? DateTime.fromISO(finishTime.toString())
: DateTime.now();
const formatted = Interval.fromDateTimes(start, finish)
.toDuration()
.valueOf();
const shortEnglishHumanizer = humanizeDuration.humanizer({
language: 'shortEn',
languages: {
shortEn: {
y: () => 'y',
mo: () => 'mo',
w: () => 'w',
d: () => 'd',
h: () => 'h',
m: () => 'm',
s: () => 's',
ms: () => 'ms',
},
},
});
return shortEnglishHumanizer(formatted, {
largest: 2,
round: true,
spacer: '',
});
};
const columns: TableColumn[] = [
{
title: 'ID',
@@ -193,7 +153,7 @@ const columns: TableColumn[] = [
render: (row: Partial<RepoBuild>) => (
<Box display="flex" alignItems="center">
<Typography>
{getBuildDuration(row.startTime, row.finishTime)}
{getDurationFromDates(row.startTime, row.finishTime)}
</Typography>
</Box>
),
@@ -0,0 +1,35 @@
/*
* Copyright 2021 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { getDurationFromDates } from './getDurationFromDates';
describe('getDurationFromDates with undefined startTime and valid finishTime', () => {
it('should return empty result', () => {
const finishTime = new Date('2021-10-15T11:00:00.0000000Z');
const result = getDurationFromDates(undefined, finishTime);
expect(result).toEqual('');
});
});
describe('getDurationFromDates with undefined startTime and undefined finishTime', () => {
it('should return empty result', () => {
const result = getDurationFromDates(undefined, undefined);
expect(result).toEqual('');
});
});
@@ -0,0 +1,58 @@
/*
* Copyright 2021 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { DateTime, Interval } from 'luxon';
import humanizeDuration from 'humanize-duration';
export const getDurationFromDates = (
startTime?: Date,
finishTime?: Date,
): string => {
if (!startTime || (!startTime && !finishTime)) {
return '';
}
const start = DateTime.fromISO(startTime.toString());
const finish = finishTime
? DateTime.fromISO(finishTime.toString())
: DateTime.now();
const formatted = Interval.fromDateTimes(start, finish)
.toDuration()
.valueOf();
const shortEnglishHumanizer = humanizeDuration.humanizer({
language: 'shortEn',
languages: {
shortEn: {
y: () => 'y',
mo: () => 'mo',
w: () => 'w',
d: () => 'd',
h: () => 'h',
m: () => 'm',
s: () => 's',
ms: () => 'ms',
},
},
});
return shortEnglishHumanizer(formatted, {
largest: 2,
round: true,
spacer: '',
});
};