55
66use crate :: DownloadsMap ;
77use crate :: paths:: parse_path;
8+ use crate :: user_agent:: should_count_user_agent;
89use chrono:: NaiveDate ;
910use std:: borrow:: Cow ;
1011use tokio:: io:: { AsyncBufRead , AsyncBufReadExt } ;
@@ -18,6 +19,7 @@ const FIELD_DATE: &str = "date";
1819const FIELD_METHOD : & str = "cs-method" ;
1920const FIELD_PATH : & str = "cs-uri-stem" ;
2021const FIELD_STATUS : & str = "sc-status" ;
22+ const FIELD_USER_AGENT : & str = "cs(User-Agent)" ;
2123
2224#[ instrument( level = "debug" , skip( reader) ) ]
2325pub async fn count_downloads ( reader : impl AsyncBufRead + Unpin ) -> anyhow:: Result < DownloadsMap > {
@@ -26,6 +28,7 @@ pub async fn count_downloads(reader: impl AsyncBufRead + Unpin) -> anyhow::Resul
2628 let mut method_index = None ;
2729 let mut path_index = None ;
2830 let mut status_index = None ;
31+ let mut user_agent_index = None ;
2932
3033 let mut downloads = DownloadsMap :: new ( ) ;
3134
@@ -47,6 +50,7 @@ pub async fn count_downloads(reader: impl AsyncBufRead + Unpin) -> anyhow::Resul
4750 method_index = fields. iter ( ) . position ( |f| f == & FIELD_METHOD ) ;
4851 path_index = fields. iter ( ) . position ( |f| f == & FIELD_PATH ) ;
4952 status_index = fields. iter ( ) . position ( |f| f == & FIELD_STATUS ) ;
53+ user_agent_index = fields. iter ( ) . position ( |f| f == & FIELD_USER_AGENT ) ;
5054
5155 continue ;
5256 }
@@ -76,6 +80,12 @@ pub async fn count_downloads(reader: impl AsyncBufRead + Unpin) -> anyhow::Resul
7680 continue ;
7781 }
7882
83+ let user_agent = get_optional_value ( & values, user_agent_index) ;
84+ if user_agent. is_some_and ( |ua| !should_count_user_agent ( ua) ) {
85+ // Ignore requests from user agents that should not be counted.
86+ continue ;
87+ }
88+
7989 let path = get_value ( & values, path_index, FIELD_PATH ) ;
8090
8191 // Deal with paths like `/crates/tikv-jemalloc-sys/tikv-jemalloc-sys-0.5.4%252B5.3.0-patched.crate`.
@@ -120,6 +130,10 @@ fn get_value<'a>(values: &'a [&'a str], index: Option<usize>, field_name: &'stat
120130 } )
121131}
122132
133+ fn get_optional_value < ' a > ( values : & ' a [ & ' a str ] , index : Option < usize > ) -> Option < & ' a str > {
134+ index. and_then ( |i| values. get ( i) ) . copied ( )
135+ }
136+
123137#[ cfg( test) ]
124138mod tests {
125139 use super :: * ;
@@ -149,7 +163,6 @@ mod tests {
149163 2024-01-17 flatbuffers@23.1.21 .. 1
150164 2024-01-17 jemallocator@0.5.4 .. 1
151165 2024-01-17 leveldb-sys@2.0.9 .. 1
152- 2024-01-17 num_cpus@1.15.0 .. 1
153166 2024-01-17 paste@1.0.12 .. 1
154167 2024-01-17 quick-error@1.2.3 .. 1
155168 2024-01-17 rand@0.8.5 .. 1
0 commit comments