@@ -7,6 +7,7 @@ use diesel::prelude::{
77 ExpressionMethods , JoinOnDsl , NullableExpressionMethods , OptionalExtension , PgConnection ,
88 QueryDsl , RunQueryDsl , SelectableHelper as _,
99} ;
10+ use diesel:: sql_types:: { Array , BigInt , Integer } ;
1011use diesel_derives:: Associations ;
1112use git_testament:: { git_testament, git_testament_macros} ;
1213use graph:: blockchain:: BlockHash ;
@@ -239,6 +240,7 @@ pub(crate) fn info_from_details(
239240 non_fatal : Vec < ErrorDetail > ,
240241 sites : & [ Arc < Site > ] ,
241242 subgraph_history_blocks : i32 ,
243+ subgraph_size : status:: SubgraphSize ,
242244) -> Result < status:: Info , StoreError > {
243245 let DeploymentDetail {
244246 id,
@@ -301,6 +303,7 @@ pub(crate) fn info_from_details(
301303 entity_count,
302304 node : None ,
303305 history_blocks : subgraph_history_blocks,
306+ subgraph_size,
304307 } )
305308}
306309
@@ -422,17 +425,87 @@ pub(crate) fn deployment_statuses(
422425 . collect ( )
423426 } ;
424427
428+ let mut deployment_sizes = deployment_sizes ( conn, sites) ?;
429+
425430 details_with_fatal_error
426431 . into_iter ( )
427432 . map ( |( deployment, head, fatal) | {
428433 let detail = DeploymentDetail :: from ( ( deployment, head) ) ;
429434 let non_fatal = non_fatal_errors. remove ( & detail. id ) . unwrap_or_default ( ) ;
430435 let subgraph_history_blocks = history_blocks_map. remove ( & detail. id ) . unwrap_or_default ( ) ;
431- info_from_details ( detail, fatal, non_fatal, sites, subgraph_history_blocks)
436+ let table_sizes = deployment_sizes. remove ( & detail. id ) . unwrap_or_default ( ) ;
437+ info_from_details (
438+ detail,
439+ fatal,
440+ non_fatal,
441+ sites,
442+ subgraph_history_blocks,
443+ table_sizes,
444+ )
432445 } )
433446 . collect ( )
434447}
435448
449+ fn deployment_sizes (
450+ conn : & mut PgConnection ,
451+ sites : & [ Arc < Site > ] ,
452+ ) -> Result < HashMap < DeploymentId , status:: SubgraphSize > , StoreError > {
453+ #[ derive( QueryableByName ) ]
454+ struct SubgraphSizeRow {
455+ #[ diesel( sql_type = Integer ) ]
456+ id : DeploymentId ,
457+ #[ diesel( sql_type = BigInt ) ]
458+ row_estimate : i64 ,
459+ #[ diesel( sql_type = BigInt ) ]
460+ table_bytes : i64 ,
461+ #[ diesel( sql_type = BigInt ) ]
462+ index_bytes : i64 ,
463+ #[ diesel( sql_type = BigInt ) ]
464+ toast_bytes : i64 ,
465+ #[ diesel( sql_type = BigInt ) ]
466+ total_bytes : i64 ,
467+ }
468+
469+ let mut query = String :: from (
470+ r#"
471+ SELECT
472+ ds.id,
473+ ss.row_estimate::bigint,
474+ ss.table_bytes::bigint,
475+ ss.index_bytes::bigint,
476+ ss.toast_bytes::bigint,
477+ ss.total_bytes::bigint
478+ FROM deployment_schemas ds
479+ JOIN info.subgraph_sizes as ss on ss.name = ds.name
480+ "# ,
481+ ) ;
482+
483+ let rows = if sites. is_empty ( ) {
484+ diesel:: sql_query ( query) . load :: < SubgraphSizeRow > ( conn) ?
485+ } else {
486+ query. push_str ( " WHERE ds.id = ANY($1)" ) ;
487+ diesel:: sql_query ( query)
488+ . bind :: < Array < Integer > , _ > ( sites. iter ( ) . map ( |site| site. id ) . collect :: < Vec < _ > > ( ) )
489+ . load :: < SubgraphSizeRow > ( conn) ?
490+ } ;
491+
492+ let mut sizes: HashMap < DeploymentId , status:: SubgraphSize > = HashMap :: new ( ) ;
493+ for row in rows {
494+ sizes. insert (
495+ row. id ,
496+ status:: SubgraphSize {
497+ row_estimate : row. row_estimate ,
498+ table_bytes : row. table_bytes ,
499+ index_bytes : row. index_bytes ,
500+ toast_bytes : row. toast_bytes ,
501+ total_bytes : row. total_bytes ,
502+ } ,
503+ ) ;
504+ }
505+
506+ Ok ( sizes)
507+ }
508+
436509#[ derive( Queryable , Selectable , Identifiable , Associations ) ]
437510#[ diesel( table_name = subgraph_manifest) ]
438511#[ diesel( belongs_to( GraphNodeVersion ) ) ]
0 commit comments