Skip to content

Commit 9ed1928

Browse files
committed
context
1 parent babc38e commit 9ed1928

File tree

7 files changed

+82
-10
lines changed

7 files changed

+82
-10
lines changed

lib/bencher_client/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ macro_rules! from_client {
3232
}
3333

3434
from_client!(
35+
JsonReportContext,
3536
BenchmarkName,
3637
Boundary,
3738
BranchName,

lib/bencher_json/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub use project::{
4848
model::{JsonModel, ModelUuid},
4949
perf::{JsonPerf, JsonPerfQuery, ReportBenchmarkUuid},
5050
plot::{JsonNewPlot, JsonPlot, JsonPlots, PlotUuid},
51-
report::{JsonNewReport, JsonReport, JsonReports, ReportUuid},
51+
report::{JsonNewReport, JsonReport, JsonReportContext, JsonReports, ReportUuid},
5252
testbed::{JsonNewTestbed, JsonTestbed, JsonTestbeds, TestbedUuid},
5353
threshold::{JsonNewThreshold, JsonThreshold, JsonThresholds, ThresholdUuid},
5454
JsonNewProject, JsonProject, JsonProjects, ProjectUuid,

lib/bencher_json/src/project/report.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ pub struct JsonNewReport {
5151
pub results: Vec<String>,
5252
/// Settings for how to handle the report.
5353
pub settings: Option<JsonReportSettings>,
54+
/// Context for the report.
55+
pub context: Option<JsonReportContext>,
5456
}
5557

5658
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -80,6 +82,31 @@ pub struct JsonReportSettings {
8082
pub fold: Option<JsonFold>,
8183
}
8284

85+
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
86+
#[cfg_attr(feature = "schema", derive(JsonSchema))]
87+
pub struct JsonReportContext(pub HashMap<String, Option<String>>);
88+
89+
impl JsonReportContext {
90+
pub fn new() -> Self {
91+
Self::default()
92+
}
93+
94+
pub fn insert(&mut self, key: String, value: Option<String>) -> Option<Option<String>> {
95+
self.0.insert(key, value)
96+
}
97+
98+
pub fn remove(&mut self, key: &str) -> Option<Option<String>> {
99+
self.0.remove(key)
100+
}
101+
}
102+
103+
#[allow(clippy::implicit_hasher)]
104+
impl From<JsonReportContext> for HashMap<String, Option<String>> {
105+
fn from(context: JsonReportContext) -> Self {
106+
context.0
107+
}
108+
}
109+
83110
const MAGIC_INT: i32 = 0;
84111
const JSON_INT: i32 = 10;
85112
const RUST_INT: i32 = 20;

services/api/openapi.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9387,6 +9387,15 @@
93879387
}
93889388
]
93899389
},
9390+
"context": {
9391+
"nullable": true,
9392+
"description": "Context for the report.",
9393+
"allOf": [
9394+
{
9395+
"$ref": "#/components/schemas/JsonReportContext"
9396+
}
9397+
]
9398+
},
93909399
"end_time": {
93919400
"description": "End time for the report. Must be an ISO 8601 formatted string.",
93929401
"allOf": [
@@ -10731,6 +10740,13 @@
1073110740
"uuid"
1073210741
]
1073310742
},
10743+
"JsonReportContext": {
10744+
"type": "object",
10745+
"additionalProperties": {
10746+
"nullable": true,
10747+
"type": "string"
10748+
}
10749+
},
1073410750
"JsonReportMeasure": {
1073510751
"type": "object",
1073610752
"properties": {

services/cli/src/bencher/sub/project/report/create.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ impl From<Create> for JsonNewReport {
9999
average,
100100
fold,
101101
}),
102+
context: None,
102103
}
103104
}
104105
}

services/cli/src/bencher/sub/project/run/context.rs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,50 @@
1-
use bencher_json::{Fingerprint, GitHash};
1+
use bencher_json::{project::report::JsonReportContext, Fingerprint, GitHash};
22
use gix::Repository;
33

44
use super::branch::find_repo;
55

66
const ROOT: &str = "root";
77

8-
pub fn context() {
8+
pub fn get_context() -> Option<JsonReportContext> {
9+
let mut context = JsonReportContext::new();
10+
911
let repo = find_repo();
1012
let repo_name = repo_name(repo.as_ref());
1113
if let Some(repo_name) = repo_name {
12-
println!("{repo_name}");
14+
context.insert("bencher.dev/repo/name".to_owned(), Some(repo_name));
15+
}
16+
if let Some(repo) = &repo {
17+
if let Some(root_commit) = find_default_branch_and_root_commit(&repo) {
18+
context.insert(
19+
"bencher.dev/repo/hash".to_owned(),
20+
Some(root_commit.to_string()),
21+
);
22+
}
1323
}
1424

1525
if let Some(repo) = repo {
16-
if let Some(branch) = current_branch_name(&repo) {
17-
println!("{branch:?}");
26+
if let Some((branch_ref, branch_ref_name)) = current_branch_name(&repo) {
27+
context.insert("bencher.dev/branch/ref".to_owned(), Some(branch_ref));
28+
context.insert(
29+
"bencher.dev/branch/ref/name".to_owned(),
30+
Some(branch_ref_name),
31+
);
1832
}
1933

20-
if let Some(root_commit) = find_default_branch_and_root_commit(&repo) {
21-
println!("Root commit hash: {root_commit:?}");
34+
if let Some(hash) = current_branch_hash(&repo) {
35+
context.insert("bencher.dev/branch/hash".to_owned(), Some(hash));
2236
}
2337
}
2438

2539
let fingerprint = Fingerprint::new();
2640
if let Some(fingerprint) = fingerprint {
27-
println!("{fingerprint}");
41+
context.insert(
42+
"bencher.dev/testbed/fingerprint".to_owned(),
43+
Some(fingerprint.to_string()),
44+
);
2845
}
46+
47+
Some(context)
2948
}
3049

3150
fn repo_name(repo: Option<&Repository>) -> Option<String> {
@@ -46,6 +65,12 @@ fn current_branch_name(repo: &Repository) -> Option<(String, String)> {
4665
})
4766
}
4867

68+
fn current_branch_hash(repo: &Repository) -> Option<String> {
69+
let head_id = repo.head_id().ok()?;
70+
let head_object = head_id.object().ok()?;
71+
Some(head_object.id.to_string())
72+
}
73+
4974
fn find_default_branch_and_root_commit(repo: &Repository) -> Option<GitHash> {
5075
let head_id = repo.head_id().ok()?;
5176
let rev_walk = repo.rev_walk([head_id]).all().ok()?;

services/cli/src/bencher/sub/project/run/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,9 @@ impl Run {
187187
(start_time, end_time)
188188
};
189189

190-
context::context();
191190
let (branch, hash, start_point) = self.branch.clone().into();
191+
let context = context::get_context();
192+
println!("{context:?}");
192193
Ok(Some(JsonNewReport {
193194
branch,
194195
hash,
@@ -203,6 +204,7 @@ impl Run {
203204
average: self.average,
204205
fold: self.fold,
205206
}),
207+
context: context.map(Into::into),
206208
}))
207209
}
208210

0 commit comments

Comments
 (0)