diff --git a/drools-ansible-rulebook-integration-api/src/main/java/org/drools/ansible/rulebook/integration/api/rulesengine/RulesExecutorSession.java b/drools-ansible-rulebook-integration-api/src/main/java/org/drools/ansible/rulebook/integration/api/rulesengine/RulesExecutorSession.java index ec403f91..37497b00 100644 --- a/drools-ansible-rulebook-integration-api/src/main/java/org/drools/ansible/rulebook/integration/api/rulesengine/RulesExecutorSession.java +++ b/drools-ansible-rulebook-integration-api/src/main/java/org/drools/ansible/rulebook/integration/api/rulesengine/RulesExecutorSession.java @@ -68,7 +68,7 @@ InternalFactHandle insert(Map factMap, boolean event) { } InternalFactHandle fh = (InternalFactHandle) kieSession.insert(fact); if (event) { - sessionStatsCollector.registerProcessedEvent(fh); + sessionStatsCollector.registerProcessedEvent(this, fh); } return fh; } diff --git a/drools-ansible-rulebook-integration-api/src/main/java/org/drools/ansible/rulebook/integration/api/rulesengine/SessionStats.java b/drools-ansible-rulebook-integration-api/src/main/java/org/drools/ansible/rulebook/integration/api/rulesengine/SessionStats.java index e41a3c5a..9dcbd397 100644 --- a/drools-ansible-rulebook-integration-api/src/main/java/org/drools/ansible/rulebook/integration/api/rulesengine/SessionStats.java +++ b/drools-ansible-rulebook-integration-api/src/main/java/org/drools/ansible/rulebook/integration/api/rulesengine/SessionStats.java @@ -35,6 +35,7 @@ public class SessionStats { private final String lastRuleFired; private final String lastRuleFiredAt; + private final String lastEventReceivedAt; public SessionStats(SessionStatsCollector stats, RulesExecutorSession session, boolean disposing) { this.start = stats.getStart().toString(); @@ -58,12 +59,13 @@ public SessionStats(SessionStatsCollector stats, RulesExecutorSession session, b this.sessionId = session.getId(); this.ruleSetName = session.getRuleSetName(); this.lastRuleFired = stats.getLastRuleFired(); - this.lastRuleFiredAt = Instant.ofEpochMilli(stats.getLastRuleFiredTime()).toString(); + this.lastRuleFiredAt = stats.getLastRuleFiredTime() < 0 ? null : Instant.ofEpochMilli(stats.getLastRuleFiredTime()).toString(); + this.lastEventReceivedAt = stats.getLastEventReceivedTime() < 0 ? null : Instant.ofEpochMilli(stats.getLastEventReceivedTime()).toString(); } public SessionStats(String start, String end, String lastClockTime, int clockAdvanceCount, int numberOfRules, int numberOfDisabledRules, int rulesTriggered, int eventsProcessed, int eventsMatched, int eventsSuppressed, int permanentStorageCount, int permanentStorageSize, int asyncResponses, int bytesSentOnAsync, - long sessionId, String ruleSetName, String lastRuleFired, String lastRuleFiredAt) { + long sessionId, String ruleSetName, String lastRuleFired, String lastRuleFiredAt, String lastEventReceivedAt) { this.start = start; this.end = end; this.lastClockTime = lastClockTime; @@ -82,6 +84,7 @@ public SessionStats(String start, String end, String lastClockTime, int clockAdv this.ruleSetName = ruleSetName; this.lastRuleFired = lastRuleFired; this.lastRuleFiredAt = lastRuleFiredAt; + this.lastEventReceivedAt = lastEventReceivedAt; } @Override @@ -105,6 +108,7 @@ public String toString() { ", ruleSetName='" + ruleSetName + '\'' + ", lastRuleFired='" + lastRuleFired + '\'' + ", lastRuleFiredAt='" + lastRuleFiredAt + '\'' + + ", lastEventReceivedAt='" + lastEventReceivedAt + '\'' + ", usedMemory='" + getUsedMemory() + '\'' + ", maxAvailableMemory='" + getMaxAvailableMemory() + '\'' + '}'; @@ -178,6 +182,10 @@ public String getLastRuleFiredAt() { return lastRuleFiredAt; } + public String getLastEventReceivedAt() { + return lastEventReceivedAt; + } + public int getClockAdvanceCount() { return clockAdvanceCount; } @@ -191,10 +199,10 @@ public long getMaxAvailableMemory() { } public static SessionStats aggregate(SessionStats stats1, SessionStats stats2) { - String lastRuleFired = ""; - String lastRuleFiredAt = ""; + String lastRuleFired = null; + String lastRuleFiredAt = null; - if (Instant.parse(stats1.getLastRuleFiredAt()).compareTo(Instant.parse(stats2.getLastRuleFiredAt())) > 0) { + if (isInstant1Last(stats1.getLastRuleFiredAt(), stats2.getLastRuleFiredAt())) { lastRuleFired = stats1.getLastRuleFired(); lastRuleFiredAt = stats1.getLastRuleFiredAt(); } else { @@ -222,7 +230,13 @@ public static SessionStats aggregate(SessionStats stats1, SessionStats stats2) { -1, stats1.getRuleSetName().equals(stats2.getRuleSetName()) ? stats1.getRuleSetName() : "", lastRuleFired, - lastRuleFiredAt + lastRuleFiredAt, + isInstant1Last(stats1.getLastEventReceivedAt(), stats2.getLastEventReceivedAt()) ? stats1.getLastEventReceivedAt() : stats2.getLastEventReceivedAt() ); } + + private static boolean isInstant1Last(String instant1, String instant2) { + return instant1 != null && (instant2 == null || Instant.parse(instant1).compareTo(Instant.parse(instant2)) > 0); + } + } diff --git a/drools-ansible-rulebook-integration-api/src/main/java/org/drools/ansible/rulebook/integration/api/rulesengine/SessionStatsCollector.java b/drools-ansible-rulebook-integration-api/src/main/java/org/drools/ansible/rulebook/integration/api/rulesengine/SessionStatsCollector.java index 0c5dca02..d4112431 100644 --- a/drools-ansible-rulebook-integration-api/src/main/java/org/drools/ansible/rulebook/integration/api/rulesengine/SessionStatsCollector.java +++ b/drools-ansible-rulebook-integration-api/src/main/java/org/drools/ansible/rulebook/integration/api/rulesengine/SessionStatsCollector.java @@ -29,7 +29,8 @@ public class SessionStatsCollector { private int bytesSentOnAsync; private String lastRuleFired = ""; - private long lastRuleFiredTime; + private long lastRuleFiredTime = -1; + private long lastEventReceivedTime = -1; private int clockAdvanceCount; @@ -83,6 +84,10 @@ public long getLastRuleFiredTime() { return lastRuleFiredTime; } + public long getLastEventReceivedTime() { + return lastEventReceivedTime; + } + public int getClockAdvanceCount() { return clockAdvanceCount; } @@ -101,7 +106,8 @@ public void registerMatchedEvents(Collection events) { matchedEvents += events.size(); } - public void registerProcessedEvent(FactHandle fh) { + public void registerProcessedEvent(RulesExecutorSession session, FactHandle fh) { + lastEventReceivedTime = session.getPseudoClock().getCurrentTime(); totalEvents++; } diff --git a/drools-ansible-rulebook-integration-api/src/test/java/org/drools/ansible/rulebook/integration/api/SessionStatsTest.java b/drools-ansible-rulebook-integration-api/src/test/java/org/drools/ansible/rulebook/integration/api/SessionStatsTest.java index 28f8ab88..065538d0 100644 --- a/drools-ansible-rulebook-integration-api/src/test/java/org/drools/ansible/rulebook/integration/api/SessionStatsTest.java +++ b/drools-ansible-rulebook-integration-api/src/test/java/org/drools/ansible/rulebook/integration/api/SessionStatsTest.java @@ -73,6 +73,15 @@ public void testWithDisabledRule() { RulesExecutor rulesExecutor = RulesExecutorFactory.createFromJson(json); + SessionStats beforeFiringStats = rulesExecutor.getSessionStats(); + assertNull( beforeFiringStats.getLastRuleFiredAt() ); + assertNull( beforeFiringStats.getLastEventReceivedAt() ); + + rulesExecutor.processEvents( "{ \"sensu\": { \"data\": { \"i\":42 } } }" ).join(); + SessionStats statsAfterNonMatchingEvent = rulesExecutor.getSessionStats(); + assertNull( statsAfterNonMatchingEvent.getLastRuleFiredAt() ); + assertNotNull( statsAfterNonMatchingEvent.getLastEventReceivedAt() ); + List matchedRules = rulesExecutor.processFacts( "{ \"sensu\": { \"data\": { \"i\":1 } } }" ).join(); assertEquals( 1, matchedRules.size() ); assertEquals( "R1", matchedRules.get(0).getRule().getName() );