//Process for Test Data private static void RunTestDataProcess(Application cepApplication) { var config = new TestDataInputConfig() { NumberOfItems = 20, RefreshInterval = TimeSpan.FromMilliseconds(500), TimestampIncrement = TimeSpan.FromMilliseconds(500), AlwaysUseNow = true, EnqueueCtis = false }; AdvanceTimeSettings ats = new AdvanceTimeSettings(new AdvanceTimeGenerationSettings( TimeSpan.FromMilliseconds(750), TimeSpan.FromMilliseconds(200)), null, AdvanceTimePolicy.Drop); var data = RxStream <TestDataEvent> .Create(cepApplication, typeof(TestDataInputFactory), config, EventShape.Point, ats); var sinkConfig = new ConsoleOutputConfig() { ShowCti = true, CtiEventColor = ConsoleColor.Blue, InsertEventColor = ConsoleColor.Green }; var binding = data.ToBinding(cepApplication, typeof(ConsoleOutputFactory), sinkConfig, EventShape.Point); binding.Run("Hello"); }
private static void RunQuery(Application cepApplication) { var config = new YahooDataInputConfig() { Symbols = new string[] { "AAPL", "DELL", "MSFT", "GOOG", "GE" }, RefreshInterval = TimeSpan.FromSeconds(0.5), TimestampIncrement = TimeSpan.FromSeconds(0.5), AlwaysUseNow = true, EnqueueCtis = false }; AdvanceTimeSettings ats = new AdvanceTimeSettings(new AdvanceTimeGenerationSettings( TimeSpan.FromMilliseconds(1000), TimeSpan.FromMilliseconds(200)), null, AdvanceTimePolicy.Drop); var data = CepStream <YahooDataEvent> .Create(cepApplication, "TestData", typeof(YahooDataInputFactory), config, EventShape.Point, ats); var query = data.ToQuery(cepApplication, "Test", "Test", typeof(ConsoleOutputFactory), new ConsoleOutputConfig() { ShowCti = true, CtiEventColor = ConsoleColor.Yellow, InsertEventColor = ConsoleColor.Magenta }, EventShape.Point, StreamEventOrder.FullyOrdered); query.Start(); }
public static CepStream<CsvReading2> ReadFile(Application myApp) { // =============== Read data from CSV file ======================== var sensorInputConf = new TextFileReaderConfig { InputFileName = @"..\..\..\dataset.csv",// Location of the file where the data exist Delimiter = ',', CtiFrequency = 99, // CTI frequency depending on the Sampling Rate. CultureName = "el-GR", InputFieldOrders = new Collection<string>() { "SensorID", "SampleRate", "Value", "Type", "initTime", "count" } }; // Parameters for creating the Input Stream from the file. var atgs = new AdvanceTimeGenerationSettings(99, TimeSpan.FromSeconds(0), true); var ats = new AdvanceTimeSettings(atgs, null, AdvanceTimePolicy.Adjust); // I need an application ID to attach the input stream var myStream = CepStream<CsvReading>.Create( myApp, "input stream", typeof(TextFileReaderFactory), sensorInputConf, EventShape.Point, ats); // Get the channel. CAUTION: SensorID??? var valueStream1 = from e in myStream where e.SensorID=="EEG1" select e; var valueStream2 = from e in myStream where e.SensorID == "EEG2" select e; // =============== End of reading data from CSV file ======================== var stream = from e1 in valueStream1 from e2 in valueStream2 select new CsvReading2 { SensorID = e1.SensorID, Value1 = e1.Value, Value2 =e2.Value, initTime = e1.initTime }; return stream; // Or this one //var alphaStream = from e in valueStream.HoppingWindow<double>(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1)) // select e.alphaAgg<double>(a => a); //var detectionStream = from e in alphaStream // where e > AlphaDetection.alphaThreshold // select e; // ===================== END of Alpha Rhythm Detection ===================== }
private static void BindAndRunQuery <TPayload>(string queryName, CepStream <TPayload> queryStream, EventShape outputEventShape, List <string> inputFields, List <string> outputFields) { var inputConfig = new CsvInputConfig { InputFileName = @"..\..\..\TollInput.txt", Delimiter = new char[] { ',' }, BufferSize = 4096, CtiFrequency = 1, CultureName = "en-US", Fields = inputFields, NonPayloadFieldCount = 2, StartTimePos = 1, EndTimePos = 2 }; // The adapter recognizes empty filename as a write to console. var outputConfig = new CsvOutputConfig { OutputFileName = string.Empty, Delimiter = new string[] { "\t" }, CultureName = "en-US", Fields = outputFields }; // Note - Please change the instance name to the one you have chosen during installation using (var server = Server.Create("Default")) { Application application = server.CreateApplication("TollStationApp"); // set up input and output adapters InputAdapter inputAdapter = application.CreateInputAdapter <CsvInputFactory>("input", "Csv Input Source"); OutputAdapter outputAdapter = application.CreateOutputAdapter <CsvOutputFactory>("output", "Csv Output"); // set up the query template QueryTemplate queryTemplate = application.CreateQueryTemplate("QueryTemplate", string.Empty, queryStream); // set up advance time settings to enqueue CTIs var advanceTimeGenerationSettings = new AdvanceTimeGenerationSettings(inputConfig.CtiFrequency, TimeSpan.Zero, true); var advanceTimeSettings = new AdvanceTimeSettings(advanceTimeGenerationSettings, null, AdvanceTimePolicy.Adjust); // Bind query template to input and output streams QueryBinder queryBinder = new QueryBinder(queryTemplate); queryBinder.BindProducer <TollReading>("TollStream", inputAdapter, inputConfig, EventShape.Interval, advanceTimeSettings); queryBinder.AddConsumer("outputStream", outputAdapter, outputConfig, outputEventShape, StreamEventOrder.FullyOrdered); // Create a runnable query by binding the query template to the input stream of interval events, // and to an output stream of fully ordered point events (through an output adapter instantiated // from the output factory class) Query query = application.CreateQuery(queryName, "Hello Toll Query", queryBinder); RunQuery(query); } }
/// <summary> /// Produces output whenever the processor utilization exceeds a threshold consistently over some duration. Both the threshold /// and duration are configurable in real-time via the UI. /// </summary> /// <param name="application">Application hosting the query.</param> /// <param name="configurationSource">Real-time configuration data from UI.</param> /// <returns>IObservable event sink for the StreamInsight query.</returns> public static dynamic ProcessorUtilizationSustainedThreshold( Application application, IObservable <ThresholdConfiguration> configurationSource) { const string dataStreamName = "dataStream"; var dataStream = InputData.CreateProcessorTimeInput(application, streamName: dataStreamName); // first, smooth the processor time to avoid false negatives (when we dip below threshold) var smoothed = from win in dataStream.HoppingWindow( TimeSpan.FromSeconds(2.0), TimeSpan.FromSeconds(0.25), HoppingWindowOutputPolicy.ClipToWindowEnd) select new { Value = win.Avg(s => s.Value) }; // drive advance time settings from the data stream to the configuration stream var ats = new AdvanceTimeSettings( null, new AdvanceTimeImportSettings(dataStreamName), AdvanceTimePolicy.Adjust); var configurationStream = configurationSource.ToPointStream( application, c => PointEvent.CreateInsert(DateTime.UtcNow, c), ats, "configurationStream"); // treat the configuration stream as a signal: the start of each event is the end // of the previous event configurationStream = configurationStream .AlterEventDuration(e => TimeSpan.MaxValue) .ClipEventDuration(configurationStream, (s, e) => true); // join data and configuration streams var joined = from d in dataStream from c in configurationStream select new { d.Value, c.Duration, c.Threshold }; // use alter lifetime to apply the requested duration joined = joined.AlterEventDuration(e => e.Payload.Duration); // detect when the threshold is sustained for the requested duration var query = from win in joined.SnapshotWindow(SnapshotWindowOutputPolicy.Clip) select new { Min = win.Min(s => s.Value), Threshold = win.Avg(s => s.Threshold) } into a where a.Min > a.Threshold select a; return(from p in query.ToPointObservable() where p.EventKind == EventKind.Insert select new { p.StartTime, p.Payload.Min }); }
/// <summary> /// Produces output whenever the processor utilization exceeds a threshold consistently over some duration. Both the threshold /// and duration are configurable in real-time via the UI. /// </summary> /// <param name="application">Application hosting the query.</param> /// <param name="configurationSource">Real-time configuration data from UI.</param> /// <returns>IObservable event sink for the StreamInsight query.</returns> public static dynamic ProcessorUtilizationSustainedThreshold( Application application, IObservable<ThresholdConfiguration> configurationSource) { const string dataStreamName = "dataStream"; var dataStream = InputData.CreateProcessorTimeInput(application, streamName: dataStreamName); // first, smooth the processor time to avoid false negatives (when we dip below threshold) var smoothed = from win in dataStream.HoppingWindow( TimeSpan.FromSeconds(2.0), TimeSpan.FromSeconds(0.25), HoppingWindowOutputPolicy.ClipToWindowEnd) select new { Value = win.Avg(s => s.Value) }; // drive advance time settings from the data stream to the configuration stream var ats = new AdvanceTimeSettings( null, new AdvanceTimeImportSettings(dataStreamName), AdvanceTimePolicy.Adjust); var configurationStream = configurationSource.ToPointStream( application, c => PointEvent.CreateInsert(DateTime.UtcNow, c), ats, "configurationStream"); // treat the configuration stream as a signal: the start of each event is the end // of the previous event configurationStream = configurationStream .AlterEventDuration(e => TimeSpan.MaxValue) .ClipEventDuration(configurationStream, (s, e) => true); // join data and configuration streams var joined = from d in dataStream from c in configurationStream select new { d.Value, c.Duration, c.Threshold }; // use alter lifetime to apply the requested duration joined = joined.AlterEventDuration(e => e.Payload.Duration); // detect when the threshold is sustained for the requested duration var query = from win in joined.SnapshotWindow(SnapshotWindowOutputPolicy.Clip) select new { Min = win.Min(s => s.Value), Threshold = win.Avg(s => s.Threshold) } into a where a.Min > a.Threshold select a; return from p in query.ToPointObservable() where p.EventKind == EventKind.Insert select new { p.StartTime, p.Payload.Min }; }
public static CepStream <PerformanceCounterSample> CreateInput( Application application, string categoryName, string counterName, string instanceName, TimeSpan pollingInterval, string streamName = null) { IObservable <PerformanceCounterSample> source = new PerformanceCounterObservable(categoryName, counterName, instanceName, pollingInterval); AdvanceTimeSettings advanceTimeSettings = AdvanceTimeSettings.IncreasingStartTime; return(source.ToPointStream(application, s => PointEvent.CreateInsert(s.StartTime, s), advanceTimeSettings, streamName)); }
static void Main(string[] args) { var delay = TimeSpan.FromSeconds(.002); using (var server = Server.Create("StreamInsightTest")) { var applicaiton = server.CreateApplication("StreamInsightDemo"); var input = applicaiton.DefineObservable((DateTimeOffset? _) => CsvReader.Read("sample.csv").RateLimit(delay, Scheduler.ThreadPool)); var inputStream = input.ToPointStreamable(x => x, AdvanceTimeSettings.UnorderedStartTime(TimeSpan.FromSeconds(15))); var query = from x in inputStream.TumblingWindow(TimeSpan.FromSeconds(10)) select new Payload { X = x.Avg(p => p.X), Y = x.Avg(p => p.Y) }; var output = applicaiton.DefineObserver((DateTimeOffset? hwm, int offset) => CsvWriter.Write("output.csv", true)); query.Bind(output).Run(); Console.ReadLine(); } }
//Process for Yahoo Data private static void RunYahooDataProcess(Application cepApplication) { var config = new YahooDataInputConfig() { Symbols = new string[] { "AAPL", "DELL", "MSFT", "GOOG", "GE" }, RefreshInterval = TimeSpan.FromSeconds(5), TimestampIncrement = TimeSpan.FromSeconds(5), AlwaysUseNow = true, EnqueueCtis = false }; AdvanceTimeSettings ats = new AdvanceTimeSettings(new AdvanceTimeGenerationSettings( TimeSpan.FromMilliseconds(750), TimeSpan.FromMilliseconds(200)), null, AdvanceTimePolicy.Drop); var data = RxStream <YahooDataEvent> .Create(cepApplication, typeof(YahooDataInputFactory), config, EventShape.Point, ats); var sinkConfig = new ConsoleOutputConfig() { ShowCti = true, ShowPayloadToString = false, CtiEventColor = ConsoleColor.Blue, InsertEventColor = ConsoleColor.Green }; var myQuery = from x in data.ToPointEventStream() select new { x.Symbol, x.LastTradePrice, x.LastUpdateTime }; var binding = myQuery.ToBinding(cepApplication, typeof(ConsoleOutputFactory), sinkConfig, EventShape.Point); binding.Run("Hello"); }
public static IQStreamable <T> GetStreamable(Application app, int cti, int interval, int variance) { var ats = new AdvanceTimeSettings(new AdvanceTimeGenerationSettings((uint)cti, TimeSpan.FromTicks(-1)), null, AdvanceTimePolicy.Drop); return(app.DefineObservable(() => new EdgeGenerator <T>(interval, variance)).ToEdgeStreamable(e => e, ats)); }
public static IQStreamable <TPayload> Create(Application cepApplication, Type sourceFactoryType, object configInfo, EventShape eventShape, AdvanceTimeSettings advanceTimeSettings) { var observable = CreateObservable(cepApplication, sourceFactoryType, configInfo, eventShape); switch (eventShape) { case EventShape.Point: return(observable.ToPointStreamable(e => e.GetPointEvent(), advanceTimeSettings)); case EventShape.Interval: return(observable.ToIntervalStreamable(e => e.GetIntervalEvent(), advanceTimeSettings)); case EventShape.Edge: return(observable.ToEdgeStreamable(e => e.GetEdgeEvent(), advanceTimeSettings)); default: throw new ArgumentOutOfRangeException("eventShape"); } }
internal static void Main() { using (var server = Server.Create("Default")) { // If you want to publish this server, or connect to an already existing server, // please see the product documentation "Publishing and Connecting to a StreamInsight Server" // to find out how to replace, or add to the above server creation code. You must publish // the server for the Event Flow debugger to be able to connect to the server. var application = server.CreateApplication("TutorialApp"); // Define device configuration var inputConfig = new CsvInputConfig { InputFileName = @"..\..\..\TollInput.txt", Delimiter = new char[] { ',' }, BufferSize = 4096, CtiFrequency = 1, CultureName = "en-US", Fields = new List<string>() { "TollId", "LicensePlate", "State", "Make", "Model", "VehicleType", "VehicleWeight", "Toll", "Tag" }, NonPayloadFieldCount = 2, StartTimePos = 1, EndTimePos = 2 }; var outputConfigForPassThroughQuery = new CsvOutputConfig { // The adapter recognizes empty filename as a write to console OutputFileName = string.Empty, Delimiter = new string[] { "\t" }, CultureName = "en-US", Fields = new List<string>() { "TollId", "LicensePlate", "State", "Make", "Model", "VehicleType", "VehicleWeight", "Toll", "Tag" } }; var outputConfigForCountQuery = new CsvOutputConfig { OutputFileName = string.Empty, Delimiter = new string[] { "\t" }, CultureName = "en-US", Fields = new List<string>() { "Count" } }; // Define console trace listener TraceListener tracer = new ConsoleTraceListener(); // set up advance time settings to enqueue CTIs - to deliver output in a timely fashion var advanceTimeGenerationSettings = new AdvanceTimeGenerationSettings(inputConfig.CtiFrequency, TimeSpan.Zero, true); var advanceTimeSettings = new AdvanceTimeSettings(advanceTimeGenerationSettings, null, AdvanceTimePolicy.Adjust); // Define input stream object, mapped to stream names // Instantiate an adapter from the input factory class var inputStream = CepStream<TollReading>.Create( "TollStream", typeof(CsvInputFactory), inputConfig, EventShape.Interval, advanceTimeSettings); var passthroughQuery = from e in inputStream select e; var countQuery = from w in inputStream.TumblingWindow(TimeSpan.FromMinutes(3), HoppingWindowOutputPolicy.ClipToWindowEnd) select new TollCount { Count = w.Count() }; // Create a runnable query by binding the query template to the // input stream of interval events, and to an output stream of // fully ordered point events (through an output adapter instantiated // from the output factory class) Query query = countQuery.ToQuery( application, "HelloTollTutorial", "Hello Toll Query", typeof(CsvOutputFactory), outputConfigForCountQuery, EventShape.Interval, StreamEventOrder.FullyOrdered); query.Start(); Console.WriteLine("*** Hit Return to see Query Diagnostics after this run ***"); Console.WriteLine(); Console.ReadLine(); // Retrieve diagnostic information from the CEP server about the query. // See the section "Connecting to and Publishing a Server" in the StreamInsight MSDN documentation on // how to make the debugger client connect to the server and retrieve these diagnostics Console.WriteLine(string.Empty); RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/EventManager")), tracer); RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/PlanManager")), tracer); RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/Application/TutorialApp/Query/HelloTollTutorial")), tracer); DiagnosticSettings settings = new DiagnosticSettings(DiagnosticAspect.GenerateErrorReports, DiagnosticLevel.Always); server.SetDiagnosticSettings(new Uri("cep:/Server"), settings); tracer.WriteLine("Global Server Diagnostics"); RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/EventManager")), tracer); RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/PlanManager")), tracer); RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/Query")), tracer); tracer.WriteLine(string.Empty); tracer.WriteLine("Summary Query Diagnostics for"); RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/Application/TutorialApp/Query/HelloTollTutorial")), tracer); tracer.WriteLine(string.Empty); query.Stop(); Console.WriteLine("*** Query Completed *** Hit Return to Exit"); Console.WriteLine(); Console.ReadLine(); } return; }
private static void RunQuery(Application cepApplication) { var config = new YahooDataInputConfig() { Symbols = new string[] { "AAPL", "DELL", "MSFT", "GOOG", "GE" }, RefreshInterval = TimeSpan.FromSeconds(0.5), TimestampIncrement = TimeSpan.FromSeconds(0.5), AlwaysUseNow = true, EnqueueCtis = false }; AdvanceTimeSettings ats = new AdvanceTimeSettings(new AdvanceTimeGenerationSettings( TimeSpan.FromMilliseconds(1000), TimeSpan.FromMilliseconds(200)), null, AdvanceTimePolicy.Drop); var data = CepStream<YahooDataEvent>.Create(cepApplication, "TestData", typeof(YahooDataInputFactory), config, EventShape.Point, ats); var query = data.ToQuery(cepApplication, "Test", "Test", typeof(ConsoleOutputFactory), new ConsoleOutputConfig() { ShowCti = true, CtiEventColor = ConsoleColor.Yellow, InsertEventColor = ConsoleColor.Magenta }, EventShape.Point, StreamEventOrder.FullyOrdered); query.Start(); }
//Process for Test Data private static void RunTestDataProcess(Application cepApplication) { var config = new TestDataInputConfig() { NumberOfItems = 20, RefreshInterval = TimeSpan.FromMilliseconds(500), TimestampIncrement = TimeSpan.FromMilliseconds(500), AlwaysUseNow = true, EnqueueCtis = false }; AdvanceTimeSettings ats = new AdvanceTimeSettings(new AdvanceTimeGenerationSettings( TimeSpan.FromMilliseconds(750), TimeSpan.FromMilliseconds(200)), null, AdvanceTimePolicy.Drop); var data = RxStream<TestDataEvent>.Create(cepApplication, typeof(TestDataInputFactory), config, EventShape.Point, ats); var sinkConfig = new ConsoleOutputConfig() { ShowCti = true, CtiEventColor = ConsoleColor.Blue, InsertEventColor = ConsoleColor.Green }; var binding = data.ToBinding(cepApplication, typeof(ConsoleOutputFactory), sinkConfig, EventShape.Point); binding.Run("Hello"); }
public static void Go() { var delay = TimeSpan.FromSeconds(.002); var metaConfig = new SqlCeMetadataProviderConfiguration { DataSource = "metadata.db", CreateDataSourceIfMissing = true }; // Set up checkpointing. This needs a location to place the log files. var chkConfig = new CheckpointConfiguration { LogPath = "log", CreateLogPathIfMissing = true }; using (var server = Server.Create("Default", metaConfig, chkConfig)) { string appName = "CheckpointingDemo"; string procName = "myProc"; Application app; if (!server.Applications.TryGetValue(appName, out app)) { app = server.CreateApplication(appName); } CepProcess proc; if (app.Processes.TryGetValue(procName, out proc)) { Console.WriteLine("Resuming process..."); proc.Resume(); } else { Console.WriteLine("Creating process..."); // Without replay: //var csvIn = app.DefineEnumerable(() => XYCsvReader.Read("sample.csv").RateLimit(delay)); // With replay: var csvIn = app.DefineObservable((DateTimeOffset? hwm) => XYCsvReader.Read("sample.csv", hwm).RateLimit(delay, Scheduler.ThreadPool)); var csvStream = csvIn.ToPointStreamable(x => x, AdvanceTimeSettings.UnorderedStartTime(TimeSpan.FromSeconds(15))); var q = from x in csvStream.TumblingWindow(TimeSpan.FromMinutes(1)) select new XYPayload { X = x.Avg(p => p.X), Y = x.Avg(p => p.Y) }; // Without de-duplication: //var csvOut = app.DefineObserver(() => XYCsvWriter.Write("results.csv", true)); // With de-duplication: var csvOut = app.DefineObserver((DateTimeOffset? hwm, int offset) => XYCsvWriter.Write("output.csv", true, hwm, offset)); proc = q.Bind(csvOut).RunCheckpointable(procName); } using (CheckpointLoop(server, app.CheckpointableProcesses[procName], TimeSpan.FromSeconds(1))) { Console.WriteLine("Started checkpointing... Press enter to shut down normally..."); Console.ReadLine(); } Console.WriteLine("Deleting process and exiting."); app.Processes[procName].Delete(); } }
//Process for Yahoo Data private static void RunYahooDataProcess(Application cepApplication) { var config = new YahooDataInputConfig() { Symbols = new string[] { "AAPL", "DELL", "MSFT", "GOOG", "GE" }, RefreshInterval = TimeSpan.FromSeconds(5), TimestampIncrement = TimeSpan.FromSeconds(5), AlwaysUseNow = true, EnqueueCtis = false }; AdvanceTimeSettings ats = new AdvanceTimeSettings(new AdvanceTimeGenerationSettings( TimeSpan.FromMilliseconds(750), TimeSpan.FromMilliseconds(200)), null, AdvanceTimePolicy.Drop); var data = RxStream<YahooDataEvent>.Create(cepApplication, typeof(YahooDataInputFactory), config, EventShape.Point, ats); var sinkConfig = new ConsoleOutputConfig() { ShowCti = true, ShowPayloadToString = false, CtiEventColor = ConsoleColor.Blue, InsertEventColor = ConsoleColor.Green }; var myQuery = from x in data.ToPointEventStream() select new { x.Symbol, x.LastTradePrice, x.LastUpdateTime }; var binding = myQuery.ToBinding(cepApplication, typeof(ConsoleOutputFactory), sinkConfig, EventShape.Point); binding.Run("Hello"); }
internal static void Main() { using (Server server = Server.Create("Default")) { Application application = server.CreateApplication("SqlApplication"); SqlInputConfig inputConfig = new SqlInputConfig { ConnectionString = @"integrated security = true; database = AdventureWorks", Statement = // see here for schema: http://msdn.microsoft.com/en-us/library/ms124879.aspx @"SELECT [SalesOrderID] ,[OrderDate] ,[ShipDate] ,[TerritoryID] FROM [AdventureWorks].[Sales].[SalesOrderHeader] WHERE [OrderDate] IS NOT NULL AND [ShipDate] IS NOT NULL AND [TerritoryID] IS NOT NULL ORDER BY [OrderDate]", StartTimeColumnName = "OrderDate", EndTimeColumnName = "ShipDate", CultureName = "en-US" }; AdvanceTimeSettings inputAdvaceTimeSettings = new AdvanceTimeSettings( new AdvanceTimeGenerationSettings((uint)1, TimeSpan.FromSeconds(0), true), default(AdvanceTimeImportSettings), default(AdvanceTimePolicy)); // define a source stream of interval events from SQL input data, // the interval defined as the duration between the OrderDate and ShipDate var streamSource = CepStream<SqlInput>.Create( "SqlInputStream", typeof(SqlInputFactory), inputConfig, EventShape.Interval, inputAdvaceTimeSettings); // find time intervals during which more than 3 orders are processed within a territory // The result of this query is ""Between time T1 and T2, X many orders were processed in Y territory" var kpiQuery = from o in streamSource group o by o.TerritoryID into g from window in g.SnapshotWindow(SnapshotWindowOutputPolicy.Clip) select new { OrderCount = window.Count(), TerritoryID = g.Key } into agg where agg.OrderCount > 3 select agg; // define a sink stream of interval events reporting territories and order count. SqlOutputConfig outputConfig = new SqlOutputConfig { ConnectionString = @"integrated security = true; database = AdventureWorks", Statement = @"INSERT INTO [AdventureWorks].[Dbo].[PeakSalesByTerritory] ( OrderDate , ShipDate , OrderCount , TerritoryID) VALUES (@OrderDate, @ShipDate, @OrderCount, @TerritoryID)", StartTimeColumnName = "OrderDate", EndTimeColumnName = "ShipDate", TableName = "PeakSalesByTerritory" }; // Create the table if not present already CreateExampleTable(outputConfig.ConnectionString, outputConfig.TableName); // set this to false to output to the table bool outputToFile = false; // Write output to a file (console) or to the output table itself var query = outputToFile ? kpiQuery.ToQuery( application, "KPIQuery", @"Time intervals with order count > 3 and territories", typeof(TextFileWriterFactory), new TextFileWriterConfig { OutputFileName = string.Empty, Delimiter = '\t' }, EventShape.Interval, StreamEventOrder.FullyOrdered) : kpiQuery.ToQuery( application, "KPIQuery", @"Time intervals with order count > 3 and territories", typeof(SqlOutputFactory), outputConfig, EventShape.Interval, // EventShape.Point, StreamEventOrder.FullyOrdered); Console.WriteLine("Start query"); // Start the query query.Start(); // Wait for the query to be suspended - that is the state // it will be in as soon as the output adapter stops due to // the end of the input stream. Then retrieve diagnostics. DiagnosticView dv = server.GetDiagnosticView(query.Name); while ((string)dv[DiagnosticViewProperty.QueryState] == "Running") { // Sleep for 1s and check again Thread.Sleep(1000); dv = server.GetDiagnosticView(query.Name); } // Retrieve diagnostic information from the CEP server about the query. Console.WriteLine(string.Empty); RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/Application/SqlApplication/Query/KPIQuery")), Console.Out); query.Stop(); Console.WriteLine("\nPress Enter to quit the application"); Console.ReadLine(); } }
internal static void Main() { using (var server = Server.Create("Default")) { // If you want to publish this server, or connect to an already existing server, // please see the product documentation "Publishing and Connecting to a StreamInsight Server" // to find out how to replace, or add to the above server creation code. You must publish // the server for the Event Flow debugger to be able to connect to the server. var application = server.CreateApplication("TutorialApp"); // Define device configuration var inputConfig = new CsvInputConfig { InputFileName = @"..\..\..\TollInput.txt", Delimiter = new char[] { ',' }, BufferSize = 4096, CtiFrequency = 1, CultureName = "en-US", Fields = new List <string>() { "TollId", "LicensePlate", "State", "Make", "Model", "VehicleType", "VehicleWeight", "Toll", "Tag" }, NonPayloadFieldCount = 2, StartTimePos = 1, EndTimePos = 2 }; var outputConfigForPassThroughQuery = new CsvOutputConfig { // The adapter recognizes empty filename as a write to console OutputFileName = string.Empty, Delimiter = new string[] { "\t" }, CultureName = "en-US", Fields = new List <string>() { "TollId", "LicensePlate", "State", "Make", "Model", "VehicleType", "VehicleWeight", "Toll", "Tag" } }; var outputConfigForCountQuery = new CsvOutputConfig { OutputFileName = string.Empty, Delimiter = new string[] { "\t" }, CultureName = "en-US", Fields = new List <string>() { "Count" } }; // Define console trace listener TraceListener tracer = new ConsoleTraceListener(); // set up advance time settings to enqueue CTIs - to deliver output in a timely fashion var advanceTimeGenerationSettings = new AdvanceTimeGenerationSettings(inputConfig.CtiFrequency, TimeSpan.Zero, true); var advanceTimeSettings = new AdvanceTimeSettings(advanceTimeGenerationSettings, null, AdvanceTimePolicy.Adjust); // Define input stream object, mapped to stream names // Instantiate an adapter from the input factory class var inputStream = CepStream <TollReading> .Create( "TollStream", typeof(CsvInputFactory), inputConfig, EventShape.Interval, advanceTimeSettings); var passthroughQuery = from e in inputStream select e; var countQuery = from w in inputStream.TumblingWindow(TimeSpan.FromMinutes(3), HoppingWindowOutputPolicy.ClipToWindowEnd) select new TollCount { Count = w.Count() }; // Create a runnable query by binding the query template to the // input stream of interval events, and to an output stream of // fully ordered point events (through an output adapter instantiated // from the output factory class) Query query = countQuery.ToQuery( application, "HelloTollTutorial", "Hello Toll Query", typeof(CsvOutputFactory), outputConfigForCountQuery, EventShape.Interval, StreamEventOrder.FullyOrdered); query.Start(); Console.WriteLine("*** Hit Return to see Query Diagnostics after this run ***"); Console.WriteLine(); Console.ReadLine(); // Retrieve diagnostic information from the CEP server about the query. // See the section "Connecting to and Publishing a Server" in the StreamInsight MSDN documentation on // how to make the debugger client connect to the server and retrieve these diagnostics Console.WriteLine(string.Empty); RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/EventManager")), tracer); RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/PlanManager")), tracer); RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/Application/TutorialApp/Query/HelloTollTutorial")), tracer); DiagnosticSettings settings = new DiagnosticSettings(DiagnosticAspect.GenerateErrorReports, DiagnosticLevel.Always); server.SetDiagnosticSettings(new Uri("cep:/Server"), settings); tracer.WriteLine("Global Server Diagnostics"); RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/EventManager")), tracer); RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/PlanManager")), tracer); RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/Query")), tracer); tracer.WriteLine(string.Empty); tracer.WriteLine("Summary Query Diagnostics for"); RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/Application/TutorialApp/Query/HelloTollTutorial")), tracer); tracer.WriteLine(string.Empty); query.Stop(); Console.WriteLine("*** Query Completed *** Hit Return to Exit"); Console.WriteLine(); Console.ReadLine(); } return; }
internal static void Main() { using (Server server = Server.Create("Default")) { Application application = server.CreateApplication("SqlApplication"); SqlInputConfig inputConfig = new SqlInputConfig { ConnectionString = @"integrated security = true; database = AdventureWorks", Statement = // see here for schema: http://msdn.microsoft.com/en-us/library/ms124879.aspx @"SELECT [SalesOrderID] ,[OrderDate] ,[ShipDate] ,[TerritoryID] FROM [AdventureWorks].[Sales].[SalesOrderHeader] WHERE [OrderDate] IS NOT NULL AND [ShipDate] IS NOT NULL AND [TerritoryID] IS NOT NULL ORDER BY [OrderDate]", StartTimeColumnName = "OrderDate", EndTimeColumnName = "ShipDate", CultureName = "en-US" }; AdvanceTimeSettings inputAdvaceTimeSettings = new AdvanceTimeSettings( new AdvanceTimeGenerationSettings((uint)1, TimeSpan.FromSeconds(0), true), default(AdvanceTimeImportSettings), default(AdvanceTimePolicy)); // define a source stream of interval events from SQL input data, // the interval defined as the duration between the OrderDate and ShipDate var streamSource = CepStream <SqlInput> .Create( "SqlInputStream", typeof(SqlInputFactory), inputConfig, EventShape.Interval, inputAdvaceTimeSettings); // find time intervals during which more than 3 orders are processed within a territory // The result of this query is ""Between time T1 and T2, X many orders were processed in Y territory" var kpiQuery = from o in streamSource group o by o.TerritoryID into g from window in g.SnapshotWindow(SnapshotWindowOutputPolicy.Clip) select new { OrderCount = window.Count(), TerritoryID = g.Key } into agg where agg.OrderCount > 3 select agg; // define a sink stream of interval events reporting territories and order count. SqlOutputConfig outputConfig = new SqlOutputConfig { ConnectionString = @"integrated security = true; database = AdventureWorks", Statement = @"INSERT INTO [AdventureWorks].[Dbo].[PeakSalesByTerritory] ( OrderDate , ShipDate , OrderCount , TerritoryID) VALUES (@OrderDate, @ShipDate, @OrderCount, @TerritoryID)", StartTimeColumnName = "OrderDate", EndTimeColumnName = "ShipDate", TableName = "PeakSalesByTerritory" }; // Create the table if not present already CreateExampleTable(outputConfig.ConnectionString, outputConfig.TableName); // set this to false to output to the table bool outputToFile = false; // Write output to a file (console) or to the output table itself var query = outputToFile ? kpiQuery.ToQuery( application, "KPIQuery", @"Time intervals with order count > 3 and territories", typeof(TextFileWriterFactory), new TextFileWriterConfig { OutputFileName = string.Empty, Delimiter = '\t' }, EventShape.Interval, StreamEventOrder.FullyOrdered) : kpiQuery.ToQuery( application, "KPIQuery", @"Time intervals with order count > 3 and territories", typeof(SqlOutputFactory), outputConfig, EventShape.Interval, // EventShape.Point, StreamEventOrder.FullyOrdered); Console.WriteLine("Start query"); // Start the query query.Start(); // Wait for the query to be suspended - that is the state // it will be in as soon as the output adapter stops due to // the end of the input stream. Then retrieve diagnostics. DiagnosticView dv = server.GetDiagnosticView(query.Name); while ((string)dv[DiagnosticViewProperty.QueryState] == "Running") { // Sleep for 1s and check again Thread.Sleep(1000); dv = server.GetDiagnosticView(query.Name); } // Retrieve diagnostic information from the CEP server about the query. Console.WriteLine(string.Empty); RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/Application/SqlApplication/Query/KPIQuery")), Console.Out); query.Stop(); Console.WriteLine("\nPress Enter to quit the application"); Console.ReadLine(); } }