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 std::sync::{Arc, Mutex};
use crate::{lox_context, DbConfig}; 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 lox_library::{BridgeAuth, BridgeDb};
use sled::IVec; use sled::IVec;
@ -32,7 +32,6 @@ pub fn open_new_or_existing_db(
// Check if the lox_db already exists // Check if the lox_db already exists
if lox_db.was_recovered() { if lox_db.was_recovered() {
context = read_lox_context_from_db(lox_db.clone(), roll_back_date); context = read_lox_context_from_db(lox_db.clone(), roll_back_date);
//Otherwise, create a new Lox context //Otherwise, create a new Lox context
} else { } else {
let new_db = BridgeDb::new(); 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 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() { if lox_db.contains_key(roll_back_date.clone()).unwrap() {
// Find date/time in db and use the context from that date. // Find date/time in db and use the context from that date.
let ivec_context = lox_db
let json_exact_date = serde_json::to_vec(&roll_back_date).unwrap(); .get(IVec::from(roll_back_date.as_bytes().to_vec()))
let ivec_context = lox_db.get(IVec::from(json_exact_date)).unwrap().unwrap(); .unwrap()
.unwrap();
context = serde_json::from_slice(&ivec_context).unwrap(); context = serde_json::from_slice(&ivec_context).unwrap();
println!("Successfully used exact key {:?}", roll_back_date); println!("Successfully used exact key {:?}", roll_back_date);
} else { } else {
// If the exact date is not found, compute the start of the range as 24 hours prior to the specified date // 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()){ match compute_startdate_string(roll_back_date.clone()){
Some(start_date) => { Some(start_date) => {
let sd_string = format!("{}", start_date.format("%Y-%m-%d_%H:%M:%S")); let start_date = start_date.format("context_%Y-%m-%d_%H:%M:%S").to_string();
let start_json: Vec<u8> = serde_json::to_vec(&sd_string).unwrap(); let r: sled::Iter = lox_db.range(IVec::from(start_date.as_bytes().to_vec())..IVec::from(roll_back_date.as_bytes().to_vec()), );
let end_json: Vec<u8> = serde_json::to_vec(&roll_back_date).unwrap(); match r.last(){
let r: sled::Iter = lox_db.range(IVec::from(start_json)..IVec::from(end_json), ); Some(entry) => {
match r.last(){ let ivec_context = entry.unwrap();
Some(entry) => { let key: String = String::from_utf8(ivec_context.0.to_vec()).unwrap();
let ivec_context = entry.unwrap(); println!("Successfully used range and key {:?}", key);
let json_key: String = serde_json::from_slice(&ivec_context.0).unwrap(); context = serde_json::from_slice(&ivec_context.1).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"),
//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."),
} }
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) // 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>> { fn compute_startdate_string(date_range_end: String) -> Option<DateTime<Utc>> {
let parsed_end = let parsed_end =
DateTime::parse_from_str(&date_range_end, "context_%Y-%m-%d_%H:%M:%S").unwrap(); NaiveDateTime::parse_from_str(&date_range_end, "context_%Y-%m-%d_%H:%M:%S").unwrap();
parsed_end let dt = DateTime::<Utc>::from_utc(parsed_end, Utc);
.with_timezone(&Utc) println!("WHat is datetime here: {:?}", dt);
.checked_sub_days(Days::new(1)) 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 { fn use_last_context(db: sled::Db) -> lox_context::LoxServerContext {