In the previous article, we introduced the window alignment section in the query mode. This blog post will introduce event alignment.
Test data preparation
To facilitate test query, we first prepare a static test data source:
var sourceIntervalDataType = new []{ new { Id = "A", StartTime = DateTime.Parse("10/23/2009 11:59:01 PM"), EndTime = DateTime.Parse("10/23/2009 11:59:59 PM") }, new { Id = "A", StartTime = DateTime.Parse("10/23/2009 4:12:00 PM"), EndTime = DateTime.Parse("10/24/2009 4:12:01 PM") }, new { Id = "A", StartTime = DateTime.Parse("10/31/2009 11:59:59 PM"), EndTime = DateTime.Parse("11/1/2009 12:00:00 AM") }, new { Id = "A", StartTime = DateTime.Parse("11/1/2009 12:00:00 AM"), EndTime = DateTime.Parse("11/1/2009 12:00:00 PM") }, new { Id = "A", StartTime = DateTime.Parse("11/2/2009 12:00:01 AM"), EndTime = DateTime.Parse("11/2/2009 12:00:02 AM") }, new { Id = "A", StartTime = DateTime.Parse("11/3/2009 11:59:59 PM"), EndTime = DateTime.Parse("11/4/2009 12:00:01 AM") },};
Next, we will change sourceintervaldatatype to an interval-type complex event stream:
var source = sourceIntervalDataType.ToIntervalStream(Application, ev => IntervalEvent.CreateInsert(ev.StartTime.ToLocalTime(), ev.EndTime.ToLocalTime(), new { ev.Id }), AdvanceTimeSettings.StrictlyIncreasingStartTime);
Event alignment
Question 1: How to move/align the event to a.m. (that is, the day that begins) and keep the end time unchanged?
The function that changes the life cycle of an event has already been seen before. Good, it's altereventduration! However, it is an extended duration and cannot change the start time of an event. To Solve Problem 1, you need to use another function, altereventlifetime, to change the life cycle.
var alignStream = source.AlterEventLifetime( e => e.StartTime.Date, e => e.EndTime - e.StartTime.Date);
Altereventlifetime, as its name implies, is to change the lifecycle of an event. It can modify the event duration or the event start time. In the above Code, the previous part specifies the start time as a.m. of the current day, and the later part specifies the duration to ensure that the end time after modification is consistent with the previous one. The result of the event stream after the change is as follows:
Question 2: How to move/align the event to a.m. (that is, starting on the current day) and keep the duration unchanged?
Similarly, modify the duration of the second parameter of altereventlifetime. The Code is as follows:
var alignStream = source.AlterEventLifetime( e => e.StartTime.Date, e => e.EndTime - e.StartTime);
The output result is as follows:
The next section describes the basic modes in the streaminsight query mode.