Compare commits

...

2 Commits

72
Cargo.lock generated

@ -128,6 +128,7 @@ dependencies = [
"egui-notify",
"flume",
"once_cell",
"rayon",
"reqwest",
"ron",
"serde",
@ -626,6 +627,40 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "crossbeam-channel"
version = "0.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a33c2bf77f2df06183c3aa30d1e96c0695a313d4f9c453cc3762a6db39f99200"
dependencies = [
"cfg-if",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef"
dependencies = [
"cfg-if",
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46bd5f3f85273295a9d14aedfb86f6aadbff6d8f5295c4a9edb08e819dcf5695"
dependencies = [
"autocfg",
"cfg-if",
"crossbeam-utils",
"memoffset 0.8.0",
"scopeguard",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.15"
@ -838,6 +873,12 @@ dependencies = [
"web-sys",
]
[[package]]
name = "either"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91"
[[package]]
name = "emath"
version = "0.21.0"
@ -1533,6 +1574,15 @@ dependencies = [
"autocfg",
]
[[package]]
name = "memoffset"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d61c719bcfbcf5d62b3a09efa6088de8c54bc0bfcd3ea7ae39fcc186108b8de1"
dependencies = [
"autocfg",
]
[[package]]
name = "mime"
version = "0.3.17"
@ -2041,6 +2091,28 @@ version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f851a03551ceefd30132e447f07f96cb7011d6b658374f3aed847333adb5559"
[[package]]
name = "rayon"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d2df5196e37bcc87abebc0053e20787d73847bb33134a69841207dd0a47f03b"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b8f95bd6966f5c87776639160a66bd8ab9895d9d4ab01ddba9fc60661aebe8d"
dependencies = [
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-utils",
"num_cpus",
]
[[package]]
name = "redox_syscall"
version = "0.2.16"

@ -12,6 +12,7 @@ eframe = "0.21.3"
egui-notify = "0.6.0"
flume = "0.10.14"
once_cell = "1.17.1"
rayon = "1.7.0"
reqwest = { version = "0.11.15", features = ["json"] }
ron = "0.8.0"
serde = { version = "1.0.158", features = ["derive"] }

@ -1,55 +1,59 @@
use anyhow::{Context, Result};
use chrono::{DateTime, Local};
use serde::{Deserialize, Serialize};
use std::fs;
use crate::{ai::Params, config::CONFIG};
#[derive(Serialize, Deserialize, Clone)]
pub struct HistoryEntry {
pub date: DateTime<Local>,
pub selected_ai: String,
pub params: Params,
pub result: String,
}
pub fn write_history(entry: HistoryEntry) -> Result<()> {
// should be safe to unwrap as this function will never be called without it
let history_dir = CONFIG.app.history_dir.as_ref().unwrap();
fs::create_dir_all(history_dir)?;
let json = serde_json::to_string(&entry)?;
fs::write(
format!("{}/{}.json", history_dir, entry.date.timestamp()),
json,
)?;
Ok(())
}
/*
this should probably be async all things considered however losing out on proper error handling with ? is
not worth it. so here's hoping that rust fs (and serde_json (meaning serde too)) is fast enough to read like 10000 files
*/
pub fn read_all_history() -> Result<Vec<HistoryEntry>> {
// should be safe to unwrap as this function will never be called without it
let history_dir = CONFIG.app.history_dir.as_ref().unwrap();
let mut entries = vec![];
for dir_entry in fs::read_dir(history_dir).context("Failed to read history_dir!")? {
let dir_entry = dir_entry?;
if dir_entry.file_type()?.is_file() {
let json = fs::read_to_string(dir_entry.path())?;
let entry: HistoryEntry = serde_json::from_str(&json)?;
entries.push(entry);
}
}
entries.sort_by(|a, b| b.date.cmp(&a.date));
Ok(entries)
}
use anyhow::{Context, Result};
use chrono::{DateTime, Local};
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use std::fs;
use crate::{ai::Params, config::CONFIG};
#[derive(Serialize, Deserialize, Clone)]
pub struct HistoryEntry {
pub date: DateTime<Local>,
pub selected_ai: String,
pub params: Params,
pub result: String,
}
pub fn write_history(entry: HistoryEntry) -> Result<()> {
// should be safe to unwrap as this function will never be called without it
let history_dir = CONFIG.app.history_dir.as_ref().unwrap();
fs::create_dir_all(history_dir)?;
let json = serde_json::to_string(&entry)?;
fs::write(
format!("{}/{}.json", history_dir, entry.date.timestamp()),
json,
)?;
Ok(())
}
pub fn read_all_history() -> Result<Vec<HistoryEntry>> {
// should be safe to unwrap as this function will never be called without it
let history_dir = CONFIG.app.history_dir.as_ref().unwrap();
let (tx, rx) = flume::unbounded();
fs::read_dir(history_dir)
.context("Failed to read history_dir!")?
.par_bridge()
.try_for_each_with(tx, |tx, dir_entry| -> Result<()> {
let dir_entry = dir_entry?;
if dir_entry.file_type()?.is_file() {
let json = fs::read_to_string(dir_entry.path())?;
let entry: HistoryEntry = serde_json::from_str(&json)?;
tx.send(entry)?;
}
Ok(())
})?;
let mut entries: Vec<HistoryEntry> = rx.iter().collect();
entries.sort_by(|a, b| b.date.cmp(&a.date));
Ok(entries)
}