Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #222 from COS301-SE-2023/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
u18004874 authored Sep 27, 2023
2 parents 92520b0 + cf49717 commit b5e68d9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 34 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/app-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
branches:
- main
- dev
push:
branches:
- dev

jobs:
test:
Expand Down
72 changes: 41 additions & 31 deletions api/src/loadshedding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ pub struct LoadSheddingStage {
#[serde(skip_serializing, skip_deserializing)]
db: Option<Client>,
stage: i32,
pub update: Option<bool>
}

#[derive(Debug, Deserialize, Clone)]
Expand Down Expand Up @@ -670,6 +671,7 @@ impl LoadsheddingData {
end_time: self.end.0.timestamp(),
db: None,
stage: self.stage,
update: Some(true)
}
}
}
Expand Down Expand Up @@ -759,7 +761,7 @@ impl MunicipalityEntity {
// filter schedules to relevant ones
for schedule in unfiltered_schedules {
let keep = schedule.is_within_timeslot(&time_to_search);
println!("{:?}", time_to_search.hour());
//println!("{:?}", time_to_search.hour());
if keep {
schedules.push(schedule);
}
Expand Down Expand Up @@ -867,10 +869,10 @@ impl SuburbEntity {
.unwrap()
.with_minute(0)
.unwrap();
println!("{:?}", time_now.timestamp());
//println!("{:?}", time_now.timestamp());
let mut response: Vec<TimeSlot> = Vec::new();
let day_in_future =
get_date_time(Some((Local::now() + chrono::Duration::days(1)).timestamp()));
get_date_time(Some((get_date_time(time) + chrono::Duration::days(1)).timestamp()));

let (group, mut all_stages, schedule) = match self
.collect_information(&time_now.timestamp(), connection, db_functions)
Expand All @@ -882,7 +884,7 @@ impl SuburbEntity {
all_stages.reverse();

let mut time_to_search = time_now;
println!("{:?}", time_to_search.timestamp());
//println!("{:?}", time_to_search.timestamp());
while time_to_search < day_in_future {
let day = time_to_search.day() as i32;
let time_slots: Vec<TimeScheduleEntity> = schedule
Expand Down Expand Up @@ -1116,30 +1118,32 @@ impl SuburbEntity {
impl LoadSheddingStage {
pub async fn set_stage(&mut self) {
// get the next thing from db
let con = &self.db.as_ref().unwrap().database("production");
let now = get_date_time(None).timestamp();
let query = doc! {
"startTime" : {
"$lte" : now
}
};
let filter = doc! {
"startTime" : -1
};
let find_options = FindOneOptions::builder().sort(filter).build();
let new_status: LoadSheddingStage = con
.collection("stage_log")
.find_one(query, find_options)
.await
.unwrap()
.unwrap();
println!("self is: {:?}", self);
println!("new is: {:?}", new_status);
self.end_time = new_status.end_time;
self.start_time = new_status.start_time;
self.stage = new_status.stage;
println!("self is after operation: {:?}", self);
//println!("{:?}", self);
if let Some(client) = &self.db.as_ref() {
let con = client.database("production");
let now = get_date_time(None).timestamp();
let query = doc! {
"startTime" : {
"$lte" : now
}
};
let filter = doc! {
"startTime" : -1
};
let find_options = FindOneOptions::builder().sort(filter).build();
let new_status: LoadSheddingStage = con
.collection("stage_log")
.find_one(query, find_options)
.await
.unwrap()
.unwrap();
println!("self is: {:?}", self);
println!("new is: {:?}", new_status);
self.end_time = new_status.end_time;
self.start_time = new_status.start_time;
self.stage = new_status.stage;
println!("self is after operation: {:?}", self);
//println!("{:?}", self);
}
}

pub async fn request_stage_data_update(&mut self) -> Result<i32, reqwest::Error> {
Expand All @@ -1165,7 +1169,7 @@ impl LoadSheddingStage {
if let Some(client) = &self.db.as_ref() {
let db_con = &client.database("production");
let query = doc! {
"start_time" : -1
"startTime" : -1
};
let find_options = FindOneOptions::builder().sort(query).build();

Expand All @@ -1183,6 +1187,7 @@ impl LoadSheddingStage {
start_time: 0,
end_time: 0,
db: None,
update: Some(true)
},
};
let latest_in_db = result.start_time;
Expand Down Expand Up @@ -1225,7 +1230,11 @@ impl LoadSheddingStage {
mongodb::options::UpdateModifications::Document(doc! {
"$set" : {"stage" : new_data.stage}
});
let _ = db_data.update(update, db_con).await;
if let Some(update_value) = db_data.update {
if update_value {
let _ = db_data.update(update, db_con).await;
}
}
}
}
// else if no match
Expand Down Expand Up @@ -1280,6 +1289,7 @@ impl Fairing for StageUpdater {
start_time: 0,
end_time: 0,
db: None,
update: Some(true)
}));
let rocket = rocket.manage(Some(stage_info));
Ok(rocket)
Expand Down Expand Up @@ -1343,7 +1353,7 @@ impl<'de> Deserialize<'de> for SASTDateTime {
// hack for now because library is not being co-operative
let convert_to_sast = dt.timestamp() - 2*3600;
let sast = get_date_time(Some(convert_to_sast));
println!("{:?}", sast);
//println!("{:?}", sast);
Ok(SASTDateTime(sast))
}
}
Expand Down
4 changes: 1 addition & 3 deletions api/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ async fn test_getstats() {
assert_eq!(result,expected_output);
}

/*
#[rocket::async_test]
async fn test_ai_endpoint() {
let client = Client::tracked(build_rocket().await)
Expand All @@ -204,7 +203,6 @@ async fn test_ai_endpoint() {

assert!(body.success);
}
*/

#[rocket::async_test]
async fn test_loadshedding_helpers() {
Expand Down Expand Up @@ -653,7 +651,7 @@ const TEST_SUBURB_DATA: &'static str = r#"{
}"#;

const TEST_GETSTATS_EXPECTED_RESULT: &'static str = "{\"totalTime\":{\"on\":2520,\"off\":7560},\"perDayTimes\":{\"Sun\":{\"on\":360,\"off\":1080},\"Mon\":{\"on\":360,\"off\":1080},\"Sat\":{\"on\":360,\"off\":1080},\"Tue\":{\"on\":360,\"off\":1080},\"Thu\":{\"on\":360,\"off\":1080},\"Fri\":{\"on\":360,\"off\":1080},\"Wed\":{\"on\":360,\"off\":1080}},\"suburb\":{\"_id\":{\"$oid\":\"64b6b9b30d09aa7756061b30\"},\"municipality\":{\"$oid\":\"64b6b9b30d09aa7756061a47\"},\"name\":\"MUCKLENEUK\",\"geometry\":[1]}}";
const TEST_GETSCHEDULE_EXPECTED_RESULT: &'static str = "{\"timesOff\":[{\"start\":1694743200,\"end\":1694665800},{\"start\":1694750400,\"end\":1694673000},{\"start\":1694757600,\"end\":1694680200},{\"start\":1694764800,\"end\":1694687400},{\"start\":1694772000,\"end\":1694694600},{\"start\":1694779200,\"end\":1694701800},{\"start\":1694786400,\"end\":1694709000},{\"start\":1694793600,\"end\":1694716200},{\"start\":1694800800,\"end\":1694723400},{\"start\":1694728800,\"end\":1694737800},{\"start\":1694822400,\"end\":1694745000},{\"start\":1694829600,\"end\":1694752200},{\"start\":1694836800,\"end\":1694759400},{\"start\":1694844000,\"end\":1694766600},{\"start\":1694851200,\"end\":1694773800},{\"start\":1694858400,\"end\":1694781000},{\"start\":1694865600,\"end\":1694788200},{\"start\":1694872800,\"end\":1694795400},{\"start\":1694880000,\"end\":1694802600},{\"start\":1694887200,\"end\":1694809800},{\"start\":1694815200,\"end\":1694824200},{\"start\":1694916000,\"end\":1694838600},{\"start\":1694923200,\"end\":1694845800},{\"start\":1695038400,\"end\":1694961000},{\"start\":1694973600,\"end\":1694982600},{\"start\":1695002400,\"end\":1695011400},{\"start\":1695096000,\"end\":1695018600},{\"start\":1695031200,\"end\":1695040200},{\"start\":1695124800,\"end\":1695047400},{\"start\":1695132000,\"end\":1695054600},{\"start\":1695060000,\"end\":1695069000},{\"start\":1695074400,\"end\":1695083400},{\"start\":1695088800,\"end\":1695097800},{\"start\":1695211200,\"end\":1695133800},{\"start\":1695218400,\"end\":1695141000},{\"start\":1695146400,\"end\":1695155400},{\"start\":1695261600,\"end\":1695270600},{\"start\":1695384000,\"end\":1695306600},{\"start\":1695391200,\"end\":1695313800},{\"start\":1695319200,\"end\":1695328200},{\"start\":1695333600,\"end\":1695342600},{\"start\":1695348000,\"end\":1695357000},{\"start\":1695441600,\"end\":1695364200},{\"start\":1695448800,\"end\":1695371400},{\"start\":1695376800,\"end\":1695385800},{\"start\":1695470400,\"end\":1695393000}]}";
const TEST_GETSCHEDULE_EXPECTED_RESULT: &'static str = "{\"timesOff\":[{\"start\":1694743200,\"end\":1694665800},{\"start\":1694750400,\"end\":1694673000},{\"start\":1694757600,\"end\":1694680200},{\"start\":1694764800,\"end\":1694687400},{\"start\":1694772000,\"end\":1694694600},{\"start\":1694779200,\"end\":1694701800},{\"start\":1694786400,\"end\":1694709000},{\"start\":1694793600,\"end\":1694716200},{\"start\":1694800800,\"end\":1694723400},{\"start\":1694728800,\"end\":1694737800},{\"start\":1694822400,\"end\":1694745000},{\"start\":1694829600,\"end\":1694752200}]}";
const POLYGON_DATA: &'static str = r#"{
"_id": { "$oid": "64b6b9b30d09aa7756061a47" },
"name": "tshwane",
Expand Down

0 comments on commit b5e68d9

Please sign in to comment.