Commit 04a16e16 authored by Johannes Barthel's avatar Johannes Barthel
Browse files

make debug output optional

parent 8722ace6
......@@ -55,6 +55,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
name = "cleanup-meeting-data"
version = "0.2.0"
dependencies = [
"lazy_static",
"regex",
"reqwest",
"serde",
......
......@@ -26,3 +26,4 @@ serde = { version = "1.0", features = ["derive"] }
serde_derive = "1.0"
reqwest = { version = "0.11", features = ["blocking", "json"] }
serde_json = "1.0"
lazy_static = "1.4.0"
......@@ -2,9 +2,23 @@ use serde::Deserialize;
use std::collections::HashSet;
use std::fs;
lazy_static::lazy_static! {
static ref DEBUG: bool = {
if let Ok(debug) = std::env::var("DEBUG") {
if *(&["true", "1"].contains(&debug.as_str())) {
true
} else {
false
}
} else {
false
}
};
}
#[derive(Deserialize)]
struct PadList {
#[serde(rename="padIDs")]
#[serde(rename = "padIDs")]
pad_ids: Vec<String>,
}
......@@ -22,21 +36,21 @@ struct EtherpadApiVersion {
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct MongoRunningMeeting {
meeting_id : String,
meeting_id: String,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct MongoCaption {
meeting_id : String,
pad_id : String
meeting_id: String,
pad_id: String,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct MongoNote {
meeting_id : String,
note_id : String
meeting_id: String,
note_id: String,
}
fn get_meetings_on_disk() -> HashSet<String> {
......@@ -59,7 +73,9 @@ fn get_stored_notes(etherpad_api_version: &str, etherpad_api_key: &str) -> Vec<S
"http://localhost:9001/api/{}/listAllPads?apikey={}",
&etherpad_api_version, &etherpad_api_key
);
dbg!(&api_call);
if *DEBUG {
dbg!(&api_call);
}
reqwest::blocking::get(&api_call)
.unwrap()
.json::<EtherpadApiQuery>()
......@@ -69,14 +85,25 @@ fn get_stored_notes(etherpad_api_version: &str, etherpad_api_key: &str) -> Vec<S
}
fn get_mongo_running_meetings() -> HashSet<String> {
let stdout = String::from_utf8(std::process::Command::new("mongo").args(&[
"--quiet", "--eval", r#"db.meetings.find({},{"meetingId": true, "_id": false}).toArray()"#,
"127.0.1.1/meteor"
])
.output().unwrap().stdout).unwrap();
let running_meetings : Vec<MongoRunningMeeting> = serde_json::from_str(&stdout).unwrap();
running_meetings.into_iter().map(|running_meeting| running_meeting.meeting_id).collect()
let stdout = String::from_utf8(
std::process::Command::new("mongo")
.args(&[
"--quiet",
"--eval",
r#"db.meetings.find({},{"meetingId": true, "_id": false}).toArray()"#,
"127.0.1.1/meteor",
])
.output()
.unwrap()
.stdout,
)
.unwrap();
let running_meetings: Vec<MongoRunningMeeting> = serde_json::from_str(&stdout).unwrap();
running_meetings
.into_iter()
.map(|running_meeting| running_meeting.meeting_id)
.collect()
}
fn get_mongo_subtitles_by_meeting_id() -> HashSet<(String, String)> {
......@@ -85,20 +112,34 @@ fn get_mongo_subtitles_by_meeting_id() -> HashSet<(String, String)> {
"127.0.1.1/meteor"
])
.output().unwrap().stdout).unwrap();
let subtitles : Vec<MongoCaption> = serde_json::from_str(&stdout).unwrap();
subtitles.into_iter().map(|subtitle| (subtitle.meeting_id, subtitle.pad_id)).collect()
let subtitles: Vec<MongoCaption> = serde_json::from_str(&stdout).unwrap();
subtitles
.into_iter()
.map(|subtitle| (subtitle.meeting_id, subtitle.pad_id))
.collect()
}
fn get_mongo_notes_by_meeting_id() -> HashSet<(String, String)> {
let stdout = String::from_utf8(std::process::Command::new("mongo").args(&[
"--quiet", "--eval", r#"db.note.find({},{"meetingId": true, "noteId": true, "_id": false}).toArray()"#,
"127.0.1.1/meteor"
])
.output().unwrap().stdout).unwrap();
let subtitles : Vec<MongoNote> = serde_json::from_str(&stdout).unwrap();
subtitles.into_iter().map(|subtitle| (subtitle.meeting_id, subtitle.note_id)).collect()
let stdout = String::from_utf8(
std::process::Command::new("mongo")
.args(&[
"--quiet",
"--eval",
r#"db.note.find({},{"meetingId": true, "noteId": true, "_id": false}).toArray()"#,
"127.0.1.1/meteor",
])
.output()
.unwrap()
.stdout,
)
.unwrap();
let subtitles: Vec<MongoNote> = serde_json::from_str(&stdout).unwrap();
subtitles
.into_iter()
.map(|subtitle| (subtitle.meeting_id, subtitle.note_id))
.collect()
}
fn main() {
......@@ -121,24 +162,33 @@ fn main() {
let all_note_pads_by_meeting_id = get_mongo_notes_by_meeting_id();
let all_subtitle_pads_by_meeting_id = get_mongo_subtitles_by_meeting_id();
let all_pads_by_meeting_id = all_note_pads_by_meeting_id.union(&all_subtitle_pads_by_meeting_id);
let all_pads_by_meeting_id =
all_note_pads_by_meeting_id.union(&all_subtitle_pads_by_meeting_id);
let running_meetings = get_mongo_running_meetings();
if *DEBUG {
dbg!(&running_meetings);
}
println!("found {} running meetings.", running_meetings.len());
let active_pads : HashSet::<String> =
all_pads_by_meeting_id.filter_map(|(meeting_id, note_id)| {
if running_meetings.contains(meeting_id) {
println!(
"pad {} belongs to running meeting {}",
&note_id, &meeting_id
);
Some(note_id.clone())
} else {
println!("stale pad {}", &note_id);
None
}
}).collect();
let active_pads: HashSet<String> = all_pads_by_meeting_id
.filter_map(|(meeting_id, note_id)| {
if running_meetings.contains(meeting_id) {
if *DEBUG {
println!(
"pad {} belongs to running meeting {}",
&note_id, &meeting_id
);
}
Some(note_id.clone())
} else {
if *DEBUG {
println!("stale pad {}", &note_id);
}
None
}
})
.collect();
let notes_to_remove: HashSet<&str> = stored_notes
.iter()
......@@ -152,7 +202,10 @@ fn main() {
})
.collect();
dbg!(notes_to_remove.len());
println!(
"found {} notes that will be removed.",
notes_to_remove.len()
);
let kept_notes: Vec<&str> = stored_notes
.iter()
......@@ -172,15 +225,12 @@ fn main() {
}
}
dbg!(&running_meetings);
println!("found {} running meetings.", running_meetings.len());
let stale_meetings: Vec<String> = meetings_on_disk
.difference(&running_meetings)
.map(|x| format!("/var/bigbluebutton/{}", x))
.collect();
println!("found {} stale meetings.", stale_meetings.len());
println!("found {} stale meetings on disk.", stale_meetings.len());
for dirname in stale_meetings {
print!("deleting {}...", dirname);
......@@ -188,7 +238,11 @@ fn main() {
println!(" removed!");
}
dbg!(&kept_notes);
println!("done cleaning up.");
if *DEBUG {
dbg!(&kept_notes);
}
println!(
"done cleaning up. {} pads are currently active.",
kept_notes.len()
);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment