Skip to content

Commit

Permalink
Merge pull request #1 from meilisearch/tag_views
Browse files Browse the repository at this point in the history
Various changes for tag support
  • Loading branch information
dureuill authored Jun 3, 2024
2 parents 648fc98 + 896ff72 commit 16dc39a
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 22 deletions.
150 changes: 131 additions & 19 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt::Display;
use std::rc::Rc;
use std::str::FromStr;

use anyhow::Context;
use anyhow::{anyhow, Context};
use askama::Template;
use axum::{extract::Query, http::StatusCode, routing::get, Router};
use axum_sqlx_tx::Layer;
Expand Down Expand Up @@ -63,6 +63,13 @@ mod filters {
))
}

pub fn tag(tag_ref: impl Display) -> ::askama::Result<String> {
Ok(format!(
r#"<a href="https://github.com/meilisearch/meilisearch/tree/{}">{}</a>"#,
tag_ref, tag_ref
))
}

pub fn trim_outer_p(text: impl Display) -> ::askama::Result<String> {
Ok(text
.to_string()
Expand Down Expand Up @@ -220,6 +227,8 @@ impl Default for WorkloadSummaryTotal {
}

async fn home(mut tx: Tx) -> Result<HomeTemplate> {
const WORKLOAD_ON_MAIN: usize = 5;
const RECENT_WORKLOADS: usize = 15;
let latest = latest_completed_invocations(&mut tx, 40).await?;

let mut workloads_on_main = Vec::new();
Expand All @@ -232,12 +241,12 @@ async fn home(mut tx: Tx) -> Result<HomeTemplate> {
reason,
} in latest
{
if workloads_on_main.len() > 5 && recent_workloads.len() > 5 {
if workloads_on_main.len() > WORKLOAD_ON_MAIN && recent_workloads.len() > RECENT_WORKLOADS {
break;
}
match branch {
Some(branch) if branch == "main" => {
if workloads_on_main.len() > 5 {
if workloads_on_main.len() > WORKLOAD_ON_MAIN {
continue;
}
let baseline_commit = latest_commits_for_branch(
Expand Down Expand Up @@ -278,7 +287,7 @@ async fn home(mut tx: Tx) -> Result<HomeTemplate> {
continue;
}
branch => {
if recent_workloads.len() > 5 {
if recent_workloads.len() > RECENT_WORKLOADS {
continue;
}
// FIXME: determine baseline_branch with heuristics:
Expand Down Expand Up @@ -314,7 +323,7 @@ async fn home(mut tx: Tx) -> Result<HomeTemplate> {
}
}

let latest_invocation_progress = latest_invocation_progress(&mut tx, 5).await?;
let latest_invocation_progress = latest_invocation_progress(&mut tx, 8).await?;

Ok(HomeTemplate {
latest_invocation_progress,
Expand Down Expand Up @@ -439,6 +448,8 @@ struct ViewSpansTemplate {
target_commit_sha1: String,
target_branch: Option<String>,
baseline_branch: Option<String>,
target_tag: Option<String>,
baseline_tag: Option<String>,
span_stats: SpanStats,
}

Expand Down Expand Up @@ -495,10 +506,19 @@ struct SpanChange {
#[derive(Deserialize)]
struct ViewSpansQuery {
workload_name: String,
commit_sha1: String,
// target
commit_sha1: Option<String>,
#[serde(default)]
target_branch: Option<String>,
#[serde(default)]
target_tag: Option<String>,

// baseline
#[serde(default)]
baseline_commit: Option<String>,
#[serde(default)]
baseline_tag: Option<String>,
#[serde(default)]
baseline_branch: Option<String>,
}

Expand All @@ -508,22 +528,74 @@ async fn view_spans(
workload_name,
commit_sha1,
target_branch,
target_tag,
baseline_commit,
baseline_tag,
baseline_branch,
}): Query<ViewSpansQuery>,
) -> Result<ViewSpansTemplate> {
let latest_commits = if let Some(branch) = baseline_branch.as_deref() {
latest_commits_for_branch(&mut tx, branch, 5, Some(&commit_sha1), &workload_name).await?
} else {
latest_commits_for_worfklow(&mut tx, 1, &commit_sha1, &workload_name).await?
// determine target commit_sha1
let commit_sha1 = 'commit: {
if let Some(commit_sha1) = commit_sha1 {
break 'commit commit_sha1;
}
if let Some(tag) = target_tag.as_deref() {
let commit = commit_for_tag(&mut tx, tag, &workload_name).await?;
if let Some(commit) = commit {
break 'commit commit.sha1;
} else {
return Err(anyhow!("Could not find commit for tag {}", tag))
.with_code(StatusCode::NOT_FOUND);
}
}
if let Some(branch) = target_branch.as_deref() {
let mut latest_commits =
latest_commits_for_branch(&mut tx, branch, 1, None, &workload_name).await?;
if let Some(commit) = latest_commits.pop() {
break 'commit commit.sha1;
} else {
return Err(anyhow!("Could not find commmit on branch {}", branch))
.with_code(StatusCode::NOT_FOUND);
}
}
return Err(anyhow!(
"Specify one of commit_sha1, target_branch or target_tag"
))
.with_code(StatusCode::BAD_REQUEST);
};

let baseline_commit = 'commit: {
if let Some(baseline_commit) = baseline_commit {
break 'commit Some(baseline_commit);
}
if let Some(baseline_tag) = baseline_tag.as_deref() {
if let Some(commit) = commit_for_tag(&mut tx, baseline_tag, &workload_name).await? {
break 'commit Some(commit.sha1);
}
}
if let Some(baseline_branch) = baseline_branch.as_deref() {
let mut latest_commits = latest_commits_for_branch(
&mut tx,
baseline_branch,
1,
Some(&commit_sha1),
&workload_name,
)
.await?;
if let Some(commit) = latest_commits.pop() {
break 'commit Some(commit.sha1);
}
}
let mut latest_commits =
latest_commits_for_worfklow(&mut tx, 1, &commit_sha1, &workload_name).await?;
latest_commits.pop().map(|commit| commit.sha1)
};

let commit_data = data_for_commit(&mut tx, &commit_sha1, &workload_name).await?;
let mut data_for_latest_commits = Vec::new();
for commit in &latest_commits {
data_for_latest_commits.push(data_for_commit(&mut tx, &commit.sha1, &workload_name).await?);
}
if let Some(latest_commit) = latest_commits.first() {
let data_for_latest_commit = data_for_latest_commits.first().unwrap();
let report = compare_commits(commit_data, data_for_latest_commit);

if let Some(baseline_commit) = baseline_commit {
let baseline_data = data_for_commit(&mut tx, &baseline_commit, &workload_name).await?;
let report = compare_commits(commit_data, &baseline_data);

let mut total_time = None;
let mut improvements = Vec::new();
Expand All @@ -533,7 +605,7 @@ async fn view_spans(
let mut news = Vec::new();
let mut olds = Vec::new();

let baseline_sha1 = Rc::new(latest_commit.sha1.clone());
let baseline_sha1 = Rc::new(baseline_commit.clone());

for (span, change) in report.into_iter() {
if span == "::meta::total" {
Expand Down Expand Up @@ -568,6 +640,8 @@ async fn view_spans(
span_stats,
target_branch,
baseline_branch,
target_tag,
baseline_tag,
})
} else {
let span_stats = commit_data
Expand All @@ -579,6 +653,8 @@ async fn view_spans(
target_commit_sha1: commit_sha1,
target_branch,
baseline_branch,
target_tag,
baseline_tag,
span_stats: SpanStats::NoComparison(span_stats),
})
}
Expand Down Expand Up @@ -985,6 +1061,30 @@ async fn latest_commits_for_worfklow(
Ok(commits)
}

async fn commit_for_tag(tx: &mut Tx, tag: &str, workload_name: &str) -> Result<Option<Commit>> {
let query = sqlx::query(
r#"SELECT commits.sha1 FROM commits
INNER JOIN workloads ON invocations.uuid = workloads.invocation_uuid
INNER JOIN invocations ON commits.sha1 = invocations.commit_sha1
WHERE workloads.name = ? AND invocations.tag = ? AND invocations.status = ?
GROUP BY commits.sha1
ORDER BY commits.date DESC
LIMIT 1"#,
)
.bind(workload_name)
.bind(tag)
.bind(InvocationStatus::Completed.to_db_status());

let commit = query
.map(|row: SqliteRow| Commit { sha1: row.get(0) })
.fetch_optional(tx)
.await
.context("fetching commit for tag")
.with_code(StatusCode::INTERNAL_SERVER_ERROR)?;

Ok(commit)
}

async fn latest_commits_for_branch(
tx: &mut Tx,
branch: &str,
Expand Down Expand Up @@ -1030,12 +1130,14 @@ async fn latest_commits_for_branch(
} else {
sqlx::query(
r#"SELECT commits.sha1 FROM commits
INNER JOIN workloads ON invocations.uuid = workloads.invocation_uuid
INNER JOIN invocations ON commits.sha1 = invocations.commit_sha1
WHERE invocations.branch = ? AND invocations.status = ?
WHERE workloads.name = ? AND invocations.branch = ? AND invocations.status = ?
GROUP BY commits.sha1
ORDER BY commits.date DESC
LIMIT ?"#,
)
.bind(workload_name)
.bind(branch)
.bind(InvocationStatus::Completed.to_db_status())
.bind(count)
Expand Down Expand Up @@ -1064,6 +1166,8 @@ async fn view_span(
baseline,
target_branch,
baseline_branch,
target_tag,
baseline_tag,
}): Query<ViewSpanQuery>,
) -> Result<ViewSpanTemplate> {
let target_data = data_for_span(&mut tx, &target, &workload_name, &span).await?;
Expand Down Expand Up @@ -1102,6 +1206,8 @@ async fn view_span(
baseline_commit_sha1: baseline,
baseline_branch,
target_branch,
target_tag,
baseline_tag,
plot,
})
}
Expand Down Expand Up @@ -1170,6 +1276,8 @@ struct ViewSpanTemplate {
baseline_commit_sha1: Option<String>,
target_branch: Option<String>,
baseline_branch: Option<String>,
target_tag: Option<String>,
baseline_tag: Option<String>,
plot: String,
}

Expand All @@ -1184,4 +1292,8 @@ struct ViewSpanQuery {
target_branch: Option<String>,
#[serde(default)]
baseline_branch: Option<String>,
#[serde(default)]
target_tag: Option<String>,
#[serde(default)]
baseline_tag: Option<String>,
}
23 changes: 20 additions & 3 deletions templates/commit_subtitle.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
{% macro target_tag_str %}
{% match target_tag %}
{% when Some with (tag) %}
(tagged {{tag|tag|safe}})
{% when None %}
{% endmatch %}
{% endmacro %}

{% macro target_branch_str %}
{% match target_branch %}
{% when Some with (branch) %}
Expand All @@ -6,16 +14,25 @@
{% endmatch %}
{% endmacro %}

{% macro baseline_tag_str %}
{% match baseline_tag %}
{% when Some with (tag) %}
(tagged {{tag|tag|safe}})
{% when None %}
{% endmatch %}
{% endmacro %}

{% macro baseline_str %}
{% match baseline_branch %}
{% when Some with (branch) %}
branch {{branch|branch|safe}} (on {{baseline_commit|commit|safe}})
branch {{branch|branch|safe}} (on {{baseline_commit|commit|safe}}) {% call baseline_tag_str() %}
{% when None %}
{{baseline_commit|commit|safe}}
{{baseline_commit|commit|safe}} {% call baseline_tag_str() %}
{% endmatch %}
{% endmacro %}

<h2 class="subtitle">Comparing {{target_commit|commit|safe}} {% call target_branch_str() %} to {% call baseline_str() %}
<h2 class="subtitle">Comparing {{target_commit|commit|safe}} {% call target_tag_str() %} {% call target_branch_str() %}
to {% call baseline_str() %}
</h2>


Expand Down

0 comments on commit 16dc39a

Please sign in to comment.