add helper methods
This commit is contained in:
1 parent
42aae20fa3
commit
4b02bbec37
2 files changed
+37
-1
No files matched your search
@@ -86,7 +86,7 @@ export const getResponseColor = (
|
|||||||
return normalizeToHex(safe.end);
|
return normalizeToHex(safe.end);
|
||||||
};
|
};
|
||||||
|
|
||||||
const MIN_HEIGHT_PCT = 8;
|
export const MIN_HEIGHT_PCT = 8;
|
||||||
const CAP_PERCENTILE = 95;
|
const CAP_PERCENTILE = 95;
|
||||||
|
|
||||||
const percentile = (sortedValues: number[], p: number): number => {
|
const percentile = (sortedValues: number[], p: number): number => {
|
||||||
@@ -114,3 +114,24 @@ export const computeBarHeights = (
|
|||||||
return Math.max(MIN_HEIGHT_PCT, Math.min(100, (value / cap) * 100));
|
return Math.max(MIN_HEIGHT_PCT, Math.min(100, (value / cap) * 100));
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const computeDayBarHeights = (
|
||||||
|
buckets: { date: string; avgResponseTime: number | null }[]
|
||||||
|
): Map<string, number> => {
|
||||||
|
const values = buckets.map((bucket) =>
|
||||||
|
typeof bucket.avgResponseTime === "number" && Number.isFinite(bucket.avgResponseTime)
|
||||||
|
? Math.max(0, bucket.avgResponseTime)
|
||||||
|
: 0
|
||||||
|
);
|
||||||
|
|
||||||
|
const sorted = values.slice().sort((a, b) => a - b);
|
||||||
|
const cap = percentile(sorted, CAP_PERCENTILE);
|
||||||
|
return new Map(
|
||||||
|
buckets.map((bucket, i) => [
|
||||||
|
bucket.date,
|
||||||
|
cap <= 0
|
||||||
|
? MIN_HEIGHT_PCT
|
||||||
|
: Math.max(MIN_HEIGHT_PCT, Math.min(100, (values[i] / cap) * 100)),
|
||||||
|
])
|
||||||
|
);
|
||||||
|
};
|
||||||
@@ -120,3 +120,18 @@ export const formatDuration = (ms: number, verbose: boolean = false, hasSpace =
|
|||||||
}
|
}
|
||||||
return formatted;
|
return formatted;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const enumerateDays = (days: number, timeZone: string): string[] => {
|
||||||
|
const formatter = new Intl.DateTimeFormat("en-CA", {
|
||||||
|
timeZone,
|
||||||
|
year: "numeric",
|
||||||
|
month: "2-digit",
|
||||||
|
day: "2-digit",
|
||||||
|
});
|
||||||
|
const [year, month, day] = formatter.format(new Date()).split("-").map(Number);
|
||||||
|
const todayUtc = Date.UTC(year, month - 1, day);
|
||||||
|
const DAY_MS = 24 * 60 * 60 * 1000;
|
||||||
|
return Array.from({ length: days }, (_, i) =>
|
||||||
|
new Date(todayUtc - (days - 1 - i) * DAY_MS).toISOString().slice(0, 10)
|
||||||
|
);
|
||||||
|
};
|
||||||
Reference in new issue
Block a user