mirror of
https://github.com/n08i40k/schedule-parser-rusted.git
synced 2025-12-06 09:47:50 +03:00
feat(schedule)!: move schedule parser, downloader, and updater to external library
This can be used to support more schedule formats in the future.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use crate::database::driver;
|
||||
use crate::database::models::{FCM, User};
|
||||
use crate::database::models::{User, FCM};
|
||||
use crate::extractors::base::{AsyncExtractor, FromRequestAsync};
|
||||
use crate::state::AppState;
|
||||
use crate::utility::jwt;
|
||||
@@ -7,7 +7,7 @@ use actix_macros::MiddlewareError;
|
||||
use actix_web::body::BoxBody;
|
||||
use actix_web::dev::Payload;
|
||||
use actix_web::http::header;
|
||||
use actix_web::{FromRequest, HttpRequest, web};
|
||||
use actix_web::{web, FromRequest, HttpRequest};
|
||||
use derive_more::Display;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt::Debug;
|
||||
|
||||
@@ -14,8 +14,6 @@ mod state;
|
||||
|
||||
mod database;
|
||||
|
||||
mod xls_downloader;
|
||||
|
||||
mod extractors;
|
||||
mod middlewares;
|
||||
mod routes;
|
||||
|
||||
@@ -18,8 +18,9 @@ async fn sign_up_combined(
|
||||
}
|
||||
|
||||
if !app_state
|
||||
.get_schedule_snapshot()
|
||||
.get_schedule_snapshot("eng_polytechnic")
|
||||
.await
|
||||
.unwrap()
|
||||
.data
|
||||
.groups
|
||||
.contains_key(&data.group)
|
||||
|
||||
@@ -40,8 +40,9 @@ pub async fn telegram_complete(
|
||||
|
||||
// проверка на существование группы
|
||||
if !app_state
|
||||
.get_schedule_snapshot()
|
||||
.get_schedule_snapshot("eng_polytechnic")
|
||||
.await
|
||||
.unwrap()
|
||||
.data
|
||||
.groups
|
||||
.contains_key(&data.group)
|
||||
|
||||
@@ -31,8 +31,9 @@ pub async fn group(user: AsyncExtractor<User>, app_state: web::Data<AppState>) -
|
||||
None => Err(ErrorCode::SignUpNotCompleted),
|
||||
|
||||
Some(group) => match app_state
|
||||
.get_schedule_snapshot()
|
||||
.get_schedule_snapshot("eng_polytechnic")
|
||||
.await
|
||||
.unwrap()
|
||||
.data
|
||||
.groups
|
||||
.get(group)
|
||||
|
||||
@@ -6,8 +6,9 @@ use actix_web::{get, web};
|
||||
#[get("/group-names")]
|
||||
pub async fn group_names(app_state: web::Data<AppState>) -> Response {
|
||||
let mut names: Vec<String> = app_state
|
||||
.get_schedule_snapshot()
|
||||
.get_schedule_snapshot("eng_polytechnic")
|
||||
.await
|
||||
.unwrap()
|
||||
.data
|
||||
.groups
|
||||
.keys()
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::state::{AppState, ScheduleSnapshot};
|
||||
use crate::state::AppState;
|
||||
use actix_macros::{OkResponse, ResponderJson};
|
||||
use actix_web::web;
|
||||
use schedule_parser::schema::ScheduleEntry;
|
||||
use providers::base::{ScheduleEntry, ScheduleSnapshot};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::ops::Deref;
|
||||
@@ -32,7 +32,12 @@ impl From<ScheduleEntry> for ScheduleEntryResponse {
|
||||
|
||||
impl ScheduleView {
|
||||
pub async fn from(app_state: &web::Data<AppState>) -> Self {
|
||||
let schedule = app_state.get_schedule_snapshot().await.clone();
|
||||
let schedule = app_state
|
||||
.get_schedule_snapshot("eng_polytechnic")
|
||||
.await
|
||||
.unwrap()
|
||||
.deref()
|
||||
.clone();
|
||||
|
||||
Self {
|
||||
url: schedule.url,
|
||||
@@ -60,7 +65,13 @@ pub struct CacheStatus {
|
||||
|
||||
impl CacheStatus {
|
||||
pub async fn from(value: &web::Data<AppState>) -> Self {
|
||||
From::<&ScheduleSnapshot>::from(value.get_schedule_snapshot().await.deref())
|
||||
From::<&ScheduleSnapshot>::from(
|
||||
value
|
||||
.get_schedule_snapshot("eng_polytechnic")
|
||||
.await
|
||||
.unwrap()
|
||||
.deref(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ use self::schema::*;
|
||||
use crate::AppState;
|
||||
use crate::routes::schema::ResponseError;
|
||||
use actix_web::{get, web};
|
||||
use schedule_parser::schema::ScheduleEntry;
|
||||
use providers::base::ScheduleEntry;
|
||||
|
||||
#[utoipa::path(responses(
|
||||
(status = OK, body = ScheduleEntry),
|
||||
@@ -18,8 +18,9 @@ use schedule_parser::schema::ScheduleEntry;
|
||||
#[get("/teacher/{name}")]
|
||||
pub async fn teacher(name: web::Path<String>, app_state: web::Data<AppState>) -> ServiceResponse {
|
||||
match app_state
|
||||
.get_schedule_snapshot()
|
||||
.get_schedule_snapshot("eng_polytechnic")
|
||||
.await
|
||||
.unwrap()
|
||||
.data
|
||||
.teachers
|
||||
.get(&name.into_inner())
|
||||
|
||||
@@ -6,8 +6,9 @@ use actix_web::{get, web};
|
||||
#[get("/teacher-names")]
|
||||
pub async fn teacher_names(app_state: web::Data<AppState>) -> Response {
|
||||
let mut names: Vec<String> = app_state
|
||||
.get_schedule_snapshot()
|
||||
.get_schedule_snapshot("eng_polytechnic")
|
||||
.await
|
||||
.unwrap()
|
||||
.data
|
||||
.teachers
|
||||
.keys()
|
||||
|
||||
@@ -19,8 +19,9 @@ pub async fn change_group(
|
||||
}
|
||||
|
||||
if !app_state
|
||||
.get_schedule_snapshot()
|
||||
.get_schedule_snapshot("eng_polytechnic")
|
||||
.await
|
||||
.unwrap()
|
||||
.data
|
||||
.groups
|
||||
.contains_key(&data.group)
|
||||
|
||||
6
src/state/env/mod.rs
vendored
6
src/state/env/mod.rs
vendored
@@ -1,11 +1,15 @@
|
||||
pub mod schedule;
|
||||
pub mod telegram;
|
||||
pub mod vk_id;
|
||||
|
||||
#[cfg(not(test))]
|
||||
pub mod yandex_cloud;
|
||||
|
||||
pub use self::schedule::ScheduleEnvData;
|
||||
pub use self::telegram::TelegramEnvData;
|
||||
pub use self::vk_id::VkIdEnvData;
|
||||
|
||||
#[cfg(not(test))]
|
||||
pub use self::yandex_cloud::YandexCloudEnvData;
|
||||
|
||||
#[derive(Default)]
|
||||
@@ -13,5 +17,7 @@ pub struct AppEnv {
|
||||
pub schedule: ScheduleEnvData,
|
||||
pub telegram: TelegramEnvData,
|
||||
pub vk_id: VkIdEnvData,
|
||||
|
||||
#[cfg(not(test))]
|
||||
pub yandex_cloud: YandexCloudEnvData,
|
||||
}
|
||||
|
||||
2
src/state/env/schedule.rs
vendored
2
src/state/env/schedule.rs
vendored
@@ -2,6 +2,7 @@ use std::env;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ScheduleEnvData {
|
||||
#[cfg(not(test))]
|
||||
pub url: Option<String>,
|
||||
pub auto_update: bool,
|
||||
}
|
||||
@@ -9,6 +10,7 @@ pub struct ScheduleEnvData {
|
||||
impl Default for ScheduleEnvData {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
#[cfg(not(test))]
|
||||
url: env::var("SCHEDULE_INIT_URL").ok(),
|
||||
auto_update: !env::var("SCHEDULE_DISABLE_AUTO_UPDATE")
|
||||
.is_ok_and(|v| v.eq("1") || v.eq("true")),
|
||||
|
||||
@@ -1,69 +1,88 @@
|
||||
mod env;
|
||||
mod fcm_client;
|
||||
mod schedule;
|
||||
|
||||
pub use crate::state::env::AppEnv;
|
||||
use crate::state::fcm_client::FCMClientData;
|
||||
use crate::xls_downloader::basic_impl::BasicXlsDownloader;
|
||||
use actix_web::web;
|
||||
use diesel::{Connection, PgConnection};
|
||||
use firebase_messaging_rs::FCMClient;
|
||||
use std::ops::DerefMut;
|
||||
use tokio::sync::{MappedMutexGuard, Mutex, MutexGuard};
|
||||
|
||||
pub use self::schedule::{Schedule, ScheduleSnapshot};
|
||||
pub use crate::state::env::AppEnv;
|
||||
use providers::base::{ScheduleProvider, ScheduleSnapshot};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::{Mutex, MutexGuard};
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
/// Common data provided to endpoints.
|
||||
pub struct AppState {
|
||||
cancel_token: CancellationToken,
|
||||
database: Mutex<PgConnection>,
|
||||
downloader: Mutex<BasicXlsDownloader>,
|
||||
schedule: Mutex<Schedule>,
|
||||
providers: HashMap<String, Arc<dyn ScheduleProvider>>,
|
||||
env: AppEnv,
|
||||
fcm_client: Option<Mutex<FCMClient>>,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
pub async fn new() -> Result<Self, self::schedule::Error> {
|
||||
pub async fn new() -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
||||
|
||||
let mut _self = Self {
|
||||
downloader: Mutex::new(BasicXlsDownloader::new()),
|
||||
let env = AppEnv::default();
|
||||
let providers: HashMap<String, Arc<dyn ScheduleProvider>> = HashMap::from([(
|
||||
"eng_polytechnic".to_string(),
|
||||
providers::EngelsPolytechnicProvider::new({
|
||||
#[cfg(test)]
|
||||
{
|
||||
providers::EngelsPolytechnicUpdateSource::Prepared(ScheduleSnapshot {
|
||||
url: "".to_string(),
|
||||
fetched_at: chrono::DateTime::default(),
|
||||
updated_at: chrono::DateTime::default(),
|
||||
data: providers::test_utils::engels_polytechnic::test_result().unwrap(),
|
||||
})
|
||||
}
|
||||
|
||||
schedule: Mutex::new(Schedule::default()),
|
||||
#[cfg(not(test))]
|
||||
{
|
||||
if let Some(url) = &env.schedule.url {
|
||||
providers::EngelsPolytechnicUpdateSource::Url(url.clone())
|
||||
} else {
|
||||
providers::EngelsPolytechnicUpdateSource::GrabFromSite {
|
||||
yandex_api_key: env.yandex_cloud.api_key.clone(),
|
||||
yandex_func_id: env.yandex_cloud.func_id.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.await?,
|
||||
)]);
|
||||
|
||||
let this = Self {
|
||||
cancel_token: CancellationToken::new(),
|
||||
database: Mutex::new(
|
||||
PgConnection::establish(&database_url)
|
||||
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url)),
|
||||
),
|
||||
env: AppEnv::default(),
|
||||
env,
|
||||
providers,
|
||||
fcm_client: FCMClientData::new().await,
|
||||
};
|
||||
|
||||
if _self.env.schedule.auto_update {
|
||||
_self
|
||||
.get_schedule()
|
||||
.await
|
||||
.init(_self.get_downloader().await.deref_mut(), &_self.env)
|
||||
.await?;
|
||||
if this.env.schedule.auto_update {
|
||||
for (_, provider) in &this.providers {
|
||||
let provider = provider.clone();
|
||||
let cancel_token = this.cancel_token.clone();
|
||||
|
||||
tokio::spawn(async move { provider.start_auto_update_task(cancel_token).await });
|
||||
}
|
||||
}
|
||||
|
||||
Ok(_self)
|
||||
Ok(this)
|
||||
}
|
||||
|
||||
pub async fn get_downloader(&'_ self) -> MutexGuard<'_, BasicXlsDownloader> {
|
||||
self.downloader.lock().await
|
||||
}
|
||||
pub async fn get_schedule_snapshot(&'_ self, provider: &str) -> Option<Arc<ScheduleSnapshot>> {
|
||||
if let Some(provider) = self.providers.get(provider) {
|
||||
return Some(provider.get_schedule().await);
|
||||
}
|
||||
|
||||
pub async fn get_schedule(&'_ self) -> MutexGuard<'_, Schedule> {
|
||||
self.schedule.lock().await
|
||||
}
|
||||
|
||||
pub async fn get_schedule_snapshot(&'_ self) -> MappedMutexGuard<'_, ScheduleSnapshot> {
|
||||
let snapshot =
|
||||
MutexGuard::<'_, Schedule>::map(self.schedule.lock().await, |schedule| unsafe {
|
||||
schedule.snapshot.assume_init_mut()
|
||||
});
|
||||
|
||||
snapshot
|
||||
None
|
||||
}
|
||||
|
||||
pub async fn get_database(&'_ self) -> MutexGuard<'_, PgConnection> {
|
||||
@@ -83,6 +102,6 @@ impl AppState {
|
||||
}
|
||||
|
||||
/// Create a new object web::Data<AppState>.
|
||||
pub async fn new_app_state() -> Result<web::Data<AppState>, self::schedule::Error> {
|
||||
pub async fn new_app_state() -> Result<web::Data<AppState>, Box<dyn std::error::Error>> {
|
||||
Ok(web::Data::new(AppState::new().await?))
|
||||
}
|
||||
|
||||
@@ -1,290 +0,0 @@
|
||||
use crate::state::env::AppEnv;
|
||||
use crate::utility::hasher::DigestHasher;
|
||||
use chrono::{DateTime, Utc};
|
||||
use derive_more::{Display, Error};
|
||||
use schedule_parser::parse_xls;
|
||||
use schedule_parser::schema::{ParseError, ParseResult};
|
||||
use sha1::{Digest, Sha1};
|
||||
use std::hash::Hash;
|
||||
use std::mem::MaybeUninit;
|
||||
|
||||
use crate::xls_downloader::basic_impl::BasicXlsDownloader;
|
||||
use crate::xls_downloader::interface::{FetchError, XLSDownloader};
|
||||
|
||||
/// Represents errors that can occur during schedule-related operations.
|
||||
#[derive(Debug, Display, Error)]
|
||||
pub enum Error {
|
||||
/// An error occurred while querying the Yandex Cloud API for a URL.
|
||||
///
|
||||
/// This may result from network failures, invalid API credentials, or issues with the Yandex Cloud Function invocation.
|
||||
/// See [`QueryUrlError`] for more details about specific causes.
|
||||
QueryUrlFailed(QueryUrlError),
|
||||
|
||||
/// The schedule snapshot creation process failed.
|
||||
///
|
||||
/// This can happen due to URL conflicts (same URL already in use), failed network requests,
|
||||
/// download errors, or invalid XLS file content. See [`SnapshotCreationError`] for details.
|
||||
SnapshotCreationFailed(SnapshotCreationError),
|
||||
}
|
||||
/// Errors that may occur when querying the Yandex Cloud API to retrieve a URL.
|
||||
#[derive(Debug, Display, Error)]
|
||||
pub enum QueryUrlError {
|
||||
/// Occurs when the request to the Yandex Cloud API fails.
|
||||
///
|
||||
/// This may be due to network issues, invalid API key, incorrect function ID, or other
|
||||
/// problems with the Yandex Cloud Function invocation.
|
||||
#[display("An error occurred during the request to the Yandex Cloud API: {_0}")]
|
||||
RequestFailed(reqwest::Error),
|
||||
}
|
||||
|
||||
/// Errors that may occur during the creation of a schedule snapshot.
|
||||
#[derive(Debug, Display, Error)]
|
||||
pub enum SnapshotCreationError {
|
||||
/// The URL is the same as the one already being used (no update needed).
|
||||
#[display("The URL is the same as the one already being used.")]
|
||||
SameUrl,
|
||||
|
||||
/// The URL query for the XLS file failed to execute, either due to network issues or invalid API parameters.
|
||||
#[display("Failed to fetch URL: {_0}")]
|
||||
FetchFailed(FetchError),
|
||||
|
||||
/// Downloading the XLS file content failed after successfully obtaining the URL.
|
||||
#[display("Download failed: {_0}")]
|
||||
DownloadFailed(FetchError),
|
||||
|
||||
/// The XLS file could not be parsed into a valid schedule format.
|
||||
#[display("Schedule data is invalid: {_0}")]
|
||||
InvalidSchedule(ParseError),
|
||||
}
|
||||
|
||||
/// Represents a snapshot of the schedule parsed from an XLS file.
|
||||
#[derive(Clone)]
|
||||
pub struct ScheduleSnapshot {
|
||||
/// Timestamp when the Polytechnic website was queried for the schedule.
|
||||
pub fetched_at: DateTime<Utc>,
|
||||
|
||||
/// Timestamp indicating when the schedule was last updated on the Polytechnic website.
|
||||
///
|
||||
/// <note>
|
||||
/// This value is determined by the website's content and does not depend on the application.
|
||||
/// </note>
|
||||
pub updated_at: DateTime<Utc>,
|
||||
|
||||
/// URL pointing to the XLS file containing the source schedule data.
|
||||
pub url: String,
|
||||
|
||||
/// Parsed schedule data in the application's internal representation.
|
||||
pub data: ParseResult,
|
||||
}
|
||||
|
||||
impl ScheduleSnapshot {
|
||||
/// Converting the schedule data into a hash.
|
||||
/// ### Important!
|
||||
/// The hash does not depend on the dates.
|
||||
/// If the application is restarted, but the file with source schedule will remain unchanged, then the hash will not change.
|
||||
pub fn hash(&self) -> String {
|
||||
let mut hasher = DigestHasher::from(Sha1::new());
|
||||
|
||||
self.data.teachers.iter().for_each(|e| e.hash(&mut hasher));
|
||||
self.data.groups.iter().for_each(|e| e.hash(&mut hasher));
|
||||
|
||||
hasher.finalize()
|
||||
}
|
||||
|
||||
/// Simply updates the value of [`ScheduleSnapshot::fetched_at`].
|
||||
/// Used for auto-updates.
|
||||
pub fn update(&mut self) {
|
||||
self.fetched_at = Utc::now();
|
||||
}
|
||||
|
||||
/// Constructs a new `ScheduleSnapshot` by downloading and parsing schedule data from the specified URL.
|
||||
///
|
||||
/// This method first checks if the provided URL is the same as the one already configured in the downloader.
|
||||
/// If different, it updates the downloader's URL, fetches the XLS content, parses it, and creates a snapshot.
|
||||
/// Errors are returned for URL conflicts, network issues, download failures, or invalid data.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `downloader`: A mutable reference to an `XLSDownloader` implementation used to fetch and parse the schedule data.
|
||||
/// * `url`: The source URL pointing to the XLS file containing schedule data.
|
||||
///
|
||||
/// returns: Result<ScheduleSnapshot, SnapshotCreationError>
|
||||
pub async fn new(
|
||||
downloader: &mut BasicXlsDownloader,
|
||||
url: String,
|
||||
) -> Result<Self, SnapshotCreationError> {
|
||||
if downloader.url.as_ref().is_some_and(|_url| _url.eq(&url)) {
|
||||
return Err(SnapshotCreationError::SameUrl);
|
||||
}
|
||||
|
||||
let head_result = downloader.set_url(&*url).await.map_err(|error| {
|
||||
if let FetchError::Unknown(error) = &error {
|
||||
sentry::capture_error(&error);
|
||||
}
|
||||
|
||||
SnapshotCreationError::FetchFailed(error)
|
||||
})?;
|
||||
|
||||
let xls_data = downloader
|
||||
.fetch(false)
|
||||
.await
|
||||
.map_err(|error| {
|
||||
if let FetchError::Unknown(error) = &error {
|
||||
sentry::capture_error(&error);
|
||||
}
|
||||
|
||||
SnapshotCreationError::DownloadFailed(error)
|
||||
})?
|
||||
.data
|
||||
.unwrap();
|
||||
|
||||
let parse_result = parse_xls(&xls_data).map_err(|error| {
|
||||
sentry::capture_error(&error);
|
||||
|
||||
SnapshotCreationError::InvalidSchedule(error)
|
||||
})?;
|
||||
|
||||
Ok(ScheduleSnapshot {
|
||||
fetched_at: head_result.requested_at,
|
||||
updated_at: head_result.uploaded_at,
|
||||
url,
|
||||
data: parse_result,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Schedule {
|
||||
pub snapshot: MaybeUninit<ScheduleSnapshot>,
|
||||
}
|
||||
|
||||
impl Default for Schedule {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
snapshot: MaybeUninit::uninit(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Schedule {
|
||||
/// Queries the Yandex Cloud Function (FaaS) to obtain a URL for the schedule file.
|
||||
///
|
||||
/// This sends a POST request to the specified Yandex Cloud Function endpoint,
|
||||
/// using the provided API key for authentication. The returned URI is combined
|
||||
/// with the "https://politehnikum-eng.ru" base domain to form the complete URL.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `api_key` - Authentication token for Yandex Cloud API
|
||||
/// * `func_id` - ID of the target Yandex Cloud Function to invoke
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Result containing:
|
||||
/// - `Ok(String)` - Complete URL constructed from the Function's response
|
||||
/// - `Err(QueryUrlError)` - If the request or response processing fails
|
||||
async fn query_url(api_key: &str, func_id: &str) -> Result<String, QueryUrlError> {
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let uri = client
|
||||
.post(format!(
|
||||
"https://functions.yandexcloud.net/{}?integration=raw",
|
||||
func_id
|
||||
))
|
||||
.header("Authorization", format!("Api-Key {}", api_key))
|
||||
.send()
|
||||
.await
|
||||
.map_err(|error| QueryUrlError::RequestFailed(error))?
|
||||
.text()
|
||||
.await
|
||||
.map_err(|error| QueryUrlError::RequestFailed(error))?;
|
||||
|
||||
Ok(format!("https://politehnikum-eng.ru{}", uri.trim()))
|
||||
}
|
||||
|
||||
/// Initializes the schedule by fetching the URL from the environment or Yandex Cloud Function (FaaS)
|
||||
/// and creating a [`ScheduleSnapshot`] with the downloaded data.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `downloader`: Mutable reference to an `XLSDownloader` implementation used to fetch and parse the schedule
|
||||
/// * `app_env`: Reference to the application environment containing either a predefined URL or Yandex Cloud credentials
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns `Ok(())` if the snapshot was successfully initialized, or an `Error` if:
|
||||
/// - URL query to Yandex Cloud failed ([`QueryUrlError`])
|
||||
/// - Schedule snapshot creation failed ([`SnapshotCreationError`])
|
||||
pub async fn init(
|
||||
&mut self,
|
||||
downloader: &mut BasicXlsDownloader,
|
||||
app_env: &AppEnv,
|
||||
) -> Result<(), Error> {
|
||||
let url = if let Some(url) = &app_env.schedule.url {
|
||||
log::info!("The default link {} will be used", url);
|
||||
url.clone()
|
||||
} else {
|
||||
log::info!("Obtaining a link using FaaS...");
|
||||
Self::query_url(
|
||||
&*app_env.yandex_cloud.api_key,
|
||||
&*app_env.yandex_cloud.func_id,
|
||||
)
|
||||
.await
|
||||
.map_err(|error| Error::QueryUrlFailed(error))?
|
||||
};
|
||||
|
||||
log::info!("For the initial setup, a link {} will be used", url);
|
||||
|
||||
let snapshot = ScheduleSnapshot::new(downloader, url)
|
||||
.await
|
||||
.map_err(|error| Error::SnapshotCreationFailed(error))?;
|
||||
|
||||
log::info!("Schedule snapshot successfully created!");
|
||||
|
||||
self.snapshot.write(snapshot);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Updates the schedule snapshot by querying the latest URL from FaaS and checking for changes.
|
||||
/// If the URL hasn't changed, only updates the [`fetched_at`] timestamp. If changed, downloads
|
||||
/// and parses the new schedule data.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `downloader`: XLS file downloader used to fetch and parse the schedule data
|
||||
/// * `app_env`: Application environment containing Yandex Cloud configuration and auto-update settings
|
||||
///
|
||||
/// returns: `Result<(), Error>` - Returns error if URL query fails or schedule parsing encounters issues
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Uses `unsafe` to access the initialized snapshot, guaranteed valid by prior `init()` call
|
||||
#[allow(unused)] // TODO: сделать авто апдейт
|
||||
pub async fn update(
|
||||
&mut self,
|
||||
downloader: &mut BasicXlsDownloader,
|
||||
app_env: &AppEnv,
|
||||
) -> Result<(), Error> {
|
||||
assert!(app_env.schedule.auto_update);
|
||||
|
||||
let url = Self::query_url(
|
||||
&*app_env.yandex_cloud.api_key,
|
||||
&*app_env.yandex_cloud.func_id,
|
||||
)
|
||||
.await
|
||||
.map_err(|error| Error::QueryUrlFailed(error))?;
|
||||
|
||||
let snapshot = match ScheduleSnapshot::new(downloader, url).await {
|
||||
Ok(snapshot) => snapshot,
|
||||
Err(SnapshotCreationError::SameUrl) => {
|
||||
unsafe { self.snapshot.assume_init_mut() }.update();
|
||||
return Ok(());
|
||||
}
|
||||
Err(error) => return Err(Error::SnapshotCreationFailed(error)),
|
||||
};
|
||||
|
||||
self.snapshot.write(snapshot);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,8 @@
|
||||
#[cfg(test)]
|
||||
pub(crate) mod tests {
|
||||
use crate::state::{AppState, ScheduleSnapshot, new_app_state};
|
||||
use crate::state::{new_app_state, AppState};
|
||||
use actix_web::web;
|
||||
use log::info;
|
||||
use schedule_parser::test_utils::test_result;
|
||||
use std::default::Default;
|
||||
use tokio::sync::OnceCell;
|
||||
|
||||
pub fn test_env() {
|
||||
@@ -17,19 +15,12 @@ pub(crate) mod tests {
|
||||
pub async fn test_app_state() -> web::Data<AppState> {
|
||||
let state = new_app_state().await.unwrap();
|
||||
|
||||
state.get_schedule().await.snapshot.write(ScheduleSnapshot {
|
||||
fetched_at: Default::default(),
|
||||
updated_at: Default::default(),
|
||||
url: "".to_string(),
|
||||
data: test_result().unwrap(),
|
||||
});
|
||||
|
||||
state.clone()
|
||||
}
|
||||
|
||||
pub async fn static_app_state() -> web::Data<AppState> {
|
||||
static STATE: OnceCell<web::Data<AppState>> = OnceCell::const_new();
|
||||
|
||||
|
||||
STATE.get_or_init(|| test_app_state()).await.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
use sha1::Digest;
|
||||
use sha1::digest::OutputSizeUser;
|
||||
use sha1::digest::typenum::Unsigned;
|
||||
use std::hash::Hasher;
|
||||
|
||||
/// Hesher returning hash from the algorithm implementing Digest
|
||||
pub struct DigestHasher<D: Digest> {
|
||||
digest: D,
|
||||
}
|
||||
|
||||
impl<D> DigestHasher<D>
|
||||
where
|
||||
D: Digest,
|
||||
{
|
||||
/// Obtain hash.
|
||||
pub fn finalize(self) -> String {
|
||||
static ALPHABET: [char; 16] = [
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
|
||||
];
|
||||
|
||||
let mut hex = String::with_capacity(<D as OutputSizeUser>::OutputSize::USIZE * 2);
|
||||
|
||||
for byte in self.digest.finalize().0.into_iter() {
|
||||
let byte: u8 = byte;
|
||||
|
||||
hex.push(ALPHABET[(byte >> 4) as usize]);
|
||||
hex.push(ALPHABET[(byte & 0xF) as usize]);
|
||||
}
|
||||
|
||||
hex
|
||||
}
|
||||
}
|
||||
|
||||
impl<D> From<D> for DigestHasher<D>
|
||||
where
|
||||
D: Digest,
|
||||
{
|
||||
/// Creating a hash from an algorithm implementing Digest.
|
||||
fn from(digest: D) -> Self {
|
||||
DigestHasher { digest }
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Digest> Hasher for DigestHasher<D> {
|
||||
/// Stopper to prevent calling the standard Hasher result.
|
||||
fn finish(&self) -> u64 {
|
||||
unimplemented!("Do not call finish()");
|
||||
}
|
||||
|
||||
fn write(&mut self, bytes: &[u8]) {
|
||||
self.digest.update(bytes);
|
||||
}
|
||||
}
|
||||
@@ -1,199 +0,0 @@
|
||||
use crate::xls_downloader::interface::{FetchError, FetchOk, FetchResult, XLSDownloader};
|
||||
use chrono::{DateTime, Utc};
|
||||
use std::sync::Arc;
|
||||
|
||||
pub struct BasicXlsDownloader {
|
||||
pub url: Option<String>,
|
||||
}
|
||||
|
||||
async fn fetch_specified(url: &str, head: bool) -> FetchResult {
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let response = if head {
|
||||
client.head(url)
|
||||
} else {
|
||||
client.get(url)
|
||||
}
|
||||
.header("User-Agent", ua_generator::ua::spoof_chrome_ua())
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| FetchError::unknown(Arc::new(e)))?;
|
||||
|
||||
if response.status().as_u16() != 200 {
|
||||
return Err(FetchError::bad_status_code(response.status().as_u16()));
|
||||
}
|
||||
|
||||
let headers = response.headers();
|
||||
|
||||
let content_type = headers
|
||||
.get("Content-Type")
|
||||
.ok_or(FetchError::bad_headers("Content-Type"))?;
|
||||
|
||||
if !headers.contains_key("etag") {
|
||||
return Err(FetchError::bad_headers("etag"));
|
||||
}
|
||||
|
||||
let last_modified = headers
|
||||
.get("last-modified")
|
||||
.ok_or(FetchError::bad_headers("last-modified"))?;
|
||||
|
||||
if content_type != "application/vnd.ms-excel" {
|
||||
return Err(FetchError::bad_content_type(content_type.to_str().unwrap()));
|
||||
}
|
||||
|
||||
let last_modified = DateTime::parse_from_rfc2822(&last_modified.to_str().unwrap())
|
||||
.unwrap()
|
||||
.with_timezone(&Utc);
|
||||
|
||||
Ok(if head {
|
||||
FetchOk::head(last_modified)
|
||||
} else {
|
||||
FetchOk::get(last_modified, response.bytes().await.unwrap().to_vec())
|
||||
})
|
||||
}
|
||||
|
||||
impl BasicXlsDownloader {
|
||||
pub fn new() -> Self {
|
||||
BasicXlsDownloader { url: None }
|
||||
}
|
||||
}
|
||||
|
||||
impl XLSDownloader for BasicXlsDownloader {
|
||||
async fn fetch(&self, head: bool) -> FetchResult {
|
||||
if self.url.is_none() {
|
||||
Err(FetchError::NoUrlProvided)
|
||||
} else {
|
||||
fetch_specified(&*self.url.as_ref().unwrap(), head).await
|
||||
}
|
||||
}
|
||||
|
||||
async fn set_url(&mut self, url: &str) -> FetchResult {
|
||||
let result = fetch_specified(url, true).await;
|
||||
|
||||
if let Ok(_) = result {
|
||||
self.url = Some(url.to_string());
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::xls_downloader::basic_impl::{BasicXlsDownloader, fetch_specified};
|
||||
use crate::xls_downloader::interface::{FetchError, XLSDownloader};
|
||||
|
||||
#[tokio::test]
|
||||
async fn bad_url() {
|
||||
let url = "bad_url";
|
||||
|
||||
let results = [
|
||||
fetch_specified(url, true).await,
|
||||
fetch_specified(url, false).await,
|
||||
];
|
||||
|
||||
assert!(results[0].is_err());
|
||||
assert!(results[1].is_err());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn bad_status_code() {
|
||||
let url = "https://www.google.com/not-found";
|
||||
|
||||
let results = [
|
||||
fetch_specified(url, true).await,
|
||||
fetch_specified(url, false).await,
|
||||
];
|
||||
|
||||
assert!(results[0].is_err());
|
||||
assert!(results[1].is_err());
|
||||
|
||||
let expected_error = FetchError::BadStatusCode { status_code: 404 };
|
||||
|
||||
assert_eq!(*results[0].as_ref().err().unwrap(), expected_error);
|
||||
assert_eq!(*results[1].as_ref().err().unwrap(), expected_error);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn bad_headers() {
|
||||
let url = "https://www.google.com/favicon.ico";
|
||||
|
||||
let results = [
|
||||
fetch_specified(url, true).await,
|
||||
fetch_specified(url, false).await,
|
||||
];
|
||||
|
||||
assert!(results[0].is_err());
|
||||
assert!(results[1].is_err());
|
||||
|
||||
let expected_error = FetchError::BadHeaders {
|
||||
expected_header: "ETag".to_string(),
|
||||
};
|
||||
|
||||
assert_eq!(*results[0].as_ref().err().unwrap(), expected_error);
|
||||
assert_eq!(*results[1].as_ref().err().unwrap(), expected_error);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn bad_content_type() {
|
||||
let url = "https://s3.aero-storage.ldragol.ru/679e5d1145a6ad00843ad3f1/67ddb59fd46303008396ac96%2Fexample.txt";
|
||||
|
||||
let results = [
|
||||
fetch_specified(url, true).await,
|
||||
fetch_specified(url, false).await,
|
||||
];
|
||||
|
||||
assert!(results[0].is_err());
|
||||
assert!(results[1].is_err());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn ok() {
|
||||
let url = "https://s3.aero-storage.ldragol.ru/679e5d1145a6ad00843ad3f1/67ddb5fad46303008396ac97%2Fschedule.xls";
|
||||
|
||||
let results = [
|
||||
fetch_specified(url, true).await,
|
||||
fetch_specified(url, false).await,
|
||||
];
|
||||
|
||||
assert!(results[0].is_ok());
|
||||
assert!(results[1].is_ok());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn downloader_set_ok() {
|
||||
let url = "https://s3.aero-storage.ldragol.ru/679e5d1145a6ad00843ad3f1/67ddb5fad46303008396ac97%2Fschedule.xls";
|
||||
|
||||
let mut downloader = BasicXlsDownloader::new();
|
||||
|
||||
assert!(downloader.set_url(url).await.is_ok());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn downloader_set_err() {
|
||||
let url = "bad_url";
|
||||
|
||||
let mut downloader = BasicXlsDownloader::new();
|
||||
|
||||
assert!(downloader.set_url(url).await.is_err());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn downloader_ok() {
|
||||
let url = "https://s3.aero-storage.ldragol.ru/679e5d1145a6ad00843ad3f1/67ddb5fad46303008396ac97%2Fschedule.xls";
|
||||
|
||||
let mut downloader = BasicXlsDownloader::new();
|
||||
|
||||
assert!(downloader.set_url(url).await.is_ok());
|
||||
assert!(downloader.fetch(false).await.is_ok());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn downloader_no_url_provided() {
|
||||
let downloader = BasicXlsDownloader::new();
|
||||
let result = downloader.fetch(false).await;
|
||||
|
||||
assert!(result.is_err());
|
||||
assert_eq!(result.err().unwrap(), FetchError::NoUrlProvided);
|
||||
}
|
||||
}
|
||||
@@ -1,100 +0,0 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use derive_more::{Display, Error};
|
||||
use std::mem::discriminant;
|
||||
use std::sync::Arc;
|
||||
use utoipa::ToSchema;
|
||||
|
||||
/// XLS data retrieval errors.
|
||||
#[derive(Clone, Debug, ToSchema, Display, Error)]
|
||||
pub enum FetchError {
|
||||
/// File url is not set.
|
||||
#[display("The link to the timetable was not provided earlier.")]
|
||||
NoUrlProvided,
|
||||
|
||||
/// Unknown error.
|
||||
#[display("An unknown error occurred while downloading the file.")]
|
||||
#[schema(value_type = String)]
|
||||
Unknown(Arc<reqwest::Error>),
|
||||
|
||||
/// Server returned a status code different from 200.
|
||||
#[display("Server returned a status code {status_code}.")]
|
||||
BadStatusCode { status_code: u16 },
|
||||
|
||||
/// The url leads to a file of a different type.
|
||||
#[display("The link leads to a file of type '{content_type}'.")]
|
||||
BadContentType { content_type: String },
|
||||
|
||||
/// Server doesn't return expected headers.
|
||||
#[display("Server doesn't return expected header(s) '{expected_header}'.")]
|
||||
BadHeaders { expected_header: String },
|
||||
}
|
||||
|
||||
impl FetchError {
|
||||
pub fn unknown(error: Arc<reqwest::Error>) -> Self {
|
||||
Self::Unknown(error)
|
||||
}
|
||||
|
||||
pub fn bad_status_code(status_code: u16) -> Self {
|
||||
Self::BadStatusCode { status_code }
|
||||
}
|
||||
|
||||
pub fn bad_content_type(content_type: &str) -> Self {
|
||||
Self::BadContentType {
|
||||
content_type: content_type.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn bad_headers(expected_header: &str) -> Self {
|
||||
Self::BadHeaders {
|
||||
expected_header: expected_header.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for FetchError {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
discriminant(self) == discriminant(other)
|
||||
}
|
||||
}
|
||||
|
||||
/// Result of XLS data retrieval.
|
||||
pub struct FetchOk {
|
||||
/// File upload date.
|
||||
pub uploaded_at: DateTime<Utc>,
|
||||
|
||||
/// Date data received.
|
||||
pub requested_at: DateTime<Utc>,
|
||||
|
||||
/// File data.
|
||||
pub data: Option<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl FetchOk {
|
||||
/// Result without file content.
|
||||
pub fn head(uploaded_at: DateTime<Utc>) -> Self {
|
||||
FetchOk {
|
||||
uploaded_at,
|
||||
requested_at: Utc::now(),
|
||||
data: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Full result.
|
||||
pub fn get(uploaded_at: DateTime<Utc>, data: Vec<u8>) -> Self {
|
||||
FetchOk {
|
||||
uploaded_at,
|
||||
requested_at: Utc::now(),
|
||||
data: Some(data),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type FetchResult = Result<FetchOk, FetchError>;
|
||||
|
||||
pub trait XLSDownloader {
|
||||
/// Get data about the file, and optionally its content.
|
||||
async fn fetch(&self, head: bool) -> FetchResult;
|
||||
|
||||
/// Setting the file link.
|
||||
async fn set_url(&mut self, url: &str) -> FetchResult;
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
pub mod basic_impl;
|
||||
pub mod interface;
|
||||
Reference in New Issue
Block a user