77use crate :: db:: DbFetchOptions ;
88use crate :: db:: check_limit;
99use crate :: helpers:: const_max_len;
10- use crate :: helpers:: datetime_rfc3339_concise;
10+ use crate :: helpers:: datetime_opt_rfc3339_concise;
11+ use crate :: helpers:: display_option_blank;
1112use anyhow:: Context ;
1213use async_bb8_diesel:: AsyncRunQueryDsl ;
1314use chrono:: { DateTime , Utc } ;
@@ -17,7 +18,6 @@ use diesel::prelude::*;
1718use nexus_db_queries:: context:: OpContext ;
1819use nexus_db_queries:: db:: DataStore ;
1920use nexus_db_queries:: db:: model;
20- use nexus_db_queries:: db:: pagination:: paginated;
2121use nexus_types:: fm;
2222use omicron_common:: api:: external:: DataPageParams ;
2323use omicron_common:: api:: external:: PaginationOrder ;
@@ -26,7 +26,6 @@ use omicron_uuid_kinds::SitrepUuid;
2626use tabled:: Tabled ;
2727use uuid:: Uuid ;
2828
29- use nexus_db_schema:: schema:: fm_sitrep:: dsl as sitrep_dsl;
3029use nexus_db_schema:: schema:: fm_sitrep_history:: dsl as history_dsl;
3130use nexus_db_schema:: schema:: inv_collection:: dsl as inv_collection_dsl;
3231
@@ -100,7 +99,7 @@ pub(super) async fn cmd_db_sitrep(
10099) -> anyhow:: Result < ( ) > {
101100 match args. command {
102101 Commands :: History ( ref args) => {
103- cmd_db_sitrep_history ( datastore, fetch_opts, args) . await
102+ cmd_db_sitrep_history ( opctx , datastore, fetch_opts, args) . await
104103 }
105104 Commands :: Info { sitrep, ref args } => {
106105 cmd_db_sitrep_show ( opctx, datastore, fetch_opts, args, sitrep) . await
@@ -119,6 +118,7 @@ pub(super) async fn cmd_db_sitrep(
119118}
120119
121120pub ( super ) async fn cmd_db_sitrep_history (
121+ opctx : & OpContext ,
122122 datastore : & DataStore ,
123123 fetch_opts : & DbFetchOptions ,
124124 args : & SitrepHistoryArgs ,
@@ -138,53 +138,58 @@ pub(super) async fn cmd_db_sitrep_history(
138138 struct SitrepRow {
139139 v : u32 ,
140140 id : Uuid ,
141- #[ tabled( display_with = "datetime_rfc3339_concise" ) ]
142- created_at : DateTime < Utc > ,
141+ #[ tabled( display_with = "display_option_blank" ) ]
142+ orphans : Option < usize > ,
143+ #[ tabled( display_with = "datetime_opt_rfc3339_concise" ) ]
144+ created_at : Option < DateTime < Utc > > ,
143145 comment : String ,
144146 }
145147
146- let conn = datastore. pool_connection_for_tests ( ) . await ?;
147148 let marker = args. from . map ( model:: SqlU32 :: new) ;
148149 let pagparams = DataPageParams {
149150 marker : marker. as_ref ( ) ,
150151 direction : PaginationOrder :: Descending ,
151152 limit : fetch_opts. fetch_limit ,
152153 } ;
153- let sitreps: Vec < ( model:: SitrepVersion , model:: SitrepMetadata ) > =
154- paginated (
155- history_dsl:: fm_sitrep_history,
156- history_dsl:: version,
157- & pagparams,
158- )
159- . inner_join (
160- sitrep_dsl:: fm_sitrep. on ( history_dsl:: sitrep_id. eq ( sitrep_dsl:: id) ) ,
161- )
162- . select ( (
163- model:: SitrepVersion :: as_select ( ) ,
164- model:: SitrepMetadata :: as_select ( ) ,
165- ) )
166- . load_async ( & * conn)
154+ let versions = datastore
155+ . fm_sitrep_version_list ( & opctx, & pagparams)
167156 . await
168157 . with_context ( ctx) ?;
169158
170- check_limit ( & sitreps, fetch_opts. fetch_limit , ctx) ;
171-
172- let rows = sitreps. into_iter ( ) . map ( |( version, metadata) | {
173- let model:: SitrepMetadata {
174- id,
175- time_created,
176- comment,
177- creator_id : _,
178- parent_sitrep_id : _,
179- inv_collection_id : _,
180- } = metadata;
181- SitrepRow {
182- v : version. version . into ( ) ,
183- id : id. into_untyped_uuid ( ) ,
159+ check_limit ( & versions, fetch_opts. fetch_limit , ctx) ;
160+
161+ let mut rows = Vec :: with_capacity ( versions. len ( ) ) ;
162+ for v in versions {
163+ let orphans = match datastore. fm_sitrep_list_orphaned ( & opctx, & v) . await
164+ {
165+ Ok ( o) => Some ( o. len ( ) ) ,
166+ Err ( e) => {
167+ eprintln ! (
168+ "failed to list orphaned sitreps at v{}: {e}" ,
169+ v. version
170+ ) ;
171+ None
172+ }
173+ } ;
174+ let ( comment, time_created) =
175+ match datastore. fm_sitrep_metadata_read ( & opctx, v. id ) . await {
176+ Ok ( s) => ( s. comment , Some ( s. time_created ) ) ,
177+ Err ( e) => {
178+ eprintln ! (
179+ "failed to get fetch metadata for sitrep {} (v{}): {e}" ,
180+ v. id, v. version
181+ ) ;
182+ ( "<ERROR>" . to_string ( ) , None )
183+ }
184+ } ;
185+ rows. push ( SitrepRow {
186+ v : v. version ,
187+ id : v. id . into_untyped_uuid ( ) ,
188+ orphans,
184189 created_at : time_created,
185190 comment,
186- }
187- } ) ;
191+ } ) ;
192+ }
188193
189194 let table = tabled:: Table :: new ( rows)
190195 . with ( tabled:: settings:: Style :: empty ( ) )
0 commit comments