Ruminations of idle rants and ramblings of a code monkey

How long did that edge event take?–The Sequel

Code Sample | StreamInsight

This is beginning to become a recurring theme around here. It turns out that there is yet another way to get a composite event that will tell you how long a particular event “took”. This goes back to the initial post, where I was using a subject to create a new event that included the original payload but with a Duration property, allowing you to get the total milliseconds for a specific event. You see, subjects have become one of my mostest favoritest features with the whole Reactive model in StreamInsight 2.1; there’s just so much that they let you do that you couldn’t do before. But this particular scenario was possible without subjects and it really should have smacked me in the face. Rather than using a subject, we can use a UDSO (specifically, an edge stream operator) to achieve the exact same thing but with fewer moving parts. Oh … and using a UDSO doesn’t create an independent timeline, whereas using the Subject does create a new timeline in the process. Overall, the UDSO is far simpler, easier to understand and you don’t have to worry about any CTI violations or funkiness thereabouts. With that, here’s the operator:

  1. public class DurationEvent<TPayload>{
  2.     public TPayload Payload;
  3.     public double TotalMilliseconds;
  4. }
  6. [DataContract]
  7. public sealed class EventDurationOperator<TPayload> : CepEdgeStreamOperator<TPayload, DurationEvent<TPayload>>
  8. {
  9.    public override bool IsEmpty
  10.    {
  11.        get { return true; }
  12.    }
  14.    public override DateTimeOffset? NextCti
  15.    {
  16.        get { return null; }
  17.    }
  19.    public override IEnumerable<DurationEvent<TPayload>> ProcessEvent(EdgeEvent<TPayload> inputEvent)
  20.    {
  21.        if(inputEvent.EdgeType == EdgeType.End){
  22.             //Create the new duration event.
  23.             yield return new DurationEvent<TPayload>{
  24.                 Payload = inputEvent.Payload,
  25.                 TotalMilliseconds = (inputEvent.EndTime - inputEvent.StartTime).TotalMilliseconds
  26.             };
  27.         }
  28.    }
  29. }

As you can see, the output here is identical to the output from the previous solution but the code is far, far simpler. Using the operator is simpler than the subject as well.

Using the Operator
  1. var source = items.ToEdgeStreamable(e => e.GetEvent(startTime), AdvanceTimeSettings.IncreasingStartTime);
  2. var results = source.Scan(() => new EventDurationOperator<DataItem>());

That makes the operator, rather than the subject, the winner here … it keeps true to that Golden Rule of Programming – the KISS principle. (That’s Keep It Simple Stupid, in case you were wondering.)