|
| 1 | +use bencher_context::ReportContext; |
| 2 | +use bencher_valid::{DateTime, GitHash, ResourceId}; |
| 3 | +#[cfg(feature = "schema")] |
| 4 | +use schemars::JsonSchema; |
| 5 | +use serde::{Deserialize, Serialize}; |
| 6 | + |
| 7 | +use crate::{ |
| 8 | + project::{ |
| 9 | + branch::{JsonUpdateStartPoint, DEFAULT_BRANCH}, |
| 10 | + report::{JsonReportSettings, JsonReportThresholds}, |
| 11 | + testbed::DEFAULT_TESTBED, |
| 12 | + }, |
| 13 | + JsonNewReport, NameId, |
| 14 | +}; |
| 15 | + |
| 16 | +#[derive(Debug, Serialize, Deserialize)] |
| 17 | +#[cfg_attr(feature = "schema", derive(JsonSchema))] |
| 18 | +pub struct JsonNewRun { |
| 19 | + /// Organization UUID or slug. |
| 20 | + /// If the organization is not provided, it will be created. |
| 21 | + pub organization: Option<ResourceId>, |
| 22 | + /// Project UUID, slug, or name. |
| 23 | + /// If the project is not provided or does not exist, it will be created. |
| 24 | + /// If a name is provided, the `organization` field must also be provided. |
| 25 | + pub project: Option<NameId>, |
| 26 | + /// Branch UUID, slug, or name. |
| 27 | + /// If the branch is not provided or does not exist, it will be created. |
| 28 | + pub branch: Option<NameId>, |
| 29 | + /// Full `git` commit hash. |
| 30 | + /// All reports with the same `git` commit hash will be considered part of the same branch version. |
| 31 | + /// This can be useful for tracking the performance of a specific commit across multiple testbeds. |
| 32 | + pub hash: Option<GitHash>, |
| 33 | + /// The start point for the report branch. |
| 34 | + /// If the branch does not exist, the start point will be used to create a new branch. |
| 35 | + /// If the branch already exists and the start point is not provided, the current branch will be used. |
| 36 | + /// If the branch already exists and the start point provided is different, a new branch head will be created from the new start point. |
| 37 | + /// If a new branch or new branch head is created with a start point, |
| 38 | + /// historical branch versions from the start point branch will be shallow copied over to the new branch. |
| 39 | + /// That is, historical metrics data for the start point branch will appear in queries for the branch. |
| 40 | + /// For example, pull request branches often use their base branch as their start point branch. |
| 41 | + /// If a new branch is created, it is not kept in sync with the start point branch. |
| 42 | + pub start_point: Option<JsonUpdateStartPoint>, |
| 43 | + /// Testbed UUID, slug, or name. |
| 44 | + /// If the testbed is not provided or does not exist, it will be created. |
| 45 | + pub testbed: Option<NameId>, |
| 46 | + /// Thresholds to use for the branch, testbed, and measures in the report. |
| 47 | + /// If a threshold does not exist, it will be created. |
| 48 | + /// If a threshold exists and the model is different, it will be updated with the new model. |
| 49 | + /// If a measure name or slug is provided, the measure will be created if it does not exist. |
| 50 | + pub thresholds: Option<JsonReportThresholds>, |
| 51 | + /// Start time for the report. Must be an ISO 8601 formatted string. |
| 52 | + pub start_time: DateTime, |
| 53 | + /// End time for the report. Must be an ISO 8601 formatted string. |
| 54 | + pub end_time: DateTime, |
| 55 | + /// An array of benchmarks results. |
| 56 | + pub results: Vec<String>, |
| 57 | + /// Settings for how to handle the results. |
| 58 | + pub settings: Option<JsonReportSettings>, |
| 59 | + /// Context for the report. |
| 60 | + pub context: Option<ReportContext>, |
| 61 | +} |
| 62 | + |
| 63 | +impl From<JsonNewRun> for JsonNewReport { |
| 64 | + fn from(run: JsonNewRun) -> Self { |
| 65 | + let JsonNewRun { |
| 66 | + organization: _, |
| 67 | + project: _, |
| 68 | + branch, |
| 69 | + hash, |
| 70 | + start_point, |
| 71 | + testbed, |
| 72 | + thresholds, |
| 73 | + start_time, |
| 74 | + end_time, |
| 75 | + results, |
| 76 | + settings, |
| 77 | + context, |
| 78 | + } = run; |
| 79 | + let branch = branch |
| 80 | + .or_else(|| { |
| 81 | + context |
| 82 | + .as_ref() |
| 83 | + .and_then(|ctx| ctx.branch_ref_name().and_then(|branch| branch.parse().ok())) |
| 84 | + }) |
| 85 | + .unwrap_or_else(|| DEFAULT_BRANCH.clone()); |
| 86 | + let hash = hash.or_else(|| { |
| 87 | + context |
| 88 | + .as_ref() |
| 89 | + .and_then(|ctx| ctx.branch_hash().and_then(|hash| hash.parse().ok())) |
| 90 | + }); |
| 91 | + let testbed = testbed |
| 92 | + .or_else(|| { |
| 93 | + context |
| 94 | + .as_ref() |
| 95 | + .and_then(|ctx| ctx.testbed_os().and_then(|testbed| testbed.parse().ok())) |
| 96 | + }) |
| 97 | + .unwrap_or_else(|| DEFAULT_TESTBED.clone()); |
| 98 | + Self { |
| 99 | + branch, |
| 100 | + hash, |
| 101 | + start_point, |
| 102 | + testbed, |
| 103 | + thresholds, |
| 104 | + start_time, |
| 105 | + end_time, |
| 106 | + results, |
| 107 | + settings, |
| 108 | + context, |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments