Skip to content

Commit

Permalink
Preallocating new lists, fixing off by one.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisoelkers committed May 6, 2015
1 parent 5f38f91 commit af150bd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
Expand Up @@ -97,9 +97,10 @@ public void call(Stream stream, AlertCondition.CheckResult result) {

protected List<Message> getAlarmBacklog(AlertCondition.CheckResult result) {
final AlertCondition alertCondition = result.getTriggeredCondition();
final int effectiveBacklogSize = Math.min(alertCondition.getBacklog(), result.getMatchingMessages().size());
final List<MessageSummary> backlogSummaries = result.getMatchingMessages()
.subList(0, Math.min(alertCondition.getBacklog(), result.getMatchingMessages().size()));
final List<Message> backlog = Lists.newArrayList();
.subList(0, effectiveBacklogSize-1);
final List<Message> backlog = Lists.newArrayListWithCapacity(effectiveBacklogSize);

for (MessageSummary messageSummary : backlogSummaries) {
backlog.add(messageSummary.getRawMessage());
Expand Down
Expand Up @@ -36,6 +36,7 @@

import javax.annotation.Nullable;
import java.text.DecimalFormat;
import java.util.Collections;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -140,17 +141,21 @@ protected CheckResult runCheck() {
}

if (triggered) {
final List<MessageSummary> summaries = Lists.newArrayList();
final String resultDescription = "Field " + field + " had a " + type + " of "
+ decimalFormat.format(result) + " in the last " + time + " minutes with trigger condition "
+ thresholdType + " than " + decimalFormat.format(threshold) + ". "
+ "(Current grace time: " + grace + " minutes)";

final List<MessageSummary> summaries;
if (getBacklog() > 0) {
for (ResultMessage resultMessage : fieldStatsResult.getSearchHits()) {
final List<ResultMessage> searchHits = fieldStatsResult.getSearchHits();
summaries = Lists.newArrayListWithCapacity(searchHits.size());
for (ResultMessage resultMessage : searchHits) {
final Message msg = new Message(resultMessage.getMessage());
summaries.add(new MessageSummary(resultMessage.getIndex(), msg));
}
} else {
summaries = Collections.emptyList();
}

return new CheckResult(true, this, resultDescription, Tools.iso8601(), summaries);
Expand Down

0 comments on commit af150bd

Please sign in to comment.