From cfc239e3c9f5d7e1633e2ca71e0888c6c6400236 Mon Sep 17 00:00:00 2001 From: Rajat Dabade Date: Fri, 29 Dec 2023 12:54:02 +0530 Subject: [PATCH] refactor: added 3 days global timestamp (#4290) * refactor: added 3 days global timestamp * refactor: updated 3 days data in right container * refactor: common function for calculate start and end time --- .../NewWidget/RightContainer/timeItems.ts | 6 ++ .../TopNav/DateTimeSelection/config.ts | 6 +- frontend/src/lib/getMinMax.ts | 5 ++ frontend/src/lib/getStartAndEndTime/index.ts | 71 ++++++------------- 4 files changed, 39 insertions(+), 49 deletions(-) diff --git a/frontend/src/container/NewWidget/RightContainer/timeItems.ts b/frontend/src/container/NewWidget/RightContainer/timeItems.ts index 1a2ecfeb5a..dd6f8cd552 100644 --- a/frontend/src/container/NewWidget/RightContainer/timeItems.ts +++ b/frontend/src/container/NewWidget/RightContainer/timeItems.ts @@ -28,6 +28,10 @@ export const timeItems: timePreferance[] = [ name: 'Last 1 day', enum: 'LAST_1_DAY', }, + { + name: 'Last 3 days', + enum: 'LAST_3_DAYS', + }, { name: 'Last 1 week', enum: 'LAST_1_WEEK', @@ -47,6 +51,7 @@ export type timePreferenceType = | LAST_1_HR | LAST_6_HR | LAST_1_DAY + | LAST_3_DAYS | LAST_1_WEEK; type GLOBAL_TIME = 'GLOBAL_TIME'; @@ -56,6 +61,7 @@ type LAST_30_MIN = 'LAST_30_MIN'; type LAST_1_HR = 'LAST_1_HR'; type LAST_6_HR = 'LAST_6_HR'; type LAST_1_DAY = 'LAST_1_DAY'; +type LAST_3_DAYS = 'LAST_3_DAYS'; type LAST_1_WEEK = 'LAST_1_WEEK'; export default timeItems; diff --git a/frontend/src/container/TopNav/DateTimeSelection/config.ts b/frontend/src/container/TopNav/DateTimeSelection/config.ts index cada5a3194..b99f6f6ae2 100644 --- a/frontend/src/container/TopNav/DateTimeSelection/config.ts +++ b/frontend/src/container/TopNav/DateTimeSelection/config.ts @@ -9,6 +9,7 @@ type SixHour = '6hr'; type OneHour = '1hr'; type FourHour = '4hr'; type OneDay = '1day'; +type ThreeDay = '3days'; type OneWeek = '1week'; type Custom = 'custom'; @@ -23,7 +24,8 @@ export type Time = | OneHour | Custom | OneWeek - | OneDay; + | OneDay + | ThreeDay; export const Options: Option[] = [ { value: '5min', label: 'Last 5 min' }, @@ -32,6 +34,7 @@ export const Options: Option[] = [ { value: '1hr', label: 'Last 1 hour' }, { value: '6hr', label: 'Last 6 hour' }, { value: '1day', label: 'Last 1 day' }, + { value: '3days', label: 'Last 3 days' }, { value: '1week', label: 'Last 1 week' }, { value: 'custom', label: 'Custom' }, ]; @@ -48,6 +51,7 @@ export const RelativeDurationOptions: Option[] = [ { value: '1hr', label: 'Last 1 hour' }, { value: '6hr', label: 'Last 6 hour' }, { value: '1day', label: 'Last 1 day' }, + { value: '3days', label: 'Last 3 days' }, { value: '1week', label: 'Last 1 week' }, ]; diff --git a/frontend/src/lib/getMinMax.ts b/frontend/src/lib/getMinMax.ts index 320d509131..b10ac7a982 100644 --- a/frontend/src/lib/getMinMax.ts +++ b/frontend/src/lib/getMinMax.ts @@ -6,6 +6,7 @@ import getMinAgo from './getStartAndEndTime/getMinAgo'; const GetMinMax = ( interval: Time, dateTimeRange?: [number, number], + // eslint-disable-next-line sonarjs/cognitive-complexity ): GetMinMaxPayload => { let maxTime = new Date().getTime(); let minTime = 0; @@ -32,6 +33,10 @@ const GetMinMax = ( // one day = 24*60(min) const minTimeAgo = getMinAgo({ minutes: 24 * 60 }).getTime(); minTime = minTimeAgo; + } else if (interval === '3days') { + // three day = one day * 3 + const minTimeAgo = getMinAgo({ minutes: 24 * 60 * 3 }).getTime(); + minTime = minTimeAgo; } else if (interval === '1week') { // one week = one day * 7 const minTimeAgo = getMinAgo({ minutes: 24 * 60 * 7 }).getTime(); diff --git a/frontend/src/lib/getStartAndEndTime/index.ts b/frontend/src/lib/getStartAndEndTime/index.ts index cfebd73e2e..ee012858d7 100644 --- a/frontend/src/lib/getStartAndEndTime/index.ts +++ b/frontend/src/lib/getStartAndEndTime/index.ts @@ -3,6 +3,19 @@ import { timePreferenceType } from 'container/NewWidget/RightContainer/timeItems import getMicroSeconds from './getMicroSeconds'; import getMinAgo from './getMinAgo'; +const calculateStartAndEndTime = ( + minutes: number, + endString: string, +): Payload => { + const agodate = getMinAgo({ minutes }).getTime(); + const agoString = getMicroSeconds({ time: agodate }); + + return { + start: agoString, + end: endString, + }; +}; + const GetStartAndEndTime = ({ type, minTime, @@ -12,73 +25,35 @@ const GetStartAndEndTime = ({ const endString = getMicroSeconds({ time: end }); if (type === 'LAST_5_MIN') { - const agodate = getMinAgo({ minutes: 5 }).getTime(); - const agoString = getMicroSeconds({ time: agodate }); - - return { - start: agoString, - end: endString, - }; + return calculateStartAndEndTime(5, endString); } if (type === 'LAST_30_MIN') { - const agodate = getMinAgo({ minutes: 30 }).getTime(); - const agoString = getMicroSeconds({ time: agodate }); - - return { - start: agoString, - end: endString, - }; + return calculateStartAndEndTime(30, endString); } if (type === 'LAST_1_HR') { - const agodate = getMinAgo({ minutes: 60 }).getTime(); - const agoString = getMicroSeconds({ time: agodate }); - - return { - start: agoString, - end: endString, - }; + return calculateStartAndEndTime(60, endString); } if (type === 'LAST_15_MIN') { - const agodate = getMinAgo({ minutes: 15 }).getTime(); - const agoString = getMicroSeconds({ time: agodate }); - - return { - start: agoString, - end: endString, - }; + return calculateStartAndEndTime(15, endString); } if (type === 'LAST_6_HR') { - const agoDate = getMinAgo({ minutes: 6 * 60 }).getTime(); - const agoString = getMicroSeconds({ time: agoDate }); - - return { - start: agoString, - end: endString, - }; + return calculateStartAndEndTime(6 * 60, endString); } if (type === 'LAST_1_DAY') { - const agoDate = getMinAgo({ minutes: 24 * 60 }).getTime(); - const agoString = getMicroSeconds({ time: agoDate }); + return calculateStartAndEndTime(24 * 60, endString); + } - return { - start: agoString, - end: endString, - }; + if (type === 'LAST_3_DAYS') { + return calculateStartAndEndTime(24 * 60 * 3, endString); } if (type === 'LAST_1_WEEK') { - const agoDate = getMinAgo({ minutes: 24 * 60 * 7 }).getTime(); - const agoString = getMicroSeconds({ time: agoDate }); - - return { - start: agoString, - end: endString, - }; + return calculateStartAndEndTime(24 * 60 * 7, endString); } return {