Skip to content

Commit

Permalink
Implemented faster startups (#1257)
Browse files Browse the repository at this point in the history
implemented faster startups
  • Loading branch information
CommanderStorm authored Jun 29, 2024
1 parent 976ddd7 commit 6f26d5b
Show file tree
Hide file tree
Showing 20 changed files with 299 additions and 127 deletions.

This file was deleted.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Add down migration script here
DROP INDEX IF EXISTS hash_lut;
alter table de drop column hash;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Add up migration script here
alter table de add hash BIGINT default 0; -- the chance of an empty hash is astronomically slim
CREATE INDEX IF NOT EXISTS hash_lut ON de(key, hash);
16 changes: 8 additions & 8 deletions server/main-api/src/calendar/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ impl Event {
r#"INSERT INTO calendar (id,room_code,start_at,end_at,stp_title_de,stp_title_en,stp_type,entry_type,detailed_entry_type)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
ON CONFLICT (id) DO UPDATE SET
room_code = $2,
start_at = $3,
end_at = $4,
stp_title_de = $5,
stp_title_en = $6,
stp_type = $7,
entry_type = $8,
detailed_entry_type = $9"#,
room_code = EXCLUDED.room_code,
start_at = EXCLUDED.start_at,
end_at = EXCLUDED.end_at,
stp_title_de = EXCLUDED.stp_title_de,
stp_title_en = EXCLUDED.stp_title_en,
stp_type = EXCLUDED.stp_type,
entry_type = EXCLUDED.entry_type,
detailed_entry_type = EXCLUDED.detailed_entry_type"#,
self.id,
self.room_code,
self.start_at,
Expand Down
37 changes: 21 additions & 16 deletions server/main-api/src/setup/database/alias.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::time::Instant;

use log::info;
use log::debug;
use serde::Deserialize;

#[derive(Debug)]
struct Alias {
pub(super) struct Alias {
alias: String,
key: String, // the key is the id of the entry
r#type: String, // what we display in the url
Expand Down Expand Up @@ -90,9 +90,9 @@ impl Alias {
r#"INSERT INTO aliases (alias, key, type, visible_id)
VALUES ($1, $2, $3, $4)
ON CONFLICT (alias,key) DO UPDATE SET
key = $2,
type = $3,
visible_id = $4"#,
key = EXCLUDED.key,
type = EXCLUDED.type,
visible_id = EXCLUDED.visible_id"#,
self.alias,
self.key,
self.r#type,
Expand All @@ -102,24 +102,29 @@ impl Alias {
.await
}
}

pub async fn load_all_to_db(
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<(), crate::BoxedError> {
pub async fn download_updates(
keys_which_need_updating: &[String],
) -> Result<Vec<Alias>, crate::BoxedError> {
let cdn_url = std::env::var("CDN_URL").unwrap_or_else(|_| "https://nav.tum.de/cdn".to_string());
let raw_aliase = reqwest::get(format!("{cdn_url}/api_data.json"))
Ok(reqwest::get(format!("{cdn_url}/api_data.json"))
.await?
.json::<Vec<AliasData>>()
.await?;
let start = Instant::now();
let set_aliase = raw_aliase
.await?
.into_iter()
.filter(|d| keys_which_need_updating.is_empty() || keys_which_need_updating.contains(&d.id))
.map(AliasIterator::from)
.flat_map(IntoIterator::into_iter);
for task in set_aliase {
.flat_map(IntoIterator::into_iter)
.collect::<Vec<Alias>>())
}
pub async fn load_all_to_db(
aliases: Vec<Alias>,
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<(), crate::BoxedError> {
let start = Instant::now();
for task in aliases {
task.store(tx).await?;
}
info!("loaded aliases in {elapsed:?}", elapsed = start.elapsed());
debug!("loaded aliases in {elapsed:?}", elapsed = start.elapsed());

Ok(())
}
Loading

0 comments on commit 6f26d5b

Please sign in to comment.