@@ -18,7 +18,7 @@ const PROJECT_ID: &str = "p2rxzX0q";
1818pub struct GlobalDataContainer {
1919 last_full_refresh : DateTime < Utc > ,
2020 last_player_refresh : DateTime < Utc > ,
21- data : GlobalData ,
21+ pub data : GlobalData ,
2222}
2323
2424impl Default for GlobalDataContainer {
@@ -33,7 +33,8 @@ impl Default for GlobalDataContainer {
3333 latest_version : String :: new ( ) ,
3434 } ,
3535 notes : String :: new ( ) ,
36- user_agents : DashMap :: new ( ) ,
36+ request_user_agents : DashMap :: new ( ) ,
37+ gateway_user_agents : DashMap :: new ( ) ,
3738 } ,
3839 }
3940 }
@@ -48,7 +49,9 @@ pub struct GlobalData {
4849 #[ serde( skip_serializing_if = "String::is_empty" ) ]
4950 notes : String ,
5051 #[ serde( skip) ]
51- pub user_agents : DashMap < String , u32 > ,
52+ pub request_user_agents : DashMap < String , u32 > ,
53+ #[ serde( skip) ]
54+ pub gateway_user_agents : DashMap < String , u32 > ,
5255}
5356
5457#[ derive( Serialize ) ]
@@ -63,7 +66,8 @@ impl GlobalData {
6366 online_players : online,
6467 modrinth_data : self . modrinth_data . clone ( ) ,
6568 notes : self . notes . clone ( ) ,
66- user_agents : self . user_agents . clone ( ) ,
69+ request_user_agents : self . request_user_agents . clone ( ) ,
70+ gateway_user_agents : self . gateway_user_agents . clone ( ) ,
6771 }
6872 }
6973}
@@ -75,7 +79,8 @@ impl Clone for GlobalData {
7579 online_players : self . online_players ,
7680 modrinth_data : self . modrinth_data . clone ( ) ,
7781 notes : self . notes . clone ( ) ,
78- user_agents : self . user_agents . clone ( ) ,
82+ request_user_agents : self . request_user_agents . clone ( ) ,
83+ gateway_user_agents : self . gateway_user_agents . clone ( ) ,
7984 }
8085 }
8186}
@@ -112,15 +117,17 @@ pub async fn get(
112117 return Ok ( Json ( cloned) ) ;
113118 }
114119 let data = if full_refresh {
115- let agents = data_container. data . user_agents . clone ( ) ;
120+ let request_user_agents = data_container. data . request_user_agents . clone ( ) ;
121+ let gateway_user_agents = data_container. data . gateway_user_agents . clone ( ) ;
116122 GlobalData {
117123 total_players : get_total_players ( & database) . await ?,
118124 online_players : online_users. len ( ) as u32 ,
119125 modrinth_data : fetch_modrinth_data ( client) . await ?,
120126 notes : ( cl_args. notes_file . as_ref ( ) )
121127 . map ( |file| read_to_string ( file) . unwrap_or_else ( |_| String :: new ( ) ) )
122128 . unwrap_or_else ( String :: new) ,
123- user_agents : agents,
129+ request_user_agents,
130+ gateway_user_agents,
124131 }
125132 } else {
126133 data_container
@@ -163,11 +170,15 @@ pub async fn metrics(
163170 writeln ! ( response, "lifetime_players {lifetime_players}" ) ;
164171 writeln ! ( response, "online_players {online_players}" ) ;
165172 let data_container = global_data. read ( ) . await ;
166- let agents = data_container. data . user_agents . clone ( ) ;
167- for ( agent, count) in agents {
173+ let request_agents = data_container. data . request_user_agents . clone ( ) ;
174+ for ( agent, count) in request_agents {
168175 writeln ! ( response, "request_count{{user_agent=\" {agent}\" }} {count}" ) ;
169176 }
170- data_container. data . user_agents . clear ( ) ;
177+ data_container. data . request_user_agents . clear ( ) ;
178+ let gateway_agents = data_container. data . gateway_user_agents . clone ( ) ;
179+ for ( agent, count) in gateway_agents {
180+ writeln ! ( response, "connections{{user_agent=\" {agent}\" }} {count}" ) ;
181+ }
171182 } ;
172183
173184 Ok ( response)
@@ -205,11 +216,14 @@ async fn fetch_modrinth_data(client: Client) -> Result<ModrinthData, ApiError> {
205216 } )
206217}
207218
208- pub struct UserAgent ;
219+ pub struct RequestUserAgentCounter ;
209220
210- impl FromRequestParts < ApiState > for UserAgent {
221+ impl FromRequestParts < ApiState > for RequestUserAgentCounter {
211222 type Rejection = ApiError ;
212- async fn from_request_parts ( parts : & mut Parts , state : & ApiState ) -> Result < UserAgent , Self :: Rejection > {
223+ async fn from_request_parts (
224+ parts : & mut Parts ,
225+ state : & ApiState ,
226+ ) -> Result < RequestUserAgentCounter , Self :: Rejection > {
213227 if parts. uri . path ( ) . ends_with ( "metrics" ) {
214228 return Ok ( Self ) ;
215229 }
@@ -222,8 +236,8 @@ impl FromRequestParts<ApiState> for UserAgent {
222236 . replace ( "\\ " , "" )
223237 . replace ( "\" " , "" ) ;
224238
225- let a = state. global_data . read ( ) . await ;
226- let agents = & a . data . user_agents ;
239+ let container = state. global_data . read ( ) . await ;
240+ let agents = & container . data . request_user_agents ;
227241 if agents. contains_key ( & agent) {
228242 let prev = agents. get ( & agent) . map ( |v| * v. value ( ) ) . unwrap_or ( 0 ) ;
229243 agents. insert ( agent, prev + 1 ) ;
0 commit comments