Skip to content

Commit ffbd562

Browse files
committed
move back Bound to the site
1 parent abc1193 commit ffbd562

File tree

8 files changed

+161
-158
lines changed

8 files changed

+161
-158
lines changed

collector/src/lib.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
use chrono::NaiveDate;
12
pub use database::{Commit, PatchName, QueryLabel};
23
use serde::Deserialize;
34
use std::cmp::PartialOrd;
5+
use std::fmt;
46
use std::process::{self, Command};
57

68
pub mod api;
@@ -21,6 +23,94 @@ use std::time::{Duration, Instant};
2123
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Deserialize)]
2224
pub struct DeltaTime(#[serde(with = "round_float")] pub f64);
2325

26+
/// The bound for finding an artifact
27+
///
28+
/// This can either be the upper or lower bound.
29+
/// In the case of commits or tags this is an exact bound, but for dates
30+
/// it's a best effort (i.e., if the bound is a date but there are no artifacts
31+
/// for that date, we'll find the artifact that most closely matches).
32+
#[derive(Default, Debug, Clone, PartialEq, Eq)]
33+
pub enum Bound {
34+
/// An unverified git commit (in sha form) or a tag of a commit (e.g., "1.53.0")
35+
Commit(String),
36+
/// A date in time
37+
Date(NaiveDate),
38+
/// No bound
39+
#[default]
40+
None,
41+
}
42+
43+
impl Bound {
44+
/// Tests whether `self` matches commit when searching from the left
45+
pub fn left_match(&self, commit: &Commit) -> bool {
46+
match self {
47+
Bound::Commit(sha) => commit.sha == **sha,
48+
Bound::Date(date) => commit.is_master() && commit.date.0.naive_utc().date() >= *date,
49+
Bound::None => {
50+
let last_month = chrono::Utc::now().date_naive() - chrono::Duration::days(30);
51+
commit.is_master() && last_month <= commit.date.0.naive_utc().date()
52+
}
53+
}
54+
}
55+
56+
/// Tests whether `self` matches commit when searching from the right
57+
pub fn right_match(&self, commit: &Commit) -> bool {
58+
match self {
59+
Bound::Commit(sha) => commit.sha == **sha,
60+
Bound::Date(date) => commit.is_master() && commit.date.0.date_naive() <= *date,
61+
Bound::None => commit.is_master(),
62+
}
63+
}
64+
}
65+
66+
impl serde::Serialize for Bound {
67+
fn serialize<S>(&self, serializer: S) -> ::std::result::Result<S::Ok, S::Error>
68+
where
69+
S: serde::ser::Serializer,
70+
{
71+
let s = match *self {
72+
Bound::Commit(ref s) => s.clone(),
73+
Bound::Date(ref date) => date.format("%Y-%m-%d").to_string(),
74+
Bound::None => String::new(),
75+
};
76+
serializer.serialize_str(&s)
77+
}
78+
}
79+
80+
impl<'de> Deserialize<'de> for Bound {
81+
fn deserialize<D>(deserializer: D) -> ::std::result::Result<Bound, D::Error>
82+
where
83+
D: serde::de::Deserializer<'de>,
84+
{
85+
struct BoundVisitor;
86+
87+
impl<'de> serde::de::Visitor<'de> for BoundVisitor {
88+
type Value = Bound;
89+
90+
fn visit_str<E>(self, value: &str) -> ::std::result::Result<Bound, E>
91+
where
92+
E: serde::de::Error,
93+
{
94+
if value.is_empty() {
95+
return Ok(Bound::None);
96+
}
97+
98+
let bound = value
99+
.parse::<chrono::NaiveDate>()
100+
.map(Bound::Date)
101+
.unwrap_or(Bound::Commit(value.to_string()));
102+
Ok(bound)
103+
}
104+
105+
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106+
f.write_str("either a YYYY-mm-dd date or a collection ID (usually commit hash)")
107+
}
108+
}
109+
110+
deserializer.deserialize_str(BoundVisitor)
111+
}
112+
}
113+
24114
pub fn null_means_nan<'de, D>(deserializer: D) -> ::std::result::Result<f64, D::Error>
25115
where
26116
D: serde::de::Deserializer<'de>,

