@@ -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+
1115interface 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
90101export 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
105123export 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