Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include request duration in History modal #429

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading