11'use server' ;
22
3- import type { TypeGetStatsEventsByOrganizationParams } from '@/__generated__/data-contracts' ;
3+ import type { TypeGetStatsEventsByOrganizationAggregatedParams } from '@/__generated__/data-contracts' ;
44import Api from '@/libs/Api' ;
5+ import { dayjs , FORMAT_API_DATE } from '@/libs/Dayjs' ;
56
67export type EventsByOrganizationSectionType = {
78 events : number ;
@@ -17,35 +18,26 @@ export type EventsByOrganizationType = {
1718 high : number ;
1819} ;
1920
20- export async function getStatsEventsByOrganization ( query ?: TypeGetStatsEventsByOrganizationParams ) : Promise < EventsByOrganizationType [ ] > {
21- const { data } = await Api . getStatsEventsByOrganization ( query ) ;
22-
23- const groupedData = data . reduce ( ( acc , item ) => {
24- const { name, id, events, collision_probability_range } = item ;
25-
26- const key = id as string ;
27-
28- if ( ! acc [ key ] ) {
29- acc [ key ] = { name, id : key , total_events : 0 , low : 0 , medium : 0 , high : 0 } ;
30- }
31-
32- switch ( collision_probability_range ) {
33- case '< 1e-5' :
34- acc [ key ] . low = events ;
35- acc [ key ] . total_events += events ;
36- break ;
37- case '1e-3 .. 1e-5' :
38- acc [ key ] . medium = events ;
39- acc [ key ] . total_events += events ;
40- break ;
41- case '> 1e-3' :
42- acc [ key ] . high = events ;
43- acc [ key ] . total_events += events ;
44- break ;
45- }
46-
47- return acc ;
48- } , { } as { [ key : string ] : EventsByOrganizationType } ) ;
49-
50- return Object . values ( groupedData ) ;
21+ export async function getStatsEventsByOrganization ( query ?: TypeGetStatsEventsByOrganizationAggregatedParams ) : Promise < EventsByOrganizationType [ ] > {
22+ const params : TypeGetStatsEventsByOrganizationAggregatedParams = { } ;
23+ if ( query ?. start_date ) {
24+ params . start_date = dayjs ( query . start_date ) . format ( FORMAT_API_DATE ) ;
25+ }
26+ if ( query ?. end_date ) {
27+ params . end_date = dayjs ( query . end_date ) . format ( FORMAT_API_DATE ) ;
28+ }
29+ const { data } = await Api . getStatsEventsByOrganizationAggregated ( params ) ;
30+
31+ if ( data . length === 0 ) {
32+ return [ ] ;
33+ }
34+
35+ return data . map ( item => ( {
36+ name : item . organization_name ,
37+ id : item . organization_id ,
38+ total_events : item . total ?? 0 ,
39+ low : item [ '< 1e-5' ] ?? 0 ,
40+ medium : item [ '1e-3 .. 1e-5' ] ?? 0 ,
41+ high : item [ '> 1e-3' ] ?? 0 ,
42+ } ) ) ;
5143} ;
0 commit comments