예제 #1
0
        private async Task updateGraph(int increase = 1)
        {
            double dateValue = OxyPlot.Axes.DateTimeAxis.ToDouble(DateTime.Today);

            if (IncreaseGraph == null)
            {
                IncreaseGraph = new DatePlot(typeof(T).Name + "Handler", "Date", "Tracker Increase", "dd-MMM", false);
                IncreaseGraph.AddValue("Value", 0, DateTime.Today.AddMilliseconds(-1));
                IncreaseGraph.AddValue("Value", increase, DateTime.Today);
            }

            else
            {
                if (IncreaseGraph.PlotDataPoints.Last().Value.Key < dateValue)
                {
                    //Only show past year
                    IncreaseGraph.PlotDataPoints = IncreaseGraph.PlotDataPoints.SkipWhile(x => (DateTime.Today - OxyPlot.Axes.DateTimeAxis.ToDateTime(x.Value.Key)).Days >= 365).ToList();
                    for (int i = (int)(dateValue - IncreaseGraph.PlotDataPoints.Last().Value.Key) - 1; i > 0; i--)
                    {
                        IncreaseGraph.AddValue("Value", 0, DateTime.Today.AddDays(-i));
                    }
                    IncreaseGraph.AddValue("Value", increase, DateTime.Today);
                }
                else
                {
                    IncreaseGraph.AddValue("Value", IncreaseGraph.PlotDataPoints.Last().Value.Value + increase, DateTime.Today, replace: true);
                }
            }

            await StaticBase.Database.GetCollection <DatePlot>("TrackerHandler").ReplaceOneAsync(x => x.ID.Equals(typeof(T).Name + "Handler"), IncreaseGraph, new UpdateOptions {
                IsUpsert = true
            });
        }
예제 #2
0
        public override void PostInitialisation()
        {
            var collection = StaticBase.Database.GetCollection <T>(typeof(T).Name).FindSync <T>(x => true).ToList();

            IncreaseGraph = StaticBase.Database.GetCollection <DatePlot>("TrackerHandler").FindSync <DatePlot>(x => x.ID.Equals(typeof(T).Name + "Handler")).FirstOrDefault();
            IncreaseGraph?.InitPlot("Date", "Tracker Increase", "dd-MMM", false);

            trackers = collection.ToDictionary(x => x.Name);

            trackers = (trackers == null ? new Dictionary <string, T>() : trackers);

            if (collection.Count > 0)
            {
                int gap = trackerInterval / collection.Count;

                for (int i = trackers.Count - 1; i >= 0; i--)
                {
                    try
                    {
                        var cur = trackers[trackers.Keys.ElementAt(i)];
                        //cur.SetTimer(trackerInterval, gap * (i + 1) + 20000);
                        bool save = cur.ChannelConfig.Count == 0;
                        cur.Conversion(trackers.Count - i);
                        cur.PostInitialisation(trackers.Count - i);
                        if (save)
                        {
                            UpdateDBAsync(cur).Wait();
                        }
                        cur.OnMinorEventFired += OnMinorEvent;
                        cur.OnMajorEventFired += OnMajorEvent;
                    }
                    catch (Exception e)
                    {
                        Program.MopsLog(new LogMessage(LogSeverity.Error, "", $"Error on PostInitialisation, {e.Message}", e));
                    }
                }

                //Start Twitter STREAM after all are initialised
                if (typeof(T) == typeof(TwitterTracker))
                {
                    TwitterTracker.STREAM.StreamStopped += (sender, args) => { Program.MopsLog(new LogMessage(LogSeverity.Info, "", $"TwitterSTREAM stopped. {args.DisconnectMessage?.Reason ?? ""}", args.Exception)); TwitterTracker.RestartStream(); };
                    TwitterTracker.STREAM.StreamStarted += (sender, args) => Program.MopsLog(new LogMessage(LogSeverity.Info, "", "TwitterSTREAM started."));
                    TwitterTracker.STREAM.WarningFallingBehindDetected += (sender, args) => Program.MopsLog(new LogMessage(LogSeverity.Warning, "", $"TwitterSTREAM falling behind, {args.WarningMessage.Message} ({args.WarningMessage.PercentFull}%)"));
                    TwitterTracker.STREAM.FilterLevel = Tweetinvi.Streaming.Parameters.StreamFilterLevel.Low;
                    TwitterTracker.STREAM.StartStreamMatchingAllConditionsAsync();
                }
            }

            nextTracker = new System.Threading.Timer(LoopTrackers);
            loopQueue   = trackers.Values.ToList();
            if (trackers.FirstOrDefault().Value is BaseUpdatingTracker)
            {
                nextUpdate  = new System.Threading.Timer(LoopTrackersUpdate);
                loopQueue   = trackers.Where(x => (x.Value as BaseUpdatingTracker).ToUpdate.Count == 0).Select(x => x.Value).ToList();
                updateQueue = trackers.Where(x => (x.Value as BaseUpdatingTracker).ToUpdate.Count > 0).Select(x => x.Value).ToList();
                nextUpdate.Change(5000, updateInterval / (updateQueue.Count > 0 ? updateQueue.Count : 1));
            }
            nextTracker.Change(5000, trackerInterval / (loopQueue.Count > 0 ? loopQueue.Count : 1));
        }