Skip to content

Commit

Permalink
fix: create a count method in AlertEvent repository and improve the w…
Browse files Browse the repository at this point in the history
…ay analytics are fetched in AlertService

https://gravitee.atlassian.net/browse/APIM-3190
gravitee-io/issues#9362
(cherry picked from commit 0583ec4)

# Conflicts:
#	gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/resources/scripts/create-index.js
  • Loading branch information
gaetanmaisse committed Nov 3, 2023
1 parent 44412d3 commit 549c382
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 38 deletions.
Expand Up @@ -38,6 +38,14 @@ public interface AlertEventRepository extends CrudRepository<AlertEvent, String>
*/
Page<AlertEvent> search(AlertEventCriteria criteria, Pageable pageable);

/**
* Counts the number of {@link AlertEvent}s that match the given criteria.
*
* @param criteria The criteria to match the {@link AlertEvent}s against.
* @return The number of {@link AlertEvent}s that match the given criteria.
*/
long count(AlertEventCriteria criteria);

/**
* delete all events of the provided alert
* @param alertId
Expand Down
Expand Up @@ -122,6 +122,37 @@ public Page<AlertEvent> search(AlertEventCriteria criteria, Pageable pageable) {
return getResultAsPage(pageable, events);
}

@Override
public long count(AlertEventCriteria criteria) {
final List<Object> args = new ArrayList<>();
final StringBuilder builder = new StringBuilder("select count(*) from " + this.tableName + " ev ");

boolean started = false;
if (criteria.getFrom() > 0) {
builder.append(WHERE_CLAUSE);
builder.append("created_at >= ?");
args.add(new Timestamp(criteria.getFrom()));
started = true;
}
if (criteria.getTo() > 0) {
builder.append(started ? AND_CLAUSE : WHERE_CLAUSE);
builder.append("created_at < ?");
args.add(new Timestamp(criteria.getTo()));
started = true;
}

if (criteria.getAlert() != null && !criteria.getAlert().isEmpty()) {
builder.append(started ? AND_CLAUSE : WHERE_CLAUSE);
builder.append("alert = ?");
args.add(criteria.getAlert());
}

String sql = builder.toString();
LOGGER.debug("SQL: {}", sql);
LOGGER.debug("Args: {}", args);
return jdbcTemplate.queryForObject(sql, args.toArray(), Long.class);
}

@Override
public void deleteAll(String alertId) {
jdbcTemplate.update("delete from " + this.tableName + " where alert = ?", alertId);
Expand Down
Expand Up @@ -126,6 +126,11 @@ public Page<AlertEvent> search(AlertEventCriteria criteria, Pageable pageable) {
);
}

@Override
public long count(AlertEventCriteria criteria) {
return internalAlertEventRepo.count(criteria);
}

@Override
public Set<AlertEvent> findAll() throws TechnicalException {
return internalAlertEventRepo.findAll().stream().map(alertEventMongo -> mapper.map(alertEventMongo)).collect(Collectors.toSet());
Expand Down
Expand Up @@ -28,4 +28,6 @@ public interface AlertEventMongoRepositoryCustom {
Page<AlertEventMongo> search(AlertEventCriteria criteria, Pageable pageable);

void deleteAll(String alertId);

long count(AlertEventCriteria criteria);
}
Expand Up @@ -40,22 +40,11 @@ public class AlertEventMongoRepositoryImpl implements AlertEventMongoRepositoryC

@Override
public Page<AlertEventMongo> search(AlertEventCriteria criteria, Pageable pageable) {
Query query = new Query();

if (criteria.getAlert() != null) {
query.addCriteria(Criteria.where("alert").is(criteria.getAlert()));
}

// set range query
if (criteria.getFrom() != 0 && criteria.getTo() != 0) {
query.addCriteria(Criteria.where("createdAt").gte(new Date(criteria.getFrom())).lt(new Date(criteria.getTo())));
}
Query query = buildQueryFromCriteria(criteria);
long total = mongoTemplate.count(query, AlertEventMongo.class);

// set sort by updated at
query.with(Sort.by(Sort.Direction.DESC, "createdAt"));

long total = mongoTemplate.count(query, AlertEventMongo.class);

// set pageable
if (pageable != null) {
query.with(PageRequest.of(pageable.pageNumber(), pageable.pageSize()));
Expand All @@ -71,4 +60,23 @@ public void deleteAll(String alertId) {
Query query = new Query().addCriteria(Criteria.where("alert").is(alertId));
mongoTemplate.remove(query, AlertEventMongo.class);
}

@Override
public long count(AlertEventCriteria criteria) {
Query query = buildQueryFromCriteria(criteria);
return mongoTemplate.count(query, AlertEventMongo.class);
}

private static Query buildQueryFromCriteria(AlertEventCriteria criteria) {
Query query = new Query();

if (criteria.getAlert() != null) {
query.addCriteria(Criteria.where("alert").is(criteria.getAlert()));
}

if (criteria.getFrom() != 0 && criteria.getTo() != 0) {
query.addCriteria(Criteria.where("createdAt").gte(new Date(criteria.getFrom())).lt(new Date(criteria.getTo())));
}
return query;
}
}
Expand Up @@ -147,6 +147,7 @@ db.getCollection(`${prefix}alert_triggers`).reIndex();
db.getCollection(`${prefix}alert_events`).dropIndexes();
db.getCollection(`${prefix}alert_events`).createIndex({ alert: 1 }, { name: "a1" });
db.getCollection(`${prefix}alert_events`).createIndex({ createdAt: 1 }, { name: "c1" });
db.getCollection(`${prefix}alert_events`).createIndex({"alert": 1, "createdAt": 1}, {"name": "a1c1"});
db.getCollection(`${prefix}alert_events`).reIndex();

// "customUserFields" collection
Expand Down
Expand Up @@ -141,4 +141,22 @@ public void shouldDeleteAll() throws Exception {
assertEquals("2 before", 2, before);
assertEquals("0 after", 0, after);
}

@Test
public void shouldCountWithCriteria() {
long count = alertEventRepository.count(
new AlertEventCriteria.Builder().alert("alert-parent-id").from(1439022010900L).to(1439022010999L).build()
);
assertEquals(1, count);
}

@Test
public void shouldSearchWithCriteria() {
Page<AlertEvent> result = alertEventRepository.search(
new AlertEventCriteria.Builder().alert("alert-parent-id").from(1439022010900L).to(1439022010999L).build(),
null
);
assertEquals(1, result.getTotalElements());
assertEquals("an-alert-to-update", result.getContent().get(0).getId());
}
}
Expand Up @@ -9,8 +9,8 @@
"id": "an-alert-to-update",
"alert": "alert-parent-id",
"message": "Message of the alert to update",
"createdAt": 1439022010883,
"updatedAt": 1439022010883
"createdAt": 1439022010920,
"updatedAt": 1439022010920
},
{
"id": "latest-alert",
Expand Down
Expand Up @@ -334,23 +334,27 @@ public List<AlertTriggerEntity> findByReferenceWithEventCounts(final AlertRefere

final Date from = new Date(System.currentTimeMillis());

Map<String, Integer> counters = new HashMap<>();
counters.put(
"5m",
countEvents(entity.getId(), from.toInstant().minus(Duration.ofMinutes(5)).toEpochMilli(), from.getTime())
);
counters.put(
"1h",
countEvents(entity.getId(), from.toInstant().minus(Duration.ofHours(1)).toEpochMilli(), from.getTime())
);
counters.put(
"1d",
countEvents(entity.getId(), from.toInstant().minus(Duration.ofDays(1)).toEpochMilli(), from.getTime())
);
counters.put(
"1M",
countEvents(entity.getId(), from.toInstant().minus(Duration.ofDays(30)).toEpochMilli(), from.getTime())
);
Map<String, Integer> counters = Map
.of(
"5m",
from.toInstant().minus(Duration.ofMinutes(5)),
"1h",
from.toInstant().minus(Duration.ofHours(1)),
"1d",
from.toInstant().minus(Duration.ofDays(1)),
"1M",
from.toInstant().minus(Duration.ofDays(30))
)
.entrySet()
// Get the count of events for each time period in parallel to speed up the process
.parallelStream()
.map(entry ->
new AbstractMap.SimpleEntry<>(
entry.getKey(),
countEvents(entity.getId(), entry.getValue().toEpochMilli(), from.getTime())
)
)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

entity.setCounters(counters);
return entity;
Expand Down Expand Up @@ -796,12 +800,8 @@ private Map<String, Object> getPlanMetadata(String plan) {
}

private int countEvents(final String triggerId, final long from, final long to) {
return (int) alertEventRepository
.search(
new AlertEventCriteria.Builder().alert(triggerId).from(from).to(to).build(),
new PageableBuilder().pageNumber(0).pageSize(1).build()
)
.getTotalElements();
AlertEventCriteria criteria = new AlertEventCriteria.Builder().alert(triggerId).from(from).to(to).build();
return Math.toIntExact(alertEventRepository.count(criteria));
}

private Optional<AlertEvent> getLastEvent(final String triggerId) {
Expand Down

0 comments on commit 549c382

Please sign in to comment.