@@ -50,12 +50,6 @@ struct DatedStats {
50
50
storage_size : u64 ,
51
51
}
52
52
53
- #[ derive( Debug , Serialize ) ]
54
- struct TitleAndId {
55
- title : String ,
56
- id : String ,
57
- }
58
-
59
53
#[ derive( Debug , Serialize ) ]
60
54
enum DataSetType {
61
55
Logs ,
@@ -76,12 +70,25 @@ pub struct HomeResponse {
76
70
datasets : Vec < DataSet > ,
77
71
}
78
72
73
+ #[ derive( Debug , Serialize ) ]
74
+ pub enum ResourceType {
75
+ Alert ,
76
+ Correlation ,
77
+ Dashboard ,
78
+ Filter ,
79
+ DataSet ,
80
+ }
81
+
82
+ #[ derive( Debug , Serialize ) ]
83
+ pub struct Resource {
84
+ id : String ,
85
+ name : String ,
86
+ resource_type : ResourceType ,
87
+ }
88
+
79
89
#[ derive( Debug , Serialize ) ]
80
90
pub struct HomeSearchResponse {
81
- alerts : Vec < TitleAndId > ,
82
- correlations : Vec < TitleAndId > ,
83
- dashboards : Vec < TitleAndId > ,
84
- filters : Vec < TitleAndId > ,
91
+ resources : Vec < Resource > ,
85
92
}
86
93
87
94
pub async fn generate_home_response ( key : & SessionKey ) -> Result < HomeResponse , PrismHomeError > {
@@ -255,25 +262,37 @@ async fn get_stream_stats_for_date(
255
262
256
263
pub async fn generate_home_search_response (
257
264
key : & SessionKey ,
265
+ query_value : & str ,
258
266
) -> Result < HomeSearchResponse , PrismHomeError > {
259
- let ( alert_titles, correlation_titles, dashboard_titles, filter_titles) = tokio:: join!(
260
- get_alert_titles( key) ,
261
- get_correlation_titles( key) ,
262
- get_dashboard_titles( key) ,
263
- get_filter_titles( key)
267
+ let mut resources = Vec :: new ( ) ;
268
+ let ( alert_titles, correlation_titles, dashboard_titles, filter_titles, stream_titles) = tokio:: join!(
269
+ get_alert_titles( key, query_value) ,
270
+ get_correlation_titles( key, query_value) ,
271
+ get_dashboard_titles( key, query_value) ,
272
+ get_filter_titles( key, query_value) ,
273
+ get_stream_titles( key)
264
274
) ;
265
275
266
276
let alerts = alert_titles?;
277
+ resources. extend ( alerts) ;
267
278
let correlations = correlation_titles?;
279
+ resources. extend ( correlations) ;
268
280
let dashboards = dashboard_titles?;
281
+ resources. extend ( dashboards) ;
269
282
let filters = filter_titles?;
270
-
271
- Ok ( HomeSearchResponse {
272
- alerts,
273
- correlations,
274
- dashboards,
275
- filters,
276
- } )
283
+ resources. extend ( filters) ;
284
+ let stream_titles = stream_titles?;
285
+
286
+ for title in stream_titles {
287
+ if title. to_lowercase ( ) . contains ( query_value) {
288
+ resources. push ( Resource {
289
+ id : title. clone ( ) ,
290
+ name : title,
291
+ resource_type : ResourceType :: DataSet ,
292
+ } ) ;
293
+ }
294
+ }
295
+ Ok ( HomeSearchResponse { resources } )
277
296
}
278
297
279
298
// Helper functions to split the work
@@ -295,59 +314,108 @@ async fn get_stream_titles(key: &SessionKey) -> Result<Vec<String>, PrismHomeErr
295
314
Ok ( stream_titles)
296
315
}
297
316
298
- async fn get_alert_titles ( key : & SessionKey ) -> Result < Vec < TitleAndId > , PrismHomeError > {
299
- let alert_titles = ALERTS
317
+ async fn get_alert_titles (
318
+ key : & SessionKey ,
319
+ query_value : & str ,
320
+ ) -> Result < Vec < Resource > , PrismHomeError > {
321
+ let alerts = ALERTS
300
322
. list_alerts_for_user ( key. clone ( ) )
301
323
. await ?
302
324
. iter ( )
303
- . map ( |alert| TitleAndId {
304
- title : alert. title . clone ( ) ,
305
- id : alert. id . to_string ( ) ,
325
+ . filter_map ( |alert| {
326
+ if alert. title . to_lowercase ( ) . contains ( query_value)
327
+ || alert. id . to_string ( ) . to_lowercase ( ) . contains ( query_value)
328
+ {
329
+ Some ( Resource {
330
+ id : alert. id . to_string ( ) ,
331
+ name : alert. title . clone ( ) ,
332
+ resource_type : ResourceType :: Alert ,
333
+ } )
334
+ } else {
335
+ None
336
+ }
306
337
} )
307
338
. collect_vec ( ) ;
308
339
309
- Ok ( alert_titles )
340
+ Ok ( alerts )
310
341
}
311
342
312
- async fn get_correlation_titles ( key : & SessionKey ) -> Result < Vec < TitleAndId > , PrismHomeError > {
313
- let correlation_titles = CORRELATIONS
343
+ async fn get_correlation_titles (
344
+ key : & SessionKey ,
345
+ query_value : & str ,
346
+ ) -> Result < Vec < Resource > , PrismHomeError > {
347
+ let correlations = CORRELATIONS
314
348
. list_correlations ( key)
315
349
. await ?
316
350
. iter ( )
317
- . map ( |corr| TitleAndId {
318
- title : corr. title . clone ( ) ,
319
- id : corr. id . clone ( ) ,
351
+ . filter_map ( |correlation| {
352
+ if correlation. title . to_lowercase ( ) . contains ( query_value)
353
+ || correlation. id . to_lowercase ( ) . contains ( query_value)
354
+ {
355
+ Some ( Resource {
356
+ id : correlation. id . to_string ( ) ,
357
+ name : correlation. title . clone ( ) ,
358
+ resource_type : ResourceType :: Correlation ,
359
+ } )
360
+ } else {
361
+ None
362
+ }
320
363
} )
321
364
. collect_vec ( ) ;
322
365
323
- Ok ( correlation_titles )
366
+ Ok ( correlations )
324
367
}
325
368
326
- async fn get_dashboard_titles ( key : & SessionKey ) -> Result < Vec < TitleAndId > , PrismHomeError > {
369
+ async fn get_dashboard_titles (
370
+ key : & SessionKey ,
371
+ query_value : & str ,
372
+ ) -> Result < Vec < Resource > , PrismHomeError > {
327
373
let dashboard_titles = DASHBOARDS
328
374
. list_dashboards ( key)
329
375
. await
330
376
. iter ( )
331
- . map ( |dashboard| TitleAndId {
332
- title : dashboard. name . clone ( ) ,
333
- id : dashboard. dashboard_id . as_ref ( ) . unwrap ( ) . clone ( ) ,
377
+ . filter_map ( |dashboard| {
378
+ let dashboard_id = dashboard. dashboard_id . as_ref ( ) . unwrap ( ) . clone ( ) ;
379
+ if dashboard. name . to_lowercase ( ) . contains ( query_value)
380
+ || dashboard_id. to_lowercase ( ) . contains ( query_value)
381
+ {
382
+ Some ( Resource {
383
+ id : dashboard_id,
384
+ name : dashboard. name . clone ( ) ,
385
+ resource_type : ResourceType :: Dashboard ,
386
+ } )
387
+ } else {
388
+ None
389
+ }
334
390
} )
335
391
. collect_vec ( ) ;
336
392
337
393
Ok ( dashboard_titles)
338
394
}
339
395
340
- async fn get_filter_titles ( key : & SessionKey ) -> Result < Vec < TitleAndId > , PrismHomeError > {
396
+ async fn get_filter_titles (
397
+ key : & SessionKey ,
398
+ query_value : & str ,
399
+ ) -> Result < Vec < Resource > , PrismHomeError > {
341
400
let filter_titles = FILTERS
342
401
. list_filters ( key)
343
402
. await
344
403
. iter ( )
345
- . map ( |filter| TitleAndId {
346
- title : filter. filter_name . clone ( ) ,
347
- id : filter. filter_id . as_ref ( ) . unwrap ( ) . clone ( ) ,
404
+ . filter_map ( |filter| {
405
+ let filter_id = filter. filter_id . as_ref ( ) . unwrap ( ) . clone ( ) ;
406
+ if filter. filter_name . to_lowercase ( ) . contains ( query_value)
407
+ || filter_id. to_lowercase ( ) . contains ( query_value)
408
+ {
409
+ Some ( Resource {
410
+ id : filter_id,
411
+ name : filter. filter_name . clone ( ) ,
412
+ resource_type : ResourceType :: Filter ,
413
+ } )
414
+ } else {
415
+ None
416
+ }
348
417
} )
349
418
. collect_vec ( ) ;
350
-
351
419
Ok ( filter_titles)
352
420
}
353
421
@@ -363,6 +431,8 @@ pub enum PrismHomeError {
363
431
StreamError ( #[ from] StreamError ) ,
364
432
#[ error( "ObjectStorageError: {0}" ) ]
365
433
ObjectStorageError ( #[ from] ObjectStorageError ) ,
434
+ #[ error( "Invalid query parameter: {0}" ) ]
435
+ InvalidQueryParameter ( String ) ,
366
436
}
367
437
368
438
impl actix_web:: ResponseError for PrismHomeError {
@@ -373,6 +443,7 @@ impl actix_web::ResponseError for PrismHomeError {
373
443
PrismHomeError :: CorrelationError ( e) => e. status_code ( ) ,
374
444
PrismHomeError :: StreamError ( e) => e. status_code ( ) ,
375
445
PrismHomeError :: ObjectStorageError ( _) => StatusCode :: INTERNAL_SERVER_ERROR ,
446
+ PrismHomeError :: InvalidQueryParameter ( _) => StatusCode :: BAD_REQUEST ,
376
447
}
377
448
}
378
449
0 commit comments