1717 */
1818
1919use core:: str;
20- use std:: fs ;
20+ use std:: { collections :: HashMap , fs } ;
2121
2222use actix_web:: {
2323 web:: { self , Path } ,
@@ -36,18 +36,18 @@ use crate::{
3636 handlers:: http:: {
3737 base_path_without_preceding_slash,
3838 cluster:: {
39- self , fetch_daily_stats_from_ingestors, fetch_stats_from_ingestors,
40- sync_streams_with_ingestors,
39+ self , fetch_daily_stats, fetch_stats_from_ingestors, sync_streams_with_ingestors,
4140 utils:: { merge_quried_stats, IngestionStats , QueriedStats , StorageStats } ,
4241 } ,
43- logstream:: { error:: StreamError , get_stats_date } ,
42+ logstream:: error:: StreamError ,
4443 modal:: { NodeMetadata , NodeType } ,
4544 } ,
4645 hottier:: HotTierManager ,
4746 parseable:: { StreamNotFound , PARSEABLE } ,
48- stats:: { self , Stats } ,
47+ stats,
4948 storage:: { ObjectStoreFormat , StreamType , STREAM_ROOT_DIRECTORY } ,
5049} ;
50+ const STATS_DATE_QUERY_PARAM : & str = "date" ;
5151
5252pub async fn delete ( stream_name : Path < String > ) -> Result < impl Responder , StreamError > {
5353 let stream_name = stream_name. into_inner ( ) ;
@@ -142,34 +142,27 @@ pub async fn get_stats(
142142 return Err ( StreamNotFound ( stream_name. clone ( ) ) . into ( ) ) ;
143143 }
144144
145- let query_string = req. query_string ( ) ;
146- if !query_string. is_empty ( ) {
147- let date_key = query_string. split ( '=' ) . collect :: < Vec < & str > > ( ) [ 0 ] ;
148- let date_value = query_string. split ( '=' ) . collect :: < Vec < & str > > ( ) [ 1 ] ;
149- if date_key != "date" {
150- return Err ( StreamError :: Custom {
151- msg : "Invalid query parameter" . to_string ( ) ,
152- status : StatusCode :: BAD_REQUEST ,
153- } ) ;
154- }
145+ let query_map = web:: Query :: < HashMap < String , String > > :: from_query ( req. query_string ( ) )
146+ . map_err ( |_| StreamError :: InvalidQueryParameter ( STATS_DATE_QUERY_PARAM . to_string ( ) ) ) ?;
155147
156- if !date_value. is_empty ( ) {
157- let querier_stats = get_stats_date ( & stream_name, date_value) . await ?;
148+ if !query_map. is_empty ( ) {
149+ let date_value = query_map. get ( STATS_DATE_QUERY_PARAM ) . ok_or_else ( || {
150+ StreamError :: InvalidQueryParameter ( STATS_DATE_QUERY_PARAM . to_string ( ) )
151+ } ) ?;
158152
153+ if !date_value. is_empty ( ) {
159154 // this function requires all the ingestor stream jsons
160155 let path = RelativePathBuf :: from_iter ( [ & stream_name, STREAM_ROOT_DIRECTORY ] ) ;
161156 let obs = PARSEABLE
162157 . storage
163158 . get_object_store ( )
164159 . get_objects (
165160 Some ( & path) ,
166- Box :: new ( |file_name| {
167- file_name. starts_with ( ".ingestor" ) && file_name. ends_with ( "stream.json" )
168- } ) ,
161+ Box :: new ( |file_name| file_name. ends_with ( "stream.json" ) ) ,
169162 )
170163 . await ?;
171164
172- let mut ingestor_stream_jsons = Vec :: new ( ) ;
165+ let mut stream_jsons = Vec :: new ( ) ;
173166 for ob in obs {
174167 let stream_metadata: ObjectStoreFormat = match serde_json:: from_slice ( & ob) {
175168 Ok ( d) => d,
@@ -178,20 +171,14 @@ pub async fn get_stats(
178171 continue ;
179172 }
180173 } ;
181- ingestor_stream_jsons . push ( stream_metadata) ;
174+ stream_jsons . push ( stream_metadata) ;
182175 }
183176
184- let ingestor_stats =
185- fetch_daily_stats_from_ingestors ( date_value, & ingestor_stream_jsons) ?;
177+ let stats = fetch_daily_stats ( date_value, & stream_jsons) ?;
186178
187- let total_stats = Stats {
188- events : querier_stats. events + ingestor_stats. events ,
189- ingestion : querier_stats. ingestion + ingestor_stats. ingestion ,
190- storage : querier_stats. storage + ingestor_stats. storage ,
191- } ;
192- let stats = serde_json:: to_value ( total_stats) ?;
179+ let stats = serde_json:: to_value ( stats) ?;
193180
194- return Ok ( ( web:: Json ( stats) , StatusCode :: OK ) ) ;
181+ return Ok ( web:: Json ( stats) ) ;
195182 }
196183 }
197184
@@ -238,5 +225,5 @@ pub async fn get_stats(
238225
239226 let stats = serde_json:: to_value ( stats) ?;
240227
241- Ok ( ( web:: Json ( stats) , StatusCode :: OK ) )
228+ Ok ( web:: Json ( stats) )
242229}
0 commit comments