BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-call-model / src / report / storage.rs

storage.rs

d39bdfed History
storage.rs 3.5 KiB Download Raw
1use std::fs;
2use std::io;
3use std::path::{Path, PathBuf};
4
5use super::Document;
6
7#[cfg(not(test))]
8const REPORT_DIRECTORY: &str = "call-reports";
9const KEEP_REPORTS: usize = 30;
10const JSON_EXTENSION: &str = "json";
11const MARKDOWN_EXTENSION: &str = "md";
12
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct SavedReport {
15 pub json: PathBuf,
16 pub markdown: PathBuf,
17}
18
19#[cfg(test)]
20pub fn report_directory() -> PathBuf {
21 std::env::temp_dir().join("rusty-teams-test-call-reports")
22}
23
24#[cfg(not(test))]
25pub fn report_directory() -> PathBuf {
26 directories::ProjectDirs::from("", "", store::DATA_DIR_NAME)
27 .map(|directories| directories.data_local_dir().join(REPORT_DIRECTORY))
28 .unwrap_or_else(|| PathBuf::from(REPORT_DIRECTORY))
29}
30
31pub fn save_report(directory: &Path, document: &Document) -> io::Result<SavedReport> {
32 fs::create_dir_all(directory)?;
33 let stem = free_stem(directory, &document.stem);
34 let saved = SavedReport {
35 json: directory.join(format!("{stem}.{JSON_EXTENSION}")),
36 markdown: directory.join(format!("{stem}.{MARKDOWN_EXTENSION}")),
37 };
38 fs::write(&saved.json, &document.json)?;
39 fs::write(&saved.markdown, &document.markdown)?;
40 prune(directory, KEEP_REPORTS);
41 Ok(saved)
42}
43
44fn free_stem(directory: &Path, stem: &str) -> String {
45 let taken = |candidate: &str| directory.join(format!("{candidate}.{JSON_EXTENSION}")).exists();
46 if !taken(stem) {
47 return stem.to_owned();
48 }
49 (2..)
50 .map(|counter| format!("{stem}-{counter}"))
51 .find(|candidate| !taken(candidate))
52 .expect("an unbounded counter finds a free name")
53}
54
55fn prune(directory: &Path, keep: usize) {
56 let Ok(entries) = fs::read_dir(directory) else {
57 return;
58 };
59 let mut stems: Vec<String> = entries
60 .filter_map(Result::ok)
61 .map(|entry| entry.path())
62 .filter(|path| path.extension().is_some_and(|extension| extension == JSON_EXTENSION))
63 .filter_map(|path| path.file_stem().map(|stem| stem.to_string_lossy().into_owned()))
64 .collect();
65 stems.sort();
66 let surplus = stems.len().saturating_sub(keep);
67 for stem in &stems[..surplus] {
68 for extension in [JSON_EXTENSION, MARKDOWN_EXTENSION] {
69 let _ = fs::remove_file(directory.join(format!("{stem}.{extension}")));
70 }
71 }
72}
73
74#[cfg(test)]
75mod tests {
76 use super::*;
77
78 fn document(stem: &str) -> Document {
79 Document { stem: stem.to_owned(), json: "{}".to_owned(), markdown: "# x".to_owned() }
80 }
81
82 #[test]
83 fn the_newest_thirty_reports_are_kept_with_both_files() {
84 let directory = tempfile::tempdir().unwrap();
85 for index in 0..33 {
86 save_report(directory.path(), &document(&format!("20260101T0000{index:02}Z-1to1"))).unwrap();
87 }
88 let mut names: Vec<String> =
89 fs::read_dir(directory.path()).unwrap().map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned()).collect();
90 names.sort();
91 assert_eq!(names.len(), 60);
92 assert_eq!(names[0], "20260101T000003Z-1to1.json");
93 assert_eq!(names[59], "20260101T000032Z-1to1.md");
94 }
95
96 #[test]
97 fn a_report_in_the_same_second_gets_a_counter() {
98 let directory = tempfile::tempdir().unwrap();
99 let first = save_report(directory.path(), &document("20260101T000000Z-echo")).unwrap();
100 let second = save_report(directory.path(), &document("20260101T000000Z-echo")).unwrap();
101 assert_ne!(first.json, second.json);
102 assert!(second.json.to_string_lossy().ends_with("20260101T000000Z-echo-2.json"));
103 }
104}