Skip to content

Commit

Permalink
made sure that room data is saved to the database
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed May 28, 2023
1 parent 62a4786 commit dad7e39
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion server/calendar/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct XMLEvent {
pub last_scrape: NaiveDateTime,
}

#[derive(Insertable, Queryable, AsChangeset)]
#[derive(Insertable, Queryable, AsChangeset, Clone)]
#[diesel(table_name = crate::schema::rooms)]
pub struct Room {
pub key: String,
Expand Down
44 changes: 42 additions & 2 deletions server/calendar/src/scrape_task/main_api_connector.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use chrono::{NaiveDateTime, Utc};
use diesel::PgConnection;
use log::error;
use regex::Regex;
use serde::Deserialize;
Expand Down Expand Up @@ -56,8 +58,46 @@ pub async fn get_all_ids() -> Vec<Room> {
return vec![];
}
};
match rooms {
let rooms: Vec<Room> = match rooms {
Ok(rooms) => rooms.into_iter().flat_map(Room::from).collect(),
Err(e) => panic!("Failed to parse main-api response: {e:#?}"),
}
};
let start_time = Utc::now().naive_utc();
let conn = &mut crate::utils::establish_connection();
store_in_db(conn, &rooms, &start_time);
delete_stale_results(conn, start_time);
rooms
}

fn store_in_db(conn: &mut PgConnection, rooms_to_store: &[Room], start_time: &NaiveDateTime) {
use crate::schema::rooms::dsl::*;
use diesel::prelude::*;
rooms_to_store
.iter()
.map(|room| crate::models::Room {
key: room.sap_id.clone(),
tumonline_org_id: room.tumonline_org_id,
tumonline_calendar_id: room.tumonline_calendar_id,
tumonline_room_id: room.tumonline_room_id,
last_scrape: *start_time,
})
.for_each(|room| {
let res = diesel::insert_into(rooms)
.values(&room)
.on_conflict(key)
.do_update()
.set(&room)
.execute(conn);
if let Err(e) = res {
error!("Error inserting into database: {e:?}");
}
});
}
fn delete_stale_results(conn: &mut PgConnection, start_time: NaiveDateTime) {
use crate::schema::rooms::dsl::*;
use diesel::prelude::*;
diesel::delete(rooms)
.filter(last_scrape.le(start_time))
.execute(conn)
.expect("Failed to delete stale rooms");
}

0 comments on commit dad7e39

Please sign in to comment.