From 2f07243ac23a0aff3236338a8210ab3789ede314 Mon Sep 17 00:00:00 2001 From: Lucas Pickering Date: Tue, 17 Dec 2024 21:02:01 -0500 Subject: [PATCH] Include request duration in History modal --- CHANGELOG.md | 1 + crates/tui/src/http.rs | 60 +++++++++++++++++------- crates/tui/src/view/component/history.rs | 9 +++- 3 files changed, 52 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d9754d7..6faf0949 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), - The query is now auto-applied when changed (with a 500ms debounce), and drops focus on the text box when Enter is pressed - Refactor UI event handling logic - This shouldn't have any noticable impact on the user, but if you notice any bugs please open an issue +- Include request duration in History modal ### Fixed diff --git a/crates/tui/src/http.rs b/crates/tui/src/http.rs index 85780f15..6670b654 100644 --- a/crates/tui/src/http.rs +++ b/crates/tui/src/http.rs @@ -287,7 +287,7 @@ impl RequestStore { // Add what we loaded from the DB .chain(loaded.into_iter().map(RequestStateSummary::Response)) // Sort descending - .sorted_by_key(RequestStateSummary::time) + .sorted_by_key(RequestStateSummary::start_time) .rev() // De-duplicate double-loaded requests .unique_by(RequestStateSummary::id); @@ -626,7 +626,8 @@ pub enum RequestStateSummary { Response(ExchangeSummary), RequestError { id: RequestId, - time: DateTime, + start_time: DateTime, + end_time: DateTime, }, } @@ -642,24 +643,48 @@ impl RequestStateSummary { } } - /// Get the time of the request state. For in-flight or completed requests, - /// this is when it *started*. - pub fn time(&self) -> DateTime { + /// Get the start time of the request state. For in-flight or completed + /// requests, this is when it *started*. + pub fn start_time(&self) -> DateTime { match self { - Self::Building { - start_time: time, .. - } - | Self::BuildError { - start_time: time, .. - } - | Self::Loading { - start_time: time, .. + Self::Building { start_time, .. } + | Self::BuildError { start_time, .. } + | Self::Loading { start_time, .. } + | Self::Cancelled { start_time, .. } + | Self::RequestError { start_time, .. } => *start_time, + Self::Response(exchange) => exchange.start_time, + } + } + + /// Elapsed time for the active request. If pending, this is a running + /// total. Otherwise end time - start time. + pub fn duration(&self) -> TimeDelta { + // It'd be nice to dedupe this with the calculation used for + // RequestMetadata, but it's not that easy + match self { + // In-progress states + Self::Building { start_time, .. } + | Self::Loading { start_time, .. } => Utc::now() - start_time, + + // Error states + Self::BuildError { + start_time, + end_time, + .. } | Self::Cancelled { - start_time: time, .. + start_time, + end_time, + .. } - | Self::RequestError { time, .. } => *time, - Self::Response(exchange) => exchange.start_time, + | Self::RequestError { + start_time, + end_time, + .. + } => *end_time - *start_time, + + // Completed + Self::Response(exchange) => exchange.end_time - exchange.start_time, } } } @@ -699,7 +724,8 @@ impl From<&RequestState> for RequestStateSummary { } RequestState::RequestError { error } => Self::RequestError { id: error.request.id, - time: error.start_time, + start_time: error.start_time, + end_time: error.end_time, }, } } diff --git a/crates/tui/src/view/component/history.rs b/crates/tui/src/view/component/history.rs index e860456d..1eea10e3 100644 --- a/crates/tui/src/view/component/history.rs +++ b/crates/tui/src/view/component/history.rs @@ -123,7 +123,14 @@ impl Generate for &RequestStateSummary { Span::styled("Request error", styles.text.error) } }; - vec![self.time().generate(), " ".into(), description].into() + vec![ + self.start_time().generate(), + " / ".into(), + self.duration().generate(), + " ".into(), + description, + ] + .into() } }