@@ -475,46 +475,59 @@ export class StorageProvidersController extends ControllerBase {
475475
476476 const lastMonth = DateTime . now ( ) . toUTC ( ) . minus ( { month : 1 } ) . toJSDate ( ) ;
477477
478- const [ avgMetricValues , ipniReporting ] = await Promise . all ( [
479- this . prismaService . storage_provider_url_finder_metric_value . groupBy ( {
480- by : [ 'provider' , 'metric_id' ] ,
481- where : {
482- provider : {
483- in : query . storageProvidersIds ,
478+ const [ avgMetricValues , ipniSuccessReporting , ipniAllReporting ] =
479+ await Promise . all ( [
480+ this . prismaService . storage_provider_url_finder_metric_value . groupBy ( {
481+ by : [ 'provider' , 'metric_id' ] ,
482+ where : {
483+ provider : {
484+ in : query . storageProvidersIds ,
485+ } ,
486+ tested_at : {
487+ gte : lastMonth ,
488+ } ,
484489 } ,
485- tested_at : {
486- gte : lastMonth ,
490+ _avg : {
491+ value : true ,
487492 } ,
488- } ,
489- _avg : {
490- value : true ,
491- } ,
492- } ) ,
493- this . prismaService . storage_provider_url_finder_daily_snapshot . groupBy ( {
494- by : [ 'provider' ] ,
495- where : {
496- provider : {
497- in : query . storageProvidersIds ,
493+ } ) ,
494+ this . prismaService . storage_provider_url_finder_daily_snapshot . groupBy ( {
495+ by : [ 'provider' ] ,
496+ where : {
497+ provider : {
498+ in : query . storageProvidersIds ,
499+ } ,
500+ snapshot_date : {
501+ gte : lastMonth ,
502+ } ,
503+ result_code : StorageProviderUrlFinderMetricResultCodeType . SUCCESS ,
498504 } ,
499- snapshot_date : {
500- gte : lastMonth ,
505+ _count : {
506+ result_code : true ,
501507 } ,
502- result_code : {
503- not : StorageProviderUrlFinderMetricResultCodeType . SUCCESS ,
508+ } ) ,
509+ this . prismaService . storage_provider_url_finder_daily_snapshot . groupBy ( {
510+ by : [ 'provider' ] ,
511+ where : {
512+ provider : {
513+ in : query . storageProvidersIds ,
514+ } ,
515+ snapshot_date : {
516+ gte : lastMonth ,
517+ } ,
504518 } ,
505- } ,
506- _count : {
507- result_code : true ,
508- } ,
509- } ) ,
510- ] ) ;
519+ _count : {
520+ result_code : true ,
521+ } ,
522+ } ) ,
523+ ] ) ;
511524
512525 const sliDataResponse : GetStorageProvidersSliDataResponse = {
513526 sliMetadata : { } ,
514527 data : { } ,
515528 } ;
516529
517- if ( avgMetricValues . length === 0 && ipniReporting . length === 0 ) {
530+ if ( avgMetricValues . length === 0 && ipniSuccessReporting . length === 0 ) {
518531 return sliDataResponse ;
519532 }
520533
@@ -535,27 +548,62 @@ export class StorageProvidersController extends ControllerBase {
535548 { } as Record < string , ( typeof metricMetadata ) [ number ] > ,
536549 ) ;
537550
538- for ( const metric of avgMetricValues ) {
539- const meta = metricMetadataById [ metric . metric_id ] ;
551+ for ( let i = 0 ; i < query . storageProvidersIds . length ; i ++ ) {
552+ const sp = query . storageProvidersIds [ i ] ;
553+
554+ const spMetrics = avgMetricValues . filter (
555+ ( metric ) => metric . provider === sp ,
556+ ) ;
557+
558+ const spIpniSuccessReporting = ipniSuccessReporting . find (
559+ ( report ) => report . provider === sp ,
560+ ) ;
561+
562+ const spIpniAllReporting = ipniAllReporting . find (
563+ ( report ) => report . provider === sp ,
564+ ) ;
565+
566+ if ( ! spMetrics . length && ! spIpniSuccessReporting && ! spIpniAllReporting ) {
567+ sliDataResponse . data [ sp ] = [ ] ;
568+ continue ;
569+ }
540570
541- if ( ! meta ) continue ;
571+ sliDataResponse . data [ sp ] = spMetrics
572+ . map ( ( metric ) => {
573+ const meta = metricMetadataById [ metric . metric_id ] ;
542574
575+ if ( ! meta ) return null ;
576+
577+ return {
578+ sliMetricValue : metric . _avg . value ?. toString ( ) ?? null ,
579+ sliMetricType : meta . metric_type ,
580+ } ;
581+ } )
582+ . filter ( ( metric ) => metric !== null ) ;
583+
584+ if ( spIpniAllReporting ?. _count ?. result_code > 0 ) {
585+ const ipniSuccessReportingCount =
586+ spIpniSuccessReporting ?. _count ?. result_code ?? 0 ;
587+
588+ const ipniAllReportingCount = spIpniAllReporting . _count ?. result_code ;
589+
590+ sliDataResponse . data [ sp ] . push ( {
591+ sliMetricValue : (
592+ ipniSuccessReportingCount / ipniAllReportingCount
593+ ) . toString ( ) ,
594+ sliMetricType : 'IPNI_REPORTING' ,
595+ } ) ;
596+ }
597+ }
598+
599+ for ( const meta of metricMetadata ) {
543600 sliDataResponse . sliMetadata [ meta . metric_type ] = {
544601 sliMetricType :
545602 meta . metric_type as StorageProviderUrlFinderSliMetricType ,
546603 sliMetricName : meta . name ,
547604 sliMetricDescription : meta . description ,
548605 sliMetricUnit : meta . unit ,
549606 } ;
550-
551- if ( ! sliDataResponse . data [ metric . provider ] ) {
552- sliDataResponse . data [ metric . provider ] = [ ] ;
553- }
554-
555- sliDataResponse . data [ metric . provider ] . push ( {
556- sliMetricValue : metric . _avg . value ?. toString ( ) ?? null ,
557- sliMetricType : meta . metric_type ,
558- } ) ;
559607 }
560608
561609 sliDataResponse . sliMetadata [
@@ -564,25 +612,10 @@ export class StorageProvidersController extends ControllerBase {
564612 sliMetricType : StorageProviderUrlFinderSliMetricType . IPNI_REPORTING ,
565613 sliMetricName : 'IPNI Reporting' ,
566614 sliMetricDescription :
567- 'Whether the storage provider has reported to IPNI in the last month ' ,
615+ 'Percentage of days in the last month on which the storage provider successfully reported to IPNI' ,
568616 sliMetricUnit : '%' ,
569617 } ;
570618
571- for ( const report of ipniReporting ) {
572- if ( ! sliDataResponse . data [ report . provider ] ) {
573- sliDataResponse . data [ report . provider ] = [ ] ;
574- }
575-
576- // this gives the percentage of days in the last month that the provider did not report successfully to IPNI
577- const percentage =
578- ( report . _count . result_code / ipniReporting . length ) * 100 ;
579-
580- sliDataResponse . data [ report . provider ] . push ( {
581- sliMetricValue : percentage . toFixed ( 2 ) ,
582- sliMetricType : 'IPNI_REPORTING' ,
583- } ) ;
584- }
585-
586619 return sliDataResponse ;
587620 }
588621}
0 commit comments