public static IDisposable CheckpointLoop(Server server, CepCheckpointableProcess proc, TimeSpan delay) { Uri queryUri = server.Enumerate(new Uri(proc.Name.ToString() + "/Query")).First(); // Don't start checkpointing until the query is actually running. string queryState; do { // sleep for a second Thread.Sleep(TimeSpan.FromSeconds(1)); DiagnosticView dv = server.GetDiagnosticView(queryUri); queryState = (string)dv[DiagnosticViewProperty.QueryState]; }while ("Running" != queryState); return(Observable.Interval(delay, Scheduler.ThreadPool).Subscribe( i => { var task = proc.CheckpointAsync(); task.Wait(); if (task.IsFaulted) { return; } Console.WriteLine("** Checkpointed! ({0})", i); }, (Exception e) => Console.WriteLine("Error encountered during checkpointing: {0}", e.Message))); }
/// <summary> /// Takes a diagnostic view and outputs all its entries (properties) to the given text writer. /// </summary> /// <param name="dv">Diagnostic view to output</param> /// <param name="traceListener">Tracer to receive the diagnostic data.</param> private static void RetrieveDiagnostics(DiagnosticView dv, System.IO.TextWriter traceListener) { traceListener.WriteLine("Diagnostic View for '" + dv.ObjectName + "':"); foreach (KeyValuePair <string, object> dp in dv) { traceListener.WriteLine(" " + dp.Key + ": " + dp.Value); } }
private static void RetrieveDiagnostics(DiagnosticView diagview, TraceListener traceListener) { // Display diagnostics for diagnostic view object traceListener.WriteLine("Diagnostic View for '" + diagview.ObjectName + "':"); foreach (KeyValuePair <string, object> diagprop in diagview) { traceListener.WriteLine(" " + diagprop.Key + ": " + diagprop.Value); } }
public void RetrieveDiagnostics(DiagnosticView diagview, string fileName) { using (StreamWriter sw = new StreamWriter(fileName, true)) { // sw.WriteLine("Diagnostic View for '" + diagview.ObjectName + "':"); foreach (KeyValuePair <string, object> diagprop in diagview) { sw.WriteLine(" " + diagprop.Key + " - " + diagprop.Value); } } }
public void FileWrite(DiagnosticView diagview, int dimFer) { using (StreamWriter sw = new StreamWriter("logs.txt", append: true)) { // Display diagnostics for diagnostic view object //sw.WriteLine("Diagnostic View for '" + diagview.ObjectName + "':"); DB dB = new DB(); foreach (KeyValuePair <string, object> diagnostics in diagview) { if (diagnostics.Key.Contains("Cpu") || diagnostics.Key.Contains("QueryTotalConsumedEventLatency") || diagnostics.Key.Contains("AllEventsMemory")) { //sw.WriteLine(" " + diagnostics.Key + ": " + diagnostics.Value); dB.WriteToDB(dimFer, diagnostics.Key.ToString(), diagnostics.Value.ToString()); } } //sw.WriteLine("--------------------------------------------"); } }
/// <summary> /// Takes a diagnostic view and dumps all its entries to the given /// text writer. /// </summary> /// <param name="diagview">Diagnostic view to dump.</param> /// <param name="traceListener">Tracer to receive the diagnostic data.</param> private static void RetrieveDiagnostics(DiagnosticView diagview, System.IO.TextWriter traceListener) { // Display diagnostics for diagnostic view object traceListener.WriteLine("Diagnostic View for '" + diagview.ObjectName + "':"); foreach (KeyValuePair<string, object> diagprop in diagview) { traceListener.WriteLine(" " + diagprop.Key + ": " + diagprop.Value); } }
/// <summary> /// Takes a diagnostic view and outputs all its entries (properties) to the given text writer. /// </summary> /// <param name="dv">Diagnostic view to output</param> /// <param name="traceListener">Tracer to receive the diagnostic data.</param> private static void RetrieveDiagnostics(DiagnosticView dv, System.IO.TextWriter traceListener) { traceListener.WriteLine("Diagnostic View for '" + dv.ObjectName + "':"); foreach (KeyValuePair<string, object> dp in dv) { traceListener.WriteLine(" " + dp.Key + ": " + dp.Value); } }
/// <summary> /// Main program. /// </summary> internal static void Main() { Console.WriteLine("Creating CEP Server"); // Creating an embedded server. // // In order to connect to an existing server instead of creating a // new one, Server.Connect() can be used, with the appropriate // endpoint exposed by the remote server. // // NOTE: replace "Default" with the instance name you provided // during StreamInsight setup. using (Server server = Server.Create("Default")) { // Define the runtime configuration for input adapter var stockTicksInputConf = new TextFileReaderConfig { InputFileName = @"..\..\..\StockTicks.csv", Delimiter = ',', CtiFrequency = 9, CultureName = "en-US", InputFieldOrders = new Collection <string>() { "StockSymbol", "StockPrice", "PriceChange" } }; // Define the runtime configuration for the output adapter. // Specify an empty file name, which will just dump the output // events on the console. var outputConf = new TextFileWriterConfig { OutputFileName = string.Empty, Delimiter = '\t' }; try { // Create application in the server. The application will serve // as a container for actual CEP objects and queries. Console.WriteLine("Creating CEP Application"); Application application = server.CreateApplication("PatternDetectorSample"); // Create an input stream directly from the adapter, var stockStream = CepStream <StockTick> .Create("stockTicksInput", typeof(TextFileReaderFactory), stockTicksInputConf, EventShape.Point); // Execute pattern detection over the stream var patternResult = from w in stockStream.TumblingWindow(TimeSpan.FromHours(1), HoppingWindowOutputPolicy.ClipToWindowEnd) select w.AfaOperator <StockTick, RegisterType>(typeof(AfaEqualDownticksUpticks).AssemblyQualifiedName.ToString()); // Alternate example of using a count window for pattern detection: // var patternResult = from w in stockStream.CountByStartTimeWindow(4, CountWindowOutputPolicy.PointAlignToWindowEnd) // select w.AfaOperator<StockTick, RegisterType>(typeof(AfaEqualDownticksUpticks).AssemblyQualifiedName.ToString()); // Turn this logic into a query, outputting to the tracer output adapter. var query = patternResult.ToQuery( application, "EqualDownticksUpticksQuery", "Detecting equal number of downticks and upticks in a stock quote stream", typeof(TextFileWriterFactory), outputConf, EventShape.Point, StreamEventOrder.FullyOrdered); // Start the query Console.WriteLine("Starting 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 stream. 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 some diagnostic information from the CEP server // about the query. Console.WriteLine(string.Empty); RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/EventManager")), Console.Out); RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/PlanManager")), Console.Out); RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/Application/PatternDetectorSample/Query/EqualDownticksUpticksQuery")), Console.Out); query.Stop(); } catch (Exception e) { Console.WriteLine(e); Console.ReadLine(); } } Console.WriteLine("\nPress enter to exit application"); Console.ReadLine(); }
/// <summary> /// Main program. /// </summary> internal static void Main() { Console.WriteLine("Creating CEP Server"); // Creating an embedded server. // // In order to connect to an existing server instead of creating a // new one, Server.Connect() can be used, with the appropriate // endpoint exposed by the remote server. // // NOTE: replace "Default" with the instance name you provided // during StreamInsight setup. using (Server server = Server.Create("Default")) { try { // Create application in the server. The application will serve // as a container for actual CEP objects and queries. Console.WriteLine("Creating CEP Application"); Application application = server.CreateApplication("TrafficJoinSample"); // Create query logic as a query template Console.WriteLine("Registering LINQ query template"); QueryTemplate queryTemplate = CreateQueryTemplate(application); // Register adapter factories, so that the engine will be able // to instantiate adapters when the query starts. Console.WriteLine("Registering Adapter Factories"); InputAdapter csvInputAdapter = application.CreateInputAdapter <TextFileReaderFactory>("CSV Input", "Reading tuples from a CSV file"); OutputAdapter csvOutputAdapter = application.CreateOutputAdapter <TextFileWriterFactory>("CSV Output", "Writing result events to a CSV file"); // bind query to event producers and consumers QueryBinder queryBinder = BindQuery(csvInputAdapter, csvOutputAdapter, queryTemplate); // Create bound query that can be run Console.WriteLine("Registering bound query"); Query query = application.CreateQuery("TrafficSensorQuery", "Minute average count, filtered by location threshold", queryBinder); 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 stream. 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 some diagnostic information from the CEP server // about the query. Console.WriteLine(string.Empty); RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/EventManager")), Console.Out); RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/PlanManager")), Console.Out); RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/Application/TrafficJoinSample/Query/TrafficSensorQuery")), Console.Out); query.Stop(); } catch (Exception e) { Console.WriteLine(e); Console.ReadLine(); } } Console.WriteLine("\npress enter to exit application"); Console.ReadLine(); }
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(); } }