예제 #1
0
        public void begin(Application myApp, int capacity, string processName)
        {
            string conn_string = this.CONNECTION_STRING;
            string sql_query   = this.SQL_QUERY;

            System.Reactive.Linq.IQbservable <T> qSource2 = myApp.DefineObservable <T>(() => new ObservablePoller <T>(conn_string, sql_query, 5000));
            var streamable = qSource2.ToPointStreamable(x => PointEvent.CreateInsert(DateTime.Now, x), AdvanceTimeSettings.IncreasingStartTime);

            requestHolder             = new RequestManager.RequestHolder(this.TABLENAME, capacity);
            requestHolder.rowBody     = new RequestManager.RowBody();
            requestHolder.rowBody.Row = new List <RequestManager.RowElement>();

            // DEFINE a simple observer SINK (writes the value to the server console)
            string hbase_row_key = this.HBASE_ROW_KEY.Clone().ToString();
            var    mySink2       = myApp.DefineObserver(() => Observer.Create <T>(x => Sink <T>(x, hbase_row_key)));


            // Compose a QUERY over the source (able to filter out results here if desired)

            var myQuery2 = from e in streamable
                           select e;

            // BIND the query to the sink and RUN it
            using (IDisposable proc = myQuery2.Bind <T>(mySink2).Run(processName))
            {
                Console.WriteLine("----------------------------------------------------------------");
                Console.WriteLine("MyStreamInsightServer is running, press Enter to stop the server");
                Console.WriteLine("----------------------------------------------------------------");
                Console.WriteLine(" ");
                Console.ReadLine();
            }
            Console.ReadLine();
        }
예제 #2
0
        /// <summary>
        /// Example of cross join and projections
        /// </summary>
        /// <param name="ericSEKStream"></param>
        /// <param name="ericUSDStream"></param>
        /// <param name="USDSEKStream"></param>
        private static void crossJoinProjectionExample(IEnumerable <StockQuote> ericSEKObservable, IEnumerable <StockQuote> ericUSDObservable, IEnumerable <StockQuote> USDSEKObservable, Application application)
        {
            var ericUSDStream = ericUSDObservable.ToPointStream(application, e => PointEvent <StockQuote> .CreateInsert(new DateTimeOffset(e.TimeStamp, TimeSpan.Zero), e), AdvanceTimeSettings.IncreasingStartTime);
            var ericSEKStream = ericSEKObservable.ToPointStream(application, e => PointEvent <StockQuote> .CreateInsert(new DateTimeOffset(e.TimeStamp, TimeSpan.Zero), e), AdvanceTimeSettings.IncreasingStartTime);
            var USDSEKStream  = USDSEKObservable.ToPointStream(application, e => PointEvent <StockQuote> .CreateInsert(new DateTimeOffset(e.TimeStamp, TimeSpan.Zero), e), AdvanceTimeSettings.IncreasingStartTime);

            var ericRecalcCepStream = from eUSD in ericUSDStream
                                      from eXch in USDSEKStream
                                      where eUSD.FieldID == "Close"
                                      select new StockQuote()
            {
                StockID   = "ERIC-Recalc",
                FieldID   = "Close",
                Value     = eUSD.Value * eXch.Value,                       // Convert ERIC USD quote to SEK
                TimeStamp = eUSD.TimeStamp
            };

            var ericCompareCepStream = from eRecalc in ericRecalcCepStream
                                       from eSEK in ericSEKStream
                                       where eSEK.FieldID == "Close"
                                       select new StockQuote()
            {
                StockID   = "ERIC-Compare",
                FieldID   = "Diff",
                Value     = eSEK.Value - eRecalc.Value,
                TimeStamp = eRecalc.TimeStamp
            };

            runQuery(ericCompareCepStream);
        }
예제 #3
0
        static void Main(string[] args)
        {
            while (inputQuote.FirstOrDefault().LastTradePrice == null)
            {
                Fetch(inputQuote);
            }


            using (Server server = Server.Create("default")) {
                Application application = server.CreateApplication("app");

                IQStreamable <YahooQuote> inputStream = null;

                inputStream = application.DefineObservable(() => ToObservableInterval(inputQuote, TimeSpan.FromMilliseconds(1000), Scheduler.ThreadPool)).ToPointStreamable(
                    r => PointEvent <YahooQuote> .CreateInsert(DateTimeOffset.Now, r),
                    AdvanceTimeSettings.StrictlyIncreasingStartTime);

                var myQuery = from evt in inputStream
                              select evt;

                foreach (var outputSample in myQuery.ToEnumerable())
                {
                    Console.WriteLine(outputSample);
                }

                Console.WriteLine("Done. Press ENTER to terminate.");
                Console.ReadLine();
            }
        }
