@@ -13,6 +13,7 @@ use async_channel::{self as channel, Receiver, Sender};
13
13
use pgp:: types:: PublicKeyTrait ;
14
14
use pgp:: SignedPublicKey ;
15
15
use ratelimit:: Ratelimit ;
16
+ use serde:: Serialize ;
16
17
use tokio:: sync:: { Mutex , Notify , RwLock } ;
17
18
18
19
use crate :: aheader:: EncryptPreference ;
@@ -1053,7 +1054,17 @@ impl Context {
1053
1054
}
1054
1055
1055
1056
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 ) ]
1057
1068
struct ChatNumbers {
1058
1069
protected : u32 ,
1059
1070
protection_broken : u32 ,
@@ -1063,9 +1074,6 @@ impl Context {
1063
1074
unencrypted_mua : u32 ,
1064
1075
}
1065
1076
1066
- let mut res = String :: new ( ) ;
1067
- res += & format ! ( "core_version {}\n " , get_version_str( ) ) ;
1068
-
1069
1077
let num_msgs: u32 = self
1070
1078
. sql
1071
1079
. query_get_value (
@@ -1074,21 +1082,20 @@ impl Context {
1074
1082
)
1075
1083
. await ?
1076
1084
. unwrap_or_default ( ) ;
1077
- res += & format ! ( "num_msgs {}\n " , num_msgs) ;
1078
1085
1079
1086
let num_chats: u32 = self
1080
1087
. sql
1081
1088
. query_get_value ( "SELECT COUNT(*) FROM chats WHERE id>9 AND blocked!=1" , ( ) )
1082
1089
. await ?
1083
1090
. unwrap_or_default ( ) ;
1084
- res += & format ! ( "num_chats {}\n " , num_chats) ;
1085
1091
1086
1092
let db_size = tokio:: fs:: metadata ( & self . sql . dbfile ) . await ?. len ( ) ;
1087
- res += & format ! ( "db_size_bytes {}\n " , db_size) ;
1088
1093
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 ( ) ;
1092
1099
1093
1100
// how many of the chats active in the last months are:
1094
1101
// - protected
@@ -1098,7 +1105,7 @@ impl Context {
1098
1105
// - unencrypted and the contact uses Delta Chat
1099
1106
// - unencrypted and the contact uses a classical MUA
1100
1107
let three_months_ago = time ( ) . saturating_sub ( 3600 * 24 * 30 * 3 ) ;
1101
- let chats = self
1108
+ let chat_numbers = self
1102
1109
. sql
1103
1110
. query_map (
1104
1111
"SELECT c.protected, m.param, m.msgrmsg
@@ -1153,12 +1160,6 @@ impl Context {
1153
1160
} ,
1154
1161
)
1155
1162
. 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) ;
1162
1163
1163
1164
let self_reporting_id = match self . get_config ( Config :: SelfReportingId ) . await ? {
1164
1165
Some ( id) => id,
@@ -1169,9 +1170,17 @@ impl Context {
1169
1170
id
1170
1171
}
1171
1172
} ;
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
+ } ;
1173
1182
1174
- Ok ( res )
1183
+ Ok ( serde_json :: to_string_pretty ( & statistics ) ? )
1175
1184
}
1176
1185
1177
1186
/// Drafts a message with statistics about the usage of Delta Chat.
0 commit comments