Skip to content

Commit 1799422

Browse files
committed
Put the statistics into a json attachment
1 parent 8bee633 commit 1799422

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

src/context.rs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use anyhow::{bail, ensure, Context as _, Result};
1212
use async_channel::{self as channel, Receiver, Sender};
1313
use pgp::types::PublicKeyTrait;
1414
use ratelimit::Ratelimit;
15+
use serde::Serialize;
1516
use tokio::sync::{Mutex, Notify, RwLock};
1617

1718
use crate::chat::{get_chat_cnt, ChatId, ProtectionStatus};
@@ -1062,7 +1063,17 @@ impl Context {
10621063
}
10631064

10641065
async fn get_self_report(&self) -> Result<String> {
1065-
#[derive(Default)]
1066+
#[derive(Serialize)]
1067+
struct Statistics {
1068+
core_version: String,
1069+
num_msgs: u32,
1070+
num_chats: u32,
1071+
db_size: u64,
1072+
key_created: i64,
1073+
chat_numbers: ChatNumbers,
1074+
self_reporting_id: String,
1075+
}
1076+
#[derive(Default, Serialize)]
10661077
struct ChatNumbers {
10671078
protected: u32,
10681079
protection_broken: u32,
@@ -1072,9 +1083,6 @@ impl Context {
10721083
unencrypted_mua: u32,
10731084
}
10741085

1075-
let mut res = String::new();
1076-
res += &format!("core_version {}\n", get_version_str());
1077-
10781086
let num_msgs: u32 = self
10791087
.sql
10801088
.query_get_value(
@@ -1083,21 +1091,20 @@ impl Context {
10831091
)
10841092
.await?
10851093
.unwrap_or_default();
1086-
res += &format!("num_msgs {num_msgs}\n");
10871094

10881095
let num_chats: u32 = self
10891096
.sql
10901097
.query_get_value("SELECT COUNT(*) FROM chats WHERE id>9 AND blocked!=1", ())
10911098
.await?
10921099
.unwrap_or_default();
1093-
res += &format!("num_chats {num_chats}\n");
10941100

10951101
let db_size = tokio::fs::metadata(&self.sql.dbfile).await?.len();
1096-
res += &format!("db_size_bytes {db_size}\n");
10971102

1098-
let secret_key = &load_self_secret_key(self).await?.primary_key;
1099-
let key_created = secret_key.public_key().created_at().timestamp();
1100-
res += &format!("key_created {key_created}\n");
1103+
let key_created = load_self_secret_key(self)
1104+
.await?
1105+
.primary_key
1106+
.created_at()
1107+
.timestamp();
11011108

11021109
// how many of the chats active in the last months are:
11031110
// - protected
@@ -1107,7 +1114,7 @@ impl Context {
11071114
// - unencrypted and the contact uses Delta Chat
11081115
// - unencrypted and the contact uses a classical MUA
11091116
let three_months_ago = time().saturating_sub(3600 * 24 * 30 * 3);
1110-
let chats = self
1117+
let chat_numbers = self
11111118
.sql
11121119
.query_map(
11131120
"SELECT c.protected, m.param, m.msgrmsg
@@ -1162,12 +1169,6 @@ impl Context {
11621169
},
11631170
)
11641171
.await?;
1165-
res += &format!("chats_protected {}\n", chats.protected);
1166-
res += &format!("chats_protection_broken {}\n", chats.protection_broken);
1167-
res += &format!("chats_opportunistic_dc {}\n", chats.opportunistic_dc);
1168-
res += &format!("chats_opportunistic_mua {}\n", chats.opportunistic_mua);
1169-
res += &format!("chats_unencrypted_dc {}\n", chats.unencrypted_dc);
1170-
res += &format!("chats_unencrypted_mua {}\n", chats.unencrypted_mua);
11711172

11721173
let self_reporting_id = match self.get_config(Config::SelfReportingId).await? {
11731174
Some(id) => id,
@@ -1178,9 +1179,17 @@ impl Context {
11781179
id
11791180
}
11801181
};
1181-
res += &format!("self_reporting_id {self_reporting_id}");
1182+
let statistics = Statistics {
1183+
core_version: get_version_str().to_string(),
1184+
num_msgs,
1185+
num_chats,
1186+
db_size,
1187+
key_created,
1188+
chat_numbers,
1189+
self_reporting_id,
1190+
};
11821191

1183-
Ok(res)
1192+
Ok(serde_json::to_string_pretty(&statistics)?)
11841193
}
11851194

11861195
/// Drafts a message with statistics about the usage of Delta Chat.

0 commit comments

Comments
 (0)