예제 #4
0
        static void Main(string[] args)
        {
            string port         = "8088";
            string wcfSourceURL = String.Format(@"http://*****:*****@"http://localhost:{0}/StreamInsight/wcf/Sink/", port);

            using (var server = Server.Create("Default"))
            {
                string AppName = "TestApp";

                if (server.Applications.ContainsKey(AppName))
                {
                    server.Applications[AppName].Delete();
                }
                var app = server.CreateApplication(AppName);

                //WCF Artifacts
                var observableWcfSource = app.DefineObservable(() => new WcfObservable(wcfSourceURL));
                var observableWcfSink   = app.DefineObserver(() => new WcfObserver(wcfSinkURL));
                var r     = new Random(0);
                var query = from x in observableWcfSource.ToPointStreamable(i => PointEvent.CreateInsert <int>(i.T, i.I), AdvanceTimeSettings.IncreasingStartTime).TumblingWindow(TimeSpan.FromMilliseconds(1000))
                            let avgCpu = x.Avg(e => e)
                                         select new OutputEvent {
                    O = (byte)avgCpu, Color = (byte)(avgCpu > 70 ? 2 : avgCpu > 45 ? 1 : 0)
                };

                Console.WriteLine("StreamInsight application using wcf artifacts (WcfObservable)");
                using (query.Bind(observableWcfSink).Run())
                {
                    Console.WriteLine("Sending events...");
                    Console.WriteLine("Press <Enter> to exit.");
                    Console.ReadLine();
                }
            }
        }
예제 #5
0
        public static List <PointEvent <YahooQuote> > GetPointEvents(string[] symbols)
        {
            string symbolList = String.Join("%2C", symbols.Select(w => "%22" + w + "%22").ToArray());
            string url        = string.Format(BASE_URL, symbolList);
            List <PointEvent <YahooQuote> > Quotes = new List <PointEvent <YahooQuote> >();

            try {
                XDocument doc = XDocument.Load(url);
                while (doc.Root.Element("results").IsEmpty)
                {
                    doc = XDocument.Load(url);
                }
                XElement results = doc.Root.Element("results");
                foreach (string symbol in symbols)
                {
                    YahooQuote quote = new YahooQuote(symbol);
                    quote = ParseQuote(symbol, results);
                    Quotes.Add(PointEvent <YahooQuote> .CreateInsert(quote.LastUpdate, quote));
                    Quotes.Add(PointEvent <YahooQuote> .CreateCti(quote.LastUpdate));
                }
            } catch (Exception e) {
                Console.WriteLine("GetPointEventException: {0}", e.Message);
            }

            return(Quotes);
        }
 public PointEvent <TPayloadType> GetPointEvent()
 {
     if (this.EventKind == EventKind.Insert)
     {
         return(PointEvent <TPayloadType> .CreateInsert(this.Start, Payload));
     }
     return(PointEvent <TPayloadType> .CreateCti(Start));
 }
예제 #7
0
        static void RunQuery(NorthwindEntities northwind, Application application)
        {
            // Issue OData queries to determine start and end times for orders.
            // So that the sources behave like temporal streams, we order by the
            // corresponding dates.
            var ordersWithRegions =
                from o in northwind.Orders
                where o.ShipRegion != null
                select o;

            var orderStartTimes =
                from o in ordersWithRegions
                where o.OrderDate != null
                orderby o.OrderDate
                select new { StartTime = (DateTime)o.OrderDate, o.OrderID, o.ShipRegion };

            var orderEndTimes =
                from o in ordersWithRegions
                where o.ShippedDate != null
                orderby o.ShippedDate
                select new { EndTime = (DateTime)o.ShippedDate, o.OrderID };

            // Map OData queries to StreamInsight inputs
            var startStream = orderStartTimes.ToPointStream(application, s =>
                                                            PointEvent.CreateInsert(s.StartTime, s), AdvanceTimeSettings.IncreasingStartTime);

            var endStream = orderEndTimes.ToPointStream(application, e =>
                                                        PointEvent.CreateInsert(e.EndTime, e), AdvanceTimeSettings.IncreasingStartTime);

            // Use clip to synthesize events lasting from the start of each order to the end
            // of each order.
            var clippedStream = startStream
                                .AlterEventDuration(e => TimeSpan.MaxValue)
                                .ClipEventDuration(endStream, (s, e) => s.OrderID == e.OrderID);

            // Count the number of coincident orders per region
            var counts = from o in clippedStream
                         group o by o.ShipRegion into g
                         from win in g.SnapshotWindow(SnapshotWindowOutputPolicy.Clip)
                         select new { ShipRegion = g.Key, Count = win.Count() };

            // Display output whenever there are more than 2 active orders in a region.
            const int threshold = 2;
            var       query     = from c in counts
                                  where c.Count > threshold
                                  select c;

            // Map the query to an IEnumerable sink
            var sink = from i in query.ToIntervalEnumerable()
                       where i.EventKind == EventKind.Insert
                       select new { i.StartTime, i.EndTime, i.Payload.Count, i.Payload.ShipRegion };

            foreach (var r in sink)
            {
                Console.WriteLine(r);
            }
        }
예제 #8
0
        /// <summary>
        /// Example of utilising a user defined function for filtering
        /// </summary>
        /// <param name="ericUSDStream"></param>
        /// <param name="outputConfig"></param>
        /// <param name="adapterStopSignal"></param>
        private static void userFilterExample(IEnumerable <StockQuote> enumerable, Application application)
        {
            var cepStream = enumerable.ToPointStream(application, e => PointEvent <StockQuote> .CreateInsert(new DateTimeOffset(e.TimeStamp, TimeSpan.Zero), e), AdvanceTimeSettings.IncreasingStartTime);

            var filteredCepStream = from e in cepStream
                                    where UserDefinedFilter(e.FieldID)
                                    select e;

            runQuery(filteredCepStream);
        }
예제 #9
0
        /// <summary>
        /// Example of filter function
        /// </summary>
        /// <param name="cepStream"></param>
        /// <param name="outputConfig"></param>
        /// <param name="adapterStopSignal"></param>
        private static void filterExample(IEnumerable <StockQuote> enumerable, Application application)
        {
            // Convert enumerable to CEP Stream for using in the query
            var cepStream = enumerable.ToPointStream(application, e => PointEvent <StockQuote> .CreateInsert(new DateTimeOffset(e.TimeStamp, TimeSpan.Zero), e), AdvanceTimeSettings.IncreasingStartTime);

            // Return only "Close" values using a where-clause
            var filteredCepStream = from e in cepStream
                                    where e.FieldID == "Close"
                                    select e;

            runQuery(filteredCepStream);
        }
예제 #10
0
        /// <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 });
        }
