Skip to content

Commit

Permalink
Include request duration in History modal
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasPickering committed Dec 18, 2024
1 parent a296e91 commit 2f07243
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
60 changes: 43 additions & 17 deletions crates/tui/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -626,7 +626,8 @@ pub enum RequestStateSummary {
Response(ExchangeSummary),
RequestError {
id: RequestId,
time: DateTime<Utc>,
start_time: DateTime<Utc>,
end_time: DateTime<Utc>,
},
}

Expand All @@ -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<Utc> {
/// 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<Utc> {
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,
}
}
}
Expand Down Expand Up @@ -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,
},
}
}
Expand Down
9 changes: 8 additions & 1 deletion crates/tui/src/view/component/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down

0 comments on commit 2f07243

Please sign in to comment.