Skip to content

Commit

Permalink
made shure that the new connection information is propagated
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Jun 3, 2023
1 parent afffab8 commit 9ff31fd
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 50 deletions.
48 changes: 36 additions & 12 deletions server/calendar/src/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,57 @@ pub fn configure(cfg: &mut web::ServiceConfig) {
cfg.service(calendar_handler);
}

fn get_calendar_url(requested_key: &str, conn: &mut PgConnection) -> QueryResult<String> {
use crate::schema::rooms::dsl::*;
let room = rooms
.filter(key.eq(requested_key))
.first::<crate::models::Room>(conn)?;
Ok(format!(
"https://campus.tum.de/tumonline/wbKalender.wbRessource?pResNr={}",
room.tumonline_calendar_id
))
}

fn get_entries(
requested_key: &str,
args: CalendarQueryArgs,
conn: &mut PgConnection,
) -> QueryResult<Vec<XMLEvent>> {
use crate::schema::calendar::dsl::*;
calendar
.filter(key.eq(&requested_key))
.filter(dtstart.ge(&args.start))
.filter(dtend.le(&args.end))
.load::<XMLEvent>(conn)
}

#[get("/{id}")]
pub async fn calendar_handler(
params: web::Path<String>,
web::Query(args): web::Query<CalendarQueryArgs>,
) -> HttpResponse {
let id = params.into_inner();
let conn = &mut utils::establish_connection();
use crate::schema::calendar::dsl::*;
let results = calendar
.filter(key.eq(&id))
.filter(dtstart.ge(&args.start))
.filter(dtend.le(&args.end))
.load::<XMLEvent>(conn);
match results {
Ok(results) => {
let results = get_entries(&id, args, conn);
let calendar_url = get_calendar_url(&id, conn);
match (results, calendar_url) {
(Ok(results), Ok(calendar_url)) => {
let last_sync = results.iter().map(|e| e.last_scrape).min().unwrap();
let tumonline_room_number = results.iter().map(|e| e.tumonline_id).next().unwrap();
let calendar_url = format!("https://campus.tum.de/tumonline/wbKalender.wbRessource?pResNr={tumonline_room_number}");
let events = results.into_iter().map(Event::from).collect();
HttpResponse::Ok().json(Events {
events,
last_sync,
calendar_url,
})
}
Err(e) => {
error!("Error loading calendar: {e:?}");
(Err(e), _) => {
error!("Error loading calendar entries: {e:?}");
HttpResponse::InternalServerError()
.content_type("text/plain")
.body("Error loading calendar")
}
(_, Err(e)) => {
error!("Error loading calendar_url: {e:?}");
HttpResponse::InternalServerError()
.content_type("text/plain")
.body("Error loading calendar")
Expand Down
11 changes: 10 additions & 1 deletion server/calendar/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use diesel::Insertable;
#[diesel(table_name = crate::schema::calendar)]
pub struct XMLEvent {
pub key: String,
pub tumonline_id: i32,
pub dtstart: NaiveDateTime,
pub dtend: NaiveDateTime,
pub dtstamp: NaiveDateTime,
Expand All @@ -28,3 +27,13 @@ pub struct XMLEvent {
pub comment: String,
pub last_scrape: NaiveDateTime,
}

#[derive(Insertable, Queryable, AsChangeset)]
#[diesel(table_name = crate::schema::rooms)]
pub struct Room {
pub key: String,
pub tumonline_org_id: i32,
pub tumonline_calendar_id: i32,
pub tumonline_room_id: i32,
pub last_scrape: NaiveDateTime,
}
11 changes: 10 additions & 1 deletion server/calendar/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
diesel::table! {
calendar (single_event_id) {
key -> Varchar,
tumonline_id -> Int4,
dtstart -> Timestamp,
dtend -> Timestamp,
dtstamp -> Timestamp,
Expand All @@ -26,3 +25,13 @@ diesel::table! {
last_scrape -> Timestamp,
}
}

diesel::table! {
rooms (key) {
key -> Varchar,
tumonline_org_id -> Int4,
tumonline_calendar_id -> Int4,
tumonline_room_id -> Int4,
last_scrape -> Timestamp,
}
}
2 changes: 1 addition & 1 deletion server/calendar/src/scrape_task/main_api_connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct ReducedRoomProps {
tumonline_room_nr: Option<i32>,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Default)]
pub struct Room {
pub sap_id: String,
pub tumonline_org_id: i32,
Expand Down
12 changes: 4 additions & 8 deletions server/calendar/src/scrape_task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod main_api_connector;
mod scrape_room_task;
pub mod tumonline_calendar_connector;

use crate::scrape_task::main_api_connector::get_all_ids;
use crate::scrape_task::main_api_connector::{get_all_ids, Room};
use crate::scrape_task::scrape_room_task::ScrapeRoomTask;
use crate::scrape_task::tumonline_calendar_connector::{Strategy, XMLEvents};
use crate::utils;
Expand Down Expand Up @@ -65,11 +65,7 @@ impl ScrapeTask {
// It is critical for successfully scraping that we are not blocked.
sleep(Duration::from_millis(50)).await;

work_queue.push(scrape(
(room.sap_id.clone(), room.tumonline_calendar_id),
start.date_naive(),
self.time_window,
));
work_queue.push(scrape(room, start.date_naive(), self.time_window));
}
}
work_queue.next().await;
Expand Down Expand Up @@ -112,11 +108,11 @@ impl ScrapeTask {
}
}

async fn scrape(id: (String, i32), from: NaiveDate, duration: chrono::Duration) {
async fn scrape(room: Room, from: NaiveDate, duration: chrono::Duration) {
let _timer = REQ_TIME_HISTOGRAM.start_timer(); // drop as observe

// request and parse the xml file
let mut request_queue = vec![ScrapeRoomTask::new(id, from, duration)];
let mut request_queue = vec![ScrapeRoomTask::new(room, from, duration)];
let mut success_cnt = 0;
while !request_queue.is_empty() {
let mut new_request_queue = vec![];
Expand Down
34 changes: 13 additions & 21 deletions server/calendar/src/scrape_task/scrape_room_task.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
use chrono::{Duration, NaiveDate};
use crate::scrape_task::main_api_connector::Room;
use chrono::NaiveDate;

#[derive(Clone, Debug)]
pub(crate) struct ScrapeRoomTask {
pub(crate) key: String,
pub(crate) room_id: i32,
pub(crate) room: Room,
pub(crate) from: NaiveDate,
pub(crate) to: NaiveDate,
}

impl ScrapeRoomTask {
pub fn new((key, room_id): (String, i32), from: NaiveDate, duration: Duration) -> Self {
pub fn new(room: Room, from: NaiveDate, duration: chrono::Duration) -> Self {
let to = from + duration;
Self {
key,
room_id,
from,
to,
}
Self { room, from, to }
}
pub fn num_days(&self) -> u64 {
// we want to count from the morning of "from" to the evening of "to" => +1
Expand All @@ -29,14 +24,12 @@ impl ScrapeRoomTask {
let lower_middle = self.from + chrono::Days::new(mid_offset);
(
Self {
key: self.key.clone(),
room_id: self.room_id,
room: self.room.clone(),
from: self.from,
to: lower_middle,
},
Self {
key: self.key.clone(),
room_id: self.room_id,
room: self.room.clone(),
from: lower_middle + chrono::Days::new(1),
to: self.to,
},
Expand All @@ -47,13 +40,14 @@ impl ScrapeRoomTask {
#[cfg(test)]
mod test_scrape_task {
use super::ScrapeRoomTask;
use chrono::{Duration, NaiveDate};
use crate::scrape_task::main_api_connector::Room;
use chrono::NaiveDate;
use pretty_assertions::assert_eq;

#[test]
fn test_split() {
let start = NaiveDate::from_ymd_opt(2020, 1, 1).unwrap();
let task = ScrapeRoomTask::new(("".to_string(), 0), start, Duration::days(365));
let task = ScrapeRoomTask::new(Room::default(), start, chrono::Duration::days(365));
let (o1, o2) = task.split();
assert_eq!(task.from, start);
assert_eq!(task.to, NaiveDate::from_ymd_opt(2020, 12, 31).unwrap());
Expand All @@ -64,8 +58,7 @@ mod test_scrape_task {
#[test]
fn test_split_small() {
let task = ScrapeRoomTask {
key: "".to_string(),
room_id: 0,
room: Room::default(),
from: NaiveDate::from_ymd_opt(2020, 1, 1).unwrap(),
to: NaiveDate::from_ymd_opt(2020, 1, 2).unwrap(),
};
Expand All @@ -82,8 +75,7 @@ mod test_scrape_task {
#[test]
fn test_num_days() {
let mut task = ScrapeRoomTask {
key: "".to_string(),
room_id: 0,
room: Room::default(),
from: NaiveDate::from_ymd_opt(2020, 1, 1).unwrap(),
to: NaiveDate::from_ymd_opt(2020, 1, 1).unwrap(),
};
Expand All @@ -96,7 +88,7 @@ mod test_scrape_task {
#[test]
fn test_same_day() {
let start = NaiveDate::from_ymd_opt(2020, 1, 1).unwrap();
let task = ScrapeRoomTask::new(("".to_string(), 0), start, Duration::days(0));
let task = ScrapeRoomTask::new(Room::default(), start, chrono::Duration::days(0));
assert_eq!(task.from, start);
assert_eq!(task.to, NaiveDate::from_ymd_opt(2020, 1, 1).unwrap());
assert_eq!(task.num_days(), 1);
Expand Down
12 changes: 6 additions & 6 deletions server/calendar/src/scrape_task/tumonline_calendar_connector.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::models::XMLEvent;
use crate::scrape_task::main_api_connector::Room;
use crate::scrape_task::scrape_room_task::ScrapeRoomTask;
use crate::{schema, utils};
use chrono::{NaiveDateTime, Utc};
Expand Down Expand Up @@ -61,7 +62,7 @@ fn construct_hm(elem: &Element) -> HashMap<String, String> {
hm
}

fn xml_event_from_hm(key: String, tumonline_id: i32, hm: HashMap<String, String>) -> XMLEvent {
fn xml_event_from_hm(key: String, hm: HashMap<String, String>) -> XMLEvent {
let other_keys = hm
.keys()
.filter(|s| {
Expand Down Expand Up @@ -97,7 +98,6 @@ fn xml_event_from_hm(key: String, tumonline_id: i32, hm: HashMap<String, String>
}
XMLEvent {
key,
tumonline_id,
dtstart: extract_dt(&hm, "dtstart").unwrap(),
dtend: extract_dt(&hm, "dtend").unwrap(),
dtstamp: extract_dt(&hm, "dtstamp").unwrap(),
Expand Down Expand Up @@ -169,7 +169,7 @@ impl XMLEvents {
}
})
}
fn new(key: String, tumonline_id: i32, body: String) -> Option<Self> {
fn new(room: Room, body: String) -> Option<Self> {
let root = body.parse::<Element>();
let root = match root {
Ok(root) => root,
Expand Down Expand Up @@ -202,7 +202,7 @@ impl XMLEvents {
_ => false,
};
if valid_status {
events.push(xml_event_from_hm(key.clone(), tumonline_id, hm));
events.push(xml_event_from_hm(room.sap_id.clone(), hm));
}
}
Some(XMLEvents { events })
Expand All @@ -219,7 +219,7 @@ impl XMLEvents {
//get the xml file from TUMonline
let url = format!(
"{CALENDAR_BASE_URL}?roomID={room_id}&timeMode=absolute&fromDate={from}&untilDate={to}&token={token}&buildingCode=",
room_id=task.room_id,
room_id=task.room.tumonline_calendar_id,
from=task.from.format("%Y%m%d"),
to=task.to.format("%Y%m%d")
);
Expand All @@ -234,7 +234,7 @@ impl XMLEvents {
let backoff_duration = Duration::from_millis(backoff_ms);
match body {
RequestStatus::Success(body) => {
return XMLEvents::new(task.key, task.room_id, body).ok_or(Strategy::NoRetry);
return XMLEvents::new(task.room.clone(), body).ok_or(Strategy::NoRetry);
}
// This consistently means, that there is no data for this room
RequestStatus::NotFound => return Err(Strategy::NoRetry),
Expand Down

0 comments on commit 9ff31fd

Please sign in to comment.