Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion atcoder-problems-backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ FROM rust:1.38
COPY --from=builder /app/target/release/crawl /usr/bin/crawl
COPY --from=builder /app/target/release/dump_json /usr/bin/dump_json
COPY --from=builder /app/target/release/delta_update /usr/bin/delta_update
COPY --from=builder /app/target/release/update /usr/bin/update
Original file line number Diff line number Diff line change
@@ -1,58 +1,70 @@
extern crate openssl; // Just for musl-compiler
use openssl_probe; // Just for musl-compiler

use atcoder_problems_backend::error::MapHandlerError;
use atcoder_problems_backend::sql::models::Submission;
use atcoder_problems_backend::sql::{
AcceptedCountClient, LanguageCountClient, ProblemInfoUpdater, ProblemsSubmissionUpdater,
RatedPointSumClient, StreakUpdater, SubmissionClient, SubmissionRequest,
};
use diesel::{Connection, PgConnection};
use lambda_runtime::error::HandlerError;
use lambda_runtime::lambda;
use lambda_runtime::Context;
use log::{self, info};
use simple_logger;
use std::env;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
openssl_probe::init_ssl_cert_env_vars(); // Just for musl-compiler

simple_logger::init_with_level(log::Level::Info)?;
lambda!(handler);
Ok(())
}

fn handler(_: String, _: Context) -> Result<String, HandlerError> {
info!("Started!");

info!("Connecting to SQL ...");
let url = env::var("SQL_URL")?;
let conn = PgConnection::establish(&url)?;
let conn = PgConnection::establish(&url).herr()?;

info!("Loading submissions ...");
let all_accepted_submissions: Vec<Submission> =
conn.get_submissions(SubmissionRequest::AllAccepted)?;
let all_accepted_submissions: Vec<Submission> = conn
.get_submissions(SubmissionRequest::AllAccepted)
.herr()?;

info!(
"There are {} AC submissions.",
all_accepted_submissions.len()
);

info!("Executing update_accepted_count...");
conn.update_accepted_count(&all_accepted_submissions)?;
conn.update_accepted_count(&all_accepted_submissions)
.herr()?;

info!("Executing update_problem_solver_count...");
conn.update_solver_count()?;
conn.update_solver_count().herr()?;

info!("Executing update_rated_point_sums...");
conn.update_rated_point_sum(&all_accepted_submissions)?;
conn.update_rated_point_sum(&all_accepted_submissions)
.herr()?;

info!("Executing update_language_count...");
conn.update_language_count(&all_accepted_submissions)?;
conn.update_language_count(&all_accepted_submissions)
.herr()?;

info!("Executing update_submissions_of_problems...");
conn.update_submissions_of_problems(&all_accepted_submissions)?;
conn.update_submissions_of_problems(&all_accepted_submissions)
.herr()?;

info!("Executing update_problem_points...");
conn.update_problem_points()?;
conn.update_problem_points().herr()?;

info!("Executing update_streak_count...");
conn.update_streak_count(&all_accepted_submissions)?;
conn.update_streak_count(&all_accepted_submissions).herr()?;

info!("Finished");

Ok(())
Ok("Finished".to_owned())
}
6 changes: 3 additions & 3 deletions atcoder-problems-backend/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ use diesel::{ConnectionResult, QueryResult};
use lambda_runtime::error::HandlerError;

pub trait MapHandlerError<T> {
fn map_handler_error(self) -> Result<T, HandlerError>;
fn herr(self) -> Result<T, HandlerError>;
}

impl<T> MapHandlerError<T> for ConnectionResult<T> {
fn map_handler_error(self) -> Result<T, HandlerError> {
fn herr(self) -> Result<T, HandlerError> {
self.map_err(|e| HandlerError::from(e.to_string().as_str()))
}
}

impl<T> MapHandlerError<T> for QueryResult<T> {
fn map_handler_error(self) -> Result<T, HandlerError> {
fn herr(self) -> Result<T, HandlerError> {
self.map_err(|e| HandlerError::from(e.to_string().as_str()))
}
}
2 changes: 1 addition & 1 deletion atcoder-problems-backend/src/lambda/time_submissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ where
from_second: from_epoch_second,
count: 1000,
})
.map_handler_error()?;
.herr()?;
let max_id = submissions.iter().map(|s| s.id).max().unwrap_or(0);

let mut hasher = Md5::new();
Expand Down
2 changes: 1 addition & 1 deletion atcoder-problems-backend/src/lambda/user_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ where
.ok_or_else(|| HandlerError::from("There is no user."))?;

info!("UserInfo API");
let user_info = get_user_info(&self.connection, user_id).map_handler_error()?;
let user_info = get_user_info(&self.connection, user_id).herr()?;

let body = serde_json::to_string(&user_info)?;
Ok(LambdaOutput::new200(body, None))
Expand Down
7 changes: 2 additions & 5 deletions atcoder-problems-backend/src/lambda/user_submissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ where
let user_id = e
.param("user")
.ok_or_else(|| HandlerError::from("There is no user."))?;
let count: i64 = self
.connection
.get_user_submission_count(user_id)
.map_handler_error()?;
let count: i64 = self.connection.get_user_submission_count(user_id).herr()?;

let mut hasher = Md5::new();
hasher.input(user_id.as_bytes());
Expand All @@ -45,7 +42,7 @@ where
let submissions = self
.connection
.get_submissions(SubmissionRequest::UserAll { user_id })
.map_handler_error()?;
.herr()?;
let body = serde_json::to_string(&submissions)?;
Ok(LambdaOutput::new200(body, Some(etag)))
}
Expand Down