From e00f809d00bb89a430f7291a10b9750418dd512c Mon Sep 17 00:00:00 2001 From: Dominic Grimm Date: Fri, 3 Mar 2023 17:13:19 +0100 Subject: [PATCH] Update --- bvplan/Cargo.lock | 49 +++-- bvplan/Cargo.toml | 2 - bvplan/js/bvplan.js | 4 - bvplan/scss/bvplan.scss | 8 +- bvplan/src/bin/web.rs | 41 ++++- bvplan/src/cache.rs | 50 +---- bvplan/src/templates.rs | 37 +++- bvplan/src/worker/get_substitutions.rs | 43 +++-- bvplan/src/worker/mod.rs | 12 +- bvplan/templates/base.html | 11 +- bvplan/templates/bvplan.html | 242 ++++++++++++------------- bvplan/templates/bvplan_display.html | 144 +++++++++++++++ docker-compose.yml | 10 +- 13 files changed, 416 insertions(+), 237 deletions(-) create mode 100644 bvplan/templates/bvplan_display.html diff --git a/bvplan/Cargo.lock b/bvplan/Cargo.lock index 69f5656..7c4a7f0 100644 --- a/bvplan/Cargo.lock +++ b/bvplan/Cargo.lock @@ -21,15 +21,15 @@ dependencies = [ [[package]] name = "actix-http" -version = "3.3.0" +version = "3.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0070905b2c4a98d184c4e81025253cb192aa8a73827553f38e9410801ceb35bb" +checksum = "c2079246596c18b4a33e274ae10c0e50613f4d32a4198e09c7b93771013fed74" dependencies = [ "actix-codec", "actix-rt", "actix-service", "actix-utils", - "ahash", + "ahash 0.8.3", "base64", "bitflags", "brotli", @@ -145,7 +145,7 @@ dependencies = [ "actix-service", "actix-utils", "actix-web-codegen", - "ahash", + "ahash 0.7.6", "bytes", "bytestring", "cfg-if", @@ -221,6 +221,18 @@ dependencies = [ "version_check", ] +[[package]] +name = "ahash" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c99f64d1e06488f620f932677e24bc6e2897582980441ae90a671415bd7ec2f" +dependencies = [ + "cfg-if", + "getrandom 0.2.8", + "once_cell", + "version_check", +] + [[package]] name = "aho-corasick" version = "0.7.20" @@ -450,12 +462,11 @@ dependencies = [ [[package]] name = "async-lock" -version = "2.6.0" +version = "2.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8101efe8695a6c17e02911402145357e718ac92d3ff88ae8419e84b1707b685" +checksum = "fa24f727524730b077666307f2734b4a1a1c57acb79193127dcc8914d5242dd7" dependencies = [ "event-listener", - "futures-lite", ] [[package]] @@ -636,7 +647,6 @@ dependencies = [ "env_logger", "envconfig", "fancy-regex", - "flate2", "lazy_static", "log", "minify-html", @@ -644,7 +654,6 @@ dependencies = [ "reqwest", "scraper", "serde", - "serde_json", "static-files", "stdext", "tikv-jemallocator", @@ -694,8 +703,8 @@ dependencies = [ [[package]] name = "celery" -version = "0.5.2" -source = "git+https://github.com/rusty-celery/rusty-celery.git?branch=main#6a71f338dc6decdb647ebfa48628444e1cfc6582" +version = "0.5.3" +source = "git+https://github.com/rusty-celery/rusty-celery.git?branch=main#32cb1c97d3bf8b57a1f4a501ec50024fbb0cea7f" dependencies = [ "async-trait", "base64", @@ -723,8 +732,8 @@ dependencies = [ [[package]] name = "celery-codegen" -version = "0.5.2" -source = "git+https://github.com/rusty-celery/rusty-celery.git?branch=main#6a71f338dc6decdb647ebfa48628444e1cfc6582" +version = "0.5.3" +source = "git+https://github.com/rusty-celery/rusty-celery.git?branch=main#32cb1c97d3bf8b57a1f4a501ec50024fbb0cea7f" dependencies = [ "proc-macro2", "quote", @@ -876,9 +885,9 @@ dependencies = [ [[package]] name = "crossbeam-utils" -version = "0.8.14" +version = "0.8.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fb766fa798726286dbbb842f174001dab8abc7b627a1dd86e0b7222a95d929f" +checksum = "3c063cd8cc95f5c377ed0d4b49a4b21f632396ff690e8470c29b3359b346984b" dependencies = [ "cfg-if", ] @@ -1674,9 +1683,9 @@ checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440" [[package]] name = "jobserver" -version = "0.1.25" +version = "0.1.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "068b1ee6743e4d11fb9c6a1e6064b3693a1b600e7f5f5988047d98b3dc9fb90b" +checksum = "936cfd212a0155903bcbc060e316fb6cc7cbf2e1907329391ebadc1fe0ce77c2" dependencies = [ "libc", ] @@ -3099,9 +3108,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.25.0" +version = "1.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8e00990ebabbe4c14c08aca901caed183ecd5c09562a12c824bb53d3c3fd3af" +checksum = "03201d01c3c27a29c8a5cee5b55a93ddae1ccf6f08f65365c2c918f8c1b76f64" dependencies = [ "autocfg", "bytes", @@ -3114,7 +3123,7 @@ dependencies = [ "signal-hook-registry", "socket2", "tokio-macros", - "windows-sys 0.42.0", + "windows-sys 0.45.0", ] [[package]] diff --git a/bvplan/Cargo.toml b/bvplan/Cargo.toml index 505522f..0beb34f 100644 --- a/bvplan/Cargo.toml +++ b/bvplan/Cargo.toml @@ -28,7 +28,6 @@ diesel = { version = "2.0.2", features = ["i-implement-a-third-party-backend-and env_logger = "0.9.3" envconfig = "0.10.0" fancy-regex = "0.11.0" -flate2 = "1.0.25" lazy_static = "1.4.0" log = "0.4.17" minify-html = "0.10.8" @@ -36,7 +35,6 @@ r2d2_redis = "0.14.0" reqwest = "0.11.13" scraper = "0.14.0" serde = "1.0.148" -serde_json = "1.0.93" static-files = "0.2.3" stdext = "0.3.1" tokio = { version = "1.22.0", features = ["full"] } diff --git a/bvplan/js/bvplan.js b/bvplan/js/bvplan.js index 8236f1b..d28260b 100644 --- a/bvplan/js/bvplan.js +++ b/bvplan/js/bvplan.js @@ -38,7 +38,3 @@ function scrollDown() { } window.scrollTo(0, 0); -setTimeout( - $(document).height() > $(window).height() ? scrollDown : reload, - waitDelay -); diff --git a/bvplan/scss/bvplan.scss b/bvplan/scss/bvplan.scss index e9b9b1f..804f962 100644 --- a/bvplan/scss/bvplan.scss +++ b/bvplan/scss/bvplan.scss @@ -7,10 +7,13 @@ body { body { min-height: 100%; margin: 0 5vw; - overflow: hidden; display: flex; flex-direction: column; font-family: Arial, Helvetica, sans-serif; + + &.display { + overflow: hidden; + } } #info { @@ -84,7 +87,8 @@ body { } #footer { - margin: auto 0 2vh; + margin-top: auto; + padding-bottom: 2vh; text-align: center; p { diff --git a/bvplan/src/bin/web.rs b/bvplan/src/bin/web.rs index a3b7dba..0c3300f 100644 --- a/bvplan/src/bin/web.rs +++ b/bvplan/src/bin/web.rs @@ -38,7 +38,45 @@ async fn index(redis_pool: web::Data) -> HttpResponse { }; if let Some(html) = match redis::cmd("GET") - .arg(cache::keys::SUBSTITUTIONS_HTML) + .arg(cache::keys::HTML) + .query::>(redis_conn.deref_mut()) + { + Ok(x) => x, + Err(_) => { + return templates::StatusCode { + status_code: http::StatusCode::INTERNAL_SERVER_ERROR, + message: None, + } + .to_response() + } + } { + HttpResponse::Accepted() + .content_type("text/html; charset=utf-8") + .body(html) + } else { + templates::StatusCode { + status_code: http::StatusCode::INTERNAL_SERVER_ERROR, + message: None, + } + .to_response() + } +} + +#[get("/display")] +async fn display(redis_pool: web::Data) -> HttpResponse { + let redis_conn = &mut match redis_pool.get() { + Ok(x) => x, + Err(_) => { + return templates::StatusCode { + status_code: http::StatusCode::INTERNAL_SERVER_ERROR, + message: None, + } + .to_response() + } + }; + + if let Some(html) = match redis::cmd("GET") + .arg(cache::keys::DISPLAY_HTML) .query::>(redis_conn.deref_mut()) { Ok(x) => x, @@ -74,6 +112,7 @@ async fn main() -> std::io::Result<()> { .wrap(middleware::Compress::default()) .wrap(middleware::Logger::default()) .service(index) + .service(display) .service(ResourceFiles::new("/static", generated)) .default_service(web::route().to(not_found)) }) diff --git a/bvplan/src/cache.rs b/bvplan/src/cache.rs index 64012f5..44eb80c 100644 --- a/bvplan/src/cache.rs +++ b/bvplan/src/cache.rs @@ -4,7 +4,6 @@ use lazy_static::lazy_static; use r2d2_redis::{r2d2, redis, RedisConnectionManager}; use crate::config; -use crate::db; pub type RedisPool = r2d2::Pool; pub type ConnectionPool = r2d2::PooledConnection; @@ -25,52 +24,7 @@ lazy_static! { } pub mod keys { - use super::*; - // pub const SUBSTITUTIONS: &str = "substs"; - pub const SUBSTITUTIONS_HTML: &str = "substs_html"; - - pub mod substitutions { - use serde::{Deserialize, Serialize}; - - use super::*; - - #[derive(Deserialize, Serialize, Debug)] - pub struct Substitution { - #[serde(rename = "p")] - pub period: usize, - #[serde(rename = "cl")] - pub classes: Vec, - #[serde(rename = "ps")] - pub prev_subject: Option, - #[serde(rename = "s")] - pub subject: Option, - #[serde(rename = "t")] - pub teachers: Option>, - #[serde(rename = "pr")] - pub prev_rooms: Vec, - #[serde(rename = "r")] - pub rooms: Vec, - #[serde(rename = "tx")] - pub text: Option, - } - - #[derive(Deserialize, Serialize, Debug)] - pub struct SubstitutionQuery { - #[serde(rename = "d")] - pub date: String, - #[serde(rename = "w")] - pub week_type: db::models::WeekType, - #[serde(rename = "q")] - pub queried_at: String, - #[serde(rename = "i")] - pub last_import_time: String, - #[serde(rename = "y")] - pub schoolyear: String, - #[serde(rename = "t")] - pub tenant: String, - #[serde(rename = "s")] - pub substitutions: Vec, - } - } + pub const HTML: &str = "html"; + pub const DISPLAY_HTML: &str = "disp_html"; } diff --git a/bvplan/src/templates.rs b/bvplan/src/templates.rs index 59be487..d5859f9 100644 --- a/bvplan/src/templates.rs +++ b/bvplan/src/templates.rs @@ -2,8 +2,6 @@ use actix_web::{body::BoxBody, http, HttpResponse, HttpResponseBuilder, Response use askama::Template; use std::fmt; -use crate::cache; - struct ActixError(askama::Error); impl fmt::Debug for ActixError { @@ -37,10 +35,43 @@ impl TemplateToResponseWithStatusCode for T { } } +pub mod substitutions { + use crate::db; + + #[derive(Clone, Debug)] + pub struct Substitution { + pub period: usize, + pub classes: Vec, + pub prev_subject: Option, + pub subject: Option, + pub teachers: Option>, + pub prev_rooms: Vec, + pub rooms: Vec, + pub text: Option, + } + + #[derive(Clone, Debug)] + pub struct SubstitutionQuery { + pub date: String, + pub week_type: db::models::WeekType, + pub queried_at: String, + pub last_import_time: String, + pub schoolyear: String, + pub tenant: String, + pub substitutions: Vec, + } +} + #[derive(Template)] #[template(path = "bvplan.html")] pub struct BVplan { - pub data: cache::keys::substitutions::SubstitutionQuery, + pub data: substitutions::SubstitutionQuery, +} + +#[derive(Template)] +#[template(path = "bvplan_display.html")] +pub struct BVplanDisplay { + pub data: substitutions::SubstitutionQuery, } #[derive(Template)] diff --git a/bvplan/src/worker/get_substitutions.rs b/bvplan/src/worker/get_substitutions.rs index c6b291e..901ac57 100644 --- a/bvplan/src/worker/get_substitutions.rs +++ b/bvplan/src/worker/get_substitutions.rs @@ -7,9 +7,6 @@ use diesel::prelude::*; use fancy_regex::Regex; use lazy_static::lazy_static; use r2d2_redis::redis; -use std::fs; -use std::io::Write; -use std::path::Path; use std::thread; use std::time::Duration; use std::vec; @@ -371,7 +368,7 @@ fn cache_substitutions( } }; - Ok(Some(cache::keys::substitutions::Substitution { + Ok(Some(templates::substitutions::Substitution { period: get_period(×, true, s.start_time) .context("Could not find period from start time")?, classes, @@ -427,7 +424,7 @@ fn cache_substitutions( ) }); - let query = cache::keys::substitutions::SubstitutionQuery { + let query = templates::substitutions::SubstitutionQuery { date: format!( "{} {}", date.format("%d.%m.%Y"), @@ -455,22 +452,30 @@ fn cache_substitutions( substitutions, }; - fs::write( - Path::new("/backups").join(format!( - "{}_{}_{}.json.gz", - date, queried_at, substitution_query_id - )), - { - let mut e = flate2::write::ZlibEncoder::new(vec![], flate2::Compression::best()); - e.write_all(serde_json::to_string(&query)?.as_bytes())?; - e.finish()? - }, - )?; - redis::cmd("SET") - .arg(cache::keys::SUBSTITUTIONS_HTML) + .arg(cache::keys::HTML) .arg(minify_html::minify( - templates::BVplan { data: query }.render()?.as_bytes(), + templates::BVplan { + data: query.to_owned(), + } + .render()? + .as_bytes(), + &minify_html::Cfg { + do_not_minify_doctype: false, + ensure_spec_compliant_unquoted_attribute_values: false, + keep_spaces_between_attributes: false, + minify_css: true, + minify_js: true, + ..Default::default() + }, + )) + .query::<()>(redis_conn)?; + redis::cmd("SET") + .arg(cache::keys::DISPLAY_HTML) + .arg(minify_html::minify( + templates::BVplanDisplay { data: query } + .render()? + .as_bytes(), &minify_html::Cfg { do_not_minify_doctype: false, ensure_spec_compliant_unquoted_attribute_values: false, diff --git a/bvplan/src/worker/mod.rs b/bvplan/src/worker/mod.rs index f91e81b..cdc4162 100644 --- a/bvplan/src/worker/mod.rs +++ b/bvplan/src/worker/mod.rs @@ -25,7 +25,7 @@ pub async fn init() { tasks = [ get_substitutions::get_substitutions, update_meta::update_meta, - cleanup::cleanup, + // cleanup::cleanup, ], task_routes = [ "*" => QUEUE_NAME, @@ -57,11 +57,11 @@ pub fn beat() -> impl std::future::Future< schedule = DeltaSchedule::new(Duration::from_hours(6)), args = (), }, - "cleanup" => { - cleanup::cleanup, - schedule = DeltaSchedule::new(Duration::from_hours(1)), - args = (), - } + // "cleanup" => { + // cleanup::cleanup, + // schedule = DeltaSchedule::new(Duration::from_hours(1)), + // args = (), + // } ], task_routes = [ "*" => QUEUE_NAME, diff --git a/bvplan/templates/base.html b/bvplan/templates/base.html index d047e50..8462088 100644 --- a/bvplan/templates/base.html +++ b/bvplan/templates/base.html @@ -15,6 +15,13 @@ {% block head %}{% endblock %} + + - - {% block body %}{% endblock %} - + {% block body %}{% endblock %} diff --git a/bvplan/templates/bvplan.html b/bvplan/templates/bvplan.html index 05cd9d7..9723748 100644 --- a/bvplan/templates/bvplan.html +++ b/bvplan/templates/bvplan.html @@ -6,138 +6,136 @@ - - {% endblock %} {% block body %} -
-
-

BVplan

- dergrimm.net -
-
- {{ data.tenant }}, {{ data.schoolyear }} -
- Stand: {{ data.queried_at }} -
- Vertretungsplan (offline): {{ data.last_import_time }} -
-
+ +
+
+

BVplan

+ dergrimm.net +
+
+ {{ data.tenant }}, {{ data.schoolyear }} +
+ Stand: {{ data.queried_at }} +
+ Vertretungsplan (offline): {{ data.last_import_time }} +
+
-
- - +
+
- {{ data.date }}, Woche {{ data.week_type }} -
+ - - - - - - - - - - + + + + + + + + + + - - - - - - - - - - - - + + + + + + + + + + + + - - {% for subst in data.substitutions %} - - - - + {% for subst in data.substitutions %} + + + + + + + - + - - - - - - {% endfor %} - -
+ {{ data.date }}, Woche {{ data.week_type }} +
StundeKlasse(n)(Fach)FachVertreter(Raum)RaumText
StundeKlasse(n)(Fach)FachVertreter(Raum)RaumText
{{ subst.period }}{{ subst.classes|join(", ") }} - {% match subst.prev_subject %} - {% when Some with (x) %} - {{ x }} - {% when None %} +
{{ subst.period }}{{ subst.classes|join(", ") }} + {% match subst.prev_subject %} + {% when Some with (x) %} + {{ x }} + {% when None %} + --- + {% endmatch %} + + {% match subst.subject %} + {% when Some with (x) %} + {{ x }} + {% when None %} + --- + {% endmatch %} + + {% match subst.teachers %} + {% when Some with (x) %} + {{ x|join(", ") }} + {% when None %} + --- + {% endmatch %} + + {% if subst.prev_rooms.is_empty() %} --- - {% endmatch %} - - {% match subst.subject %} - {% when Some with (x) %} - {{ x }} - {% when None %} + {% else %} + {{ subst.prev_rooms|join(", ") }} + {% endif %} + + {% if subst.rooms.is_empty() %} --- - {% endmatch %} - - {% match subst.teachers %} - {% when Some with (x) %} - {{ x|join(", ") }} - {% when None %} - --- - {% endmatch %} - - {% if subst.prev_rooms.is_empty() %} - --- - {% else %} - {{ subst.prev_rooms|join(", ") }} - {% endif %} - - {% if subst.rooms.is_empty() %} - --- - {% else %} - {{ subst.rooms|join(", ") }} - {% endif %} - - {% match subst.text %} - {% when Some with (x) %} - {{ x }} - {% when None %} - {% endmatch %} -
-
+ {% else %} + {{ subst.rooms|join(", ") }} + {% endif %} + + + {% match subst.text %} + {% when Some with (x) %} + {{ x }} + {% when None %} + {% endmatch %} + + + {% endfor %} + + + - + - + + + {% endblock %} diff --git a/bvplan/templates/bvplan_display.html b/bvplan/templates/bvplan_display.html new file mode 100644 index 0000000..afddc27 --- /dev/null +++ b/bvplan/templates/bvplan_display.html @@ -0,0 +1,144 @@ +{% extends "base.html" %} + +{% block title %}BVplan{% endblock %} + +{% block head %} + + + +{% endblock %} + +{% block body %} + +
+
+

BVplan

+ dergrimm.net +
+
+ {{ data.tenant }}, {{ data.schoolyear }} +
+ Stand: {{ data.queried_at }} +
+ Vertretungsplan (offline): {{ data.last_import_time }} +
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + {% for subst in data.substitutions %} + + + + + + + + + + + {% endfor %} + +
+ {{ data.date }}, Woche {{ data.week_type }} +
StundeKlasse(n)(Fach)FachVertreter(Raum)RaumText
{{ subst.period }}{{ subst.classes|join(", ") }} + {% match subst.prev_subject %} + {% when Some with (x) %} + {{ x }} + {% when None %} + --- + {% endmatch %} + + {% match subst.subject %} + {% when Some with (x) %} + {{ x }} + {% when None %} + --- + {% endmatch %} + + {% match subst.teachers %} + {% when Some with (x) %} + {{ x|join(", ") }} + {% when None %} + --- + {% endmatch %} + + {% if subst.prev_rooms.is_empty() %} + --- + {% else %} + {{ subst.prev_rooms|join(", ") }} + {% endif %} + + {% if subst.rooms.is_empty() %} + --- + {% else %} + {{ subst.rooms|join(", ") }} + {% endif %} + + {% match subst.text %} + {% when Some with (x) %} + {{ x }} + {% when None %} + {% endmatch %} +
+
+ + + + + + +{% endblock %} diff --git a/docker-compose.yml b/docker-compose.yml index 640eb6c..7eefb7b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -24,6 +24,9 @@ x-bvplan: BVPLAN_UNTIS_VPLAN_URL: ${BVPLAN_UNTIS_VPLAN_URL} BVPLAN_UNTIS_VPLAN_USERNAME: ${BVPLAN_UNTIS_VPLAN_USERNAME} BVPLAN_UNTIS_VPLAN_PASSWORD: ${BVPLAN_UNTIS_VPLAN_PASSWORD} + volumes: + - /etc/timezone:/etc/timezone:ro + - /etc/localtime:/etc/localtime:ro services: nginx: @@ -82,10 +85,6 @@ services: worker: <<: *bvplan command: worker - volumes: - - /etc/timezone:/etc/timezone:ro - - /etc/localtime:/etc/localtime:ro - - ./data/backups:/backups web: <<: *bvplan @@ -95,9 +94,6 @@ services: - rabbitmq - worker - redis - volumes: - - /etc/timezone:/etc/timezone:ro - - /etc/localtime:/etc/localtime:ro volumes: db: