Skip to content

Commit c04d76d

Browse files
committed
update endpoints to return something more ui friendly
1 parent 01f69bc commit c04d76d

File tree

3 files changed

+220
-31
lines changed

3 files changed

+220
-31
lines changed

database/src/lib.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ impl Ord for Commit {
199199

200200
/// The compilation profile (i.e., how the crate was built)
201201
#[derive(
202-
Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, serde::Serialize, serde::Deserialize,
202+
Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, serde::Deserialize, serde::Serialize,
203203
)]
204204
pub enum Profile {
205205
/// A checked build (i.e., no codegen)
@@ -356,9 +356,7 @@ impl PartialOrd for Scenario {
356356
/// https://doc.rust-lang.org/nightly/rustc/platform-support.html
357357
///
358358
/// Presently we only support x86_64
359-
#[derive(
360-
Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, serde::Serialize, serde::Deserialize,
361-
)]
359+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
362360
pub enum Target {
363361
/// `x86_64-unknown-linux-gnu`
364362
X86_64UnknownLinuxGnu,
@@ -393,9 +391,7 @@ impl fmt::Display for Target {
393391
}
394392

395393
/// The codegen backend used for compilation.
396-
#[derive(
397-
Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, serde::Serialize, serde::Deserialize,
398-
)]
394+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
399395
pub enum CodegenBackend {
400396
/// The default LLVM backend
401397
Llvm,
@@ -807,7 +803,7 @@ pub struct ArtifactCollection {
807803
pub end_time: DateTime<Utc>,
808804
}
809805

810-
#[derive(Debug, Copy, Clone, PartialEq, serde::Deserialize, serde::Serialize)]
806+
#[derive(Debug, Copy, Clone, PartialEq)]
811807
pub enum BenchmarkRequestStatus {
812808
WaitingForArtifacts,
813809
ArtifactsReady,
@@ -824,7 +820,7 @@ const BENCHMARK_REQUEST_STATUS_IN_PROGRESS_STR: &str = "in_progress";
824820
const BENCHMARK_REQUEST_STATUS_COMPLETED_STR: &str = "completed";
825821

826822
impl BenchmarkRequestStatus {
827-
pub(crate) fn as_str(&self) -> &str {
823+
pub fn as_str(&self) -> &str {
828824
match self {
829825
Self::WaitingForArtifacts => BENCHMARK_REQUEST_STATUS_WAITING_FOR_ARTIFACTS_STR,
830826
Self::ArtifactsReady => BENCHMARK_REQUEST_STATUS_ARTIFACTS_READY_STR,
@@ -865,7 +861,7 @@ const BENCHMARK_REQUEST_TRY_STR: &str = "try";
865861
const BENCHMARK_REQUEST_MASTER_STR: &str = "master";
866862
const BENCHMARK_REQUEST_RELEASE_STR: &str = "release";
867863

868-
#[derive(Debug, Clone, PartialEq, serde::Deserialize, serde::Serialize)]
864+
#[derive(Debug, Clone, PartialEq)]
869865
pub enum BenchmarkRequestType {
870866
/// A Try commit
871867
Try {
@@ -893,7 +889,7 @@ impl fmt::Display for BenchmarkRequestType {
893889
}
894890
}
895891

896-
#[derive(Debug, Clone, PartialEq, serde::Deserialize, serde::Serialize)]
892+
#[derive(Debug, Clone, PartialEq)]
897893
pub struct BenchmarkRequest {
898894
commit_type: BenchmarkRequestType,
899895
// When was the compiler artifact created
@@ -986,6 +982,10 @@ impl BenchmarkRequest {
986982
self.created_at
987983
}
988984

985+
pub fn commit_date(&self) -> Option<DateTime<Utc>> {
986+
self.commit_date
987+
}
988+
989989
pub fn is_master(&self) -> bool {
990990
matches!(self.commit_type, BenchmarkRequestType::Master { .. })
991991
}
@@ -1058,7 +1058,7 @@ impl BenchmarkRequestIndex {
10581058
}
10591059
}
10601060

1061-
#[derive(Debug, Clone, PartialEq, serde::Deserialize, serde::Serialize)]
1061+
#[derive(Debug, Clone, PartialEq)]
10621062
pub enum BenchmarkJobStatus {
10631063
Queued,
10641064
InProgress {
@@ -1101,7 +1101,7 @@ impl fmt::Display for BenchmarkJobStatus {
11011101
}
11021102

11031103
#[derive(Debug, Copy, Clone, PartialEq, serde::Deserialize, serde::Serialize)]
1104-
pub struct BenchmarkSet(u32);
1104+
pub struct BenchmarkSet(pub u32);
11051105

11061106
impl BenchmarkSet {
11071107
pub fn new(id: u32) -> Self {
@@ -1115,7 +1115,7 @@ impl BenchmarkSet {
11151115
/// Each request is split into several `BenchmarkJob`s. Collectors poll the
11161116
/// queue and claim a job only when its `benchmark_set` matches one of the sets
11171117
/// they are responsible for.
1118-
#[derive(Debug, Clone, PartialEq, serde::Deserialize, serde::Serialize)]
1118+
#[derive(Debug, Clone, PartialEq)]
11191119
pub struct BenchmarkJob {
11201120
id: u32,
11211121
target: Target,
@@ -1169,6 +1169,10 @@ impl BenchmarkJob {
11691169
pub fn status(&self) -> &BenchmarkJobStatus {
11701170
&self.status
11711171
}
1172+
1173+
pub fn created_at(&self) -> DateTime<Utc> {
1174+
self.created_at
1175+
}
11721176
}
11731177

11741178
/// Describes the final state of a job
@@ -1188,7 +1192,7 @@ impl BenchmarkJobConclusion {
11881192
}
11891193

11901194
/// The configuration for a collector
1191-
#[derive(Debug, PartialEq, serde::Deserialize, serde::Serialize)]
1195+
#[derive(Debug, PartialEq)]
11921196
pub struct CollectorConfig {
11931197
name: String,
11941198
target: Target,
@@ -1226,7 +1230,7 @@ impl CollectorConfig {
12261230

12271231
/// The data that can be retrived from the database directly to populate the
12281232
/// status page
1229-
#[derive(Debug, PartialEq, serde::Deserialize, serde::Serialize)]
1233+
#[derive(Debug, PartialEq)]
12301234
pub struct PartialStatusPageData {
12311235
/// A Vector of; completed requests with any associated errors
12321236
pub completed_requests: Vec<(BenchmarkRequest, Vec<String>)>,

site/src/api.rs

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -392,19 +392,89 @@ pub mod status {
392392
}
393393

394394
pub mod status_new {
395-
use database::{BenchmarkJob, BenchmarkRequest, CollectorConfig};
395+
use chrono::{DateTime, Utc};
396+
use database::BenchmarkSet;
396397
use serde::Serialize;
397398

399+
#[derive(Serialize, Debug)]
400+
#[serde(rename_all = "camelCase")]
401+
pub struct BenchmarkRequestStatusUi {
402+
pub state: String,
403+
pub completed_at: Option<DateTime<Utc>>,
404+
pub duration: Option<u32>,
405+
}
406+
407+
#[derive(Serialize, Debug)]
408+
#[serde(rename_all = "camelCase")]
409+
pub struct BenchmarkRequestTypeUi {
410+
pub r#type: String,
411+
pub tag: Option<String>,
412+
pub parent_sha: Option<String>,
413+
pub pr: Option<u32>,
414+
}
415+
416+
#[derive(Serialize, Debug)]
417+
#[serde(rename_all = "camelCase")]
418+
pub struct BenchmarkRequestUi {
419+
pub status: BenchmarkRequestStatusUi,
420+
pub request_type: BenchmarkRequestTypeUi,
421+
pub commit_date: Option<DateTime<Utc>>,
422+
pub created_at: DateTime<Utc>,
423+
pub backends: Vec<String>,
424+
pub profiles: String,
425+
pub errors: Vec<String>,
426+
}
427+
428+
#[derive(Serialize, Debug)]
429+
#[serde(rename_all = "camelCase")]
430+
pub struct BenchmarkJobStatusUi {
431+
pub state: String,
432+
pub started_at: Option<DateTime<Utc>>,
433+
pub completed_at: Option<DateTime<Utc>>,
434+
pub collector_name: Option<String>,
435+
}
436+
437+
#[derive(Serialize, Debug)]
438+
#[serde(rename_all = "camelCase")]
439+
pub struct BenchmarkJobUi {
440+
pub target: String,
441+
pub backend: String,
442+
pub profile: String,
443+
pub request_tag: String,
444+
pub benchmark_set: BenchmarkSet,
445+
pub created_at: DateTime<Utc>,
446+
pub status: BenchmarkJobStatusUi,
447+
pub deque_counter: u32,
448+
}
449+
450+
#[derive(Serialize, Debug)]
451+
#[serde(rename_all = "camelCase")]
452+
pub struct BenchmarkInProgressUi {
453+
pub request: BenchmarkRequestUi,
454+
pub jobs: Vec<BenchmarkJobUi>,
455+
}
456+
457+
#[derive(Serialize, Debug)]
458+
#[serde(rename_all = "camelCase")]
459+
pub struct CollectorConfigUi {
460+
pub name: String,
461+
pub target: String,
462+
pub benchmark_set: BenchmarkSet,
463+
pub is_active: bool,
464+
pub last_heartbeat_at: DateTime<Utc>,
465+
pub date_added: DateTime<Utc>,
466+
}
467+
398468
#[derive(Serialize, Debug)]
399469
pub struct Response {
400470
/// Completed requests alongside any errors
401-
pub completed: Vec<(BenchmarkRequest, Vec<String>)>,
471+
pub completed: Vec<BenchmarkRequestUi>,
402472
/// In progress requests alongside the jobs associated with the request
403-
pub in_progress: Vec<(BenchmarkRequest, Vec<BenchmarkJob>)>,
473+
pub in_progress: Vec<BenchmarkInProgressUi>,
404474
/// Configuration for all collectors including ones that are inactive
405-
pub collector_configs: Vec<CollectorConfig>,
475+
pub collector_configs: Vec<CollectorConfigUi>,
406476
/// The current queue
407-
pub queue: Vec<BenchmarkRequest>,
477+
pub queue: Vec<BenchmarkRequestUi>,
408478
}
409479
}
410480

Lines changed: 125 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,107 @@
11
use std::sync::Arc;
22

3+
use crate::api::status_new::{
4+
BenchmarkInProgressUi, BenchmarkJobStatusUi, BenchmarkJobUi, BenchmarkRequestStatusUi,
5+
BenchmarkRequestTypeUi, BenchmarkRequestUi, CollectorConfigUi,
6+
};
37
use crate::api::{status_new, ServerResult};
48
use crate::job_queue::build_queue;
59
use crate::load::SiteCtxt;
10+
use database::{
11+
BenchmarkJob, BenchmarkJobStatus, BenchmarkRequest, BenchmarkRequestStatus, CollectorConfig,
12+
};
13+
14+
fn benchmark_request_status_to_ui(status: BenchmarkRequestStatus) -> BenchmarkRequestStatusUi {
15+
let (completed_at, duration) = match status {
16+
BenchmarkRequestStatus::Completed {
17+
duration,
18+
completed_at,
19+
} => (Some(completed_at), u32::try_from(duration.as_millis()).ok()),
20+
_ => (None, None),
21+
};
22+
23+
BenchmarkRequestStatusUi {
24+
state: status.as_str().to_owned(),
25+
completed_at,
26+
duration,
27+
}
28+
}
29+
30+
fn benchmark_request_type_to_ui(req: &BenchmarkRequest) -> BenchmarkRequestTypeUi {
31+
BenchmarkRequestTypeUi {
32+
r#type: match (req.is_release(), req.is_master()) {
33+
(true, _) => "Release",
34+
(_, true) => "Master",
35+
_ => "Try",
36+
}
37+
.to_owned(),
38+
tag: req.tag().map(|it| it.to_owned()),
39+
parent_sha: req.parent_sha().map(|it| it.to_owned()),
40+
pr: req.pr().copied(),
41+
}
42+
}
43+
44+
fn benchmark_request_to_ui(
45+
req: &BenchmarkRequest,
46+
errors: Vec<String>,
47+
) -> anyhow::Result<BenchmarkRequestUi> {
48+
Ok(BenchmarkRequestUi {
49+
status: benchmark_request_status_to_ui(req.status()),
50+
request_type: benchmark_request_type_to_ui(req),
51+
commit_date: req.commit_date(),
52+
created_at: req.created_at(),
53+
backends: req.backends()?.iter().map(|it| it.to_string()).collect(),
54+
profiles: req.profiles()?.iter().map(|it| it.to_string()).collect(),
55+
errors,
56+
})
57+
}
58+
59+
fn benchmark_job_status_to_ui(status: &BenchmarkJobStatus) -> BenchmarkJobStatusUi {
60+
let (started_at, completed_at, collector_name_ref) = match status {
61+
BenchmarkJobStatus::Queued => (None, None, None),
62+
BenchmarkJobStatus::InProgress {
63+
started_at,
64+
collector_name,
65+
} => (Some(*started_at), None, Some(collector_name)),
66+
BenchmarkJobStatus::Completed {
67+
started_at,
68+
completed_at,
69+
collector_name,
70+
..
71+
} => (Some(*started_at), Some(*completed_at), Some(collector_name)),
72+
};
73+
74+
BenchmarkJobStatusUi {
75+
state: status.as_str().to_owned(),
76+
started_at,
77+
completed_at,
78+
collector_name: collector_name_ref.cloned(),
79+
}
80+
}
81+
82+
fn benchmark_job_to_ui(job: &BenchmarkJob) -> BenchmarkJobUi {
83+
BenchmarkJobUi {
84+
target: job.target().as_str().to_owned(),
85+
backend: job.backend().as_str().to_owned(),
86+
profile: job.profile().as_str().to_owned(),
87+
request_tag: job.request_tag().to_owned(),
88+
benchmark_set: job.benchmark_set(),
89+
created_at: job.created_at(),
90+
status: benchmark_job_status_to_ui(job.status()),
91+
deque_counter: job.deque_count(),
92+
}
93+
}
94+
95+
fn collector_config_to_ui(config: &CollectorConfig) -> CollectorConfigUi {
96+
CollectorConfigUi {
97+
name: config.name().to_owned(),
98+
target: config.target().as_str().to_owned(),
99+
benchmark_set: config.benchmark_set(),
100+
is_active: config.is_active(),
101+
last_heartbeat_at: config.last_heartbeat_at(),
102+
date_added: config.date_added(),
103+
}
104+
}
6105

7106
pub async fn handle_status_page_new(ctxt: Arc<SiteCtxt>) -> ServerResult<status_new::Response> {
8107
let conn = ctxt.conn().await;
@@ -12,11 +111,14 @@ pub async fn handle_status_page_new(ctxt: Arc<SiteCtxt>) -> ServerResult<status_
12111
let collector_configs = conn
13112
.get_collector_configs()
14113
.await
15-
.map_err(error_to_string)?;
114+
.map_err(error_to_string)?
115+
.iter()
116+
.map(collector_config_to_ui)
117+
.collect();
16118
// The query gives us `max_completed_requests` number of completed requests
17119
// and all inprogress requests without us needing to specify
18120
//
19-
// TODO; for `in_progress` requests we could look at the the completed
121+
// @TODO; for `in_progress` requests we could look at the the completed
20122
// `requests`, then use the `duration_ms` to display an estimated job
21123
// finish time. Could also do that on the frontend but probably makes
22124
// sense to do in SQL.
@@ -31,15 +133,28 @@ pub async fn handle_status_page_new(ctxt: Arc<SiteCtxt>) -> ServerResult<status_
31133
// @TODO; do we need both the queue and the inprogress jobs from the database?
32134
let queue = build_queue(&*conn, &index).await.map_err(error_to_string)?;
33135

136+
let mut completed: Vec<BenchmarkRequestUi> = vec![];
137+
for it in partial_data.completed_requests {
138+
completed.push(benchmark_request_to_ui(&it.0, it.1).map_err(error_to_string)?);
139+
}
140+
141+
let mut in_progress: Vec<BenchmarkInProgressUi> = vec![];
142+
for it in partial_data.in_progress {
143+
in_progress.push(BenchmarkInProgressUi {
144+
request: benchmark_request_to_ui(&it.0, vec![]).map_err(error_to_string)?,
145+
jobs: it.1.iter().map(benchmark_job_to_ui).collect(),
146+
});
147+
}
148+
149+
let mut queue_ui: Vec<BenchmarkRequestUi> = vec![];
150+
for it in queue {
151+
queue_ui.push(benchmark_request_to_ui(&it, vec![]).map_err(error_to_string)?);
152+
}
153+
34154
Ok(status_new::Response {
35-
completed: partial_data
36-
.completed_requests
37-
.iter()
38-
// @TODO Remove this
39-
.map(|it| (it.0.clone(), it.2.clone()))
40-
.collect(),
41-
in_progress: partial_data.in_progress,
155+
completed,
156+
in_progress,
42157
collector_configs,
43-
queue,
158+
queue: queue_ui,
44159
})
45160
}

0 commit comments

Comments
 (0)