fix(quota): accept epoch reset timestamps (#1241)

* fix(quota): accept epoch reset timestamps

* fix(quota): format epoch reset windows

---------

Co-authored-by: Isaac Sanchez <isanchez-hawkins@arize.com>
This commit is contained in:
Isaac Sanchez-Hawkins
2026-05-13 10:30:42 +03:00
committed by GitHub
co-authored by Isaac Sanchez
parent f059bc8dd6
commit c005c59186
2 changed files with 19 additions and 3 deletions
@@ -27,8 +27,10 @@ export const formatResetTime = (timestamp) => {
}
};
const hasResetTimestamp = (resetAt) => resetAt !== null && resetAt !== undefined && resetAt !== '';
export const calculateResetAfterSeconds = (resetAt) => {
if (!resetAt) return null;
if (!hasResetTimestamp(resetAt)) return null;
const resetAtTime = new Date(resetAt).getTime();
if (!Number.isFinite(resetAtTime)) return null;
const delta = Math.floor((resetAtTime - Date.now()) / 1000);
@@ -37,7 +39,7 @@ export const calculateResetAfterSeconds = (resetAt) => {
export const toUsageWindow = ({ usedPercent, windowSeconds, resetAt, valueLabel }) => {
const resetAfterSeconds = calculateResetAfterSeconds(resetAt);
const resetFormatted = resetAt ? formatResetTime(resetAt) : null;
const resetFormatted = hasResetTimestamp(resetAt) ? formatResetTime(resetAt) : null;
return {
usedPercent,
remainingPercent: usedPercent !== null ? Math.max(0, 100 - usedPercent) : null,
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
import { calculateResetAfterSeconds, formatResetTime } from './formatters.js';
import { calculateResetAfterSeconds, formatResetTime, toUsageWindow } from './formatters.js';
describe('formatResetTime', () => {
it('returns null for invalid timestamps', () => {
@@ -12,6 +12,10 @@ describe('formatResetTime', () => {
});
describe('calculateResetAfterSeconds', () => {
it('accepts an epoch reset timestamp', () => {
expect(calculateResetAfterSeconds(0)).toBe(0);
});
it('returns null for invalid timestamps', () => {
expect(calculateResetAfterSeconds('not-a-date')).toBeNull();
expect(calculateResetAfterSeconds(NaN)).toBeNull();
@@ -19,3 +23,13 @@ describe('calculateResetAfterSeconds', () => {
expect(calculateResetAfterSeconds(-Infinity)).toBeNull();
});
});
describe('toUsageWindow', () => {
it('formats epoch reset timestamps', () => {
const usageWindow = toUsageWindow({ resetAt: 0 });
expect(usageWindow.resetAfterSeconds).toBe(0);
expect(usageWindow.resetAtFormatted).toBe(formatResetTime(0));
expect(usageWindow.resetAfterFormatted).toBe(formatResetTime(0));
});
});