Skip to content

Commit

Permalink
Merge branch 'master' of github.com:elastic/kibana into remove/karma
Browse files Browse the repository at this point in the history
  • Loading branch information
spalger committed Jul 23, 2020
2 parents 91f0989 + 4c2dc8d commit 147c726
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
14 changes: 14 additions & 0 deletions x-pack/plugins/security_solution/public/resolver/lib/date.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('date', () => {

const initialTime = new Date('6/1/2020').getTime();

const oneMillisecond = new Date(initialTime + 1).getTime();
const oneSecond = new Date(initialTime + 1 * second).getTime();
const oneMinute = new Date(initialTime + 1 * minute).getTime();
const oneHour = new Date(initialTime + 1 * hour).getTime();
Expand All @@ -25,6 +26,7 @@ describe('date', () => {
const oneMonth = new Date(initialTime + 1 * month).getTime();
const oneYear = new Date(initialTime + 1 * year).getTime();

const almostASecond = new Date(initialTime + 999).getTime();
const almostAMinute = new Date(initialTime + 59.9 * second).getTime();
const almostAnHour = new Date(initialTime + 59.9 * minute).getTime();
const almostADay = new Date(initialTime + 23.9 * hour).getTime();
Expand All @@ -34,6 +36,14 @@ describe('date', () => {
const threeYears = new Date(initialTime + 3 * year).getTime();

it('should return the correct singular relative time', () => {
expect(getFriendlyElapsedTime(initialTime, initialTime)).toEqual({
duration: '<1',
durationType: 'millisecond',
});
expect(getFriendlyElapsedTime(initialTime, oneMillisecond)).toEqual({
duration: 1,
durationType: 'millisecond',
});
expect(getFriendlyElapsedTime(initialTime, oneSecond)).toEqual({
duration: 1,
durationType: 'second',
Expand Down Expand Up @@ -65,6 +75,10 @@ describe('date', () => {
});

it('should return the correct pluralized relative time', () => {
expect(getFriendlyElapsedTime(initialTime, almostASecond)).toEqual({
duration: 999,
durationType: 'milliseconds',
});
expect(getFriendlyElapsedTime(initialTime, almostAMinute)).toEqual({
duration: 59,
durationType: 'seconds',
Expand Down
26 changes: 15 additions & 11 deletions x-pack/plugins/security_solution/public/resolver/lib/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export const getFriendlyElapsedTime = (
const startTime = typeof from === 'number' ? from : parseInt(from, 10);
const endTime = typeof to === 'number' ? to : parseInt(to, 10);
const elapsedTimeInMs = endTime - startTime;

if (Number.isNaN(elapsedTimeInMs)) {
return null;
}
Expand All @@ -31,52 +30,57 @@ export const getFriendlyElapsedTime = (
const month = day * 30;
const year = day * 365;

let duration: number;
let duration: DurationDetails['duration'];
let singularType: DurationTypes;
let pluralType: DurationTypes;
switch (true) {
case elapsedTimeInMs >= year:
duration = elapsedTimeInMs / year;
duration = Math.floor(elapsedTimeInMs / year);
singularType = 'year';
pluralType = 'years';
break;
case elapsedTimeInMs >= month:
duration = elapsedTimeInMs / month;
duration = Math.floor(elapsedTimeInMs / month);
singularType = 'month';
pluralType = 'months';
break;
case elapsedTimeInMs >= week:
duration = elapsedTimeInMs / week;
duration = Math.floor(elapsedTimeInMs / week);
singularType = 'week';
pluralType = 'weeks';
break;
case elapsedTimeInMs >= day:
duration = elapsedTimeInMs / day;
duration = Math.floor(elapsedTimeInMs / day);
singularType = 'day';
pluralType = 'days';
break;
case elapsedTimeInMs >= hour:
duration = elapsedTimeInMs / hour;
duration = Math.floor(elapsedTimeInMs / hour);
singularType = 'hour';
pluralType = 'hours';
break;
case elapsedTimeInMs >= minute:
duration = elapsedTimeInMs / minute;
duration = Math.floor(elapsedTimeInMs / minute);
singularType = 'minute';
pluralType = 'minutes';
break;
case elapsedTimeInMs >= second:
duration = elapsedTimeInMs / second;
duration = Math.floor(elapsedTimeInMs / second);
singularType = 'second';
pluralType = 'seconds';
break;
case elapsedTimeInMs === 0:
duration = '<1';
singularType = 'millisecond';
pluralType = 'millisecond'; // Would never show
break;
default:
duration = elapsedTimeInMs;
singularType = 'millisecond';
pluralType = 'milliseconds';
break;
}

const durationType = duration > 1 ? pluralType : singularType;
return { duration: Math.floor(duration), durationType };
const durationType = duration === 1 ? singularType : pluralType;
return { duration, durationType };
};
2 changes: 1 addition & 1 deletion x-pack/plugins/security_solution/public/resolver/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ export type DurationTypes =
* duration value and description string
*/
export interface DurationDetails {
duration: number;
duration: number | '<1';
durationType: DurationTypes;
}
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const StyledElapsedTime = styled.div<StyledElapsedTime>`
left: ${(props) => `${props.leftPct}%`};
padding: 6px 8px;
border-radius: 999px; // generate pill shape
transform: translate(-50%, -50%) rotateX(35deg);
transform: translate(-50%, -50%);
user-select: none;
`;

Expand Down

0 comments on commit 147c726

Please sign in to comment.