@@ -3,7 +3,10 @@ use rustis::commands::{ExpireOption, GenericCommands, StringCommands};
33use serde:: { Deserialize , Serialize } ;
44use sha2:: Digest ;
55use sqlx:: types:: Uuid ;
6- use std:: { collections:: HashMap , sync:: Arc } ;
6+ use std:: {
7+ collections:: { HashMap , HashSet } ,
8+ sync:: Arc ,
9+ } ;
710use tokio:: sync:: Mutex ;
811
912nestify:: nest! {
@@ -70,6 +73,8 @@ pub struct TelemetryLogger {
7073 database : Arc < crate :: database:: Database > ,
7174 cache : Arc < crate :: cache:: Cache > ,
7275 env : Arc < crate :: env:: Env > ,
76+
77+ client : reqwest:: Client ,
7378}
7479
7580impl TelemetryLogger {
@@ -83,9 +88,15 @@ impl TelemetryLogger {
8388 database,
8489 cache,
8590 env,
91+
92+ client : reqwest:: Client :: builder ( )
93+ . user_agent ( "Blueprint API https://blueprint.zip" )
94+ . build ( )
95+ . unwrap ( ) ,
8696 }
8797 }
8898
99+ #[ inline]
89100 pub async fn log ( & self , ip : & str , telemetry : TelemetryData ) -> Option < ( ) > {
90101 let mut processing = self . processing . lock ( ) . await ;
91102
@@ -119,13 +130,14 @@ impl TelemetryLogger {
119130 Some ( ( ) )
120131 }
121132
122- async fn lookup_ips ( & self , ips : & [ & str ] ) -> HashMap < String , ( String , String ) > {
133+ #[ inline]
134+ async fn lookup_ips ( & self , ips : & [ & str ] ) -> HashMap < String , [ String ; 2 ] > {
123135 let mut result = HashMap :: new ( ) ;
124136
125- let data = reqwest:: Client :: new ( )
137+ let data = self
138+ . client
126139 . post ( "http://ip-api.com/batch" )
127140 . header ( "Content-Type" , "application/json" )
128- . header ( "User-Agent" , "Blueprint API/1.0.0 https://blueprint.zip" )
129141 . json (
130142 & ips. iter ( )
131143 . map ( |ip| {
@@ -134,7 +146,7 @@ impl TelemetryLogger {
134146 "fields" : "continentCode,countryCode,query"
135147 } )
136148 } )
137- . collect :: < Vec < _ > > ( ) ,
149+ . collect :: < HashSet < _ > > ( ) ,
138150 )
139151 . send ( )
140152 . await
@@ -150,10 +162,10 @@ impl TelemetryLogger {
150162
151163 result. insert (
152164 entry[ "query" ] . as_str ( ) . unwrap ( ) . to_string ( ) ,
153- (
165+ [
154166 entry[ "continentCode" ] . as_str ( ) . unwrap ( ) . to_string ( ) ,
155167 entry[ "countryCode" ] . as_str ( ) . unwrap ( ) . to_string ( ) ,
156- ) ,
168+ ] ,
157169 ) ;
158170 }
159171
@@ -183,7 +195,7 @@ impl TelemetryLogger {
183195 . await ;
184196
185197 for t in telemetry. iter_mut ( ) {
186- if let Some ( ( continent, country) ) = ips. get ( & t. ip ) {
198+ if let Some ( [ continent, country] ) = ips. get ( & t. ip ) {
187199 t. continent = Some ( continent. clone ( ) ) ;
188200 t. country = Some ( country. clone ( ) ) ;
189201 }
0 commit comments