Skip to content

Commit

Permalink
perf(anubis): add problem name to get submission by id endpoint response
Browse files Browse the repository at this point in the history
  • Loading branch information
WarriorsSami committed Mar 10, 2024
1 parent f25b830 commit 94fa6c6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
4 changes: 2 additions & 2 deletions anubis-eval/src/api/get_submission_endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub async fn get_submission(

db.run(move |conn| {
match Submission::find_by_id(&submission_id, conn) {
Ok(submission) => {
Ok((submission, problem)) => {
// Check if the user is allowed to view the submission
let handle = Handle::current();
let _ = handle.enter();
Expand Down Expand Up @@ -67,7 +67,7 @@ pub async fn get_submission(

info!("Submission retrieved: {:?}", submission);
Ok(GetSubmissionResponse {
dto: submission.into(),
dto: (submission, problem).into(),
})
}
Err(e) => {
Expand Down
7 changes: 5 additions & 2 deletions anubis-eval/src/contracts/get_submission_dtos.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::domain::submission::Submission;
use chrono::{DateTime, Utc};
use rocket::serde::Serialize;
use crate::domain::problem::Problem;

#[derive(Serialize)]
#[serde(crate = "rocket::serde")]
Expand All @@ -23,12 +24,13 @@ impl<'r> rocket::response::Responder<'r, 'static> for GetSubmissionWithTestCases
}
}

impl From<Submission> for GetSubmissionWithTestCasesDto {
fn from(submission: Submission) -> Self {
impl From<(Submission, Problem)> for GetSubmissionWithTestCasesDto {
fn from((submission, problem): (Submission, Problem)) -> Self {
Self {
submission: GetSubmissionDto {
id: submission.id().to_string(),
problem_id: submission.problem_id().to_string(),
problem_name: problem.name().to_string(),
user_id: submission.user_id().to_string(),
language: submission.language().to_string(),
source_code: match submission.source_code().to_string().is_empty() {
Expand Down Expand Up @@ -65,6 +67,7 @@ impl From<Submission> for GetSubmissionWithTestCasesDto {
pub struct GetSubmissionDto {
id: String,
problem_id: String,
problem_name: String,
user_id: String,
language: String,
source_code: Option<String>,
Expand Down
39 changes: 23 additions & 16 deletions anubis-eval/src/infrastructure/submission_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Submission {
pub fn find_by_id(
id: &String,
conn: &mut PgConnection,
) -> Result<Submission, ApplicationError> {
) -> Result<(Submission, Problem), ApplicationError> {
all_submissions
.find(id.to_string())
.inner_join(all_testcases)
Expand All @@ -58,23 +58,30 @@ impl Submission {
}),
false => {
let submission = submission_and_testcases.first().unwrap().0.clone();
let testcases = submission_and_testcases
let problem = Problem::find_by_id(&submission.problem_id, conn)?;

let mut testcases = submission_and_testcases
.into_iter()
.map(|(_, testcase)| testcase.into())
.collect::<Vec<_>>();

Ok(Submission::new(
Uuid::parse_str(&submission.id).unwrap(),
Uuid::parse_str(&submission.user_id).unwrap(),
Uuid::parse_str(&submission.problem_id).unwrap(),
submission.language.into(),
submission.source_code,
submission.status.into(),
submission.score,
submission.created_at,
submission.avg_time,
submission.avg_memory,
testcases,
.collect::<Vec<TestCase>>();

testcases.sort_by_key(|a| a.testcase_id());

Ok((
Submission::new(
Uuid::parse_str(&submission.id).unwrap(),
Uuid::parse_str(&submission.user_id).unwrap(),
Uuid::parse_str(&submission.problem_id).unwrap(),
submission.language.into(),
submission.source_code,
submission.status.into(),
submission.score,
submission.created_at,
submission.avg_time,
submission.avg_memory,
testcases,
),
problem,
))
}
},
Expand Down

0 comments on commit 94fa6c6

Please sign in to comment.