File tree 1 file changed +19
-1
lines changed
1 file changed +19
-1
lines changed Original file line number Diff line number Diff line change 1
1
use std:: collections:: HashMap ;
2
- use std:: path:: Path ;
2
+ use std:: path:: { Path , PathBuf } ;
3
3
4
4
use anyhow:: Context ;
5
5
use build_helper:: metrics:: { JsonNode , JsonRoot , TestSuite } ;
@@ -74,6 +74,17 @@ Maybe it was newly added?"#,
74
74
}
75
75
76
76
pub fn download_job_metrics ( job_name : & str , sha : & str ) -> anyhow:: Result < JsonRoot > {
77
+ // Best effort cache to speed-up local re-executions of citool
78
+ let cache_path = PathBuf :: from ( ".citool-cache" ) . join ( sha) . join ( format ! ( "{job_name}.json" ) ) ;
79
+ if cache_path. is_file ( ) {
80
+ if let Ok ( metrics) = std:: fs:: read_to_string ( & cache_path)
81
+ . map_err ( |err| err. into ( ) )
82
+ . and_then ( |data| anyhow:: Ok :: < JsonRoot > ( serde_json:: from_str :: < JsonRoot > ( & data) ?) )
83
+ {
84
+ return Ok ( metrics) ;
85
+ }
86
+ }
87
+
77
88
let url = get_metrics_url ( job_name, sha) ;
78
89
let mut response = ureq:: get ( & url) . call ( ) ?;
79
90
if !response. status ( ) . is_success ( ) {
@@ -87,6 +98,13 @@ pub fn download_job_metrics(job_name: &str, sha: &str) -> anyhow::Result<JsonRoo
87
98
. body_mut ( )
88
99
. read_json ( )
89
100
. with_context ( || anyhow:: anyhow!( "cannot deserialize metrics from {url}" ) ) ?;
101
+
102
+ if let Ok ( _) = std:: fs:: create_dir_all ( cache_path. parent ( ) . unwrap ( ) ) {
103
+ if let Ok ( data) = serde_json:: to_string ( & data) {
104
+ let _ = std:: fs:: write ( cache_path, data) ;
105
+ }
106
+ }
107
+
90
108
Ok ( data)
91
109
}
92
110
You can’t perform that action at this time.
0 commit comments