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 ;
1111use anyhow:: Context ;
1212use async_bb8_diesel:: AsyncRunQueryDsl ;
1313use chrono:: { DateTime , Utc } ;
@@ -17,7 +17,6 @@ use diesel::prelude::*;
1717use nexus_db_queries:: context:: OpContext ;
1818use nexus_db_queries:: db:: DataStore ;
1919use nexus_db_queries:: db:: model;
20- use nexus_db_queries:: db:: pagination:: paginated;
2120use nexus_types:: fm;
2221use omicron_common:: api:: external:: DataPageParams ;
2322use omicron_common:: api:: external:: PaginationOrder ;
@@ -26,7 +25,6 @@ use omicron_uuid_kinds::SitrepUuid;
2625use tabled:: Tabled ;
2726use uuid:: Uuid ;
2827
29- use nexus_db_schema:: schema:: fm_sitrep:: dsl as sitrep_dsl;
3028use nexus_db_schema:: schema:: fm_sitrep_history:: dsl as history_dsl;
3129use nexus_db_schema:: schema:: inv_collection:: dsl as inv_collection_dsl;
3230
@@ -100,7 +98,7 @@ pub(super) async fn cmd_db_sitrep(
10098) -> anyhow:: Result < ( ) > {
10199 match args. command {
102100 Commands :: History ( ref args) => {
103- cmd_db_sitrep_history ( datastore, fetch_opts, args) . await
101+ cmd_db_sitrep_history ( opctx , datastore, fetch_opts, args) . await
104102 }
105103 Commands :: Info { sitrep, ref args } => {
106104 cmd_db_sitrep_show ( opctx, datastore, fetch_opts, args, sitrep) . await
@@ -119,6 +117,7 @@ pub(super) async fn cmd_db_sitrep(
119117}
120118
121119pub ( super ) async fn cmd_db_sitrep_history (
120+ opctx : & OpContext ,
122121 datastore : & DataStore ,
123122 fetch_opts : & DbFetchOptions ,
124123 args : & SitrepHistoryArgs ,
@@ -138,53 +137,48 @@ pub(super) async fn cmd_db_sitrep_history(
138137 struct SitrepRow {
139138 v : u32 ,
140139 id : Uuid ,
141- #[ tabled( display_with = "datetime_rfc3339_concise " ) ]
142- created_at : DateTime < Utc > ,
140+ #[ tabled( display_with = "datetime_opt_rfc3339_concise " ) ]
141+ created_at : Option < DateTime < Utc > > ,
143142 comment : String ,
144143 }
145144
146- let conn = datastore. pool_connection_for_tests ( ) . await ?;
147145 let marker = args. from . map ( model:: SqlU32 :: new) ;
148146 let pagparams = DataPageParams {
149147 marker : marker. as_ref ( ) ,
150148 direction : PaginationOrder :: Descending ,
151149 limit : fetch_opts. fetch_limit ,
152150 } ;
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)
151+ let versions = datastore
152+ . fm_sitrep_version_list ( & opctx, & pagparams)
167153 . await
168154 . with_context ( ctx) ?;
169155
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 ( ) ,
156+ check_limit ( & versions, fetch_opts. fetch_limit , ctx) ;
157+
158+ let mut rows = Vec :: with_capacity ( versions. len ( ) ) ;
159+ for v in versions {
160+ let ( comment, time_created) =
161+ match datastore. fm_sitrep_metadata_read ( & opctx, v. id ) . await {
162+ Ok ( s) => ( s. comment , Some ( s. time_created ) ) ,
163+ Err ( e) => {
164+ // If the sitrep has an entry in the history table, we
165+ // expect that it will not yet have been archived and
166+ // deleted, so this is an error rather a case of it just
167+ // no longer existing.
168+ eprintln ! (
169+ "failed to fetch metadata for sitrep {} (v{}): {e}" ,
170+ v. id, v. version
171+ ) ;
172+ ( "<ERROR>" . to_string ( ) , None )
173+ }
174+ } ;
175+ rows. push ( SitrepRow {
176+ v : v. version ,
177+ id : v. id . into_untyped_uuid ( ) ,
184178 created_at : time_created,
185179 comment,
186- }
187- } ) ;
180+ } ) ;
181+ }
188182
189183 let table = tabled:: Table :: new ( rows)
190184 . with ( tabled:: settings:: Style :: empty ( ) )
0 commit comments