11use std:: sync:: Arc ;
22
3+ use crate :: api:: status_new:: {
4+ BenchmarkInProgressUi , BenchmarkJobStatusUi , BenchmarkJobUi , BenchmarkRequestStatusUi ,
5+ BenchmarkRequestTypeUi , BenchmarkRequestUi , CollectorConfigUi ,
6+ } ;
37use crate :: api:: { status_new, ServerResult } ;
48use crate :: job_queue:: build_queue;
59use 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+ BenchmarkRequestStatusUi {
16+ state : status. as_str ( ) . to_string ( ) ,
17+ completed_at : match status {
18+ BenchmarkRequestStatus :: WaitingForArtifacts => None ,
19+ BenchmarkRequestStatus :: ArtifactsReady => None ,
20+ BenchmarkRequestStatus :: InProgress => None ,
21+ BenchmarkRequestStatus :: Completed { completed_at } => Some ( completed_at) ,
22+ } ,
23+ }
24+ }
25+
26+ fn benchmark_request_type_to_ui ( req : & BenchmarkRequest ) -> BenchmarkRequestTypeUi {
27+ let request_type_str = if req. is_release ( ) {
28+ "Release" . to_string ( )
29+ } else if req. is_master ( ) {
30+ "Master" . to_string ( )
31+ } else {
32+ "Try" . to_string ( )
33+ } ;
34+
35+ BenchmarkRequestTypeUi {
36+ r#type : request_type_str,
37+ tag : req. tag ( ) . map ( |it| it. to_string ( ) ) ,
38+ parent_sha : req. parent_sha ( ) . map ( |it| it. to_string ( ) ) ,
39+ pr : req. pr ( ) . map ( |it| * it) ,
40+ }
41+ }
42+
43+ fn benchmark_request_to_ui (
44+ req : & BenchmarkRequest ,
45+ errors : Vec < String > ,
46+ ) -> anyhow:: Result < BenchmarkRequestUi > {
47+ Ok ( BenchmarkRequestUi {
48+ status : benchmark_request_status_to_ui ( req. status ( ) ) ,
49+ request_type : benchmark_request_type_to_ui ( & req) ,
50+ commit_date : req. commit_date ( ) ,
51+ created_at : req. created_at ( ) ,
52+ backends : req. backends ( ) ?. iter ( ) . map ( |it| it. to_string ( ) ) . collect ( ) ,
53+ profiles : req. profiles ( ) ?. iter ( ) . map ( |it| it. to_string ( ) ) . collect ( ) ,
54+ errors,
55+ } )
56+ }
57+
58+ fn benchmark_job_status_to_ui ( status : & BenchmarkJobStatus ) -> BenchmarkJobStatusUi {
59+ match status {
60+ BenchmarkJobStatus :: Queued => BenchmarkJobStatusUi {
61+ state : status. as_str ( ) . to_string ( ) ,
62+ started_at : None ,
63+ completed_at : None ,
64+ collector_name : None ,
65+ } ,
66+ BenchmarkJobStatus :: InProgress {
67+ started_at,
68+ collector_name,
69+ } => BenchmarkJobStatusUi {
70+ state : status. as_str ( ) . to_string ( ) ,
71+ started_at : Some ( * started_at) ,
72+ completed_at : None ,
73+ collector_name : Some ( collector_name. clone ( ) ) ,
74+ } ,
75+ BenchmarkJobStatus :: Completed {
76+ started_at,
77+ completed_at,
78+ collector_name,
79+ success : _,
80+ } => BenchmarkJobStatusUi {
81+ state : status. as_str ( ) . to_string ( ) ,
82+ started_at : Some ( * started_at) ,
83+ completed_at : Some ( * completed_at) ,
84+ collector_name : Some ( collector_name. clone ( ) ) ,
85+ } ,
86+ }
87+ }
88+
89+ fn benchmark_job_to_ui ( job : & BenchmarkJob ) -> BenchmarkJobUi {
90+ BenchmarkJobUi {
91+ target : job. target ( ) . as_str ( ) . to_string ( ) ,
92+ backend : job. backend ( ) . as_str ( ) . to_string ( ) ,
93+ profile : job. profile ( ) . as_str ( ) . to_string ( ) ,
94+ request_tag : job. request_tag ( ) . to_string ( ) ,
95+ benchmark_set : job. benchmark_set ( ) . 0 ,
96+ created_at : job. created_at ( ) ,
97+ status : benchmark_job_status_to_ui ( & job. status ( ) ) ,
98+ deque_counter : job. deque_count ( ) ,
99+ }
100+ }
101+
102+ fn collector_config_to_ui ( config : & CollectorConfig ) -> CollectorConfigUi {
103+ CollectorConfigUi {
104+ name : config. name ( ) . to_string ( ) ,
105+ target : config. target ( ) . as_str ( ) . to_string ( ) ,
106+ benchmark_set : config. benchmark_set ( ) . 0 ,
107+ is_active : config. is_active ( ) ,
108+ last_heartbeat_at : config. last_heartbeat_at ( ) ,
109+ date_added : config. date_added ( ) ,
110+ }
111+ }
6112
7113pub async fn handle_status_page_new ( ctxt : Arc < SiteCtxt > ) -> ServerResult < status_new:: Response > {
8114 let conn = ctxt. conn ( ) . await ;
@@ -12,11 +118,14 @@ pub async fn handle_status_page_new(ctxt: Arc<SiteCtxt>) -> ServerResult<status_
12118 let collector_configs = conn
13119 . get_collector_configs ( )
14120 . await
15- . map_err ( error_to_string) ?;
121+ . map_err ( error_to_string) ?
122+ . iter ( )
123+ . map ( collector_config_to_ui)
124+ . collect ( ) ;
16125 // The query gives us `max_completed_requests` number of completed requests
17126 // and all inprogress requests without us needing to specify
18127 //
19- // TODO; for `in_progress` requests we could look at the the completed
128+ // @ TODO; for `in_progress` requests we could look at the the completed
20129 // `requests`, then use the `duration_ms` to display an estimated job
21130 // finish time. Could also do that on the frontend but probably makes
22131 // sense to do in SQL.
@@ -31,15 +140,28 @@ pub async fn handle_status_page_new(ctxt: Arc<SiteCtxt>) -> ServerResult<status_
31140 // @TODO; do we need both the queue and the inprogress jobs from the database?
32141 let queue = build_queue ( & * conn, & index) . await . map_err ( error_to_string) ?;
33142
143+ let mut completed: Vec < BenchmarkRequestUi > = vec ! [ ] ;
144+ for it in partial_data. completed_requests {
145+ completed. push ( benchmark_request_to_ui ( & it. 0 , it. 2 ) . map_err ( error_to_string) ?) ;
146+ }
147+
148+ let mut in_progress: Vec < BenchmarkInProgressUi > = vec ! [ ] ;
149+ for it in partial_data. in_progress {
150+ in_progress. push ( BenchmarkInProgressUi {
151+ request : benchmark_request_to_ui ( & it. 0 , vec ! [ ] ) . map_err ( error_to_string) ?,
152+ jobs : it. 1 . iter ( ) . map ( benchmark_job_to_ui) . collect ( ) ,
153+ } ) ;
154+ }
155+
156+ let mut queue_ui: Vec < BenchmarkRequestUi > = vec ! [ ] ;
157+ for it in queue {
158+ queue_ui. push ( benchmark_request_to_ui ( & it, vec ! [ ] ) . map_err ( error_to_string) ?) ;
159+ }
160+
34161 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 ,
162+ completed,
163+ in_progress,
42164 collector_configs,
43- queue,
165+ queue : queue_ui ,
44166 } )
45167}
0 commit comments