Skip to content

Commit

Permalink
[DROOLS-7602] SessionStats should also report the time of the last re…
Browse files Browse the repository at this point in the history
…ceived event (#99)
  • Loading branch information
mariofusco authored Jan 12, 2024
1 parent 79e9d97 commit f86a26f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ InternalFactHandle insert(Map<String, Object> factMap, boolean event) {
}
InternalFactHandle fh = (InternalFactHandle) kieSession.insert(fact);
if (event) {
sessionStatsCollector.registerProcessedEvent(fh);
sessionStatsCollector.registerProcessedEvent(this, fh);
}
return fh;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -105,6 +108,7 @@ public String toString() {
", ruleSetName='" + ruleSetName + '\'' +
", lastRuleFired='" + lastRuleFired + '\'' +
", lastRuleFiredAt='" + lastRuleFiredAt + '\'' +
", lastEventReceivedAt='" + lastEventReceivedAt + '\'' +
", usedMemory='" + getUsedMemory() + '\'' +
", maxAvailableMemory='" + getMaxAvailableMemory() + '\'' +
'}';
Expand Down Expand Up @@ -178,6 +182,10 @@ public String getLastRuleFiredAt() {
return lastRuleFiredAt;
}

public String getLastEventReceivedAt() {
return lastEventReceivedAt;
}

public int getClockAdvanceCount() {
return clockAdvanceCount;
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -83,6 +84,10 @@ public long getLastRuleFiredTime() {
return lastRuleFiredTime;
}

public long getLastEventReceivedTime() {
return lastEventReceivedTime;
}

public int getClockAdvanceCount() {
return clockAdvanceCount;
}
Expand All @@ -101,7 +106,8 @@ public void registerMatchedEvents(Collection<FactHandle> events) {
matchedEvents += events.size();
}

public void registerProcessedEvent(FactHandle fh) {
public void registerProcessedEvent(RulesExecutorSession session, FactHandle fh) {
lastEventReceivedTime = session.getPseudoClock().getCurrentTime();
totalEvents++;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Match> matchedRules = rulesExecutor.processFacts( "{ \"sensu\": { \"data\": { \"i\":1 } } }" ).join();
assertEquals( 1, matchedRules.size() );
assertEquals( "R1", matchedRules.get(0).getRule().getName() );
Expand Down

0 comments on commit f86a26f

Please sign in to comment.