@@ -175,6 +175,26 @@ pub(crate) struct UsageReportingOperationDetails {
175
175
referenced_fields_by_type : HashMap < String , ReferencedFieldsForType > ,
176
176
}
177
177
178
+ impl UsageReportingOperationDetails {
179
+ fn operation_name_or_default ( & self ) -> String {
180
+ self . operation_name . as_deref ( ) . unwrap_or ( "" ) . to_string ( )
181
+ }
182
+
183
+ fn operation_sig_or_default ( & self ) -> String {
184
+ self . operation_signature . as_deref ( ) . unwrap_or ( "" ) . to_string ( )
185
+ }
186
+
187
+ fn get_signature_and_operation ( & self ) -> String {
188
+ let op_name = self . operation_name . as_deref ( ) . unwrap_or ( "-" ) . to_string ( ) ;
189
+ let op_sig = self
190
+ . operation_signature
191
+ . as_deref ( )
192
+ . unwrap_or ( "" )
193
+ . to_string ( ) ;
194
+ format ! ( "# {}\n {}" , op_name, op_sig)
195
+ }
196
+ }
197
+
178
198
/// UsageReporting fields, that will be used to send stats to uplink/studio
179
199
#[ derive( Deserialize , Serialize , Debug , PartialEq , Eq , Clone ) ]
180
200
#[ serde( rename_all = "camelCase" ) ]
@@ -203,7 +223,7 @@ impl UsageReporting {
203
223
}
204
224
205
225
/// The `stats_report_key` is a unique identifier derived from schema and query.
206
- /// Metric data sent to Studio must be aggregated via grouped key of
226
+ /// Metric data sent to Studio must be aggregated via grouped key of
207
227
/// (`client_name`, `client_version`, `stats_report_key`).
208
228
/// For errors, the report key is of the form "## <error name>\n".
209
229
/// For operations not requested by PQ, the report key is of the form "# <op name>\n<op sig>".
@@ -215,62 +235,38 @@ impl UsageReporting {
215
235
/// the "operation signature".
216
236
pub ( crate ) fn get_stats_report_key ( & self ) -> String {
217
237
match self {
218
- UsageReporting :: Operation { .. } | UsageReporting :: Error { .. } => {
219
- self . get_signature_and_operation ( )
220
- } ,
238
+ UsageReporting :: Operation ( operation_details) => {
239
+ operation_details. get_signature_and_operation ( )
240
+ }
241
+ UsageReporting :: Error ( error_key) => {
242
+ format ! ( "## {}\n " , error_key)
243
+ }
221
244
UsageReporting :: PersistedQuery {
245
+ operation_details,
222
246
persisted_query_id, ..
223
247
} => {
224
248
let string_to_hash = format ! (
225
- "{}\n {}" ,
249
+ "{}\n {}\n {} " ,
226
250
persisted_query_id,
227
- self . get_operation_signature( )
251
+ operation_details. operation_name_or_default( ) ,
252
+ operation_details. operation_sig_or_default( )
228
253
) ;
229
254
format ! ( "pq# {}" , Self :: hash_string( & string_to_hash) )
230
255
}
231
256
}
232
257
}
233
258
234
- pub ( crate ) fn get_operation_signature ( & self ) -> String {
235
- match self {
259
+ pub ( crate ) fn get_operation_id ( & self ) -> String {
260
+ let string_to_hash = match self {
236
261
UsageReporting :: Operation ( operation_details)
237
262
| UsageReporting :: PersistedQuery {
238
263
operation_details, ..
239
- } => operation_details
240
- . operation_signature
241
- . clone ( )
242
- . unwrap_or ( "" . to_string ( ) ) ,
243
- UsageReporting :: Error { .. } => "" . to_string ( ) ,
244
- }
245
- }
246
-
247
- fn get_signature_and_operation ( & self ) -> String {
248
- match self {
249
- UsageReporting :: Operation ( operation_details) | UsageReporting :: PersistedQuery { operation_details, .. } => {
250
- let op_name = operation_details
251
- . operation_name
252
- . as_deref ( )
253
- . unwrap_or ( "-" )
254
- . to_string ( ) ;
255
- format ! (
256
- "# {}\n {}" ,
257
- op_name,
258
- self . get_operation_signature( ) ,
259
- )
260
- } ,
261
- UsageReporting :: Error ( error_key) => format ! ( "## {}\n " , error_key) ,
262
- }
263
- }
264
-
265
- pub ( crate ) fn get_operation_id ( & self ) -> String {
266
- Self :: hash_string ( & self . get_signature_and_operation ( ) )
267
- }
268
-
269
- fn hash_string ( string_to_hash : & String ) -> String {
270
- let mut hasher = sha1:: Sha1 :: new ( ) ;
271
- hasher. update ( string_to_hash. as_bytes ( ) ) ;
272
- let result = hasher. finalize ( ) ;
273
- hex:: encode ( result)
264
+ } => operation_details. get_signature_and_operation ( ) ,
265
+ UsageReporting :: Error ( error_key) => {
266
+ format ! ( "# # {}\n " , error_key)
267
+ }
268
+ } ;
269
+ Self :: hash_string ( & string_to_hash)
274
270
}
275
271
276
272
pub ( crate ) fn get_operation_name ( & self ) -> String {
@@ -279,9 +275,7 @@ impl UsageReporting {
279
275
| UsageReporting :: PersistedQuery {
280
276
operation_details, ..
281
277
} => operation_details
282
- . operation_name
283
- . clone ( )
284
- . unwrap_or ( "" . to_string ( ) ) ,
278
+ . operation_name_or_default ( ) ,
285
279
UsageReporting :: Error ( error_key) => format ! ( "# {}" , error_key) ,
286
280
}
287
281
}
@@ -299,16 +293,24 @@ impl UsageReporting {
299
293
pub ( crate ) fn get_query_metadata ( & self ) -> Option < QueryMetadata > {
300
294
match self {
301
295
UsageReporting :: PersistedQuery {
296
+ operation_details,
302
297
persisted_query_id, ..
303
298
} => Some ( QueryMetadata {
304
- name : self . get_operation_name ( ) ,
305
- signature : self . get_operation_signature ( ) ,
299
+ name : operation_details . operation_name_or_default ( ) ,
300
+ signature : operation_details . operation_sig_or_default ( ) ,
306
301
pq_id : persisted_query_id. clone ( ) ,
307
302
} ) ,
308
303
// For now we only want to populate query metadata for PQ operations
309
304
UsageReporting :: Operation { .. } | UsageReporting :: Error { .. } => None ,
310
305
}
311
306
}
307
+
308
+ fn hash_string ( string_to_hash : & String ) -> String {
309
+ let mut hasher = sha1:: Sha1 :: new ( ) ;
310
+ hasher. update ( string_to_hash. as_bytes ( ) ) ;
311
+ let result = hasher. finalize ( ) ;
312
+ hex:: encode ( result)
313
+ }
312
314
}
313
315
314
316
/// A list of fields that will be resolved for a given type
0 commit comments