Пример #1
0
        public override void Update(EventBean[] newData, EventBean[] oldData)
        {
            using (Instrument.With(
                       i => i.QViewProcessIRStream(this, _factory.ViewName, newData, oldData),
                       i => i.AViewProcessIRStream()))
            {
                if ((newData != null) && (newData.Length > 0))
                {
                    // If we have an empty window about to be filled for the first time, add a callback
                    bool removeSchedule = false;
                    bool addSchedule    = false;
                    long timestamp      = _agentInstanceContext.StatementContext.SchedulingService.Time;

                    // if the window is already filled, then we may need to reschedule
                    if (_currentBatch.IsNotEmpty())
                    {
                        // check if we need to reschedule
                        long callbackTime = timestamp + _timeDeltaComputation.DeltaMillisecondsAdd(timestamp);
                        if (callbackTime != _callbackScheduledTime)
                        {
                            removeSchedule = true;
                            addSchedule    = true;
                        }
                    }
                    else
                    {
                        addSchedule = true;
                    }

                    if (removeSchedule)
                    {
                        _agentInstanceContext.StatementContext.SchedulingService.Remove(_handle, _scheduleSlot);
                        _callbackScheduledTime = -1;
                    }
                    if (addSchedule)
                    {
                        long msecIntervalSize = _timeDeltaComputation.DeltaMillisecondsAdd(timestamp);
                        _agentInstanceContext.StatementContext.SchedulingService.Add(
                            msecIntervalSize, _handle, _scheduleSlot);
                        _callbackScheduledTime = msecIntervalSize + timestamp;
                    }

                    // add data points to the window
                    for (int i = 0; i < newData.Length; i++)
                    {
                        _currentBatch.Put(newData[i], timestamp);
                        InternalHandleAdded(newData[i], timestamp);
                        _lastEvent = newData[i];
                    }
                }

                if ((oldData != null) && (oldData.Length > 0))
                {
                    bool removedLastEvent = false;
                    foreach (EventBean anOldData in oldData)
                    {
                        _currentBatch.Remove(anOldData);
                        InternalHandleRemoved(anOldData);
                        if (anOldData == _lastEvent)
                        {
                            removedLastEvent = true;
                        }
                    }

                    // we may need to reschedule as the newest event may have been deleted
                    if (_currentBatch.Count == 0)
                    {
                        _agentInstanceContext.StatementContext.SchedulingService.Remove(_handle, _scheduleSlot);
                        _callbackScheduledTime = -1;
                        _lastEvent             = null;
                    }
                    else
                    {
                        // reschedule if the last event was removed
                        if (removedLastEvent)
                        {
                            ICollection <EventBean> keyset = _currentBatch.Keys;
                            EventBean[]             events = keyset.ToArray();
                            _lastEvent = events[events.Length - 1];
                            long lastTimestamp = _currentBatch.Get(_lastEvent);

                            // reschedule, newest event deleted
                            long timestamp    = _agentInstanceContext.StatementContext.SchedulingService.Time;
                            long callbackTime = lastTimestamp + _timeDeltaComputation.DeltaMillisecondsAdd(lastTimestamp);
                            long deltaFromNow = callbackTime - timestamp;
                            if (callbackTime != _callbackScheduledTime)
                            {
                                _agentInstanceContext.StatementContext.SchedulingService.Remove(_handle, _scheduleSlot);
                                _agentInstanceContext.StatementContext.SchedulingService.Add(
                                    deltaFromNow, _handle, _scheduleSlot);
                                _callbackScheduledTime = callbackTime;
                            }
                        }
                    }
                }

                // Update child views
                if (HasViews)
                {
                    using (Instrument.With(
                               i => i.QViewIndicate(this, _factory.ViewName, newData, oldData),
                               i => i.AViewIndicate()))
                    {
                        UpdateChildren(newData, oldData);
                    }
                }
            }
        }