Skip to content

Commit 5a5e333

Browse files
authored
Merge pull request #1 from congiuluc/copilot/persist-data-on-refresh
Fix ExceedanceReport crash and round aggregated numbers to 2 decimal places
2 parents c061f81 + f6038f1 commit 5a5e333

2 files changed

Lines changed: 48 additions & 16 deletions

File tree

src/pages/ExceedanceReport.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export default function ExceedanceReport({ data, profiles }: Props) {
7070
},
7171
}),
7272
columnHelper.accessor('organization', { header: 'Organization' }),
73-
columnHelper.accessor('totalMonthlyQuota', {
73+
columnHelper.accessor('quota', {
7474
header: 'Quota',
7575
cell: (info) => info.getValue().toLocaleString(),
7676
}),
@@ -116,7 +116,7 @@ export default function ExceedanceReport({ data, profiles }: Props) {
116116
Username: r.username,
117117
Name: displayName(r.username, profiles),
118118
Organization: r.organization,
119-
Quota: r.totalMonthlyQuota,
119+
Quota: r.quota,
120120
'Total Requests': r.totalRequests,
121121
'Exceeded Requests': r.exceededRequests,
122122
'Models Used': r.modelsUsed,

src/utils/dataTransforms.ts

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import type {
88
Kpis,
99
} from '../types';
1010

11+
function round2(n: number): number {
12+
return Math.round((n + Number.EPSILON) * 100) / 100;
13+
}
14+
1115
interface UserAccumulator {
1216
username: string;
1317
organization: string;
@@ -60,12 +64,12 @@ export function aggregateByUser(rows: CsvRow[]): UserAggregate[] {
6064
username: u.username,
6165
organization: u.organization,
6266
costCenter: u.costCenter,
63-
totalRequests: u.totalRequests,
64-
grossAmount: u.grossAmount,
65-
netAmount: u.netAmount,
66-
discountAmount: u.discountAmount,
67-
exceededRequests: u.exceededRequests,
68-
overQuotaCost: u.overQuotaCost,
67+
totalRequests: Math.round(u.totalRequests),
68+
grossAmount: round2(u.grossAmount),
69+
netAmount: round2(u.netAmount),
70+
discountAmount: round2(u.discountAmount),
71+
exceededRequests: Math.round(u.exceededRequests),
72+
overQuotaCost: round2(u.overQuotaCost),
6973
quota: u.quota,
7074
modelsUsed: u.modelsUsed.size,
7175
exceededModels: u.exceededModels.size,
@@ -84,7 +88,14 @@ export function aggregateByDate(rows: CsvRow[]): DateAggregate[] {
8488
d.grossAmount += parseFloat(row.gross_amount) || 0;
8589
d.netAmount += parseFloat(row.net_amount) || 0;
8690
}
87-
return Array.from(map.values()).sort((a, b) => a.date.localeCompare(b.date));
91+
return Array.from(map.values())
92+
.map((d) => ({
93+
date: d.date,
94+
totalRequests: Math.round(d.totalRequests),
95+
grossAmount: round2(d.grossAmount),
96+
netAmount: round2(d.netAmount),
97+
}))
98+
.sort((a, b) => a.date.localeCompare(b.date));
8899
}
89100

90101
export function aggregateByModel(rows: CsvRow[]): ModelAggregate[] {
@@ -99,7 +110,14 @@ export function aggregateByModel(rows: CsvRow[]): ModelAggregate[] {
99110
m.grossAmount += parseFloat(row.gross_amount) || 0;
100111
m.netAmount += parseFloat(row.net_amount) || 0;
101112
}
102-
return Array.from(map.values()).sort((a, b) => b.totalRequests - a.totalRequests);
113+
return Array.from(map.values())
114+
.map((m) => ({
115+
model: m.model,
116+
totalRequests: Math.round(m.totalRequests),
117+
grossAmount: round2(m.grossAmount),
118+
netAmount: round2(m.netAmount),
119+
}))
120+
.sort((a, b) => b.totalRequests - a.totalRequests);
103121
}
104122

105123
export function aggregateByOrg(rows: CsvRow[]): OrgAggregate[] {
@@ -119,7 +137,13 @@ export function aggregateByOrg(rows: CsvRow[]): OrgAggregate[] {
119137
entry.users.add(row.username);
120138
}
121139
return Array.from(map.values())
122-
.map((e) => ({ ...e.org, userCount: e.users.size }))
140+
.map((e) => ({
141+
...e.org,
142+
totalRequests: Math.round(e.org.totalRequests),
143+
grossAmount: round2(e.org.grossAmount),
144+
netAmount: round2(e.org.netAmount),
145+
userCount: e.users.size,
146+
}))
123147
.sort((a, b) => b.totalRequests - a.totalRequests);
124148
}
125149

@@ -151,10 +175,10 @@ export function computeKpis(rows: CsvRow[]): Kpis {
151175
}
152176

153177
return {
154-
totalRequests,
155-
grossAmount,
156-
netAmount,
157-
discountAmount,
178+
totalRequests: Math.round(totalRequests),
179+
grossAmount: round2(grossAmount),
180+
netAmount: round2(netAmount),
181+
discountAmount: round2(discountAmount),
158182
uniqueUsers: users.size,
159183
uniqueModels: models.size,
160184
exceedingUsersCount: exceedingUsers.size,
@@ -174,5 +198,13 @@ export function aggregateBySku(rows: CsvRow[]): SkuAggregate[] {
174198
s.grossAmount += parseFloat(row.gross_amount) || 0;
175199
s.netAmount += parseFloat(row.net_amount) || 0;
176200
}
177-
return Array.from(map.values()).sort((a, b) => b.totalRequests - a.totalRequests);
201+
return Array.from(map.values())
202+
.map((s) => ({
203+
sku: s.sku,
204+
product: s.product,
205+
totalRequests: Math.round(s.totalRequests),
206+
grossAmount: round2(s.grossAmount),
207+
netAmount: round2(s.netAmount),
208+
}))
209+
.sort((a, b) => b.totalRequests - a.totalRequests);
178210
}

0 commit comments

Comments
 (0)