23
23
import org .springframework .stereotype .Service ;
24
24
import org .springframework .util .CollectionUtils ;
25
25
26
- import java .util .ArrayList ;
27
- import java .util .Arrays ;
28
- import java .util .List ;
29
- import java .util .Map ;
26
+ import java .util .*;
30
27
import java .util .stream .Collectors ;
31
28
32
29
@ Service
@@ -60,11 +57,8 @@ public List<CardType> countAlertsTodayAndLastWeek() throws DashboardOverviewExce
60
57
return result ;
61
58
}
62
59
63
- List <FilterType > filters = new ArrayList <>();
64
- filters .add (new FilterType (Constants .alertStatus , OperatorType .IS_NOT , AlertStatus .AUTOMATIC_REVIEW .getCode ()));
65
-
66
60
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
68
62
.dateRange (dr -> dr .field (Constants .timestamp )
69
63
.keyed (true ).timeZone ("UTC" )
70
64
.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
90
84
if (!elasticsearchService .indexExist (Constants .SYS_INDEX_PATTERN .get (SystemIndexPattern .ALERTS )))
91
85
return new TableType ();
92
86
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 ))))
98
88
.index (Constants .SYS_INDEX_PATTERN .get (SystemIndexPattern .ALERTS ))
99
89
.aggregations (AGG_NAME , agg -> agg .terms (t -> t .field (Constants .alertNameKeyword )
100
90
.size (top ).order (List .of (Map .of ("_count" , SortOrder .Desc ))))));
@@ -124,11 +114,7 @@ public PieType countAlertsBySeverity(String from, String to, Integer top) throws
124
114
if (!elasticsearchService .indexExist (Constants .SYS_INDEX_PATTERN .get (SystemIndexPattern .ALERTS )))
125
115
return new PieType ();
126
116
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 ))))
132
118
.index (Constants .SYS_INDEX_PATTERN .get (SystemIndexPattern .ALERTS ))
133
119
.aggregations (AGG_NAME , agg -> agg .terms (t -> t .field (Constants .alertSeverityLabel )
134
120
.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
160
146
if (!elasticsearchService .indexExist (Constants .SYS_INDEX_PATTERN .get (SystemIndexPattern .ALERTS )))
161
147
return new BarType ();
162
148
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 ))))
168
150
.index (Constants .SYS_INDEX_PATTERN .get (SystemIndexPattern .ALERTS ))
169
151
.aggregations (AGG_NAME , agg -> agg .terms (t -> t .field (Constants .alertCategoryKeyword )
170
152
.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
308
290
throw new DashboardOverviewException (ctx + ": " + e .getMessage ());
309
291
}
310
292
}
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
+ }
311
305
}
0 commit comments