Skip to content

Commit b8c5e76

Browse files
committed
Put the statistics into a json attachment
1 parent 8cbc9d0 commit b8c5e76

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
@@ -13,6 +13,7 @@ use async_channel::{self as channel, Receiver, Sender};
1313
use pgp::types::PublicKeyTrait;
1414
use pgp::SignedPublicKey;
1515
use ratelimit::Ratelimit;
16+
use serde::Serialize;
1617
use tokio::sync::{Mutex, Notify, RwLock};
1718

1819
use crate::aheader::EncryptPreference;
@@ -1053,7 +1054,17 @@ impl Context {
10531054
}
10541055

10551056
async fn get_self_report(&self) -> Result<String> {
1056-
#[derive(Default)]
1057+
#[derive(Serialize)]
1058+
struct Statistics {
1059+
core_version: String,
1060+
num_msgs: u32,
1061+
num_chats: u32,
1062+
db_size: u64,
1063+
key_created: i64,
1064+
chat_numbers: ChatNumbers,
1065+
self_reporting_id: String,
1066+
}
1067+
#[derive(Default, Serialize)]
10571068
struct ChatNumbers {
10581069
protected: u32,
10591070
protection_broken: u32,
@@ -1063,9 +1074,6 @@ impl Context {
10631074
unencrypted_mua: u32,
10641075
}
10651076

1066-
let mut res = String::new();
1067-
res += &format!("core_version {}\n", get_version_str());
1068-
10691077
let num_msgs: u32 = self
10701078
.sql
10711079
.query_get_value(
@@ -1074,21 +1082,20 @@ impl Context {
10741082
)
10751083
.await?
10761084
.unwrap_or_default();
1077-
res += &format!("num_msgs {}\n", num_msgs);
10781085

10791086
let num_chats: u32 = self
10801087
.sql
10811088
.query_get_value("SELECT COUNT(*) FROM chats WHERE id>9 AND blocked!=1", ())
10821089
.await?
10831090
.unwrap_or_default();
1084-
res += &format!("num_chats {}\n", num_chats);
10851091

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

1089-
let secret_key = &load_self_secret_key(self).await?.primary_key;
1090-
let key_created = secret_key.created_at().timestamp();
1091-
res += &format!("key_created {}\n", key_created);
1094+
let key_created = load_self_secret_key(self)
1095+
.await?
1096+
.primary_key
1097+
.created_at()
1098+
.timestamp();
10921099

10931100
// how many of the chats active in the last months are:
10941101
// - protected
@@ -1098,7 +1105,7 @@ impl Context {
10981105
// - unencrypted and the contact uses Delta Chat
10991106
// - unencrypted and the contact uses a classical MUA
11001107
let three_months_ago = time().saturating_sub(3600 * 24 * 30 * 3);
1101-
let chats = self
1108+
let chat_numbers = self
11021109
.sql
11031110
.query_map(
11041111
"SELECT c.protected, m.param, m.msgrmsg
@@ -1153,12 +1160,6 @@ impl Context {
11531160
},
11541161
)
11551162
.await?;
1156-
res += &format!("chats_protected {}\n", chats.protected);
1157-
res += &format!("chats_protection_broken {}\n", chats.protection_broken);
1158-
res += &format!("chats_opportunistic_dc {}\n", chats.opportunistic_dc);
1159-
res += &format!("chats_opportunistic_mua {}\n", chats.opportunistic_mua);
1160-
res += &format!("chats_unencrypted_dc {}\n", chats.unencrypted_dc);
1161-
res += &format!("chats_unencrypted_mua {}\n", chats.unencrypted_mua);
11621163

11631164
let self_reporting_id = match self.get_config(Config::SelfReportingId).await? {
11641165
Some(id) => id,
@@ -1169,9 +1170,17 @@ impl Context {
11691170
id
11701171
}
11711172
};
1172-
res += &format!("self_reporting_id {}", self_reporting_id);
1173+
let statistics = Statistics {
1174+
core_version: get_version_str().to_string(),
1175+
num_msgs,
1176+
num_chats,
1177+
db_size,
1178+
key_created,
1179+
chat_numbers,
1180+
self_reporting_id,
1181+
};
11731182

1174-
Ok(res)
1183+
Ok(serde_json::to_string_pretty(&statistics)?)
11751184
}
11761185

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

0 commit comments

Comments
 (0)