-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolver Light Theme And Kibana Integration (#67859)
- Loading branch information
1 parent
dc99b4f
commit 04b8a49
Showing
15 changed files
with
1,178 additions
and
598 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
x-pack/plugins/security_solution/public/resolver/lib/date.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import { getFriendlyElapsedTime } from './date'; | ||
|
||
describe('date', () => { | ||
describe('getFriendlyElapsedTime', () => { | ||
const second = 1000; | ||
const minute = second * 60; | ||
const hour = minute * 60; | ||
const day = hour * 24; | ||
const week = day * 7; | ||
const month = day * 30; | ||
const year = day * 365; | ||
|
||
const initialTime = new Date('6/1/2020').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(); | ||
const oneDay = new Date(initialTime + 1 * day).getTime(); | ||
const oneWeek = new Date(initialTime + 1 * week).getTime(); | ||
const oneMonth = new Date(initialTime + 1 * month).getTime(); | ||
const oneYear = new Date(initialTime + 1 * year).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(); | ||
const almostAWeek = new Date(initialTime + 6.9 * day).getTime(); | ||
const almostAMonth = new Date(initialTime + 3.9 * week).getTime(); | ||
const almostAYear = new Date(initialTime + 11.9 * month).getTime(); | ||
const threeYears = new Date(initialTime + 3 * year).getTime(); | ||
|
||
it('should return the correct singular relative time', () => { | ||
expect(getFriendlyElapsedTime(initialTime, oneSecond)).toEqual({ | ||
duration: 1, | ||
durationType: 'second', | ||
}); | ||
expect(getFriendlyElapsedTime(initialTime, oneMinute)).toEqual({ | ||
duration: 1, | ||
durationType: 'minute', | ||
}); | ||
expect(getFriendlyElapsedTime(initialTime, oneHour)).toEqual({ | ||
duration: 1, | ||
durationType: 'hour', | ||
}); | ||
expect(getFriendlyElapsedTime(initialTime, oneDay)).toEqual({ | ||
duration: 1, | ||
durationType: 'day', | ||
}); | ||
expect(getFriendlyElapsedTime(initialTime, oneWeek)).toEqual({ | ||
duration: 1, | ||
durationType: 'week', | ||
}); | ||
expect(getFriendlyElapsedTime(initialTime, oneMonth)).toEqual({ | ||
duration: 1, | ||
durationType: 'month', | ||
}); | ||
expect(getFriendlyElapsedTime(initialTime, oneYear)).toEqual({ | ||
duration: 1, | ||
durationType: 'year', | ||
}); | ||
}); | ||
|
||
it('should return the correct pluralized relative time', () => { | ||
expect(getFriendlyElapsedTime(initialTime, almostAMinute)).toEqual({ | ||
duration: 59, | ||
durationType: 'seconds', | ||
}); | ||
expect(getFriendlyElapsedTime(initialTime, almostAnHour)).toEqual({ | ||
duration: 59, | ||
durationType: 'minutes', | ||
}); | ||
expect(getFriendlyElapsedTime(initialTime, almostADay)).toEqual({ | ||
duration: 23, | ||
durationType: 'hours', | ||
}); | ||
expect(getFriendlyElapsedTime(initialTime, almostAWeek)).toEqual({ | ||
duration: 6, | ||
durationType: 'days', | ||
}); | ||
expect(getFriendlyElapsedTime(initialTime, almostAMonth)).toEqual({ | ||
duration: 3, | ||
durationType: 'weeks', | ||
}); | ||
expect(getFriendlyElapsedTime(initialTime, almostAYear)).toEqual({ | ||
duration: 11, | ||
durationType: 'months', | ||
}); | ||
expect(getFriendlyElapsedTime(initialTime, threeYears)).toEqual({ | ||
duration: 3, | ||
durationType: 'years', | ||
}); | ||
}); | ||
}); | ||
}); |
82 changes: 82 additions & 0 deletions
82
x-pack/plugins/security_solution/public/resolver/lib/date.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { DurationDetails, DurationTypes } from '../types'; | ||
|
||
/* | ||
* Given two unix timestamps, it will return an object containing the time difference and properly pluralized friendly version of the time difference. | ||
* i.e. a time difference of 1000ms will yield => { duration: 1, durationType: 'second' } and 10000ms will yield => { duration: 10, durationType: 'seconds' } | ||
* | ||
*/ | ||
export const getFriendlyElapsedTime = ( | ||
from: number | string, | ||
to: number | string | ||
): DurationDetails | null => { | ||
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; | ||
} | ||
|
||
const second = 1000; | ||
const minute = second * 60; | ||
const hour = minute * 60; | ||
const day = hour * 24; | ||
const week = day * 7; | ||
const month = day * 30; | ||
const year = day * 365; | ||
|
||
let duration: number; | ||
let singularType: DurationTypes; | ||
let pluralType: DurationTypes; | ||
switch (true) { | ||
case elapsedTimeInMs >= year: | ||
duration = elapsedTimeInMs / year; | ||
singularType = 'year'; | ||
pluralType = 'years'; | ||
break; | ||
case elapsedTimeInMs >= month: | ||
duration = elapsedTimeInMs / month; | ||
singularType = 'month'; | ||
pluralType = 'months'; | ||
break; | ||
case elapsedTimeInMs >= week: | ||
duration = elapsedTimeInMs / week; | ||
singularType = 'week'; | ||
pluralType = 'weeks'; | ||
break; | ||
case elapsedTimeInMs >= day: | ||
duration = elapsedTimeInMs / day; | ||
singularType = 'day'; | ||
pluralType = 'days'; | ||
break; | ||
case elapsedTimeInMs >= hour: | ||
duration = elapsedTimeInMs / hour; | ||
singularType = 'hour'; | ||
pluralType = 'hours'; | ||
break; | ||
case elapsedTimeInMs >= minute: | ||
duration = elapsedTimeInMs / minute; | ||
singularType = 'minute'; | ||
pluralType = 'minutes'; | ||
break; | ||
case elapsedTimeInMs >= second: | ||
duration = elapsedTimeInMs / second; | ||
singularType = 'second'; | ||
pluralType = 'seconds'; | ||
break; | ||
default: | ||
duration = elapsedTimeInMs; | ||
singularType = 'millisecond'; | ||
pluralType = 'milliseconds'; | ||
break; | ||
} | ||
|
||
const durationType = duration > 1 ? pluralType : singularType; | ||
return { duration: Math.floor(duration), durationType }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.