11use std:: {
22 sync:: Arc ,
3- time:: { Duration , Instant , SystemTime , UNIX_EPOCH } ,
3+ time:: { Duration , SystemTime , UNIX_EPOCH } ,
44} ;
55
66use async_trait:: async_trait;
77use graphql_parser:: schema:: Document ;
88use hive_console_sdk:: agent:: { ExecutionReport , UsageAgent } ;
9- use hive_router_config:: { usage_reporting:: UsageReportingConfig , HiveRouterConfig } ;
9+ use hive_router_config:: usage_reporting:: UsageReportingConfig ;
1010use hive_router_plan_executor:: execution:: {
1111 client_request_details:: ClientRequestDetails , plan:: PlanExecutionOutput ,
1212} ;
1313use ntex:: web:: HttpRequest ;
1414use rand:: Rng ;
1515use tokio_util:: sync:: CancellationToken ;
1616
17- use crate :: background_tasks:: BackgroundTask ;
17+ use crate :: { background_tasks:: BackgroundTask , consts :: ROUTER_VERSION } ;
1818
19- pub fn from_config ( router_config : & HiveRouterConfig ) -> Option < UsageAgent > {
20- router_config. usage_reporting . as_ref ( ) . map ( |usage_config| {
21- let flush_interval = Duration :: from_secs ( usage_config. flush_interval ) ;
22- hive_console_sdk:: agent:: UsageAgent :: new (
23- usage_config. token . clone ( ) ,
24- usage_config. endpoint . clone ( ) ,
25- usage_config. target_id . clone ( ) ,
26- usage_config. buffer_size ,
27- usage_config. connect_timeout ,
28- usage_config. request_timeout ,
29- usage_config. accept_invalid_certs ,
30- flush_interval,
31- "hive-router" . to_string ( ) ,
32- )
33- } )
19+ pub fn create_hive_user_agent ( usage_config : & UsageReportingConfig ) -> UsageAgent {
20+ let user_agent = format ! ( "hive-router/{}" , ROUTER_VERSION ) ;
21+ hive_console_sdk:: agent:: UsageAgent :: new (
22+ usage_config. access_token . clone ( ) ,
23+ usage_config. endpoint . clone ( ) ,
24+ usage_config. target_id . clone ( ) ,
25+ usage_config. buffer_size ,
26+ usage_config. connect_timeout ,
27+ usage_config. request_timeout ,
28+ usage_config. accept_invalid_certs ,
29+ usage_config. flush_interval ,
30+ user_agent,
31+ )
3432}
3533
36- pub fn send_usage_report (
34+ #[ inline]
35+ pub fn collect_usage_report (
3736 schema : Arc < Document < ' static , String > > ,
38- start : Instant ,
37+ duration : Duration ,
3938 req : & HttpRequest ,
4039 client_request_details : & ClientRequestDetails ,
4140 usage_agent : & UsageAgent ,
@@ -59,13 +58,11 @@ pub fn send_usage_report(
5958 let timestamp = SystemTime :: now ( )
6059 . duration_since ( UNIX_EPOCH )
6160 . unwrap ( )
62- . as_secs ( )
63- * 1000 ;
64- let duration = start. elapsed ( ) ;
61+ . as_millis ( ) as u64 ;
6562 let execution_report = ExecutionReport {
6663 schema,
67- client_name,
68- client_version,
64+ client_name : client_name . map ( |s| s . to_owned ( ) ) ,
65+ client_version : client_version . map ( |s| s . to_owned ( ) ) ,
6966 timestamp,
7067 duration,
7168 ok : execution_result. error_count == 0 ,
@@ -77,22 +74,20 @@ pub fn send_usage_report(
7774 . map ( |op_name| op_name. to_owned ( ) ) ,
7875 persisted_document_hash : None ,
7976 } ;
80- usage_agent
81- . add_report ( execution_report)
82- . unwrap_or_else ( |err| tracing:: error!( "Failed to send usage report: {}" , err) ) ;
77+
78+ if let Err ( err) = usage_agent. add_report ( execution_report) {
79+ tracing:: error!( "Failed to send usage report: {}" , err) ;
80+ }
8381}
8482
85- fn get_header_value ( req : & HttpRequest , header_name : & str ) -> Option < String > {
86- req. headers ( )
87- . get ( header_name)
88- . and_then ( |v| v. to_str ( ) . ok ( ) )
89- . map ( |s| s. to_string ( ) )
83+ fn get_header_value < ' req > ( req : & ' req HttpRequest , header_name : & str ) -> Option < & ' req str > {
84+ req. headers ( ) . get ( header_name) . and_then ( |v| v. to_str ( ) . ok ( ) )
9085}
9186
9287#[ async_trait]
9388impl BackgroundTask for UsageAgent {
9489 fn id ( & self ) -> & str {
95- "usage_report_flush_interval "
90+ "hive_console_usage_report_task "
9691 }
9792
9893 async fn run ( & self , token : CancellationToken ) {
0 commit comments