Add better handling for roll back and exact dates
This commit is contained in:
parent
ffb05d403a
commit
e1057143fa
|
@ -1,6 +1,10 @@
|
|||
{
|
||||
"db": {
|
||||
"db_path": "lox_db"
|
||||
"db_path": "lox_db",
|
||||
"roll_back_date": {
|
||||
"date_range_start": "2023-08-04_16:35:32",
|
||||
"date_range_end": "2023-08-04_15:35:32"
|
||||
}
|
||||
},
|
||||
"rtype": {
|
||||
"endpoint": "http://127.0.0.1:7100/resource-stream",
|
||||
|
|
|
@ -9,7 +9,16 @@ pub fn write_context_to_db(db: sled::Db, context: lox_context::LoxServerContext)
|
|||
let date = Local::now().format("%Y-%m-%d_%H:%M:%S").to_string();
|
||||
let json_date = serde_json::to_vec(&date).unwrap();
|
||||
let json_result = serde_json::to_vec(&context).unwrap();
|
||||
let _ = db.insert(IVec::from(json_date), IVec::from(json_result));
|
||||
println!("Date: {:?}", date);
|
||||
let new_ivec = db.insert(
|
||||
IVec::from(json_date.clone()),
|
||||
IVec::from(json_result.clone()),
|
||||
);
|
||||
assert_eq!(
|
||||
db.get(IVec::from(json_date.clone())).unwrap().unwrap(),
|
||||
IVec::from(json_result.clone())
|
||||
);
|
||||
println!("What is the new key? {:?}", new_ivec);
|
||||
}
|
||||
|
||||
pub fn find_existing_db(
|
||||
|
@ -31,8 +40,17 @@ pub fn find_existing_db(
|
|||
match lox_db.contains_key(exact_date.clone()) {
|
||||
// Find exact date/time in db and use the context from that date.
|
||||
Ok(_) => {
|
||||
let ivec_context = lox_db.get(exact_date).unwrap().unwrap();
|
||||
let json_exact_date =
|
||||
serde_json::to_vec(&exact_date).unwrap();
|
||||
let ivec_context = lox_db
|
||||
.get(IVec::from(json_exact_date))
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
context = serde_json::from_slice(&ivec_context).unwrap();
|
||||
println!(
|
||||
"Successfully used exact key {:?}",
|
||||
exact_date
|
||||
);
|
||||
}
|
||||
// If the entered date is not found, use the last saved context
|
||||
Err(_) => {
|
||||
|
@ -46,19 +64,29 @@ pub fn find_existing_db(
|
|||
match roll_back_date.date_range_start {
|
||||
Some(start) => {
|
||||
// If so, ensure the end of the range has also been specified
|
||||
if let Some(end) = roll_back_date.date_range_end {
|
||||
if error_check_input_string(
|
||||
start.clone(),
|
||||
roll_back_date.date_range_end.clone(),
|
||||
) {
|
||||
let start_json: Vec<u8> =
|
||||
serde_json::to_vec(&start).unwrap();
|
||||
let end_json: Vec<u8> =
|
||||
serde_json::to_vec(&end).unwrap();
|
||||
serde_json::to_vec(&roll_back_date.date_range_end)
|
||||
.unwrap();
|
||||
let r: sled::Iter = lox_db.range(
|
||||
IVec::from(start_json)..IVec::from(end_json),
|
||||
);
|
||||
let ivec_context = r.last().unwrap().unwrap().1;
|
||||
let ivec_context = r.last().unwrap().unwrap();
|
||||
context =
|
||||
serde_json::from_slice(&ivec_context).unwrap();
|
||||
serde_json::from_slice(&ivec_context.1).unwrap();
|
||||
let json_key: String =
|
||||
serde_json::from_slice(&ivec_context.0).unwrap();
|
||||
println!(
|
||||
"Successfully used range and key {:?}",
|
||||
json_key
|
||||
);
|
||||
} else {
|
||||
println!("UNEXPECTED DATE: Start of range was specified but End of range was not! Using last saved context");
|
||||
println!("UNEXPECTED DATE: Start of range was specified but End of range was not! Using last saved context");
|
||||
context = use_last_context(lox_db.clone());
|
||||
}
|
||||
}
|
||||
|
@ -98,8 +126,23 @@ pub fn find_existing_db(
|
|||
Ok((lox_db, context))
|
||||
}
|
||||
|
||||
fn use_last_context(cloned_db: sled::Db) -> lox_context::LoxServerContext {
|
||||
let ivec_context = cloned_db.last().unwrap().unwrap().1;
|
||||
println!("Using db date: {:?}", ivec_context);
|
||||
serde_json::from_slice(&ivec_context).unwrap()
|
||||
fn error_check_input_string(start: String, date_range_end: Option<String>) -> bool {
|
||||
if let Some(end) = date_range_end {
|
||||
let parsed_start = Utc.datetime_from_str(&start, "%Y-%m-%d_%H:%M:%S").unwrap();
|
||||
let parsed_end = Utc.datetime_from_str(&end, "%Y-%m-%d_%H:%M:%S").unwrap();
|
||||
if parsed_start <= parsed_end {
|
||||
return true;
|
||||
}
|
||||
println!("date_range_start must be <= date_range_end! Check config file");
|
||||
return false;
|
||||
}
|
||||
println!("date_range_start specified but no date_range_end! Check config file");
|
||||
false
|
||||
}
|
||||
|
||||
fn use_last_context(cloned_db: sled::Db) -> lox_context::LoxServerContext {
|
||||
let ivec_context = cloned_db.last().unwrap().unwrap();
|
||||
let ivec_date: String = serde_json::from_slice(&ivec_context.0).unwrap();
|
||||
println!("Using last context with date: {:?}", ivec_date);
|
||||
serde_json::from_slice(&ivec_context.1).unwrap()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue