|
44 | 44 |
|
45 | 45 | import java.time.Instant;
|
46 | 46 | import java.time.temporal.ChronoUnit;
|
| 47 | +import java.util.Arrays; |
47 | 48 | import java.util.List;
|
48 | 49 | import java.util.Set;
|
49 | 50 |
|
@@ -319,35 +320,43 @@ public void addTimeSeriesSnapshotHistory(MongockTemplate mongoTemplate, CommonCo
|
319 | 320 |
|
320 | 321 | // Create the time-series collection if it doesn't exist
|
321 | 322 | if (!mongoTemplate.collectionExists(ApplicationHistorySnapshotTS.class)) {
|
322 |
| - if(mongoVersion < 5) { |
| 323 | + if (mongoVersion < 5) { |
323 | 324 | mongoTemplate.createCollection(ApplicationHistorySnapshotTS.class);
|
324 | 325 | } else {
|
325 | 326 | mongoTemplate.createCollection(ApplicationHistorySnapshotTS.class, CollectionOptions.empty().timeSeries("createdAt"));
|
326 | 327 | }
|
327 | 328 | }
|
| 329 | + |
328 | 330 | Instant thresholdDate = Instant.now().minus(commonConfig.getQuery().getAppSnapshotKeepDuration(), ChronoUnit.DAYS);
|
329 |
| - List<ApplicationHistorySnapshot> snapshots = mongoTemplate.find(new Query().addCriteria(Criteria.where("createdAt").gte(thresholdDate)), ApplicationHistorySnapshot.class); |
330 |
| - snapshots.forEach(snapshot -> { |
331 |
| - ApplicationHistorySnapshotTS applicationHistorySnapshotTS = new ApplicationHistorySnapshotTS(); |
332 |
| - applicationHistorySnapshotTS.setApplicationId(snapshot.getApplicationId()); |
333 |
| - applicationHistorySnapshotTS.setDsl(snapshot.getDsl()); |
334 |
| - applicationHistorySnapshotTS.setContext(snapshot.getContext()); |
335 |
| - applicationHistorySnapshotTS.setCreatedAt(snapshot.getCreatedAt()); |
336 |
| - applicationHistorySnapshotTS.setCreatedBy(snapshot.getCreatedBy()); |
337 |
| - applicationHistorySnapshotTS.setModifiedBy(snapshot.getModifiedBy()); |
338 |
| - applicationHistorySnapshotTS.setUpdatedAt(snapshot.getUpdatedAt()); |
339 |
| - applicationHistorySnapshotTS.setId(snapshot.getId()); |
340 |
| - mongoTemplate.insert(applicationHistorySnapshotTS); |
341 |
| - mongoTemplate.remove(snapshot); |
342 |
| - }); |
343 | 331 |
|
344 |
| - // Ensure indexes if needed |
| 332 | + // Use aggregation to move and transform data |
| 333 | + Document match = new Document("$match", |
| 334 | + new Document("createdAt", new Document("$gte", thresholdDate))); |
| 335 | + |
| 336 | + Document project = new Document("$project", new Document() |
| 337 | + .append("applicationId", 1) |
| 338 | + .append("dsl", 1) |
| 339 | + .append("context", 1) |
| 340 | + .append("createdAt", 1) |
| 341 | + .append("createdBy", 1) |
| 342 | + .append("modifiedBy", 1) |
| 343 | + .append("updatedAt", 1) |
| 344 | + .append("id", "$_id")); // Map MongoDB's default `_id` to `id` if needed. |
| 345 | + |
| 346 | + Document out = new Document("$out", "applicationHistorySnapshotTS"); // Target collection name |
| 347 | + |
| 348 | + // Execute the aggregation pipeline |
| 349 | + mongoTemplate.getDb() |
| 350 | + .getCollection("applicationHistorySnapshot") // Original collection name |
| 351 | + .aggregate(Arrays.asList(match, project, out)) |
| 352 | + .toCollection(); |
| 353 | + |
345 | 354 | ensureIndexes(mongoTemplate, ApplicationHistorySnapshotTS.class,
|
346 | 355 | makeIndex("applicationId"),
|
347 |
| - makeIndex("createdAt") |
348 |
| - ); |
| 356 | + makeIndex("createdAt")); |
349 | 357 | }
|
350 | 358 |
|
| 359 | + |
351 | 360 | private void addGidField(MongockTemplate mongoTemplate, String collectionName) {
|
352 | 361 | // Create a query to match all documents
|
353 | 362 | Query query = new Query();
|
|
0 commit comments