예제 #11
0
        /// <summary>
        /// Read events from the file, combine StartTime and Payload into events
        /// </summary>
        /// <typeparam name="T">type</typeparam>
        /// <param name="fileName">file name</param>
        /// <returns></returns>
        private static IEnumerable <PointEvent <T> > ReadEvents <T>(string fileName) where T : new()
        {
            var lines = File.ReadLines(fileName);

            var createStockTick     = CreatePayloadFunction <T>(1);
            var conversionStockTick = from l in lines
                                      select createStockTick(l.Split(','));

            var createStartTime     = CreatePayloadFunction <StartTimeInfo>(0);
            var conversionStartTime = from l in lines
                                      select createStartTime(l.Split(','));

            return(conversionStartTime.Zip(conversionStockTick, (first, second) => PointEvent.CreateInsert(first.StartTime, second)));
        }
예제 #12
0
 // Read CSV
 public static IEnumerable <PointEvent <XYPayload> > Read(string path)
 {
     foreach (string line in File.ReadLines(path))
     {
         var parts = line.Split(new char[] { ',' });
         var rval  = PointEvent.CreateInsert(
             DateTimeOffset.Parse(parts[0], DateTimeFormatInfo.InvariantInfo),
             new XYPayload
         {
             X = double.Parse(parts[1], CultureInfo.InvariantCulture),
             Y = double.Parse(parts[2], CultureInfo.InvariantCulture)
         });
         yield return(rval);
     }
 }
예제 #13
0
        /// <summary>
        /// Example of using a custom aggregation function for standard deviation calculation.
        /// </summary>
        /// <param name="ericUSDStream"></param>
        /// <param name="outputConfig"></param>
        /// <param name="adapterStopSignal"></param>
        private static void standardDeviationExample(IEnumerable <StockQuote> enumerable, Application application)
        {
            var cepStream = enumerable.ToPointStream(application, e => PointEvent <StockQuote> .CreateInsert(new DateTimeOffset(e.TimeStamp, TimeSpan.Zero), e), AdvanceTimeSettings.IncreasingStartTime);

            var stddevCepStream = from w in cepStream.Where(e => e.FieldID == "Close")
                                  .HoppingWindow(TimeSpan.FromDays(7), TimeSpan.FromDays(1), HoppingWindowOutputPolicy.ClipToWindowEnd)
                                  select new StockQuote()
            {
                StockID   = "ERIC",
                FieldID   = "7-day Stddev",
                Value     = w.StandardDeviation(),
                TimeStamp = w.Min(e => e.TimeStamp)
            };

            runQuery(stddevCepStream);
        }