database/src/selector.rs

Lines changed: 1 addition & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -24,107 +24,16 @@
2424
use std::{
2525
fmt::{self, Debug},
2626
hash::Hash,
27-
ops::RangeInclusive,
2827
sync::Arc,
2928
};
3029

3130
use async_trait::async_trait;
32-
use chrono::NaiveDate;
33-
use serde::Deserialize;
3431

3532
use crate::{
3633
comparison::Metric, interpolate::Interpolate, ArtifactId, ArtifactIdIter, Benchmark,
37-
CodegenBackend, Commit, Connection, Index, Lookup, Profile, Scenario,
34+
CodegenBackend, Connection, Index, Lookup, Profile, Scenario,
3835
};
3936

40-
/// The bound for finding an artifact
41-
///
42-
/// This can either be the upper or lower bound.
43-
/// In the case of commits or tags this is an exact bound, but for dates
44-
/// it's a best effort (i.e., if the bound is a date but there are no artifacts
45-
/// for that date, we'll find the artifact that most closely matches).
46-
#[derive(Default, Debug, Clone, PartialEq, Eq)]
47-
pub enum Bound {
48-
/// An unverified git commit (in sha form) or a tag of a commit (e.g., "1.53.0")
49-
Commit(String),
50-
/// A date in time
51-
Date(NaiveDate),
52-
/// No bound
53-
#[default]
54-
None,
55-
}
56-
57-
impl Bound {
58-
/// Tests whether `self` matches commit when searching from the left
59-
pub fn left_match(&self, commit: &Commit) -> bool {
60-
match self {
61-
Bound::Commit(sha) => commit.sha == **sha,
62-
Bound::Date(date) => commit.is_master() && commit.date.0.naive_utc().date() >= *date,
63-
Bound::None => {
64-
let last_month = chrono::Utc::now().date_naive() - chrono::Duration::days(30);
65-
commit.is_master() && last_month <= commit.date.0.naive_utc().date()
66-
}
67-
}
68-
}
69-
70-
/// Tests whether `self` matches commit when searching from the right
71-
pub fn right_match(&self, commit: &Commit) -> bool {
72-
match self {
73-
Bound::Commit(sha) => commit.sha == **sha,
74-
Bound::Date(date) => commit.is_master() && commit.date.0.date_naive() <= *date,
75-
Bound::None => commit.is_master(),
76-
}
77-
}
78-
}
79-
80-
impl serde::Serialize for Bound {
81-
fn serialize<S>(&self, serializer: S) -> ::std::result::Result<S::Ok, S::Error>
82-
where
83-
S: serde::ser::Serializer,
84-
{
85-
let s = match *self {
86-
Bound::Commit(ref s) => s.clone(),
87-
Bound::Date(ref date) => date.format("%Y-%m-%d").to_string(),
88-
Bound::None => String::new(),
89-
};
90-
serializer.serialize_str(&s)
91-
}
92-
}
93-
94-
impl<'de> Deserialize<'de> for Bound {
95-
fn deserialize<D>(deserializer: D) -> ::std::result::Result<Bound, D::Error>
96-
where
97-
D: serde::de::Deserializer<'de>,
98-
{
99-
struct BoundVisitor;
100-
101-
impl<'de> serde::de::Visitor<'de> for BoundVisitor {
102-
type Value = Bound;
103-
104-
fn visit_str<E>(self, value: &str) -> ::std::result::Result<Bound, E>
105-
where
106-
E: serde::de::Error,
107-
{
108-
if value.is_empty() {
109-
return Ok(Bound::None);
110-
}
111-
112-
let bound = value
113-
.parse::<chrono::NaiveDate>()
114-
.map(Bound::Date)
115-
.unwrap_or(Bound::Commit(value.to_string()));
116-
Ok(bound)
117-
}
118-
119-
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
120-
f.write_str("either a YYYY-mm-dd date or a collection ID (usually commit hash)")
121-
}
122-
}
123-
124-
deserializer.deserialize_str(BoundVisitor)
125-
}
126-
}
127-
12837
#[derive(Debug)]
12938
pub struct StatisticSeries {
13039
pub artifact_ids: ArtifactIdIter,
@@ -199,56 +108,6 @@ impl<T: Clone + PartialEq + fmt::Debug> Point for (T, f64) {
199108
}
200109
}
201110

