aboutsummaryrefslogtreecommitdiff
path: root/src/tool.rs
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2025-01-27 21:26:45 +0100
committermetamuffin <metamuffin@disroot.org>2025-01-27 21:26:45 +0100
commitb0361d395c24eea0bc889f5510c378bddd282169 (patch)
tree70ca3d871bc0c7a2e1ede4eab1145701ddf2d054 /src/tool.rs
parent27e6f6e1e9584e43c004bf51cc69ab8c0873f1a7 (diff)
downloadmeta-adservices-b0361d395c24eea0bc889f5510c378bddd282169.tar
meta-adservices-b0361d395c24eea0bc889f5510c378bddd282169.tar.bz2
meta-adservices-b0361d395c24eea0bc889f5510c378bddd282169.tar.zst
move host tool added
Diffstat (limited to 'src/tool.rs')
-rw-r--r--src/tool.rs51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/tool.rs b/src/tool.rs
new file mode 100644
index 0000000..c431c09
--- /dev/null
+++ b/src/tool.rs
@@ -0,0 +1,51 @@
+use crate::state::{Config, T_IMPRESSIONS_RAW, T_IMPRESSIONS_WEIGHTED};
+use anyhow::{anyhow, bail, Result};
+use redb::{Database, ReadableTable};
+
+pub fn tool_main(config: Config, action: &str, args: Vec<String>) -> Result<()> {
+ match action {
+ "move_host" => {
+ let oldhost = args.get(0).ok_or(anyhow!("2nd arg is old host"))?;
+ let newhost = args.get(1).ok_or(anyhow!("3rd arg is new host"))?;
+ move_host(config, oldhost, newhost)?;
+ }
+ _ => bail!("unknown action"),
+ }
+ Ok(())
+}
+
+pub fn move_host(config: Config, oldhost: &str, newhost: &str) -> Result<()> {
+ let db = Database::open(config.database_path)?;
+
+ let txn = db.begin_write()?;
+ {
+ let mut t_impressions = txn.open_table(T_IMPRESSIONS_RAW)?;
+ let mut t_weighted = txn.open_table(T_IMPRESSIONS_WEIGHTED)?;
+
+ let old_imp = t_impressions
+ .remove(oldhost)?
+ .map(|g| g.value())
+ .unwrap_or_default();
+ let old_wei = t_weighted
+ .remove(oldhost)?
+ .map(|g| g.value())
+ .unwrap_or_default();
+
+ let new_imp = t_impressions
+ .get(newhost)?
+ .map(|g| g.value())
+ .unwrap_or_default();
+ let new_wei = t_weighted
+ .get(newhost)?
+ .map(|g| g.value())
+ .unwrap_or_default();
+
+ println!("impressions old={old_imp} new={new_imp}");
+ println!("weighted old={old_wei} new={new_wei}");
+ t_impressions.insert(newhost, old_imp + new_imp)?;
+ t_weighted.insert(newhost, old_wei + new_wei)?;
+ }
+ txn.commit()?;
+
+ Ok(())
+}