Skip to content

Commit

Permalink
feat(runner/poll_results): add regressions threshold, colors and bett…
Browse files Browse the repository at this point in the history
…er style to logs
  • Loading branch information
adriencaccia committed Jul 5, 2024
1 parent 2c14783 commit 69e07fc
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 21 deletions.
56 changes: 45 additions & 11 deletions src/api_client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::fmt::Display;

use crate::prelude::*;
use crate::{app::Cli, config::CodSpeedConfig};
use console::style;
use gql_client::{Client as GQLClient, ClientConfig};
use nestify::nest;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -77,6 +80,22 @@ pub enum ReportConclusion {
MissingBaseRun,
Success,
}

impl Display for ReportConclusion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ReportConclusion::AcknowledgedFailure => {
write!(f, "{}", style("Acknowledged Failure").yellow().bold())
}
ReportConclusion::Failure => write!(f, "{}", style("Failure").red().bold()),
ReportConclusion::MissingBaseRun => {
write!(f, "{}", style("Missing Base Run").yellow().bold())
}
ReportConclusion::Success => write!(f, "{}", style("Success").green().bold()),
}
}
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FetchLocalRunReportHeadReport {
Expand All @@ -103,11 +122,19 @@ nest! {
#[serde(rename_all = "camelCase")]*
struct FetchLocalRunReportData {
repository: pub struct FetchLocalRunReportRepository {
pub runs: Vec<FetchLocalRunReportRun>,
settings: struct FetchLocalRunReportSettings {
allowed_regression: f64,
},
runs: Vec<FetchLocalRunReportRun>,
}
}
}

pub struct FetchLocalRunReportResponse {
pub allowed_regression: f64,
pub run: FetchLocalRunReportRun,
}

impl CodSpeedAPIClient {
pub async fn create_login_session(&self) -> Result<CreateLoginSessionPayload> {
let response = self
Expand Down Expand Up @@ -142,7 +169,7 @@ impl CodSpeedAPIClient {
pub async fn fetch_local_run_report(
&self,
vars: FetchLocalRunReportVars,
) -> Result<FetchLocalRunReportRun> {
) -> Result<FetchLocalRunReportResponse> {
let response = self
.gql_client
.query_with_vars_unwrap::<FetchLocalRunReportData, FetchLocalRunReportVars>(
Expand All @@ -151,15 +178,22 @@ impl CodSpeedAPIClient {
)
.await;
match response {
Ok(response) => match response.repository.runs.into_iter().next() {
Some(run) => Ok(run),
None => bail!(
"No runs found for owner: {}, name: {}, run_id: {}",
vars.owner,
vars.name,
vars.run_id
),
},
Ok(response) => {
let allowed_regression = response.repository.settings.allowed_regression;

match response.repository.runs.into_iter().next() {
Some(run) => Ok(FetchLocalRunReportResponse {
allowed_regression,
run,
}),
None => bail!(
"No runs found for owner: {}, name: {}, run_id: {}",
vars.owner,
vars.name,
vars.run_id
),
}
}
Err(err) => bail!("Failed to fetch local run report: {}", err),
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/queries/FetchLocalRunReport.gql
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
query FetchLocalRunReport($owner: String!, $name: String!, $runId: String!) {
repository(owner: $owner, name: $name) {
settings {
allowedRegression
}
runs(where: { id: { equals: $runId } }) {
id
status
Expand Down
36 changes: 26 additions & 10 deletions src/run/poll_results.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::time::Duration;

use console::style;
use tokio::time::{sleep, Instant};
use url::Url;

use crate::api_client::{
CodSpeedAPIClient, FetchLocalRunReportRun, FetchLocalRunReportVars, RunStatus,
CodSpeedAPIClient, FetchLocalRunReportResponse, FetchLocalRunReportVars, RunStatus,
};
use crate::prelude::*;

Expand All @@ -30,7 +31,7 @@ pub async fn poll_results(
run_id: run_id.clone(),
};

let run;
let response;
loop {
if start.elapsed() > RUN_PROCESSING_MAX_DURATION {
bail!("Polling results timed out");
Expand All @@ -40,32 +41,47 @@ pub async fn poll_results(
.fetch_local_run_report(fetch_local_run_report_vars.clone())
.await?
{
FetchLocalRunReportRun { status, .. } if status != RunStatus::Completed => {
FetchLocalRunReportResponse { run, .. } if run.status != RunStatus::Completed => {
sleep(Duration::from_secs(5)).await;
}
run_from_api => {
run = run_from_api;
reponse_from_api => {
response = reponse_from_api;
break;
}
}
}

let report = run
let report = response
.run
.head_reports
.into_iter()
.next()
.ok_or_else(|| anyhow!("No head report found in the run report"))?;

info!("Report completed, here are the results:");
if let Some(impact) = report.impact {
info!("Impact: {}%", (impact * 100.0).round());
let rounded_impact = (impact * 100.0).round();
let impact_text = if impact > 0.0 {
style(format!("+{}%", rounded_impact)).green().bold()
} else {
style(format!("{}%", rounded_impact)).red().bold()
};

info!(
"Impact: {} (allowed regression: -{}%)",
impact_text,
(response.allowed_regression * 100.0).round()
);
}
info!("Conclusion: {:?}", report.conclusion);
info!("Conclusion: {}", report.conclusion);

let mut report_url = Url::parse(config.frontend_url.as_str())?;
report_url.set_path(format!("{}/{}/runs/{}", owner, name, run.id).as_str());
report_url.set_path(format!("{}/{}/runs/{}", owner, name, response.run.id).as_str());

info!("\nTo see the full report, visit: {}", report_url);
info!(
"\nTo see the full report, visit: {}",
style(report_url).blue().bold().underlined()
);

Ok(())
}

0 comments on commit 69e07fc

Please sign in to comment.