Skip to main content

lb_rs/service/
debug.rs

1use crate::Lb;
2use crate::model::clock;
3use crate::model::errors::LbResult;
4use crate::service::lb_id::LbID;
5use basic_human_duration::ChronoHumanDuration;
6use chrono::NaiveDateTime;
7use serde::{Deserialize, Serialize};
8use std::path::PathBuf;
9use time::Duration;
10
11#[cfg(not(target_family = "wasm"))]
12use std::path::Path;
13
14#[cfg(not(target_family = "wasm"))]
15use std::env;
16
17#[cfg(not(target_family = "wasm"))]
18use chrono::{Local, TimeZone};
19
20#[cfg(not(target_family = "wasm"))]
21use crate::get_code_version;
22
23#[cfg(not(target_family = "wasm"))]
24use tokio::fs::{self, OpenOptions};
25
26#[cfg(not(target_family = "wasm"))]
27use tokio::io::AsyncWriteExt;
28
29#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
30pub struct DebugInfo {
31    pub lb_id: LbID,
32    pub time: String,
33    pub name: String,
34    pub last_synced: String,
35    pub lb_version: String,
36    pub rust_triple: String,
37    pub os_info: String,
38    pub lb_dir: String,
39    pub server_url: String,
40    pub integrity: String,
41    pub is_syncing: bool,
42    pub status: String,
43    pub panics: Vec<String>,
44}
45
46pub trait DebugInfoDisplay {
47    fn to_string(&self) -> String;
48}
49
50impl DebugInfoDisplay for LbResult<DebugInfo> {
51    fn to_string(&self) -> String {
52        match self {
53            Ok(debug_info) => serde_json::to_string_pretty(debug_info).unwrap_or_default(),
54            Err(err) => format!("Error retrieving debug info: {:?}", err),
55        }
56    }
57}
58
59impl Lb {
60    async fn human_last_synced(&self) -> String {
61        let tx = self.ro_tx().await;
62        let db = tx.db();
63
64        let last_synced = *db.last_synced.get().unwrap_or(&0);
65
66        if last_synced != 0 {
67            Duration::milliseconds(clock::get_time().0 - last_synced)
68                .format_human()
69                .to_string()
70        } else {
71            "never".to_string()
72        }
73    }
74
75    async fn lb_id(&self) -> LbResult<LbID> {
76        let mut tx = self.begin_tx().await;
77        let db = tx.db();
78
79        let lb_id = if let Some(id) = db.id.get().copied() {
80            id
81        } else {
82            let new_id = LbID::generate();
83            db.id.insert(new_id)?;
84            new_id
85        };
86
87        tx.end();
88
89        Ok(lb_id)
90    }
91
92    fn now(&self) -> String {
93        let now = chrono::Local::now();
94        now.format("%Y-%m-%d %H:%M:%S %Z").to_string()
95    }
96
97    #[cfg(not(target_family = "wasm"))]
98    async fn collect_panics(&self, populate_content: bool) -> LbResult<Vec<PanicInfo>> {
99        let mut panics = vec![];
100
101        let dir_path = &self.config.writeable_path;
102        let path = Path::new(dir_path);
103
104        let prefix = "panic---";
105        let suffix = ".log";
106        let timestamp_format = "%Y-%m-%d---%H-%M-%S";
107
108        let mut entries = fs::read_dir(path).await?;
109        while let Some(entry) = entries.next_entry().await? {
110            let file_name = entry.file_name().into_string().unwrap_or_default();
111
112            // Check if the filename matches the expected format
113            if file_name.starts_with(prefix) && file_name.ends_with(suffix) {
114                // Extract the timestamp portion from the filename
115                let timestamp_str = &file_name[prefix.len()..file_name.len() - suffix.len()];
116
117                // Parse the timestamp
118                if let Ok(time) = NaiveDateTime::parse_from_str(timestamp_str, timestamp_format) {
119                    let file_path = path.join(file_name);
120                    let content = if populate_content {
121                        let contents = fs::read_to_string(&file_path).await?;
122                        let contents = format!("time: {time}: contents: {contents}");
123                        contents
124                    } else {
125                        Default::default()
126                    };
127
128                    panics.push(PanicInfo { time, file_path, content });
129                }
130            }
131        }
132        panics.sort_by(|a, b| b.time.cmp(&a.time));
133
134        Ok(panics)
135    }
136
137    /// returns true if we have crashed within the last 5 seconds
138    #[cfg(not(target_family = "wasm"))]
139    pub async fn recent_panic(&self) -> LbResult<bool> {
140        let panics = self.collect_panics(false).await?;
141        for panic in panics {
142            let timestamp_local_time = Local
143                .from_local_datetime(&panic.time)
144                .single()
145                .unwrap_or_default();
146
147            let seconds_ago = (Local::now() - timestamp_local_time).abs().num_seconds();
148
149            if seconds_ago <= 5 {
150                return Ok(true);
151            }
152        }
153
154        Ok(false)
155    }
156
157    #[instrument(level = "debug", skip(self), err(Debug))]
158    #[cfg(not(target_family = "wasm"))]
159    pub async fn write_panic_to_file(&self, error_header: String, bt: String) -> LbResult<String> {
160        let file_name = generate_panic_filename(&self.config.writeable_path);
161        let content = generate_panic_content(&error_header, &bt);
162
163        let mut file = OpenOptions::new()
164            .create(true)
165            .append(true)
166            .open(&file_name)
167            .await?;
168
169        file.write_all(content.as_bytes()).await?;
170
171        Ok(file_name)
172    }
173
174    #[instrument(level = "debug", skip(self), err(Debug))]
175    #[cfg(not(target_family = "wasm"))]
176    pub async fn debug_info(&self, os_info: String, check_docs: bool) -> LbResult<DebugInfo> {
177        let account = self.get_account()?;
178
179        let arch = env::consts::ARCH;
180        let os = env::consts::OS;
181        let family = env::consts::FAMILY;
182
183        let (integrity, last_synced, panics, lb_id) = tokio::join!(
184            self.test_repo_integrity(check_docs),
185            self.human_last_synced(),
186            self.collect_panics(true),
187            self.lb_id()
188        );
189
190        let panics = panics?.into_iter().map(|panic| panic.content).collect();
191
192        let mut status = self.status().await;
193        status.space_used = None;
194        let status = format!("{status:?}");
195        let is_syncing = self.syncer.try_lock().is_ok();
196
197        Ok(DebugInfo {
198            time: self.now(),
199            name: account.username.clone(),
200            lb_version: get_code_version().into(),
201            lb_id: lb_id?,
202            rust_triple: format!("{arch}.{family}.{os}"),
203            server_url: account.api_url.clone(),
204            integrity: format!(
205                "{} {integrity:?}",
206                if check_docs { "" } else { "doc checks skipped" }
207            ),
208            lb_dir: self.config.writeable_path.clone(),
209            last_synced,
210            os_info,
211            status,
212            is_syncing,
213            panics,
214        })
215    }
216}
217
218#[derive(Debug, Clone, PartialEq, Eq)]
219pub struct PanicInfo {
220    pub time: NaiveDateTime,
221    pub file_path: PathBuf,
222    pub content: String,
223}
224
225pub fn generate_panic_filename(path: &str) -> String {
226    let timestamp = chrono::Local::now().format("%Y-%m-%d---%H-%M-%S");
227    format!("{path}/panic---{timestamp}.log")
228}
229
230pub fn generate_panic_content(panic_info: &str, bt: &str) -> String {
231    format!("INFO: {panic_info}\nBT: {bt}")
232}