Skip to content

Commit c4dd392

Browse files
committed
Put the statistics into a json attachment
1 parent d52d6f5 commit c4dd392

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};
@@ -1064,7 +1065,17 @@ impl Context {
10641065
}
10651066

10661067
async fn get_self_report(&self) -> Result<String> {
1067-
#[derive(Default)]
1068+
#[derive(Serialize)]
1069+
struct Statistics {
1070+
core_version: String,
1071+
num_msgs: u32,
1072+
num_chats: u32,
1073+
db_size: u64,
1074+
key_created: i64,
1075+
chat_numbers: ChatNumbers,
1076+
self_reporting_id: String,
1077+
}
1078+
#[derive(Default, Serialize)]
10681079
struct ChatNumbers {
10691080
protected: u32,
10701081
protection_broken: u32,
@@ -1074,9 +1085,6 @@ impl Context {
10741085
unencrypted_mua: u32,
10751086
}
10761087

1077-
let mut res = String::new();
1078-
res += &format!("core_version {}\n", get_version_str());
1079-
10801088
let num_msgs: u32 = self
10811089
.sql
10821090
.query_get_value(
@@ -1085,21 +1093,20 @@ impl Context {
10851093
)
10861094
.await?
10871095
.unwrap_or_default();
1088-
res += &format!("num_msgs {num_msgs}\n");
10891096

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

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

1100-
let secret_key = &load_self_secret_key(self).await?.primary_key;
1101-
let key_created = secret_key.public_key().created_at().timestamp();
1102-
res += &format!("key_created {key_created}\n");
1105+
let key_created = load_self_secret_key(self)
1106+
.await?
1107+
.primary_key
1108+
.created_at()
1109+
.timestamp();
11031110

11041111
// how many of the chats active in the last months are:
11051112
// - protected
@@ -1109,7 +1116,7 @@ impl Context {
11091116
// - unencrypted and the contact uses Delta Chat
11101117
// - unencrypted and the contact uses a classical MUA
11111118
let three_months_ago = time().saturating_sub(3600 * 24 * 30 * 3);
1112-
let chats = self
1119+
let chat_numbers = self
11131120
.sql
11141121
.query_map(
11151122
"SELECT c.protected, m.param, m.msgrmsg
@@ -1164,12 +1171,6 @@ impl Context {
11641171
},
11651172
)
11661173
.await?;
1167-
res += &format!("chats_protected {}\n", chats.protected);
1168-
res += &format!("chats_protection_broken {}\n", chats.protection_broken);
1169-
res += &format!("chats_opportunistic_dc {}\n", chats.opportunistic_dc);
1170-
res += &format!("chats_opportunistic_mua {}\n", chats.opportunistic_mua);
1171-
res += &format!("chats_unencrypted_dc {}\n", chats.unencrypted_dc);
1172-
res += &format!("chats_unencrypted_mua {}\n", chats.unencrypted_mua);
11731174

11741175
let self_reporting_id = match self.get_config(Config::SelfReportingId).await? {
11751176
Some(id) => id,
@@ -1180,9 +1181,17 @@ impl Context {
11801181
id
11811182
}
11821183
};
1183-
res += &format!("self_reporting_id {self_reporting_id}");
1184+
let statistics = Statistics {
1185+
core_version: get_version_str().to_string(),
1186+
num_msgs,
1187+
num_chats,
1188+
db_size,
1189+
key_created,
1190+
chat_numbers,
1191+
self_reporting_id,
1192+
};
11841193

1185-
Ok(res)
1194+
Ok(serde_json::to_string_pretty(&statistics)?)
11861195
}
11871196

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

0 commit comments

Comments
 (0)