@@ -244,33 +244,39 @@ impl Query {
244
244
}
245
245
}
246
246
247
- /// DateBinRecord
247
+ /// Record of counts for a given time bin.
248
248
#[ derive( Debug , Serialize , Clone ) ]
249
- pub struct DateBinRecord {
250
- pub date_bin_timestamp : String ,
249
+ pub struct CountsRecord {
250
+ /// Start time of the bin
251
+ pub counts_timestamp : String ,
252
+ /// Number of logs in the bin
251
253
pub log_count : u64 ,
252
254
}
253
255
254
- struct DateBinBounds {
256
+ struct TimeBounds {
255
257
start : DateTime < Utc > ,
256
258
end : DateTime < Utc > ,
257
259
}
258
260
259
- /// DateBin Request.
261
+ /// Request for counts, received from API/SQL query .
260
262
#[ derive( Debug , Deserialize , Clone ) ]
261
263
#[ serde( rename_all = "camelCase" ) ]
262
- pub struct DateBinRequest {
264
+ pub struct CountsRequest {
265
+ /// Name of the stream to get counts for
263
266
pub stream : String ,
267
+ /// Included start time for counts query
264
268
pub start_time : String ,
269
+ /// Excluded end time for counts query
265
270
pub end_time : String ,
271
+ /// Number of bins to divide the time range into
266
272
pub num_bins : u64 ,
267
273
}
268
274
269
- impl DateBinRequest {
275
+ impl CountsRequest {
270
276
/// This function is supposed to read maninfest files for the given stream,
271
277
/// get the sum of `num_rows` between the `startTime` and `endTime`,
272
278
/// divide that by number of bins and return in a manner acceptable for the console
273
- pub async fn get_bin_density ( & self ) -> Result < Vec < DateBinRecord > , QueryError > {
279
+ pub async fn get_bin_density ( & self ) -> Result < Vec < CountsRecord > , QueryError > {
274
280
let time_partition = STREAM_INFO
275
281
. get_time_partition ( & self . stream . clone ( ) )
276
282
. map_err ( |err| anyhow:: Error :: msg ( err. to_string ( ) ) ) ?
@@ -279,22 +285,22 @@ impl DateBinRequest {
279
285
// get time range
280
286
let time_range = TimeRange :: parse_human_time ( & self . start_time , & self . end_time ) ?;
281
287
let all_manifest_files = get_manifest_list ( & self . stream , & time_range) . await ?;
282
- // get final date bins
283
- let final_date_bins = self . get_bins ( & time_range) ;
288
+ // get bounds
289
+ let counts = self . get_bounds ( & time_range) ;
284
290
285
291
// we have start and end times for each bin
286
292
// we also have all the manifest files for the given time range
287
293
// now we iterate over start and end times for each bin
288
294
// then we iterate over the manifest files which are within that time range
289
295
// we sum up the num_rows
290
- let mut date_bin_records = Vec :: new ( ) ;
296
+ let mut counts_records = Vec :: new ( ) ;
291
297
292
- for bin in final_date_bins {
298
+ for bin in counts {
293
299
// extract start and end time to compare
294
- let date_bin_timestamp = bin. start . timestamp_millis ( ) ;
300
+ let counts_timestamp = bin. start . timestamp_millis ( ) ;
295
301
296
302
// Sum up the number of rows that fall within the bin
297
- let total_num_rows : u64 = all_manifest_files
303
+ let log_count : u64 = all_manifest_files
298
304
. iter ( )
299
305
. flat_map ( |m| & m. files )
300
306
. filter_map ( |f| {
@@ -315,18 +321,18 @@ impl DateBinRequest {
315
321
} )
316
322
. sum ( ) ;
317
323
318
- date_bin_records . push ( DateBinRecord {
319
- date_bin_timestamp : DateTime :: from_timestamp_millis ( date_bin_timestamp )
324
+ counts_records . push ( CountsRecord {
325
+ counts_timestamp : DateTime :: from_timestamp_millis ( counts_timestamp )
320
326
. unwrap ( )
321
327
. to_rfc3339 ( ) ,
322
- log_count : total_num_rows ,
328
+ log_count,
323
329
} ) ;
324
330
}
325
- Ok ( date_bin_records )
331
+ Ok ( counts_records )
326
332
}
327
333
328
334
/// Calculate the end time for each bin based on the number of bins
329
- fn get_bins ( & self , time_range : & TimeRange ) -> Vec < DateBinBounds > {
335
+ fn get_bounds ( & self , time_range : & TimeRange ) -> Vec < TimeBounds > {
330
336
let total_minutes = time_range
331
337
. end
332
338
. signed_duration_since ( time_range. start )
@@ -337,9 +343,9 @@ impl DateBinRequest {
337
343
let remainder = total_minutes % self . num_bins ;
338
344
let have_remainder = remainder > 0 ;
339
345
340
- // now create multiple bins [startTime, endTime)
346
+ // now create multiple bounds [startTime, endTime)
341
347
// Should we exclude the last one???
342
- let mut final_date_bins = vec ! [ ] ;
348
+ let mut bounds = vec ! [ ] ;
343
349
344
350
let mut start = time_range. start ;
345
351
@@ -352,32 +358,34 @@ impl DateBinRequest {
352
358
// Create bins for all but the last date
353
359
for _ in 0 ..loop_end {
354
360
let end = start + Duration :: minutes ( quotient as i64 ) ;
355
- final_date_bins . push ( DateBinBounds { start, end } ) ;
361
+ bounds . push ( TimeBounds { start, end } ) ;
356
362
start = end;
357
363
}
358
364
359
365
// Add the last bin, accounting for any remainder, should we include it?
360
366
if have_remainder {
361
- final_date_bins . push ( DateBinBounds {
367
+ bounds . push ( TimeBounds {
362
368
start,
363
369
end : start + Duration :: minutes ( remainder as i64 ) ,
364
370
} ) ;
365
371
} else {
366
- final_date_bins . push ( DateBinBounds {
372
+ bounds . push ( TimeBounds {
367
373
start,
368
374
end : start + Duration :: minutes ( quotient as i64 ) ,
369
375
} ) ;
370
376
}
371
377
372
- final_date_bins
378
+ bounds
373
379
}
374
380
}
375
381
376
- /// DateBin Response.
382
+ /// Response for the counts API
377
383
#[ derive( Debug , Serialize , Clone ) ]
378
- pub struct DateBinResponse {
384
+ pub struct CountsResponse {
385
+ /// Fields in the log stream
379
386
pub fields : Vec < String > ,
380
- pub records : Vec < DateBinRecord > ,
387
+ /// Records in the response
388
+ pub records : Vec < CountsRecord > ,
381
389
}
382
390
383
391
#[ derive( Debug , Default ) ]
0 commit comments