Skip to content

Commit b25d2af

Browse files
authored
[Dashboard] Fix peak rate date formatting with error handling (#7271)
1 parent 8910f55 commit b25d2af

File tree

1 file changed

+28
-11
lines changed
  • apps/dashboard/src/app/(app)/team/[team_slug]/(team)/~/usage/rpc

1 file changed

+28
-11
lines changed

apps/dashboard/src/app/(app)/team/[team_slug]/(team)/~/usage/rpc/page.tsx

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getTeamBySlug } from "@/api/team";
22
import { getLast24HoursRPCUsage } from "@/api/usage/rpc";
33
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
44
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
5-
import { format } from "date-fns";
5+
import { format, parseISO } from "date-fns";
66
import {
77
AlertTriangleIcon,
88
CheckCircleIcon,
@@ -63,6 +63,23 @@ export default async function RPCUsage(props: {
6363
Number(totalCounts.rateLimitedCount) +
6464
Number(totalCounts.overageCount);
6565

66+
const peakRateDate = (() => {
67+
if (peakRate.date) {
68+
try {
69+
// treat peakRate.date as UTC
70+
const date = peakRate.date.endsWith("Z")
71+
? parseISO(peakRate.date)
72+
: // force the timestamp to be in UTC
73+
parseISO(`${peakRate.date}Z`);
74+
return format(date, "MMM d, HH:mm");
75+
} catch (e) {
76+
console.error("Error parsing peak rate date", peakRate.date, e);
77+
return null;
78+
}
79+
}
80+
return null;
81+
})();
82+
6683
return (
6784
<div className="container mx-auto space-y-8 py-6">
6885
<div className="flex flex-col gap-2">
@@ -113,9 +130,7 @@ export default async function RPCUsage(props: {
113130
</div>
114131
<p className="mt-1 text-muted-foreground text-xs">
115132
<ClockIcon className="mr-1 inline h-3 w-3" />
116-
{peakRate.date
117-
? format(new Date(`${peakRate.date}Z`), "MMM d, HH:mm")
118-
: "No Requests in last 24 hours"}
133+
{peakRateDate ? peakRateDate : "No Requests in last 24 hours"}
119134
</p>
120135
</CardContent>
121136
</Card>
@@ -134,12 +149,14 @@ export default async function RPCUsage(props: {
134149
<CheckCircleIcon className="mr-1 h-3 w-3 text-green-500" />
135150
<span>
136151
{/* we count both included and overage as included */}
137-
{(
138-
((Number(totalCounts.includedCount) +
139-
Number(totalCounts.overageCount)) /
140-
Number(totalRequests)) *
141-
100
142-
).toFixed(1)}
152+
{totalRequests === 0
153+
? "0.0"
154+
: (
155+
((Number(totalCounts.includedCount) +
156+
Number(totalCounts.overageCount)) /
157+
Number(totalRequests)) *
158+
100
159+
).toFixed(1)}
143160
% successful requests
144161
</span>
145162
</div>
@@ -159,7 +176,7 @@ export default async function RPCUsage(props: {
159176
<span>
160177
{/* if there are no requests, we show 100% success rate */}
161178
{totalRequests === 0
162-
? "100"
179+
? "0.0"
163180
: (
164181
(Number(totalCounts.rateLimitedCount) /
165182
Number(totalRequests)) *

0 commit comments

Comments
 (0)