17
17
*/
18
18
19
19
use core:: str;
20
- use std:: fs ;
20
+ use std:: { collections :: HashMap , fs } ;
21
21
22
22
use actix_web:: {
23
23
web:: { self , Path } ,
@@ -36,18 +36,18 @@ use crate::{
36
36
handlers:: http:: {
37
37
base_path_without_preceding_slash,
38
38
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,
41
40
utils:: { merge_quried_stats, IngestionStats , QueriedStats , StorageStats } ,
42
41
} ,
43
- logstream:: { error:: StreamError , get_stats_date } ,
42
+ logstream:: error:: StreamError ,
44
43
modal:: { NodeMetadata , NodeType } ,
45
44
} ,
46
45
hottier:: HotTierManager ,
47
46
parseable:: { StreamNotFound , PARSEABLE } ,
48
- stats:: { self , Stats } ,
47
+ stats,
49
48
storage:: { ObjectStoreFormat , StreamType , STREAM_ROOT_DIRECTORY } ,
50
49
} ;
50
+ const STATS_DATE_QUERY_PARAM : & str = "date" ;
51
51
52
52
pub async fn delete ( stream_name : Path < String > ) -> Result < impl Responder , StreamError > {
53
53
let stream_name = stream_name. into_inner ( ) ;
@@ -142,34 +142,27 @@ pub async fn get_stats(
142
142
return Err ( StreamNotFound ( stream_name. clone ( ) ) . into ( ) ) ;
143
143
}
144
144
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 ( ) ) ) ?;
155
147
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
+ } ) ?;
158
152
153
+ if !date_value. is_empty ( ) {
159
154
// this function requires all the ingestor stream jsons
160
155
let path = RelativePathBuf :: from_iter ( [ & stream_name, STREAM_ROOT_DIRECTORY ] ) ;
161
156
let obs = PARSEABLE
162
157
. storage
163
158
. get_object_store ( )
164
159
. get_objects (
165
160
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" ) ) ,
169
162
)
170
163
. await ?;
171
164
172
- let mut ingestor_stream_jsons = Vec :: new ( ) ;
165
+ let mut stream_jsons = Vec :: new ( ) ;
173
166
for ob in obs {
174
167
let stream_metadata: ObjectStoreFormat = match serde_json:: from_slice ( & ob) {
175
168
Ok ( d) => d,
@@ -178,20 +171,14 @@ pub async fn get_stats(
178
171
continue ;
179
172
}
180
173
} ;
181
- ingestor_stream_jsons . push ( stream_metadata) ;
174
+ stream_jsons . push ( stream_metadata) ;
182
175
}
183
176
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) ?;
186
178
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) ?;
193
180
194
- return Ok ( ( web:: Json ( stats) , StatusCode :: OK ) ) ;
181
+ return Ok ( web:: Json ( stats) ) ;
195
182
}
196
183
}
197
184
@@ -238,5 +225,5 @@ pub async fn get_stats(
238
225
239
226
let stats = serde_json:: to_value ( stats) ?;
240
227
241
- Ok ( ( web:: Json ( stats) , StatusCode :: OK ) )
228
+ Ok ( web:: Json ( stats) )
242
229
}
0 commit comments