예제 #14
0
        /// <summary>
        /// Example of a grouping and average calculation on the groups
        /// </summary>
        /// <param name="ericUSDStream"></param>
        /// <param name="outputConfig"></param>
        /// <param name="adapterStopSignal"></param>
        private static void groupApplyExample(IEnumerable <StockQuote> enumerable, Application application)
        {
            var cepStream = enumerable.ToPointStream(application, e => PointEvent <StockQuote> .CreateInsert(new DateTimeOffset(e.TimeStamp, TimeSpan.Zero), e), AdvanceTimeSettings.IncreasingStartTime);

            var ericUSDGroupCepStream = from e in cepStream
                                        group e by e.FieldID into eGroup
                                        from w in eGroup.HoppingWindow(TimeSpan.FromDays(7), TimeSpan.FromDays(1), HoppingWindowOutputPolicy.ClipToWindowEnd)
                                        select new StockQuote()
            {
                StockID   = "ERIC 7-day avg",
                FieldID   = eGroup.Key,
                Value     = w.Avg(e => e.Value),
                TimeStamp = w.Min(e => e.TimeStamp)
            };

            runQuery(ericUSDGroupCepStream);
        }
예제 #15
0
        static IQStreamable <YahooQuote> CreateStream(Application application, bool isRealTime)
        {
            DateTimeOffset startTime = new DateTime(2013, 4, 5, 11, 13, 0, DateTimeKind.Local);

            if (isRealTime)
            {
                return
                    (application.DefineObservable(() => SimulateLiveData()).ToPointStreamable(
                         r => PointEvent <YahooQuote> .CreateInsert(startTime.AddSeconds(1), r),
                         AdvanceTimeSettings.StrictlyIncreasingStartTime));
            }

            return
                (application.DefineEnumerable(() => HistoricData).ToPointStreamable(
                     r => PointEvent <YahooQuote> .CreateInsert(startTime.AddSeconds(1), r),
                     AdvanceTimeSettings.StrictlyIncreasingStartTime));
        }
예제 #16
0
        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));
        }
        /// <summary>
        /// Queue the given CSV row.
        /// </summary>
        /// <returns>True if the row was successfully queued; false otherwise.</returns>
        private bool EnqueueCurrentRow()
        {
            // create the event
            var evt = PointEvent.CreateInsert(
                this.currentRow.Item1,
                new XYPayload(this.currentRow.Item2, this.currentRow.Item3));

            // queue the event
            if (Enqueue(ref evt) != EnqueueOperationResult.Success)
            {
                Util.Log("ReplayablePointCsvInputAdapter", "Encountered full while enqueueing event. Pausing.");
                ReleaseEvent(ref evt);
                Ready();
                return(false);
            }

            return(true);
        }
예제 #18
0
        static IQStreamable <SensorReading> CreateStream(Application application, bool isRealTime)
        {
            DateTime startTime = new DateTime(2011, 1, 1);

            if (isRealTime)
            {
                // Live data uses IQbservable<>
                return
                    (application.DefineObservable(() => SimulateLiveData()).ToPointStreamable(
                         r => PointEvent <SensorReading> .CreateInsert(startTime.AddSeconds(r.Time), r),
                         AdvanceTimeSettings.StrictlyIncreasingStartTime));
            }
            // Historic data uses IQueryable<>
            return
                (application.DefineEnumerable(() => HistoricData).ToPointStreamable(
                     r => PointEvent <SensorReading> .CreateInsert(startTime.AddSeconds(r.Time), r),
                     AdvanceTimeSettings.StrictlyIncreasingStartTime));
        }
예제 #19
0
        /// <summary>
        /// Example of detecting price drops larger than 10% in 7 days
        /// </summary>
        /// <param name="ericUSDStream"></param>
        /// <param name="outputConfig"></param>
        /// <param name="adapterStopSignal"></param>
        private static void bigLooserExample(IEnumerable <StockQuote> enumerable, Application application)
        {
            var cepStream = enumerable.ToPointStream(application, e => PointEvent <StockQuote> .CreateInsert(new DateTimeOffset(e.TimeStamp, TimeSpan.Zero), e), AdvanceTimeSettings.IncreasingStartTime);

            var bigLooserCepStream = (from e1 in cepStream
                                      from e2 in cepStream.ShiftEventTime(e => e.StartTime.AddDays(7))
                                      where e1.FieldID == "Close" && e2.FieldID == "Close"
                                      select new StockQuote()
            {
                StockID = "ERIC > 10% drop",
                FieldID = "Close",
                Value = (e1.Value - e2.Value) / e2.Value * 100,
                TimeStamp = e1.TimeStamp
            }).Where(e => e.Value < -10);


            runQuery(bigLooserCepStream);
        }
