Added minor fixes required for actual input

This commit is contained in:
onyinyang 2023-08-22 15:19:33 -04:00
parent b3ddc03ba6
commit 5dc2ed2a90
No known key found for this signature in database
GPG Key ID: 156A6435430C2036
1 changed files with 30 additions and 27 deletions

View File

@ -1,7 +1,7 @@
use std::sync::{Arc, Mutex};
use crate::{lox_context, DbConfig};
use chrono::{naive::Days, DateTime, Local, Utc};
use chrono::{naive::Days, DateTime, Local, NaiveDateTime, Utc};
use lox_library::{BridgeAuth, BridgeDb};
use sled::IVec;
@ -32,7 +32,6 @@ pub fn open_new_or_existing_db(
// Check if the lox_db already exists
if lox_db.was_recovered() {
context = read_lox_context_from_db(lox_db.clone(), roll_back_date);
//Otherwise, create a new Lox context
} else {
let new_db = BridgeDb::new();
@ -66,32 +65,31 @@ fn read_lox_context_from_db(
// If the date is specified and it's in the database, use that to populate the context
if lox_db.contains_key(roll_back_date.clone()).unwrap() {
// Find date/time in db and use the context from that date.
let json_exact_date = serde_json::to_vec(&roll_back_date).unwrap();
let ivec_context = lox_db.get(IVec::from(json_exact_date)).unwrap().unwrap();
let ivec_context = lox_db
.get(IVec::from(roll_back_date.as_bytes().to_vec()))
.unwrap()
.unwrap();
context = serde_json::from_slice(&ivec_context).unwrap();
println!("Successfully used exact key {:?}", roll_back_date);
} else {
// If the exact date is not found, compute the start of the range as 24 hours prior to the specified date
match compute_startdate_string(roll_back_date.clone()){
Some(start_date) => {
let sd_string = format!("{}", start_date.format("%Y-%m-%d_%H:%M:%S"));
let start_json: Vec<u8> = serde_json::to_vec(&sd_string).unwrap();
let end_json: Vec<u8> = serde_json::to_vec(&roll_back_date).unwrap();
let r: sled::Iter = lox_db.range(IVec::from(start_json)..IVec::from(end_json), );
match r.last(){
Some(entry) => {
let ivec_context = entry.unwrap();
let json_key: String = serde_json::from_slice(&ivec_context.0).unwrap();
println!("Successfully used range and key {:?}", json_key);
context = serde_json::from_slice(&ivec_context.1).unwrap()
},
//If no entries are found within the 24 hours prior to the specified date, Exit
None => panic!("UNEXPECTED DATE: No entries found in the 24 hours prior to the input roll_back_date"),
}
}
None => panic!("UNEXPECTED DATE: Tried to use input date to calculate 24 hour range but failed. Exiting."),
}
Some(start_date) => {
let start_date = start_date.format("context_%Y-%m-%d_%H:%M:%S").to_string();
let r: sled::Iter = lox_db.range(IVec::from(start_date.as_bytes().to_vec())..IVec::from(roll_back_date.as_bytes().to_vec()), );
match r.last(){
Some(entry) => {
let ivec_context = entry.unwrap();
let key: String = String::from_utf8(ivec_context.0.to_vec()).unwrap();
println!("Successfully used range and key {:?}", key);
context = serde_json::from_slice(&ivec_context.1).unwrap();
},
//If no entries are found within the 24 hours prior to the specified date, Exit
None => panic!("UNEXPECTED DATE: No entries found in the 24 hours prior to the input roll_back_date"),
}
}
None => panic!("UNEXPECTED DATE: Tried to use input date to calculate 24 hour range but failed. Exiting."),
}
}
}
// Use the last entry to populate the Lox context if no rollback date is set (which should be most common)
@ -104,10 +102,15 @@ fn read_lox_context_from_db(
fn compute_startdate_string(date_range_end: String) -> Option<DateTime<Utc>> {
let parsed_end =
DateTime::parse_from_str(&date_range_end, "context_%Y-%m-%d_%H:%M:%S").unwrap();
parsed_end
.with_timezone(&Utc)
.checked_sub_days(Days::new(1))
NaiveDateTime::parse_from_str(&date_range_end, "context_%Y-%m-%d_%H:%M:%S").unwrap();
let dt = DateTime::<Utc>::from_utc(parsed_end, Utc);
println!("WHat is datetime here: {:?}", dt);
println!(
"WHat is datetime - 1 subday here: {:?}",
dt.with_timezone(&Utc).checked_sub_days(Days::new(1))
);
dt.with_timezone(&Utc).checked_sub_days(Days::new(1))
}
fn use_last_context(db: sled::Db) -> lox_context::LoxServerContext {