12
12
import com .mongodb .client .MongoClient ;
13
13
import com .mongodb .client .MongoCollection ;
14
14
import com .mongodb .client .MongoCursor ;
15
+ import com .mongodb .client .model .Filters ;
15
16
16
17
import java .util .ArrayList ;
17
18
import java .util .List ;
18
19
import java .util .Map ;
19
20
20
21
import org .bson .Document ;
22
+ import org .bson .conversions .Bson ;
21
23
22
24
public class ThreatApiService {
23
25
@@ -173,7 +175,7 @@ public ThreatCategoryWiseCountResponse getSubCategoryWiseCount(
173
175
}
174
176
175
177
public ThreatSeverityWiseCountResponse getSeverityWiseCount (
176
- String accountId , ThreatSeverityWiseCountRequest req ) {
178
+ String accountId , ThreatSeverityWiseCountRequest req ) {
177
179
178
180
loggerMaker .info ("getSeverityWiseCount start ts " + Context .now ());
179
181
@@ -182,36 +184,24 @@ public ThreatSeverityWiseCountResponse getSeverityWiseCount(
182
184
.getDatabase (accountId )
183
185
.getCollection (MongoDBCollection .ThreatDetection .MALICIOUS_EVENTS , Document .class );
184
186
185
- List <Document > pipeline = new ArrayList <>();
186
- Document matchFilter = new Document ("$match" ,
187
- new Document ("detectedAt" , new Document ("$gte" , req .getStartTs ()).append ("$lte" , req .getEndTs ()))
188
- );
189
- pipeline .add (matchFilter );
190
- pipeline .add (
191
- new Document ("$sort" , new Document ("category" , 1 ).append ("detectedAt" , -1 ))); // sort
192
- pipeline .add (
193
- new Document (
194
- "$group" ,
195
- new Document (
196
- "_id" ,
197
- new Document ("severity" , "$severity" ))
198
- .append ("count" , new Document ("$sum" , 1 ))));
187
+ List <ThreatSeverityWiseCountResponse .SeverityCount > categoryWiseCounts = new ArrayList <>();
199
188
200
- pipeline .add (
201
- new Document (
202
- "$sort" ,
203
- new Document ("severity" , -1 ).append ("severity" , -1 ).append ("count" , -1 ))); // sort
189
+ String [] severities = { "CRITICAL" , "HIGH" , "MEDIUM" , "LOW" };
204
190
205
- List <ThreatSeverityWiseCountResponse .SeverityCount > categoryWiseCounts = new ArrayList <>();
191
+ for (String severity : severities ) {
192
+ Bson filter = Filters .and (
193
+ Filters .eq ("severity" , severity ),
194
+ Filters .gte ("detectedAt" , req .getStartTs ()),
195
+ Filters .lte ("detectedAt" , req .getEndTs ())
196
+ );
206
197
207
- try (MongoCursor <Document > cursor = coll .aggregate (pipeline ).cursor ()) {
208
- while (cursor .hasNext ()) {
209
- Document doc = cursor .next ();
210
- Document agg = (Document ) doc .get ("_id" );
198
+ long count = coll .countDocuments (filter );
199
+
200
+ if (count > 0 ) {
211
201
categoryWiseCounts .add (
212
202
ThreatSeverityWiseCountResponse .SeverityCount .newBuilder ()
213
- .setSeverity (agg . getString ( " severity" ) )
214
- .setCount (doc . getInteger ( "count" , 0 ) )
203
+ .setSeverity (severity )
204
+ .setCount (( int ) count )
215
205
.build ());
216
206
}
217
207
}
@@ -222,4 +212,5 @@ public ThreatSeverityWiseCountResponse getSeverityWiseCount(
222
212
.addAllCategoryWiseCounts (categoryWiseCounts )
223
213
.build ();
224
214
}
215
+
225
216
}
0 commit comments