Skip to content

Commit 7ed913b

Browse files
committed
Add cache for downloading job metrics
To make it easier to experiment locally.
1 parent 2192d5c commit 7ed913b

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/ci/citool/src/merge_report.rs

+17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::cmp::Reverse;
22
use std::collections::HashMap;
3+
use std::path::PathBuf;
34

45
use anyhow::Context;
56
use build_helper::metrics::{JsonRoot, TestOutcome};
@@ -56,7 +57,16 @@ Maybe it was newly added?"#,
5657
Ok(jobs)
5758
}
5859

60+
/// Downloads job metrics of the given job for the given commit.
61+
/// Caches the result on the local disk.
5962
fn download_job_metrics(job_name: &str, sha: &str) -> anyhow::Result<JsonRoot> {
63+
let cache_path = PathBuf::from(".citool-cache").join(sha).join(job_name).join("metrics.json");
64+
if let Some(cache_entry) =
65+
std::fs::read_to_string(&cache_path).ok().and_then(|data| serde_json::from_str(&data).ok())
66+
{
67+
return Ok(cache_entry);
68+
}
69+
6070
let url = get_metrics_url(job_name, sha);
6171
let mut response = ureq::get(&url).call()?;
6272
if !response.status().is_success() {
@@ -70,6 +80,13 @@ fn download_job_metrics(job_name: &str, sha: &str) -> anyhow::Result<JsonRoot> {
7080
.body_mut()
7181
.read_json()
7282
.with_context(|| anyhow::anyhow!("cannot deserialize metrics from {url}"))?;
83+
84+
// Ignore errors if cache cannot be created
85+
if std::fs::create_dir_all(cache_path.parent().unwrap()).is_ok() {
86+
if let Ok(serialized) = serde_json::to_string(&data) {
87+
let _ = std::fs::write(&cache_path, &serialized);
88+
}
89+
}
7390
Ok(data)
7491
}
7592

0 commit comments

Comments
 (0)