Skip to content

Commit

Permalink
archived_api
Browse files Browse the repository at this point in the history
  • Loading branch information
epompeii committed Jul 7, 2024
1 parent 639725b commit bb96670
Show file tree
Hide file tree
Showing 18 changed files with 174 additions and 55 deletions.
30 changes: 17 additions & 13 deletions lib/bencher_json/src/project/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ use super::boundary::JsonBoundary;

crate::typed_uuid::typed_uuid!(BenchmarkUuid);

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct JsonNewBenchmark {
/// The name of the benchmark.
/// Maximum length is 1,024 characters.
pub name: BenchmarkName,
/// The preferred slug for the benchmark.
/// If not provided, the slug will be generated from the name.
/// If the provided or generated slug is already in use, a unique slug will be generated.
/// Maximum length is 64 characters.
pub slug: Option<Slug>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct JsonBenchmarks(pub Vec<JsonBenchmark>);
Expand All @@ -27,6 +40,7 @@ pub struct JsonBenchmark {
pub slug: Slug,
pub created: DateTime,
pub modified: DateTime,
pub archived: Option<DateTime>,
}

impl fmt::Display for JsonBenchmark {
Expand All @@ -47,19 +61,7 @@ pub struct JsonBenchmarkMetric {
pub boundary: Option<JsonBoundary>,
pub created: DateTime,
pub modified: DateTime,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct JsonNewBenchmark {
/// The name of the benchmark.
/// Maximum length is 1,024 characters.
pub name: BenchmarkName,
/// The preferred slug for the benchmark.
/// If not provided, the slug will be generated from the name.
/// If the provided or generated slug is already in use, a unique slug will be generated.
/// Maximum length is 64 characters.
pub slug: Option<Slug>,
pub archived: Option<DateTime>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand All @@ -71,4 +73,6 @@ pub struct JsonUpdateBenchmark {
/// The preferred new slug for the benchmark.
/// Maximum length is 64 characters.
pub slug: Option<Slug>,
/// Set whether the benchmark is archived.
pub archived: Option<bool>,
}
15 changes: 4 additions & 11 deletions lib/bencher_json/src/project/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub struct JsonBranch {
pub start_point: Option<JsonStartPoint>,
pub created: DateTime,
pub modified: DateTime,
pub archived: Option<DateTime>,
}

impl fmt::Display for JsonBranch {
Expand Down Expand Up @@ -113,6 +114,7 @@ pub struct JsonBranchVersion {
pub start_point: Option<JsonStartPoint>,
pub created: DateTime,
pub modified: DateTime,
pub archived: Option<DateTime>,
}

#[typeshare::typeshare]
Expand All @@ -132,17 +134,8 @@ pub struct JsonUpdateBranch {
/// The preferred new slug for the branch.
/// Maximum length is 64 characters.
pub slug: Option<Slug>,
/// The full `git` commit hash for the next version of the branch.
/// This is useful in cases where there may be a race condition
/// when a start point branch has been updated but its benchmarks have not yet finished.
pub hash: Option<GitHash>,
}

impl JsonUpdateBranch {
// Check whether only the `git` hash is being updated.
pub fn is_hash_only(&self) -> bool {
self.name.is_none() && self.slug.is_none() && self.hash.is_some()
}
/// Set whether the branch is archived.
pub archived: Option<bool>,
}

#[typeshare::typeshare]
Expand Down
3 changes: 3 additions & 0 deletions lib/bencher_json/src/project/measure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ pub struct JsonMeasure {
pub units: ResourceName,
pub created: DateTime,
pub modified: DateTime,
pub archived: Option<DateTime>,
}

impl fmt::Display for JsonMeasure {
Expand All @@ -251,4 +252,6 @@ pub struct JsonUpdateMeasure {
/// The new units of measure.
/// Maximum length is 64 characters.
pub units: Option<ResourceName>,
/// Set whether the measure is archived.
pub archived: Option<bool>,
}
5 changes: 5 additions & 0 deletions lib/bencher_json/src/project/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ pub struct JsonReportQueryParams {
pub start_time: Option<DateTimeMillis>,
/// Filter for reports before the given date time in milliseconds.
pub end_time: Option<DateTimeMillis>,
/// Only return reports with an archived branch or testbed.
pub archived: Option<bool>,
}

#[derive(Debug, Clone)]
Expand All @@ -372,6 +374,7 @@ pub struct JsonReportQuery {
pub testbed: Option<NameId>,
pub start_time: Option<DateTime>,
pub end_time: Option<DateTime>,
pub archived: Option<bool>,
}

impl TryFrom<JsonReportQueryParams> for JsonReportQuery {
Expand All @@ -383,6 +386,7 @@ impl TryFrom<JsonReportQueryParams> for JsonReportQuery {
testbed,
start_time,
end_time,
archived,
} = query_params;

let branch = if let Some(branch) = branch {
Expand All @@ -401,6 +405,7 @@ impl TryFrom<JsonReportQueryParams> for JsonReportQuery {
testbed,
start_time: start_time.map(Into::into),
end_time: end_time.map(Into::into),
archived,
})
}
}
Expand Down
3 changes: 3 additions & 0 deletions lib/bencher_json/src/project/testbed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub struct JsonTestbed {
pub slug: Slug,
pub created: DateTime,
pub modified: DateTime,
pub archived: Option<DateTime>,
}

impl fmt::Display for JsonTestbed {
Expand All @@ -81,4 +82,6 @@ pub struct JsonUpdateTestbed {
/// The preferred new slug for the testbed.
/// Maximum length is 64 characters.
pub slug: Option<Slug>,
/// Set whether the testbed is archived.
pub archived: Option<bool>,
}
5 changes: 5 additions & 0 deletions lib/bencher_json/src/project/threshold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,16 @@ pub struct JsonThresholdQueryParams {
pub testbed: Option<String>,
/// Filter by measure name, exact match.
pub measure: Option<String>,
/// Only return thresholds with an archived branch, testbed, or measure.
pub archived: Option<bool>,
}

#[derive(Debug, Clone)]
pub struct JsonThresholdQuery {
pub branch: Option<NameId>,
pub testbed: Option<NameId>,
pub measure: Option<NameId>,
pub archived: Option<bool>,
}

impl TryFrom<JsonThresholdQueryParams> for JsonThresholdQuery {
Expand All @@ -83,6 +86,7 @@ impl TryFrom<JsonThresholdQueryParams> for JsonThresholdQuery {
branch,
testbed,
measure,
archived,
} = query_params;

let branch = if let Some(branch) = branch {
Expand All @@ -105,6 +109,7 @@ impl TryFrom<JsonThresholdQueryParams> for JsonThresholdQuery {
branch,
testbed,
measure,
archived,
})
}
}
Expand Down
3 changes: 2 additions & 1 deletion services/api/migrations/2024-07-07-133201_archived/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ SELECT id,
modified,
(
SELECT strftime('%s', datetime('now', 'utc'))
WHERE name LIKE '%@%/hash/%'
WHERE name LIKE '%@detached%'
OR name LIKE '%@%/hash/%'
OR name LIKE '%@%/version/%'
)
FROM branch;
Expand Down
8 changes: 8 additions & 0 deletions services/api/src/endpoints/project/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pub struct ProjBenchmarksQuery {
pub name: Option<BenchmarkName>,
/// Search by benchmark name, slug, or UUID.
pub search: Option<Search>,
/// Only return archived testbeds.
pub archived: Option<bool>,
}

#[allow(clippy::no_effect_underscore_binding, clippy::unused_async)]
Expand Down Expand Up @@ -165,6 +167,12 @@ fn get_ls_query<'q>(
);
}

if let Some(true) = query_params.archived {
query = query.filter(schema::benchmark::archived.is_not_null());
} else {
query = query.filter(schema::benchmark::archived.is_null());
};

match pagination_params.order() {
ProjBenchmarksSort::Name => match pagination_params.direction {
Some(JsonDirection::Asc) | None => query.order(schema::benchmark::name.asc()),
Expand Down
26 changes: 8 additions & 18 deletions services/api/src/endpoints/project/branches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use crate::{
model::{
project::{
branch::{QueryBranch, UpdateBranch},
version::QueryVersion,
QueryProject,
},
user::auth::{AuthUser, BearerToken, PubBearerToken},
Expand Down Expand Up @@ -56,6 +55,8 @@ pub struct ProjBranchesQuery {
pub name: Option<BranchName>,
/// Search by branch name, slug, or UUID.
pub search: Option<Search>,
/// Only return archived branches.
pub archived: Option<bool>,
}

#[allow(clippy::no_effect_underscore_binding, clippy::unused_async)]
Expand Down Expand Up @@ -174,6 +175,12 @@ fn get_ls_query<'q>(
);
}

if let Some(true) = query_params.archived {
query = query.filter(schema::branch::archived.is_not_null());
} else {
query = query.filter(schema::branch::archived.is_null());
};

match pagination_params.order() {
ProjBranchesSort::Name => match pagination_params.direction {
Some(JsonDirection::Asc) | None => query.order(schema::branch::name.asc()),
Expand Down Expand Up @@ -345,23 +352,6 @@ async fn patch_inner(
let query_branch =
QueryBranch::from_resource_id(conn_lock!(context), query_project.id, &path_params.branch)?;

// If there is a hash then try to see if there is already a code version for
// this branch with that particular hash.
// Otherwise, create a new code version for this branch with the hash.
if let Some(hash) = &json_branch.hash {
QueryVersion::get_or_increment(
conn_lock!(context),
query_project.id,
query_branch.id,
Some(hash),
)?;

// Don't mark the branch as updated if only the hash is being updated.
if json_branch.is_hash_only() {
return query_branch.into_json_for_project(conn_lock!(context), &query_project);
}
}

let update_branch = UpdateBranch::from(json_branch.clone());
diesel::update(schema::branch::table.filter(schema::branch::id.eq(query_branch.id)))
.set(&update_branch)
Expand Down
8 changes: 8 additions & 0 deletions services/api/src/endpoints/project/measures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pub struct ProjMeasuresQuery {
pub name: Option<ResourceName>,
/// Search by measure name, slug, or UUID.
pub search: Option<Search>,
/// Only return archived measures.
pub archived: Option<bool>,
}

#[allow(clippy::no_effect_underscore_binding, clippy::unused_async)]
Expand Down Expand Up @@ -165,6 +167,12 @@ fn get_ls_query<'q>(
);
}

if let Some(true) = query_params.archived {
query = query.filter(schema::measure::archived.is_not_null());
} else {
query = query.filter(schema::measure::archived.is_null());
};

match pagination_params.order() {
ProjMeasuresSort::Name => match pagination_params.direction {
Some(JsonDirection::Asc) | None => query.order(schema::measure::name.asc()),
Expand Down
18 changes: 16 additions & 2 deletions services/api/src/endpoints/project/reports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use bencher_json::{
};
use bencher_rbac::project::Permission;
use diesel::{
dsl::count, BelongingToDsl, ExpressionMethods, JoinOnDsl, QueryDsl, RunQueryDsl,
SelectableHelper,
dsl::count, BelongingToDsl, BoolExpressionMethods, ExpressionMethods, JoinOnDsl, QueryDsl,
RunQueryDsl, SelectableHelper,
};
use dropshot::{endpoint, HttpError, Path, Query, RequestContext, TypedBody};
use http::StatusCode;
Expand Down Expand Up @@ -190,6 +190,20 @@ fn get_ls_query<'q>(
query = query.filter(schema::report::end_time.le(end_time));
}

if let Some(true) = query_params.archived {
query = query.filter(
schema::branch::archived
.is_not_null()
.or(schema::testbed::archived.is_not_null()),
);
} else {
query = query.filter(
schema::branch::archived
.is_null()
.and(schema::testbed::archived.is_null()),
);
};

Ok(match pagination_params.order() {
ProjReportsSort::DateTime => match pagination_params.direction {
Some(JsonDirection::Asc) => query.order((
Expand Down
8 changes: 8 additions & 0 deletions services/api/src/endpoints/project/testbeds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pub struct ProjTestbedsQuery {
pub name: Option<ResourceName>,
/// Search by testbed name, slug, or UUID.
pub search: Option<Search>,
/// Only return archived testbeds.
pub archived: Option<bool>,
}

#[allow(clippy::no_effect_underscore_binding, clippy::unused_async)]
Expand Down Expand Up @@ -165,6 +167,12 @@ fn get_ls_query<'q>(
);
}

if let Some(true) = query_params.archived {
query = query.filter(schema::testbed::archived.is_not_null());
} else {
query = query.filter(schema::testbed::archived.is_null());
};

match pagination_params.order() {
ProjTestbedsSort::Name => match pagination_params.direction {
Some(JsonDirection::Asc) | None => query.order(schema::testbed::name.asc()),
Expand Down
21 changes: 20 additions & 1 deletion services/api/src/endpoints/project/thresholds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use bencher_json::{
JsonDirection, JsonPagination, JsonThresholds, ModelUuid, ResourceId, ThresholdUuid,
};
use bencher_rbac::project::Permission;
use diesel::{BelongingToDsl, ExpressionMethods, QueryDsl, RunQueryDsl, SelectableHelper};
use diesel::{
BelongingToDsl, BoolExpressionMethods, ExpressionMethods, QueryDsl, RunQueryDsl,
SelectableHelper,
};
use dropshot::{endpoint, HttpError, Path, Query, RequestContext, TypedBody};
use schemars::JsonSchema;
use serde::Deserialize;
Expand Down Expand Up @@ -183,6 +186,22 @@ fn get_ls_query<'q>(
filter_measure_name_id!(query, measure);
}

if let Some(true) = query_params.archived {
query = query.filter(
schema::branch::archived
.is_not_null()
.or(schema::testbed::archived.is_not_null())
.or(schema::measure::archived.is_not_null()),
);
} else {
query = query.filter(
schema::branch::archived
.is_null()
.and(schema::testbed::archived.is_null())
.and(schema::measure::archived.is_null()),
);
};

Ok(match pagination_params.order() {
ProjThresholdsSort::Created => match pagination_params.direction {
Some(JsonDirection::Asc) | None => query.order(schema::threshold::created.asc()),
Expand Down
Loading

0 comments on commit bb96670

Please sign in to comment.