1
+ use async_graphql:: SimpleObject ;
1
2
use camino:: Utf8Path ;
2
- use sqlx:: { Pool , Sqlite , SqlitePool } ;
3
+ use sqlx:: { sqlite :: SqliteRow , Pool , Row , Sqlite , SqlitePool } ;
3
4
use thiserror:: Error ;
4
5
use turbopath:: { AbsoluteSystemPath , AbsoluteSystemPathBuf } ;
5
- use turborepo_api_client:: spaces:: { CreateSpaceRunPayload , RunStatus , SpaceTaskSummary } ;
6
+ use turborepo_api_client:: spaces:: {
7
+ CreateSpaceRunPayload , RunStatus , SpaceTaskSummary , SpacesCacheStatus ,
8
+ } ;
6
9
use uuid:: Uuid ;
7
10
8
11
#[ derive( Debug , Error ) ]
@@ -15,6 +18,24 @@ pub enum Error {
15
18
Serialize ( #[ from] serde_json:: Error ) ,
16
19
}
17
20
21
+ #[ derive( Debug , Default , SimpleObject ) ]
22
+ pub struct Run {
23
+ id : String ,
24
+ start_time : i64 ,
25
+ end_time : Option < i64 > ,
26
+ exit_code : Option < u32 > ,
27
+ status : String ,
28
+ command : String ,
29
+ package_inference_root : Option < String > ,
30
+ context : String ,
31
+ git_branch : Option < String > ,
32
+ git_sha : Option < String > ,
33
+ origination_user : String ,
34
+ client_id : String ,
35
+ client_name : String ,
36
+ client_version : String ,
37
+ }
38
+
18
39
#[ derive( Clone ) ]
19
40
pub struct DatabaseHandle {
20
41
pool : Pool < Sqlite > ,
@@ -34,6 +55,71 @@ impl DatabaseHandle {
34
55
Ok ( Self { pool } )
35
56
}
36
57
58
+ pub async fn get_runs ( & self , limit : Option < u32 > ) -> Result < Vec < Run > , Error > {
59
+ let query = if let Some ( limit) = limit {
60
+ sqlx:: query (
61
+ "SELECT id, start_time, end_time, exit_code, status, command, \
62
+ package_inference_root, context, git_branch, git_sha, origination_user, \
63
+ client_id, client_name, client_version FROM runs LIMIT ?",
64
+ )
65
+ . bind ( limit)
66
+ } else {
67
+ sqlx:: query (
68
+ "SELECT id, start_time, end_time, exit_code, status, command, \
69
+ package_inference_root, context, git_branch, git_sha, origination_user, \
70
+ client_id, client_name, client_version FROM runs",
71
+ )
72
+ } ;
73
+
74
+ Ok ( query
75
+ . map ( |row| Run {
76
+ id : row. get ( "id" ) ,
77
+ start_time : row. get ( "start_time" ) ,
78
+ end_time : row. get ( "end_time" ) ,
79
+ exit_code : row. get ( "exit_code" ) ,
80
+ status : row. get ( "status" ) ,
81
+ command : row. get ( "command" ) ,
82
+ package_inference_root : row. get ( "package_inference_root" ) ,
83
+ context : row. get ( "context" ) ,
84
+ git_branch : row. get ( "git_branch" ) ,
85
+ git_sha : row. get ( "git_sha" ) ,
86
+ origination_user : row. get ( "origination_user" ) ,
87
+ client_id : row. get ( "client_id" ) ,
88
+ client_name : row. get ( "client_name" ) ,
89
+ client_version : row. get ( "client_version" ) ,
90
+ } )
91
+ . fetch_all ( & self . pool )
92
+ . await ?)
93
+ }
94
+
95
+ pub async fn get_tasks_for_run ( & self , run_id : Uuid ) -> Result < Vec < SpaceTaskSummary > , Error > {
96
+ let query = sqlx:: query (
97
+ "SELECT key, name, workspace, hash, start_time, end_time, cache_status, exit_code, \
98
+ logs FROM tasks WHERE run_id = ?",
99
+ )
100
+ . bind ( run_id. to_string ( ) ) ;
101
+ Ok ( query
102
+ . map ( |row : SqliteRow | SpaceTaskSummary {
103
+ key : row. get ( "key" ) ,
104
+ name : row. get ( "name" ) ,
105
+ workspace : row. get ( "workspace" ) ,
106
+ hash : row. get ( "hash" ) ,
107
+ start_time : row. get ( "start_time" ) ,
108
+ end_time : row. get ( "end_time" ) ,
109
+ cache : SpacesCacheStatus {
110
+ status : row. get ( "cache_status" ) ,
111
+ source : None ,
112
+ time_saved : row. get ( "time_saved" ) ,
113
+ } ,
114
+ exit_code : row. get ( "exit_code" ) ,
115
+ dependencies : row. get ( "dependencies" ) ,
116
+ dependents : row. get ( "dependents" ) ,
117
+ logs : row. get ( "logs" ) ,
118
+ } )
119
+ . fetch_all ( & self . pool )
120
+ . await ?)
121
+ }
122
+
37
123
pub async fn create_run ( & self , payload : & CreateSpaceRunPayload ) -> Result < Uuid , Error > {
38
124
let id = Uuid :: new_v4 ( ) ;
39
125
sqlx:: query (
@@ -111,3 +197,75 @@ impl DatabaseHandle {
111
197
Ok ( ( ) )
112
198
}
113
199
}
200
+
201
+ #[ cfg( test) ]
202
+ mod test {
203
+ use turbopath:: AbsoluteSystemPath ;
204
+ use turborepo_api_client:: spaces:: {
205
+ CacheStatus , CreateSpaceRunPayload , RunStatus , SpaceClientSummary , SpaceRunType ,
206
+ SpaceTaskSummary , SpacesCacheStatus ,
207
+ } ;
208
+
209
+ use crate :: DatabaseHandle ;
210
+
211
+ #[ tokio:: test]
212
+ async fn test_get_runs ( ) -> Result < ( ) , anyhow:: Error > {
213
+ let dir = tempfile:: tempdir ( ) . unwrap ( ) ;
214
+
215
+ let db = DatabaseHandle :: new (
216
+ dir. path ( ) . try_into ( ) ?,
217
+ AbsoluteSystemPath :: from_std_path ( dir. path ( ) ) ?,
218
+ )
219
+ . await ?;
220
+
221
+ let id = db
222
+ . create_run ( & CreateSpaceRunPayload {
223
+ start_time : 0 ,
224
+ status : RunStatus :: Running ,
225
+ command : "test" . to_string ( ) ,
226
+ package_inference_root : "test" . to_string ( ) ,
227
+ run_context : "" ,
228
+ git_branch : None ,
229
+ git_sha : None ,
230
+ ty : SpaceRunType :: Turbo ,
231
+ user : "test" . to_string ( ) ,
232
+ client : SpaceClientSummary {
233
+ id : "my-id" ,
234
+ name : "turbo" ,
235
+ version : "1.0.0" . to_string ( ) ,
236
+ } ,
237
+ } )
238
+ . await
239
+ . unwrap ( ) ;
240
+ let runs = db. get_runs ( None ) . await . unwrap ( ) ;
241
+ assert_eq ! ( runs. len( ) , 1 ) ;
242
+ assert_eq ! ( runs[ 0 ] . id. len( ) , 36 ) ;
243
+ assert_eq ! ( runs[ 0 ] . git_sha, Some ( "test" . to_string( ) ) ) ;
244
+ assert_eq ! ( runs[ 0 ] . status, "RUNNING" . to_string( ) ) ;
245
+
246
+ db. finish_task (
247
+ id. clone ( ) ,
248
+ & SpaceTaskSummary {
249
+ key : "test#build" . to_string ( ) ,
250
+ name : "test" . to_string ( ) ,
251
+ workspace : "test" . to_string ( ) ,
252
+ hash : "test" . to_string ( ) ,
253
+ start_time : 0 ,
254
+ end_time : 0 ,
255
+ cache : SpacesCacheStatus {
256
+ status : CacheStatus :: Miss ,
257
+ source : None ,
258
+ time_saved : 0 ,
259
+ } ,
260
+ exit_code : Some ( 0 ) ,
261
+ dependencies : vec ! [ ] ,
262
+ dependents : vec ! [ ] ,
263
+ logs : "" . to_string ( ) ,
264
+ } ,
265
+ )
266
+ . await
267
+ . unwrap ( ) ;
268
+
269
+ Ok ( ( ) )
270
+ }
271
+ }
0 commit comments