@@ -12,6 +12,7 @@ use anyhow::{bail, ensure, Context as _, Result};
12
12
use async_channel:: { self as channel, Receiver , Sender } ;
13
13
use pgp:: types:: PublicKeyTrait ;
14
14
use ratelimit:: Ratelimit ;
15
+ use serde:: Serialize ;
15
16
use tokio:: sync:: { Mutex , Notify , RwLock } ;
16
17
17
18
use crate :: chat:: { get_chat_cnt, ChatId , ProtectionStatus } ;
@@ -1062,7 +1063,17 @@ impl Context {
1062
1063
}
1063
1064
1064
1065
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 ) ]
1066
1077
struct ChatNumbers {
1067
1078
protected : u32 ,
1068
1079
protection_broken : u32 ,
@@ -1072,9 +1083,6 @@ impl Context {
1072
1083
unencrypted_mua : u32 ,
1073
1084
}
1074
1085
1075
- let mut res = String :: new ( ) ;
1076
- res += & format ! ( "core_version {}\n " , get_version_str( ) ) ;
1077
-
1078
1086
let num_msgs: u32 = self
1079
1087
. sql
1080
1088
. query_get_value (
@@ -1083,21 +1091,20 @@ impl Context {
1083
1091
)
1084
1092
. await ?
1085
1093
. unwrap_or_default ( ) ;
1086
- res += & format ! ( "num_msgs {num_msgs}\n " ) ;
1087
1094
1088
1095
let num_chats: u32 = self
1089
1096
. sql
1090
1097
. query_get_value ( "SELECT COUNT(*) FROM chats WHERE id>9 AND blocked!=1" , ( ) )
1091
1098
. await ?
1092
1099
. unwrap_or_default ( ) ;
1093
- res += & format ! ( "num_chats {num_chats}\n " ) ;
1094
1100
1095
1101
let db_size = tokio:: fs:: metadata ( & self . sql . dbfile ) . await ?. len ( ) ;
1096
- res += & format ! ( "db_size_bytes {db_size}\n " ) ;
1097
1102
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 ( ) ;
1101
1108
1102
1109
// how many of the chats active in the last months are:
1103
1110
// - protected
@@ -1107,7 +1114,7 @@ impl Context {
1107
1114
// - unencrypted and the contact uses Delta Chat
1108
1115
// - unencrypted and the contact uses a classical MUA
1109
1116
let three_months_ago = time ( ) . saturating_sub ( 3600 * 24 * 30 * 3 ) ;
1110
- let chats = self
1117
+ let chat_numbers = self
1111
1118
. sql
1112
1119
. query_map (
1113
1120
"SELECT c.protected, m.param, m.msgrmsg
@@ -1162,12 +1169,6 @@ impl Context {
1162
1169
} ,
1163
1170
)
1164
1171
. 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) ;
1171
1172
1172
1173
let self_reporting_id = match self . get_config ( Config :: SelfReportingId ) . await ? {
1173
1174
Some ( id) => id,
@@ -1178,9 +1179,17 @@ impl Context {
1178
1179
id
1179
1180
}
1180
1181
} ;
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
+ } ;
1182
1191
1183
- Ok ( res )
1192
+ Ok ( serde_json :: to_string_pretty ( & statistics ) ? )
1184
1193
}
1185
1194
1186
1195
/// Drafts a message with statistics about the usage of Delta Chat.
0 commit comments