Skip to content

Commit 4a43675

Browse files
committed
Add cache for job metrics
1 parent a3bafca commit 4a43675

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/ci/citool/src/metrics.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::collections::HashMap;
2-
use std::path::Path;
2+
use std::path::{Path, PathBuf};
33

44
use anyhow::Context;
55
use build_helper::metrics::{JsonNode, JsonRoot, TestSuite};
@@ -74,6 +74,17 @@ Maybe it was newly added?"#,
7474
}
7575

7676
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+
7788
let url = get_metrics_url(job_name, sha);
7889
let mut response = ureq::get(&url).call()?;
7990
if !response.status().is_success() {
@@ -87,6 +98,13 @@ pub fn download_job_metrics(job_name: &str, sha: &str) -> anyhow::Result<JsonRoo
8798
.body_mut()
8899
.read_json()
89100
.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+
90108
Ok(data)
91109
}
92110

0 commit comments

Comments
 (0)