Spaces:
Running
Running
File size: 4,496 Bytes
599f646 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
/**
*
* Copyright 2023-2024 InspectorRAGet Team
*
* 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 endOfDay from 'date-fns/endOfDay';
import startOfYear from 'date-fns/startOfYear';
import sub from 'date-fns/sub';
import add from 'date-fns/add';
export const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
export function calculateDuration(
endTimestamp?: number,
startTimestamp?: number,
) {
if (!endTimestamp || !startTimestamp) {
return [undefined, undefined, undefined, undefined];
}
let duration = endTimestamp - startTimestamp;
var durationInDays = Math.floor(duration / 1000 / 60 / 60 / 24);
duration -= durationInDays * 1000 * 60 * 60 * 24;
var durationInHours = Math.floor(duration / 1000 / 60 / 60);
duration -= durationInHours * 1000 * 60 * 60;
var durationInMinutes = Math.floor(duration / 1000 / 60);
duration -= durationInMinutes * 1000 * 60;
var durationInSeconds = Math.floor(duration / 1000);
return [
durationInDays,
durationInHours,
durationInMinutes,
durationInSeconds,
];
}
/**
*
* @param duration duration in seconds
*/
export function castDurationToString(duration: number) {
var durationInDays = Math.floor(duration / 60 / 60 / 24);
duration -= durationInDays * 60 * 60 * 24;
var durationInHours = Math.floor(duration / 60 / 60);
duration -= durationInHours * 60 * 60;
var durationInMinutes = Math.floor(duration / 60);
duration -= durationInMinutes * 60;
var durationInSeconds = Math.floor(duration);
return [
durationInDays,
durationInHours,
durationInMinutes,
durationInSeconds,
];
}
export const windows = {
'Past 1 week': '1W',
'Past 1 month': '1M',
'Past 3 months': '3M',
'Year to date': 'YTD',
Custom: 'CUSTOM',
};
export const getDates = (window: string) => {
// Step 1: Identify dates for x-axis
const today = endOfDay(new Date());
const dates: Date[] = [];
// Step 1.a: If time window is 1-week long
if (window === windows['Past 1 week']) {
for (let days = 7; days > 0; days--) {
dates.push(sub(today, { days: days }));
}
// Step 1.a.ii: Add today's date as final entry
dates.push(today);
}
// Step 1.b: If time window is 1-month long
if (window === windows['Past 1 month']) {
// Step 1.b.i: Add 1 month before day as a 1'st date
dates.push(endOfDay(sub(today, { months: 1 })));
// Step 1.b.ii: Add dates every 3 days apart
let nextDate = add(dates[dates.length - 1], { days: 3 });
while (
Math.floor(nextDate.getTime() / 1000) < Math.floor(today.getTime() / 1000)
) {
dates.push(nextDate);
nextDate = add(nextDate, { days: 3 });
}
// Step 1.b.iii: Add today's date as final entry
dates.push(today);
}
// Step 1.c: If time window is 3-months long,
if (window === windows['Past 3 months']) {
// Step 1.c.i: Add 3 months before day as a 1'st date
dates.push(endOfDay(sub(today, { months: 3 })));
// Step 1.c.ii: Add dates every 7 days apart
let nextDate = add(dates[dates.length - 1], { days: 7 });
while (
Math.floor(nextDate.getTime() / 1000) < Math.floor(today.getTime() / 1000)
) {
dates.push(nextDate);
nextDate = add(nextDate, { days: 7 });
}
// Step 1.c.iii: Add today's date as final entry
dates.push(today);
}
// Step 1.d: If time window is year long,
if (window === windows['Year to date']) {
// Step 1.d.i: Add 1'st day of the year as a 1'st date
dates.push(endOfDay(startOfYear(today)));
// Step 1.d.ii: Add dates every 2 weeks apart
let nextDate = add(dates[dates.length - 1], { weeks: 2 });
while (
Math.floor(nextDate.getTime() / 1000) < Math.floor(today.getTime() / 1000)
) {
dates.push(nextDate);
nextDate = add(nextDate, { weeks: 2 });
}
// Step 1.d.iii: Add today's date as final entry
dates.push(today);
}
if (window === windows['Custom']) {
}
return dates;
};
|