2323import org .springframework .stereotype .Service ;
2424import org .springframework .util .CollectionUtils ;
2525
26- import java .util .ArrayList ;
27- import java .util .Arrays ;
28- import java .util .List ;
29- import java .util .Map ;
26+ import java .util .*;
3027import java .util .stream .Collectors ;
3128
3229@ Service
@@ -60,11 +57,8 @@ public List<CardType> countAlertsTodayAndLastWeek() throws DashboardOverviewExce
6057 return result ;
6158 }
6259
63- List <FilterType > filters = new ArrayList <>();
64- filters .add (new FilterType (Constants .alertStatus , OperatorType .IS_NOT , AlertStatus .AUTOMATIC_REVIEW .getCode ()));
65-
6660 SearchRequest sr = SearchRequest .of (s -> s .index (Constants .SYS_INDEX_PATTERN .get (SystemIndexPattern .ALERTS ))
67- .query (SearchUtil .toQuery (filters )).aggregations (AGG_NAME , Aggregation .of (agg -> agg
61+ .query (SearchUtil .toQuery (this . getDefaultFilters ( Collections . emptyList ()) )).aggregations (AGG_NAME , Aggregation .of (agg -> agg
6862 .dateRange (dr -> dr .field (Constants .timestamp )
6963 .keyed (true ).timeZone ("UTC" )
7064 .ranges (r -> r .key (TODAY_KEY ).from (f -> f .expr ("now/d" )).to (t -> t .expr ("now" )))
@@ -90,11 +84,7 @@ public TableType topAlerts(String from, String to, Integer top) throws Dashboard
9084 if (!elasticsearchService .indexExist (Constants .SYS_INDEX_PATTERN .get (SystemIndexPattern .ALERTS )))
9185 return new TableType ();
9286
93- List <FilterType > filters = new ArrayList <>();
94- filters .add (new FilterType (Constants .alertStatus , OperatorType .IS_NOT , AlertStatus .AUTOMATIC_REVIEW .getCode ()));
95- filters .add (new FilterType (Constants .timestamp , OperatorType .IS_BETWEEN , List .of (from , to )));
96-
97- SearchRequest rq = SearchRequest .of (s -> s .size (0 ).query (SearchUtil .toQuery (filters ))
87+ SearchRequest rq = SearchRequest .of (s -> s .size (0 ).query (SearchUtil .toQuery (this .getDefaultFilters (List .of (from , to ))))
9888 .index (Constants .SYS_INDEX_PATTERN .get (SystemIndexPattern .ALERTS ))
9989 .aggregations (AGG_NAME , agg -> agg .terms (t -> t .field (Constants .alertNameKeyword )
10090 .size (top ).order (List .of (Map .of ("_count" , SortOrder .Desc ))))));
@@ -124,11 +114,7 @@ public PieType countAlertsBySeverity(String from, String to, Integer top) throws
124114 if (!elasticsearchService .indexExist (Constants .SYS_INDEX_PATTERN .get (SystemIndexPattern .ALERTS )))
125115 return new PieType ();
126116
127- List <FilterType > filters = new ArrayList <>();
128- filters .add (new FilterType (Constants .alertStatus , OperatorType .IS_NOT , AlertStatus .AUTOMATIC_REVIEW .getCode ()));
129- filters .add (new FilterType (Constants .timestamp , OperatorType .IS_BETWEEN , List .of (from , to )));
130-
131- SearchRequest rq = SearchRequest .of (s -> s .size (0 ).query (SearchUtil .toQuery (filters ))
117+ SearchRequest rq = SearchRequest .of (s -> s .size (0 ).query (SearchUtil .toQuery (this .getDefaultFilters (List .of (from , to ))))
132118 .index (Constants .SYS_INDEX_PATTERN .get (SystemIndexPattern .ALERTS ))
133119 .aggregations (AGG_NAME , agg -> agg .terms (t -> t .field (Constants .alertSeverityLabel )
134120 .size (top ).order (List .of (Map .of ("_count" , SortOrder .Desc ))))));
@@ -160,11 +146,7 @@ public BarType topAlertsByCategory(String from, String to, Integer top) throws D
160146 if (!elasticsearchService .indexExist (Constants .SYS_INDEX_PATTERN .get (SystemIndexPattern .ALERTS )))
161147 return new BarType ();
162148
163- List <FilterType > filters = new ArrayList <>();
164- filters .add (new FilterType (Constants .alertStatus , OperatorType .IS_NOT , AlertStatus .AUTOMATIC_REVIEW .getCode ()));
165- filters .add (new FilterType (Constants .timestamp , OperatorType .IS_BETWEEN , List .of (from , to )));
166-
167- SearchRequest rq = SearchRequest .of (s -> s .size (0 ).query (SearchUtil .toQuery (filters ))
149+ SearchRequest rq = SearchRequest .of (s -> s .size (0 ).query (SearchUtil .toQuery (this .getDefaultFilters (List .of (from , to ))))
168150 .index (Constants .SYS_INDEX_PATTERN .get (SystemIndexPattern .ALERTS ))
169151 .aggregations (AGG_NAME , agg -> agg .terms (t -> t .field (Constants .alertCategoryKeyword )
170152 .size (top ).order (List .of (Map .of ("_count" , SortOrder .Desc ))))));
@@ -308,4 +290,16 @@ public TableType topWindowsEvents(String from, String to, Integer top) throws Da
308290 throw new DashboardOverviewException (ctx + ": " + e .getMessage ());
309291 }
310292 }
293+
294+ private List <FilterType > getDefaultFilters (List <String > dateRange ){
295+ List <FilterType > filters = new ArrayList <>();
296+ filters .add (new FilterType (Constants .alertStatus , OperatorType .IS_NOT , AlertStatus .AUTOMATIC_REVIEW .getCode ()));
297+ filters .add (new FilterType (Constants .alertTags , OperatorType .IS_NOT , Constants .FALSE_POSITIVE_TAG ));
298+
299+ if (!CollectionUtils .isEmpty (dateRange )){
300+ filters .add (new FilterType (Constants .timestamp , OperatorType .IS_BETWEEN , dateRange ));
301+ }
302+
303+ return filters ;
304+ }
311305}
0 commit comments