mirror of
https://github.com/n08i40k/schedule-parser-rusted.git
synced 2025-12-06 09:47:50 +03:00
Подключение sentry.
This commit is contained in:
40
src/main.rs
40
src/main.rs
@@ -4,6 +4,7 @@ use crate::middlewares::content_type::ContentTypeBootstrap;
|
||||
use actix_web::dev::{ServiceFactory, ServiceRequest};
|
||||
use actix_web::{App, Error, HttpServer};
|
||||
use dotenvy::dotenv;
|
||||
use std::io;
|
||||
use utoipa_actix_web::AppExt;
|
||||
use utoipa_actix_web::scope::Scope;
|
||||
use utoipa_rapidoc::RapiDoc;
|
||||
@@ -69,12 +70,8 @@ pub fn get_api_scope<
|
||||
.service(vk_id_scope)
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() {
|
||||
dotenv().ok();
|
||||
|
||||
unsafe { std::env::set_var("RUST_LOG", "debug") };
|
||||
env_logger::init();
|
||||
async fn async_main() -> io::Result<()> {
|
||||
println!("Starting server...");
|
||||
|
||||
let app_state = app_state().await;
|
||||
|
||||
@@ -82,7 +79,11 @@ async fn main() {
|
||||
let (app, api) = App::new()
|
||||
.into_utoipa_app()
|
||||
.app_data(app_state.clone())
|
||||
.service(get_api_scope("/api/v1").wrap(ContentTypeBootstrap))
|
||||
.service(
|
||||
get_api_scope("/api/v1")
|
||||
.wrap(sentry_actix::Sentry::new())
|
||||
.wrap(ContentTypeBootstrap),
|
||||
)
|
||||
.split_for_parts();
|
||||
|
||||
let rapidoc_service = RapiDoc::with_openapi("/api-docs-json", api).path("/api-docs");
|
||||
@@ -96,9 +97,28 @@ async fn main() {
|
||||
app.service(rapidoc_service.custom_html(patched_rapidoc_html))
|
||||
})
|
||||
.workers(4)
|
||||
.bind(("0.0.0.0", 5050))
|
||||
.unwrap()
|
||||
.bind(("0.0.0.0", 5050))?
|
||||
.run()
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
let _guard = sentry::init((
|
||||
"https://9c33db76e89984b3f009b28a9f4b5954@sentry.n08i40k.ru/8",
|
||||
sentry::ClientOptions {
|
||||
release: sentry::release_name!(),
|
||||
send_default_pii: true,
|
||||
..Default::default()
|
||||
},
|
||||
));
|
||||
|
||||
unsafe { std::env::set_var("RUST_BACKTRACE", "1") };
|
||||
|
||||
dotenv().unwrap();
|
||||
|
||||
env_logger::init();
|
||||
|
||||
actix_web::rt::System::new().block_on(async { async_main().await })?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
use crate::parser::LessonParseResult::{Lessons, Street};
|
||||
use crate::parser::schema::LessonType::Break;
|
||||
use crate::parser::schema::{
|
||||
Day, Lesson, LessonSubGroup, LessonTime, LessonType, ParseError, ParseResult, ScheduleEntry,
|
||||
Day, ErrorCell, ErrorCellPos, Lesson, LessonSubGroup, LessonTime, LessonType, ParseError,
|
||||
ParseResult, ScheduleEntry,
|
||||
};
|
||||
use crate::parser::LessonParseResult::{Lessons, Street};
|
||||
use calamine::{open_workbook_from_rs, Reader, Xls};
|
||||
use calamine::{Reader, Xls, open_workbook_from_rs};
|
||||
use chrono::{DateTime, Duration, NaiveDateTime, Utc};
|
||||
use fuzzy_matcher::skim::SkimMatcherV2;
|
||||
use fuzzy_matcher::FuzzyMatcher;
|
||||
use fuzzy_matcher::skim::SkimMatcherV2;
|
||||
use regex::Regex;
|
||||
use std::collections::HashMap;
|
||||
use std::io::Cursor;
|
||||
@@ -56,9 +57,8 @@ fn get_string_from_cell(worksheet: &WorkSheet, row: u32, col: u32) -> Option<Str
|
||||
return None;
|
||||
}
|
||||
|
||||
static NL_RE: LazyLock<Regex, fn() -> Regex> =
|
||||
LazyLock::new(|| Regex::new(r"[\n\r]+").unwrap());
|
||||
static SP_RE: LazyLock<Regex, fn() -> Regex> = LazyLock::new(|| Regex::new(r"\s+").unwrap());
|
||||
static NL_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[\n\r]+").unwrap());
|
||||
static SP_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\s+").unwrap());
|
||||
|
||||
let trimmed_data = SP_RE
|
||||
.replace_all(&NL_RE.replace_all(&cell_data, " "), " ")
|
||||
@@ -252,7 +252,7 @@ fn parse_lesson(
|
||||
|
||||
let raw_name = raw_name_opt.unwrap();
|
||||
|
||||
static OTHER_STREET_RE: LazyLock<Regex, fn() -> Regex> =
|
||||
static OTHER_STREET_RE: LazyLock<Regex> =
|
||||
LazyLock::new(|| Regex::new(r"^[А-Я][а-я]+,?\s?[0-9]+$").unwrap());
|
||||
|
||||
if OTHER_STREET_RE.is_match(&raw_name) {
|
||||
@@ -275,7 +275,9 @@ fn parse_lesson(
|
||||
.filter(|time| time.xls_range.1.0 == cell_range.1.0)
|
||||
.collect::<Vec<&InternalTime>>();
|
||||
|
||||
let end_time = end_time_arr.first().ok_or(ParseError::LessonTimeNotFound)?;
|
||||
let end_time = end_time_arr
|
||||
.first()
|
||||
.ok_or(ParseError::LessonTimeNotFound(ErrorCellPos { row, column }))?;
|
||||
|
||||
let range: Option<[u8; 2]> = if time.default_index != None {
|
||||
let default = time.default_index.unwrap() as u8;
|
||||
@@ -389,14 +391,12 @@ fn parse_cabinets(worksheet: &WorkSheet, row: u32, column: u32) -> Vec<String> {
|
||||
|
||||
/// Getting the "pure" name of the lesson and list of teachers from the text of the lesson cell.
|
||||
fn parse_name_and_subgroups(name: &String) -> Result<(String, Vec<LessonSubGroup>), ParseError> {
|
||||
static LESSON_RE: LazyLock<Regex, fn() -> Regex> =
|
||||
static LESSON_RE: LazyLock<Regex> =
|
||||
LazyLock::new(|| Regex::new(r"(?:[А-Я][а-я]+[А-Я]{2}(?:\([0-9][а-я]+\))?)+$").unwrap());
|
||||
static TEACHER_RE: LazyLock<Regex, fn() -> Regex> =
|
||||
static TEACHER_RE: LazyLock<Regex> =
|
||||
LazyLock::new(|| Regex::new(r"([А-Я][а-я]+)([А-Я])([А-Я])(?:\(([0-9])[а-я]+\))?").unwrap());
|
||||
static CLEAN_RE: LazyLock<Regex, fn() -> Regex> =
|
||||
LazyLock::new(|| Regex::new(r"[\s.,]+").unwrap());
|
||||
static END_CLEAN_RE: LazyLock<Regex, fn() -> Regex> =
|
||||
LazyLock::new(|| Regex::new(r"[.\s]+$").unwrap());
|
||||
static CLEAN_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[\s.,]+").unwrap());
|
||||
static END_CLEAN_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[.\s]+$").unwrap());
|
||||
|
||||
let (teachers, lesson_name) = {
|
||||
let clean_name = CLEAN_RE.replace_all(&name, "").to_string();
|
||||
@@ -423,14 +423,9 @@ fn parse_name_and_subgroups(name: &String) -> Result<(String, Vec<LessonSubGroup
|
||||
|
||||
for captures in teacher_it {
|
||||
subgroups.push(LessonSubGroup {
|
||||
number: if let Some(capture) = captures.get(4) {
|
||||
capture
|
||||
.as_str()
|
||||
.to_string()
|
||||
.parse::<u8>()
|
||||
.map_err(|_| ParseError::SubgroupIndexParsingFailed)?
|
||||
} else {
|
||||
0
|
||||
number: match captures.get(4) {
|
||||
Some(capture) => capture.as_str().to_string().parse::<u8>().unwrap(),
|
||||
None => 0,
|
||||
},
|
||||
cabinet: None,
|
||||
teacher: format!(
|
||||
@@ -665,10 +660,12 @@ pub fn parse_xls(buffer: &Vec<u8>) -> Result<ParseResult, ParseError> {
|
||||
|
||||
// time
|
||||
let time_range = {
|
||||
static TIME_RE: LazyLock<Regex, fn() -> Regex> =
|
||||
static TIME_RE: LazyLock<Regex> =
|
||||
LazyLock::new(|| Regex::new(r"(\d+\.\d+)-(\d+\.\d+)").unwrap());
|
||||
|
||||
let parse_res = TIME_RE.captures(&time).ok_or(ParseError::GlobalTime)?;
|
||||
let parse_res = TIME_RE.captures(&time).ok_or(ParseError::GlobalTime(
|
||||
ErrorCell::new(row, lesson_time_column, time.clone()),
|
||||
))?;
|
||||
|
||||
let start_match = parse_res.get(1).unwrap().as_str();
|
||||
let start_parts: Vec<&str> = start_match.split(".").collect();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use derive_more::Display;
|
||||
use derive_more::{Display, Error};
|
||||
use serde::{Deserialize, Serialize, Serializer};
|
||||
use serde_repr::{Deserialize_repr, Serialize_repr};
|
||||
use std::collections::HashMap;
|
||||
@@ -115,10 +115,33 @@ pub struct ParseResult {
|
||||
pub teachers: HashMap<String, ScheduleEntry>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Display, Clone, ToSchema)]
|
||||
#[derive(Clone, Debug, Display, Error, ToSchema)]
|
||||
#[display("row {row}, column {column}")]
|
||||
pub struct ErrorCellPos {
|
||||
pub row: u32,
|
||||
pub column: u32,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Display, Error, ToSchema)]
|
||||
#[display("'{data}' at {pos}")]
|
||||
pub struct ErrorCell {
|
||||
pub pos: ErrorCellPos,
|
||||
pub data: String,
|
||||
}
|
||||
|
||||
impl ErrorCell {
|
||||
pub fn new(row: u32, column: u32, data: String) -> Self {
|
||||
Self {
|
||||
pos: ErrorCellPos { row, column },
|
||||
data,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Display, Error, ToSchema)]
|
||||
pub enum ParseError {
|
||||
/// Errors related to reading XLS file.
|
||||
#[display("{}: Failed to read XLS file.", "_0")]
|
||||
#[display("{_0:?}: Failed to read XLS file.")]
|
||||
#[schema(value_type = String)]
|
||||
BadXLS(Arc<calamine::XlsError>),
|
||||
|
||||
@@ -131,16 +154,12 @@ pub enum ParseError {
|
||||
UnknownWorkSheetRange,
|
||||
|
||||
/// Failed to read the beginning and end of the lesson from the line
|
||||
#[display("Failed to read lesson start and end times from string.")]
|
||||
GlobalTime,
|
||||
#[display("Failed to read lesson start and end times from {_0}.")]
|
||||
GlobalTime(ErrorCell),
|
||||
|
||||
/// Not found the beginning and the end corresponding to the lesson.
|
||||
#[display("No start and end times matching the lesson was found.")]
|
||||
LessonTimeNotFound,
|
||||
|
||||
/// Failed to read the subgroup index.
|
||||
#[display("Failed to read subgroup index.")]
|
||||
SubgroupIndexParsingFailed,
|
||||
#[display("No start and end times matching the lesson (at {_0}) was found.")]
|
||||
LessonTimeNotFound(ErrorCellPos),
|
||||
}
|
||||
|
||||
impl Serialize for ParseError {
|
||||
@@ -154,11 +173,8 @@ impl Serialize for ParseError {
|
||||
ParseError::UnknownWorkSheetRange => {
|
||||
serializer.serialize_str("UNKNOWN_WORK_SHEET_RANGE")
|
||||
}
|
||||
ParseError::GlobalTime => serializer.serialize_str("GLOBAL_TIME"),
|
||||
ParseError::LessonTimeNotFound => serializer.serialize_str("LESSON_TIME_NOT_FOUND"),
|
||||
ParseError::SubgroupIndexParsingFailed => {
|
||||
serializer.serialize_str("SUBGROUP_INDEX_PARSING_FAILED")
|
||||
}
|
||||
ParseError::GlobalTime(_) => serializer.serialize_str("GLOBAL_TIME"),
|
||||
ParseError::LessonTimeNotFound(_) => serializer.serialize_str("LESSON_TIME_NOT_FOUND"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ pub async fn update_download_url(
|
||||
}
|
||||
|
||||
match downloader.fetch(false).await {
|
||||
Ok(download_result) => match parse_xls(download_result.data.as_ref().unwrap()) {
|
||||
Ok(download_result) => match parse_xls(&download_result.data.unwrap()) {
|
||||
Ok(data) => {
|
||||
*schedule = Some(Schedule {
|
||||
etag: download_result.etag,
|
||||
@@ -53,7 +53,11 @@ pub async fn update_download_url(
|
||||
|
||||
Ok(CacheStatus::from(schedule.as_ref().unwrap())).into()
|
||||
}
|
||||
Err(error) => ErrorCode::InvalidSchedule(error).into_response(),
|
||||
Err(error) => {
|
||||
sentry::capture_error(&error);
|
||||
|
||||
ErrorCode::InvalidSchedule(error).into_response()
|
||||
}
|
||||
},
|
||||
Err(error) => {
|
||||
eprintln!("Unknown url provided {}", data.url);
|
||||
@@ -93,7 +97,7 @@ mod schema {
|
||||
#[schema(as = SetDownloadUrl::ErrorCode)]
|
||||
pub enum ErrorCode {
|
||||
/// Transferred link with host different from politehnikum-eng.ru.
|
||||
#[display("URL with unknown host provided. Provide url with politehnikum-eng.ru host.")]
|
||||
#[display("URL with unknown host provided. Provide url with 'politehnikum-eng.ru' host.")]
|
||||
NonWhitelistedHost,
|
||||
|
||||
/// Failed to retrieve file metadata.
|
||||
@@ -112,7 +116,7 @@ mod schema {
|
||||
OutdatedSchedule,
|
||||
|
||||
/// Failed to parse the schedule.
|
||||
#[display("{}", "_0.display()")]
|
||||
#[display("{_0}")]
|
||||
InvalidSchedule(ParseError),
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user