예제 #20
0
파일: Program.cs 프로젝트: kgisme170/mynet
        static void Main(string[] args)
        {
            Server      server      = Server.Create("MyInstance");
            Application application = server.CreateApplication("app");

            Console.WriteLine("server+application ready");
            DateTime startTime = new DateTime(2019, 1, 1);
            IQStreamable <SensorReading> inputStream =
                application.DefineObservable(() => SimulateLiveData()).ToPointStreamable(
                    r => PointEvent <SensorReading> .CreateInsert(startTime.AddSeconds(r.Time), r),
                    AdvanceTimeSettings.StrictlyIncreasingStartTime);
            int threshold = 42;

            // Alter all events 1 sec in the future.
            var alteredForward = inputStream.AlterEventStartTime(s => s.StartTime.AddSeconds(1));

            Console.WriteLine("stream ready");

            // Compare each event that occurs at input with the previous event.
            // Note that, this one works for strictly ordered, strictly (e.g 1 sec) regular streams.
            var crossedThreshold = from evt in inputStream
                                   from prev in alteredForward
                                   where prev.Value <threshold && evt.Value> threshold
                                   select new
            {
                Time = evt.Time,
                Low  = prev.Value,
                High = evt.Value
            };

            Console.WriteLine("query ready");
            var query = crossedThreshold.ToEnumerable(); // lazy初始化

            foreach (var outputSample in query)          // 迭代访问query的过程会触发inputStream的产生
            {
                Console.WriteLine(outputSample);         // 打印outputSample, 也就是select new的结果
            }

            Console.WriteLine("Done. Press ENTER to terminate");

            application.Delete();
            server.Dispose();
        }
예제 #21
0
        /// <summary>
        /// Generates a stream of stock quotes, with stock prices following a random walk.
        /// </summary>
        /// <param name="application">The application that will host the query.</param>
        /// <param name="c">The number of events to be generated.</param>
        /// <returns>The stream of stock quotes.</returns>
        static CepStream <StockTick> CreateRandomInputStream(Application application, int c)
        {
            Random         rand      = new Random(100);
            DateTimeOffset startTime = new DateTime(2011, 5, 31);

            // Create input stream using IEnumerable. The stock price follows a random
            // walk, where the price change at each tick is drawn from a uniform
            // random distribution.
            var source = from i in Enumerable.Range(0, c)
                         select PointEvent.CreateInsert(startTime.AddSeconds(i), new StockTick
            {
                StockSymbol = CreateStockSymbol(rand),
                PriceChange = rand.Next(-10, 10)
            });

            var stockStream = source.ToStream(application, AdvanceTimeSettings.StrictlyIncreasingStartTime);

            return(stockStream);
        }
예제 #22
0
        /// <summary>
        /// Our main routine. Creates a random input stream of stock quotes, and performs
        /// pattern detection for every stock symbol in this stream.
        /// </summary>
        /// <param name="args">The command line arguments (unused).</param>
        static void Main(string[] args)
        {
            using (var server = Server.Create("Default"))
            {
                var outputFile = "SampleOutput.txt";

                Console.WriteLine("The program is running and writing results to {0}...", outputFile);

                var app = server.CreateApplication("PatternDetector");

                // Read original data from text file
                var stocks = app.DefineEnumerable(() => ReadEvents <StockTick>("StockTicks.csv"));

                // Convert to IQStremable
                var stockStream = stocks.ToStreamable(AdvanceTimeSettings.StrictlyIncreasingStartTime);

                // Execute pattern detection over the stream
                var patternQuery = from s in stockStream
                                   group s by s.StockSymbol into g
                                   from r in g.Scan(() => new AfaOperator <StockTick, Register, AfaEqualDownticksUpticks>(TimeSpan.FromSeconds(10)))
                                   select new { r.Counter, StockSymbol = g.Key };

                // Output the query results using Observable/Observer pattern
                var sinkObservable = from p in patternQuery.ToPointObservable()
                                     where p.EventKind == EventKind.Insert
                                     select PointEvent.CreateInsert(p.StartTime, new Register
                {
                    Counter     = p.Payload.Counter,
                    StockSymbol = p.Payload.StockSymbol
                });

                var sinkObserver = app.DefineObserver(() => new TextFilePointEventObserver <Register>(outputFile, "\t"));

                using (sinkObservable.Subscribe(sinkObserver))
                {
                    Thread.Sleep(1000);
                }

                Console.WriteLine("Done. Press enter to exit.");
                Console.ReadLine();
            }
        }