202-
/// Finds the most appropriate `ArtifactId` for a given bound.
203-
///
204-
/// Searches the commits in the index either from the left or the right.
205-
/// If not found in those commits, searches through the artifacts in the index.
206-
pub fn artifact_id_for_bound(data: &Index, bound: Bound, is_left: bool) -> Option<ArtifactId> {
207-
let commits = data.commits();
208-
let commit = if is_left {
209-
commits
210-
.iter()
211-
.find(|commit| bound.left_match(commit))
212-
.cloned()
213-
} else {
214-
commits
215-
.iter()
216-
.rfind(|commit| bound.right_match(commit))
217-
.cloned()
218-
};
219-
commit.map(ArtifactId::Commit).or_else(|| {
220-
data.artifacts()
221-
.find(|aid| match &bound {
222-
Bound::Commit(c) => *c == **aid,
223-
Bound::Date(_) => false,
224-
Bound::None => false,
225-
})
226-
.map(|aid| ArtifactId::Tag(aid.to_string()))
227-
})
228-
}
229-
230-
pub fn range_subset(data: Vec<Commit>, range: RangeInclusive<Bound>) -> Vec<Commit> {
231-
let (a, b) = range.into_inner();
232-
233-
let left_idx = data.iter().position(|commit| a.left_match(commit));
234-
let right_idx = data.iter().rposition(|commit| b.right_match(commit));
235-
236-
if let (Some(left), Some(right)) = (left_idx, right_idx) {
237-
data.get(left..=right)
238-
.map(|s| s.to_vec())
239-
.unwrap_or_else(|| {
240-
log::error!(
241-
"Failed to compute left/right indices from {:?}..={:?}",
242-
a,
243-
b
244-
);
245-
vec![]
246-
})
247-
} else {
248-
vec![]
249-
}
250-
}
251-
252111
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
253112
pub enum Selector<T> {
254113
All,

site/src/api.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub mod dashboard {
8181

8282
pub mod graph {
8383
use super::graphs::{GraphKind, Series};
84-
use database::selector::Bound;
84+
use collector::Bound;
8585
use serde::{Deserialize, Serialize};
8686

8787
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
@@ -102,7 +102,7 @@ pub mod graph {
102102
}
103103

104104
pub mod graphs {
105-
use database::selector::Bound;
105+
use collector::Bound;
106106
use serde::{Deserialize, Serialize};
107107
use std::collections::{HashMap, HashSet};
108108

@@ -147,7 +147,7 @@ pub mod graphs {
147147
pub mod detail_graphs {
148148
use crate::api::graphs::{GraphKind, Series};
149149
use crate::api::vec_from_comma_separated;
150-
use database::selector::Bound;
150+
use collector::Bound;
151151
use serde::{Deserialize, Serialize};
152152

153153
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
@@ -170,7 +170,7 @@ pub mod detail_graphs {
170170
}
171171

172172
pub mod detail_sections {
173-
use database::selector::Bound;
173+
use collector::Bound;
174174
use serde::{Deserialize, Serialize};
175175

176176
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
@@ -207,7 +207,7 @@ pub mod detail_sections {
207207
pub mod runtime_detail_graphs {
208208
use crate::api::graphs::{GraphKind, Series};
209209
use crate::api::vec_from_comma_separated;
210-
use database::selector::Bound;
210+
use collector::Bound;
211211
use serde::{Deserialize, Serialize};
212212

213213
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
@@ -228,7 +228,7 @@ pub mod runtime_detail_graphs {
228228
}
229229

230230
pub mod bootstrap {
231-
use database::selector::Bound;
231+
use collector::Bound;
232232
use hashbrown::HashMap;
233233
use serde::{Deserialize, Serialize};
234234

@@ -252,7 +252,8 @@ pub mod bootstrap {
252252

253253
pub mod comparison {
254254
use crate::benchmark_metadata::ProfileMetadata;
255-
use database::{comparison::Metric, selector::Bound, Date};
255+
use collector::Bound;
256+
use database::{comparison::Metric, Date};
256257
use serde::{Deserialize, Serialize};
257258
use std::collections::HashMap;
258259

@@ -593,8 +594,8 @@ pub mod github {
593594
}
594595

595596
pub mod triage {
597+
use collector::Bound;
596598
use database::comparison::Metric;
597-
use database::selector::Bound;
598599
use serde::{Deserialize, Serialize};
599600

600601
#[derive(Debug, Clone, Serialize, Deserialize)]

site/src/comparison.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use serde::Serialize;
1717
use crate::api::comparison::CompileBenchmarkMetadata;
1818
use crate::benchmark_metadata::get_compile_benchmarks_metadata;
1919
use crate::server::comparison::StatComparison;
20-
use collector::compile::benchmark::ArtifactType;
21-
use database::{selector::Bound, CodegenBackend, CommitType, CompileBenchmark};
20+
use collector::{compile::benchmark::ArtifactType, Bound};
21+
use database::{CodegenBackend, CommitType, CompileBenchmark};
2222
use std::cmp;
2323
use std::collections::{HashMap, HashSet};
2424
use std::error::Error;

site/src/github/comparison_summary.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::comparison::{
44
};
55
use crate::load::SiteCtxt;
66

7-
use database::selector::Bound;
7+
use collector::Bound;
88
use database::{comparison::Metric, ArtifactId, QueuedCommit};
99

1010
use crate::github::{COMMENT_MARK_ROLLUP, COMMENT_MARK_TEMPORARY, RUST_REPO_GITHUB_API_URL};

site/src/load.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::time::Instant;
66

77
use arc_swap::{ArcSwap, Guard};
88
use chrono::{Duration, Utc};
9-
use database::selector::{self, Bound};
109
use lazy_static::lazy_static;
1110
use log::error;
1211
use parking_lot::Mutex;
@@ -16,7 +15,7 @@ use serde::{Deserialize, Serialize};
1615
use crate::db;
1716
use crate::self_profile::SelfProfileCache;
1817
use collector::compile::benchmark::category::Category;
19-
use collector::MasterCommit;
18+
use collector::{Bound, MasterCommit};
2019
use database::Pool;
2120
pub use database::{ArtifactId, Benchmark, Commit};
2221
use database::{CommitType, Date};
@@ -149,11 +148,11 @@ impl SiteCtxt {
149148
}
150149

151150
pub fn artifact_id_for_bound(&self, query: Bound, is_left: bool) -> Option<ArtifactId> {
152-
selector::artifact_id_for_bound(&self.index.load(), query, is_left)
151+
crate::selector::artifact_id_for_bound(&self.index.load(), query, is_left)
153152
}
154153

155154
pub fn data_range(&self, range: RangeInclusive<Bound>) -> Vec<Commit> {
156-
selector::range_subset(self.index.load().commits(), range)
155+
crate::selector::range_subset(self.index.load().commits(), range)
157156
}
158157

159158
/// Initialize `SiteCtxt` from database url

0 commit comments

Comments
 (0)