feat: fetch and test filtered benchmark results.

This commit is contained in:
Aravinth Manivannan 2023-02-02 00:29:52 +05:30
parent 7a2cc1646d
commit 9d128033ac
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88

View file

@ -31,6 +31,8 @@ use crate::AppData;
pub mod routes { pub mod routes {
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use super::ResultsPage;
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct Campaign { pub struct Campaign {
pub add: &'static str, pub add: &'static str,
@ -63,8 +65,26 @@ pub mod routes {
self.delete.replace("{uuid}", campaign_id) self.delete.replace("{uuid}", campaign_id)
} }
pub fn get_results_route(&self, campaign_id: &str) -> String { pub fn get_results_route(
self.results.replace("{uuid}", campaign_id) &self,
campaign_id: &str,
modifier: Option<ResultsPage>,
) -> String {
let mut res = self.results.replace("{uuid}", campaign_id);
if let Some(modifier) = modifier {
if let Some(page) = modifier.page {
res = format!("{res}?page={page}");
}
if let Some(bench_type) = modifier.bench_type {
if modifier.page.is_some() {
res = format!("{res}&bench_type={}", bench_type.to_string());
} else {
res = format!("{res}?bench_type={}", bench_type.to_string());
}
}
}
res
} }
} }
} }
@ -165,13 +185,13 @@ pub mod runners {
#[derive(Debug)] #[derive(Debug)]
struct InternalSurveyResp { struct InternalSurveyResp {
id: Option<i32>, id: i32,
submitted_at: Option<OffsetDateTime>, submitted_at: OffsetDateTime,
user_id: Option<Uuid>, user_id: Uuid,
threads: Option<i32>, threads: Option<i32>,
device_user_provided: Option<String>, device_user_provided: String,
device_software_recognised: Option<String>, device_software_recognised: String,
name: Option<String>, name: String,
} }
#[derive(Debug)] #[derive(Debug)]
@ -195,10 +215,56 @@ pub mod runners {
data: &AppData, data: &AppData,
page: usize, page: usize,
limit: usize, limit: usize,
filter: Option<SubmissionType>,
) -> ServiceResult<Vec<SurveyResponse>> { ) -> ServiceResult<Vec<SurveyResponse>> {
let mut db_responses = sqlx::query_as!( let mut db_responses = if let Some(filter) = filter {
InternalSurveyResp, sqlx::query_as!(
"SELECT InternalSurveyResp,
"SELECT
survey_responses.ID,
survey_responses.device_software_recognised,
survey_responses.threads,
survey_responses.user_id,
survey_responses.submitted_at,
survey_responses.device_user_provided,
survey_bench_type.name
FROM
survey_responses
INNER JOIN survey_bench_type ON
survey_responses.submission_bench_type_id = survey_bench_type.ID
WHERE
survey_bench_type.name = $3
AND
survey_responses.campaign_id = (
SELECT ID FROM survey_campaigns
WHERE
ID = $1
AND
user_id = (SELECT ID FROM survey_admins WHERE name = $2)
)
LIMIT $4 OFFSET $5",
uuid,
username,
filter.to_string(),
limit as i32,
page as i32,
)
.fetch_all(&data.db)
.await?
} else {
#[derive(Debug)]
struct I {
id: Option<i32>,
submitted_at: Option<OffsetDateTime>,
user_id: Option<Uuid>,
threads: Option<i32>,
device_user_provided: Option<String>,
device_software_recognised: Option<String>,
name: Option<String>,
}
let mut i = sqlx::query_as!(
I,
"SELECT
survey_responses.ID, survey_responses.ID,
survey_responses.device_software_recognised, survey_responses.device_software_recognised,
survey_responses.threads, survey_responses.threads,
@ -218,15 +284,29 @@ pub mod runners {
AND AND
user_id = (SELECT ID FROM survey_admins WHERE name = $2) user_id = (SELECT ID FROM survey_admins WHERE name = $2)
) )
LIMIT $3 OFFSET $4 LIMIT $3 OFFSET $4",
", uuid,
uuid, username,
username, limit as i32,
limit as i32, page as i32,
page as i32, )
) .fetch_all(&data.db)
.fetch_all(&data.db) .await?;
.await?;
let mut res = Vec::with_capacity(i.len());
i.drain(0..).for_each(|x| {
res.push(InternalSurveyResp {
id: x.id.unwrap(),
submitted_at: x.submitted_at.unwrap(),
user_id: x.user_id.unwrap(),
threads: x.threads,
device_user_provided: x.device_user_provided.unwrap(),
device_software_recognised: x.device_software_recognised.unwrap(),
name: x.name.unwrap(),
})
});
res
};
let mut responses = Vec::with_capacity(db_responses.len()); let mut responses = Vec::with_capacity(db_responses.len());
for r in db_responses.drain(0..) { for r in db_responses.drain(0..) {
@ -263,13 +343,12 @@ pub mod runners {
responses.push(SurveyResponse { responses.push(SurveyResponse {
benches, benches,
user, user,
device_user_provided: r.device_user_provided.unwrap(), device_user_provided: r.device_user_provided,
device_software_recognised: r.device_software_recognised.unwrap(), device_software_recognised: r.device_software_recognised,
submitted_at: r.submitted_at.unwrap().unix_timestamp(), submitted_at: r.submitted_at.unix_timestamp(),
id: r.id.unwrap() as usize, id: r.id as usize,
submission_type: SubmissionType::from_str(r.name.as_ref().unwrap()) submission_type: SubmissionType::from_str(&r.name).unwrap(),
.unwrap(), threads: r.threads.map(|t| t as usize),
threads: Some(r.threads.unwrap() as usize),
}) })
} }
Ok(responses) Ok(responses)
@ -378,12 +457,17 @@ pub fn services(cfg: &mut web::ServiceConfig) {
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct ResultsPage { pub struct ResultsPage {
page: Option<usize>, page: Option<usize>,
pub bench_type: Option<SubmissionType>,
} }
impl ResultsPage { impl ResultsPage {
pub fn page(&self) -> usize { pub fn page(&self) -> usize {
self.page.unwrap_or(0) self.page.unwrap_or(0)
} }
pub fn new(page: Option<usize>, bench_type: Option<SubmissionType>) -> Self {
Self { page, bench_type }
}
} }
#[actix_web_codegen_const_routes::get( #[actix_web_codegen_const_routes::get(
@ -397,9 +481,12 @@ pub async fn get_campaign_resutls(
data: AppData, data: AppData,
) -> ServiceResult<impl Responder> { ) -> ServiceResult<impl Responder> {
let username = id.identity().unwrap(); let username = id.identity().unwrap();
let query = query.into_inner();
let page = query.page(); let page = query.page();
let results = runners::get_results(&username, &path, &data, page, 50).await?; let results =
runners::get_results(&username, &path, &data, page, 50, query.bench_type)
.await?;
Ok(HttpResponse::Ok().json(results)) Ok(HttpResponse::Ok().json(results))
} }
@ -422,6 +509,7 @@ async fn add(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::api::v1::bench::Submission; use crate::api::v1::bench::Submission;
use crate::api::v1::bench::SubmissionType;
use crate::errors::*; use crate::errors::*;
use crate::tests::*; use crate::tests::*;
use crate::*; use crate::*;
@ -493,7 +581,7 @@ mod tests {
device_software_recognised: DEVICE_SOFTWARE_RECOGNISED.into(), device_software_recognised: DEVICE_SOFTWARE_RECOGNISED.into(),
threads: THREADS, threads: THREADS,
benches: BENCHES.clone(), benches: BENCHES.clone(),
submission_type: api::v1::bench::SubmissionType::Wasm, submission_type: SubmissionType::Wasm,
}; };
let _proof = let _proof =
@ -508,6 +596,7 @@ mod tests {
&AppData::new(data.clone()), &AppData::new(data.clone()),
0, 0,
50, 50,
None,
) )
.await .await
.unwrap(); .unwrap();
@ -518,6 +607,34 @@ mod tests {
let mut r = BENCHES.clone(); let mut r = BENCHES.clone();
r.sort_by(|a, b| a.difficulty.cmp(&b.difficulty)); r.sort_by(|a, b| a.difficulty.cmp(&b.difficulty));
assert_eq!(
super::runners::get_results(
NAME,
&uuid::Uuid::parse_str(&campaign.campaign_id).unwrap(),
&AppData::new(data.clone()),
0,
50,
Some(SubmissionType::Wasm),
)
.await
.unwrap(),
responses
);
assert_eq!(
super::runners::get_results(
NAME,
&uuid::Uuid::parse_str(&campaign.campaign_id).unwrap(),
&AppData::new(data.clone()),
0,
50,
Some(SubmissionType::Js),
)
.await
.unwrap(),
Vec::default()
);
assert_eq!(l, r); assert_eq!(l, r);
assert_eq!( assert_eq!(
responses[0].device_software_recognised, responses[0].device_software_recognised,
@ -530,7 +647,7 @@ mod tests {
&V1_API_ROUTES &V1_API_ROUTES
.admin .admin
.campaign .campaign
.get_results_route(&campaign.campaign_id), .get_results_route(&campaign.campaign_id, None),
cookies.clone() cookies.clone()
); );
assert_eq!(results_resp.status(), StatusCode::OK); assert_eq!(results_resp.status(), StatusCode::OK);