예제 #23
0
        static void Main(string[] args)
        {
            string port = "8088";
            //輸入與輸出之WCF
            string wcfSourceURL = String.Format(@"http://*****:*****@"http://localhost:{0}/StreamInsight/wcf/Sink/", port);

            using (var server = Server.Create("StreamInsightInstance1"))
            {
                string AppName = "TestApp";

                if (server.Applications.ContainsKey(AppName))
                {
                    server.Applications[AppName].Delete();
                }
                var app = server.CreateApplication(AppName);

                //WCF Artifacts
                var observableWcfSource = app.DefineObservable(() => new WcfObservable(wcfSourceURL));
                var observableWcfSink   = app.DefineObserver(() => new WcfObserver(wcfSinkURL));

                var inputStream = observableWcfSource.ToPointStreamable(
                    i => PointEvent.CreateInsert <InputEvent>(DateTime.Now, i),
                    AdvanceTimeSettings.IncreasingStartTime);

                var query = from x in inputStream
                            where x.Tp > x.Usl || x.Tp < x.Lsl
                            select new OutputEvent {
                    Tp   = x.Tp, Usl = x.Usl,
                    site = x.site, Lsl = x.Lsl, time = x.time
                };

                Console.WriteLine("StreamInsight application using wcf artifacts (WcfObservable)");
                using (query.Bind(observableWcfSink).Run())
                {
                    Console.WriteLine("Sending events...");
                    Console.WriteLine("Press <Enter> to exit.");
                    Console.ReadLine();
                }
            }
        }
예제 #24
0
파일: Queries.cs 프로젝트: DCT-UA/Monator
        public QueriesCore(string instance, bool distinctNeeded)
        {
            // TODO: Complete member initialization
            this._distinctNeeded = distinctNeeded;
            _cepServer           = Server.Create(instance);
            _cepApplication      = _cepServer.CreateApplication("Monitor");

            _pingSource          = new InputCollection <PageRequest>();
            _pingSourceGathering = new InputCollection <PageRequest>();

            _pingInputStream = _pingSource.ToPointStream(
                _cepApplication,
                s => PointEvent.CreateInsert(DateTime.UtcNow, s),
                AdvanceTimeSettings.IncreasingStartTime,
                "PagePingStream");

            _pingInputStreamGathering = _pingSourceGathering.ToPointStream(
                _cepApplication,
                s => PointEvent.CreateInsert(DateTime.UtcNow, s),
                AdvanceTimeSettings.IncreasingStartTime,
                "PagePingStream");
        }
예제 #25
0
        public void ReadCrimes(Application app)
        {
            StreamReader r = new StreamReader("crimes.csv");

            r.ReadLine();

            string line;

            while ((line = r.ReadLine()) != null)
            {
                var date     = line.Split(new char[] { ';' }, 2)[0];
                var address  = line.Split(';')[1];
                var district = line.Split(';')[2];
                var descr    = line.Split(';')[3];
                var code     = line.Split(';')[4];
                var severity = int.Parse(line.Split(';')[5]);
                var crime    = new Crime()
                {
                    Address = address, District = district, Descr = descr, Code = code, Severity = severity
                };
                PointEvent <Crime> pe = PointEvent.CreateInsert(DateTime.ParseExact(date, "dd.MM.yyyy HH:mm", CultureInfo.InvariantCulture), crime);
                _fullData.Add(pe);
            }
        }
        /// <summary>
        /// analyze data object event flow
        /// </summary>
        public void App(int TimeStamp)
        {
            Console.WriteLine("Starting observable source...");
            using (var source = new RandomObject <Car>(NoSeconds, NoValues, CountMax, NoSegments, TimeStamp))//genereaza o data la fiecare 500 milisecunde
            {
                Console.WriteLine("Started observable source.");
                using (var server = Server.Create("Default"))
                {
                    var host = new ServiceHost(server.CreateManagementService());
                    host.AddServiceEndpoint(typeof(IManagementService), new WSHttpBinding(SecurityMode.Message), "http://localhost/MyStreamInsightApp");
                    host.Open();
                    var myApp = server.CreateApplication("serverApp");
                    Console.WriteLine("convert source to stream");
                    var stream = source.ToPointStream(myApp,
                                                      e => PointEvent.CreateInsert(DateTime.Now, new Payload <Car> {
                        Value = e
                    }),
                                                      AdvanceTimeSettings.StrictlyIncreasingStartTime,
                                                      "Observable Stream");

                    //Console.ReadLine()
                    ;
                    //query that sums of events within 2 second tumbling windows
                    var thumblingResult = from ob in stream.TumblingWindow(TimeSpan.FromSeconds(2), HoppingWindowOutputPolicy.ClipToWindowEnd)
                                          select new
                    {
                        avreageT = ob.Avg(e => e.Value.Speed),
                    };
                    //List<double> list;
                    //var query = from ob in stream
                    //            where ob.Value.Speed < 50
                    //            select ob.Value;
                    //var query = from cars in stream.SnapshotWindow()
                    //                     select new {
                    //                         avreage = cars.Avg(e => e.Value.Speed),
                    //                         groupId=1
                    //                    };
                    //var query = from ob in stream.HoppingWindow(TimeSpan.FromSeconds(WinSize), TimeSpan.FromSeconds(1))
                    //            select new
                    //            {
                    //                avreage = ob.Avg(e => e.Value.Speed),
                    //                groupId = 1
                    //            };
                    var query = from rs in stream
                                group rs by rs.Value.RoadSegment into roadSeg
                                from ob in roadSeg.HoppingWindow(TimeSpan.FromSeconds(WinSize), TimeSpan.FromSeconds(5))
                                select new
                    {
                        avreage = ob.Avg(e => e.Value.Speed),
                        groupId = roadSeg.Key
                    };
                    //var query = from rs in stream
                    //            group rs by rs.Value.RoadSegment into roadSeg
                    //            from ob in roadSeg.SnapshotWindow()
                    //            select new
                    //            {
                    //                avreage = ob.Avg(e => e.Value.Speed),
                    //                groupId = roadSeg.Key
                    //            };



                    var enumerator = query.ToPointEnumerable().GetEnumerator();

                    while (enumerator.MoveNext())
                    {
                        if (enumerator.Current.EventKind == EventKind.Insert)
                        {
                            var s = enumerator.Current.Payload.ToString();
                            //if (s.Length > 0)
                            //    DB.Write(s);
                            //Console.WriteLine(s);
                            DB.Write(TimeStamp, s);

                            RetrieveDiagnostics rD = new RetrieveDiagnostics();

                            DiagnosticSettings settings = new DiagnosticSettings(DiagnosticAspect.GenerateErrorReports, DiagnosticLevel.Always);
                            server.SetDiagnosticSettings(new Uri("cep:/Server"), settings);
                            rD.FileWrite(server.GetDiagnosticView(new Uri("cep:/Server/EventManager")), WinSize);
                            rD.FileWrite(server.GetDiagnosticView(new Uri("cep:/Server/Query")), WinSize);
                        }
                    }

                    host.Close();
                }
                //Count<Car> count = new Count<Car>(50);
                //Console.WriteLine("-------------" + count.NoOptimal);
                Console.WriteLine("The end");

                source.OnCompleted();
            }
            Console.WriteLine("Stopped observable source.");
            //Console.ReadLine();
        }
예제 #27
0
        static void Main(string[] args)
        {
            using (Server server = Server.Create("Lucian"))
            {
                Application app = server.CreateApplication("TwitterAnalyzer");

                //Define the data source
                var twitterStreamable = app.DefineObservable(() =>
                                                             new TwitterStream()).ToPointStreamable(
                    tweet => PointEvent <TweetItem> .
                    CreateInsert(tweet.CreationDate, tweet),
                    AdvanceTimeSettings.IncreasingStartTime);


                //**** Example 1: Pass-through
                //var consoleObserver = app.DefineObserver(() =>
                //    Observer.Create<PointEvent<TweetItem>>(ConsoleWritePointNoCTI));

                //var binding = twitterStreamable.Bind(consoleObserver);



                ////**** Example 2: Tweets per second
                var consoleObserver = app.DefineObserver(() =>
                                                         Observer.Create <PointEvent <long> >(ConsoleWritePointNoCTI));

                //Number of Tweets per second
                var query = from window in twitterStreamable.
                            TumblingWindow(TimeSpan.FromSeconds(1))
                            select window.Count();

                var binding = query.Bind(consoleObserver);



                ////**** Example 3: Tweets per second grouped by language
                //var consoleObserver = app.DefineObserver(() =>
                //    Observer.Create<PointEvent<LanguageSummary>>(ConsoleWritePointNoCTI));

                ////Tweets per second grouped by language
                //var query = from t in twitterStreamable
                //            group t by t.Language into perLanguage
                //            from window in perLanguage.
                //                TumblingWindow(TimeSpan.FromSeconds(5))
                //            select new LanguageSummary
                //            {
                //                Language = perLanguage.Key,
                //                Count = window.Count(),
                //            };

                //var binding = query.Bind(consoleObserver);


                ////Example 4: Top 5 Languages with most Tweets every 5 seconds
                //var consoleObserver = app.DefineObserver(() =>
                //    Observer.Create<PointEvent<LanguageSummary>>(ConsoleWritePointNoCTI));

                ////Tweets per second grouped by language
                //var query = from t in twitterStreamable
                //            group t by t.Language into perLanguage
                //            from window in perLanguage.
                //                TumblingWindow(TimeSpan.FromSeconds(5))
                //            select new LanguageSummary
                //            {
                //                Language = perLanguage.Key,
                //                Count = window.Count(),
                //            };

                //var query2 = from win in query.SnapshotWindow()
                //             from e in
                //                 (from e in win
                //                  orderby e.Count descending
                //                  select e).Take(5)
                //             select e;

                //var binding = query2.Bind(consoleObserver);



                ////**** Example 5: Person with most Followers every 3 seconds
                //var consoleObserver = app.DefineObserver(() =>
                //    Observer.Create<PointEvent<MostPopularTweet>>(ConsoleWritePointNoCTI));

                //var mostFollowers = from win in twitterStreamable.
                //                        TumblingWindow(TimeSpan.FromSeconds(3))
                //                    from e in
                //                        (from e in win
                //                         orderby e.Followers descending
                //                         select e).Take(1)
                //                    select new MostPopularTweet
                //                    {
                //                        Followers = e.Followers,
                //                        Friends = e.Friends,
                //                        Text = e.Text,
                //                        User = e.User,
                //                        Language = e.Language
                //                    };

                //var binding = mostFollowers.Bind(consoleObserver);


                ////**** Example 6: Person with most followers AND most friends in 3 second window
                //var consoleObserver = app.DefineObserver(() =>
                //    Observer.Create<PointEvent<MostPopularTweet>>(ConsoleWritePointNoCTI));

                //var mostFollowers = from win in twitterStreamable.
                //                        TumblingWindow(TimeSpan.FromSeconds(3))
                //                    from e in
                //                        (from e in win
                //                         orderby e.Followers descending
                //                         select e).Take(1)
                //                    select new MostPopularTweet
                //                    {
                //                        Followers = e.Followers,
                //                        Friends = e.Friends,
                //                        Text = e.Text,
                //                        User = e.User,
                //                        Language = e.Language
                //                    };

                //var mostFriends = from win in twitterStreamable.
                //                     TumblingWindow(TimeSpan.FromSeconds(3))
                //                  from e in
                //                      (from e in win
                //                       orderby e.Friends descending
                //                       select e).Take(1)
                //                  select new MostPopularTweet
                //                  {
                //                      Followers = e.Followers,
                //                      Friends = e.Friends,
                //                      Text = e.Text,
                //                      User = e.User,
                //                      Language = e.Language
                //                  };

                //var mostPopGuyOnTwitter = from e1 in mostFollowers
                //                          join e2 in mostFriends
                //                          on e1.User equals e2.User
                //                          select e1;

                //var binding = mostPopGuyOnTwitter.Bind(consoleObserver);

                using (binding.Run())
                {
                    Console.ReadLine();
                }
            }
        }
 private static PointEvent <SourceEvent> CreatePoint(SourceEvent e)
 {
     return(PointEvent.CreateInsert <SourceEvent>(DateTime.Now, e));
 }
예제 #29
0
        static void Main(string[] args)
        {
            //Connect to a remote SI Server instance
            //using (Server server = Server.Connect(new System.ServiceModel.EndpointAddress(@"http://localhost/StreamInsight/default"))) {

            //Create the Embedded SI Server
            using (Server server = Server.Create("default")) {
                //Create a local end point for the server embedded into this program
                //This exposes SI to allow other applications to read from this SI server
                var myHost = new ServiceHost(server.CreateManagementService());
                myHost.AddServiceEndpoint(typeof(IManagementService), new WSHttpBinding(SecurityMode.Message), "http://localhost/StreamInsight/MyInstance");
                myHost.Open();

                //Create the SI application
                Application application = server.CreateApplication("StreamYahooSI");

                //Create Stream of YahooQuote Objects
                //this stream requests a quote per interval TimeSpan specified
                var inputStream = application.DefineObservable(() => Observable.Interval(TimeSpan.FromSeconds(1))).ToPointStreamable(x =>
                                                                                                                                     PointEvent <YahooQuote> .CreateInsert(DateTime.Now, YahooFinance.GetSingleQuote("AAPL")),
                                                                                                                                     AdvanceTimeSettings.IncreasingStartTime);

                //Query the stream
                var myQuery = from evt in inputStream
                              select evt;

                //setup sink as observer and specify onNext action to write to console
                var consoleObserver = application.DefineObserver(() => Observer.Create <PointEvent <YahooQuote> >(ConsoleWritePoint));

                //bind sink to observe query and run the process
                using (var proc = myQuery.Bind(consoleObserver).Run("serverProcess")) {
                    Console.WriteLine();
                    Console.WriteLine("*** Hit Enter to exit after viewing query output ***");
                    Console.WriteLine();
                    Console.ReadLine();
                }

                myHost.Close();
                Console.WriteLine();
                Console.WriteLine("Process Stopped.  Hit Enter to quit.");
                Console.ReadLine();
            }
        }
예제 #30
0
 protected override void PopulateEvent(out PointEvent <T> typedEvent)
 {
     typedEvent = PointEvent.CreateInsert(DateTime.Now, new T());
 }