Ejemplo n.º 1
0
        public override void Update(EventBean[] newData, EventBean[] oldData)
        {
            Instrument.With(
                i => i.QViewProcessIRStream(this, _timeWindowViewFactory.ViewName, newData, oldData),
                i => i.AViewProcessIRStream(),
                () =>
            {
                long timestamp = _agentInstanceContext.StatementContext.SchedulingService.Time;

                if (oldData != null)
                {
                    for (int i = 0; i < oldData.Length; i++)
                    {
                        _timeWindow.Remove(oldData[i]);
                    }
                }

                // we don't care about removed data from a prior view
                if ((newData != null) && (newData.Length > 0))
                {
                    // If we have an empty window about to be filled for the first time, schedule a callback
                    // for now plus timeDeltaComputation
                    if (_timeWindow.IsEmpty())
                    {
                        long current = _agentInstanceContext.StatementContext.SchedulingService.Time;
                        ScheduleCallback(_timeDeltaComputation.DeltaAdd(current));
                    }

                    // add data points to the timeWindow
                    for (int i = 0; i < newData.Length; i++)
                    {
                        _timeWindow.Add(timestamp, newData[i]);
                    }

                    if (_viewUpdatedCollection != null)
                    {
                        _viewUpdatedCollection.Update(newData, null);
                    }
                }

                // Update child views
                if (HasViews)
                {
                    Instrument.With(
                        i => i.QViewIndicate(this, _timeWindowViewFactory.ViewName, newData, oldData),
                        i => i.AViewIndicate(),
                        () => UpdateChildren(newData, oldData));
                }
            });
        }
Ejemplo n.º 2
0
        protected void ScheduleCallback(long delta)
        {
            ScheduleHandleCallback callback = new ProxyScheduleHandleCallback
            {
                ProcScheduledTrigger = extensionServicesContext => Instrument.With(
                    i => i.QViewScheduledEval(this, _timeLengthBatchViewFactory.ViewName),
                    i => i.AViewScheduledEval(),
                    () => SendBatch(true))
            };

            _handle = new EPStatementHandleCallback(_agentInstanceContext.EpStatementAgentInstanceHandle, callback);
            var currentTime = _agentInstanceContext.StatementContext.SchedulingService.Time;
            var scheduled   = _timeDeltaComputation.DeltaAdd(currentTime) - delta;

            _agentInstanceContext.StatementContext.SchedulingService.Add(scheduled, _handle, _scheduleSlot);
            _callbackScheduledTime = _agentInstanceContext.StatementContext.SchedulingService.Time + scheduled;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Returns the number of milliseconds.
        /// </summary>
        /// <param name="fromTime">from-time</param>
        /// <param name="agentInstanceContext">context</param>
        /// <returns>msec</returns>
        public long GetScheduleForwardDelta(long fromTime, AgentInstanceContext agentInstanceContext)
        {
            if (InstrumentationHelper.ENABLED)
            {
                InstrumentationHelper.Get().QRegIntervalValue(_timePeriodExpr);
            }
            if (_timeDeltaComputation == null)
            {
                _timeDeltaComputation = _timePeriodExpr.ConstEvaluator(new ExprEvaluatorContextStatement(agentInstanceContext.StatementContext, false));
            }
            long result = _timeDeltaComputation.DeltaAdd(fromTime);

            if (InstrumentationHelper.ENABLED)
            {
                InstrumentationHelper.Get().ARegIntervalValue(result);
            }
            return(result);
        }
Ejemplo n.º 4
0
        private void ScheduleCallback()
        {
            long afterTime = _timeDeltaComputation.DeltaAdd(
                AgentInstanceContext.StatementContext.SchedulingService.Time);

            ScheduleHandleCallback callback = new ProxyScheduleHandleCallback
            {
                ProcScheduledTrigger = extensionServicesContext => Instrument.With(
                    i => i.QViewScheduledEval(this, _timeFirstViewFactory.ViewName),
                    i => i.AViewScheduledEval(),
                    () =>
                {
                    _isClosed = true;
                    InternalHandleClosed();
                })
            };

            _handle = new EPStatementHandleCallback(AgentInstanceContext.EpStatementAgentInstanceHandle, callback);
            AgentInstanceContext.StatementContext.SchedulingService.Add(afterTime, _handle, _scheduleSlot);
        }
Ejemplo n.º 5
0
 private static ExprOptionalConstant GetExprOrConstant(ExprNode exprNode, TimeAbacus timeAbacus)
 {
     if (exprNode is ExprTimePeriod)
     {
         var timePeriod = (ExprTimePeriod)exprNode;
         if (!timePeriod.HasMonth && !timePeriod.HasYear)
         {
             // no-month and constant
             if (exprNode.IsConstantResult)
             {
                 var sec = timePeriod.EvaluateAsSeconds(null, true, null);
                 var l   = timeAbacus.DeltaForSecondsDouble(sec);
                 IntervalDeltaExprEvaluator eval = new ProxyIntervalDeltaExprEvaluator
                 {
                     ProcEvaluate = (reference, eventsPerStream, isNewData, context) => l,
                 };
                 return(new ExprOptionalConstant(eval, l));
             }
             // no-month and not constant
             else
             {
                 IntervalDeltaExprEvaluator eval = new ProxyIntervalDeltaExprEvaluator
                 {
                     ProcEvaluate = (reference, eventsPerStream, isNewData, context) =>
                     {
                         double sec = timePeriod.EvaluateAsSeconds(eventsPerStream, isNewData, context);
                         return(timeAbacus.DeltaForSecondsDouble(sec));
                     },
                 };
                 return(new ExprOptionalConstant(eval, null));
             }
         }
         // has-month
         else
         {
             // has-month and constant
             if (exprNode.IsConstantResult)
             {
                 ExprTimePeriodEvalDeltaConst timerPeriodConst = timePeriod.ConstEvaluator(null);
                 IntervalDeltaExprEvaluator   eval             = new ProxyIntervalDeltaExprEvaluator
                 {
                     ProcEvaluate = (reference, eventsPerStream, isNewData, context) =>
                     {
                         return(timerPeriodConst.DeltaAdd(reference));
                     },
                 };
                 return(new ExprOptionalConstant(eval, null));
             }
             // has-month and not constant
             else
             {
                 ExprTimePeriodEvalDeltaNonConst timerPeriodNonConst = timePeriod.NonconstEvaluator();
                 IntervalDeltaExprEvaluator      eval = new ProxyIntervalDeltaExprEvaluator
                 {
                     ProcEvaluate = (reference, eventsPerStream, isNewData, context) => timerPeriodNonConst.DeltaAdd(
                         reference, eventsPerStream, isNewData, context),
                 };
                 return(new ExprOptionalConstant(eval, null));
             }
         }
     }
     else if (ExprNodeUtility.IsConstantValueExpr(exprNode))
     {
         var  constantNode = (ExprConstantNode)exprNode;
         long l            = constantNode.GetConstantValue(null).AsLong();
         IntervalDeltaExprEvaluator eval = new ProxyIntervalDeltaExprEvaluator
         {
             ProcEvaluate = (reference, eventsPerStream, isNewData, context) => l,
         };
         return(new ExprOptionalConstant(eval, l));
     }
     else
     {
         var evaluator = exprNode.ExprEvaluator;
         IntervalDeltaExprEvaluator eval = new ProxyIntervalDeltaExprEvaluator
         {
             ProcEvaluate = (reference, eventsPerStream, isNewData, context) => evaluator.Evaluate(
                 new EvaluateParams(eventsPerStream, isNewData, context)).AsLong(),
         };
         return(new ExprOptionalConstant(eval, null));
     }
 }
Ejemplo n.º 6
0
        public override void Update(EventBean[] newData, EventBean[] oldData)
        {
            if (InstrumentationHelper.ENABLED)
            {
                InstrumentationHelper.Get().QViewProcessIRStream(this, _factory.ViewName, newData, oldData);
            }

            // we don't care about removed data from a prior view
            if ((newData == null) || (newData.Length == 0))
            {
                if (InstrumentationHelper.ENABLED)
                {
                    InstrumentationHelper.Get().AViewProcessIRStream();
                }
                return;
            }

            // If we have an empty window about to be filled for the first time, addSchedule a callback
            bool removeSchedule = false;
            bool addSchedule    = false;
            long timestamp      = _agentInstanceContext.StatementContext.SchedulingService.Time;

            if (!_currentBatch.IsEmpty())
            {
                // check if we need to reschedule
                long callbackTime = timestamp + _timeDeltaComputation.DeltaAdd(timestamp);
                if (callbackTime != _callbackScheduledTime)
                {
                    removeSchedule = true;
                    addSchedule    = true;
                }
            }
            else
            {
                addSchedule = true;
            }

            if (removeSchedule)
            {
                _agentInstanceContext.StatementContext.SchedulingService.Remove(_handle, _scheduleSlot);
            }
            if (addSchedule)
            {
                long timeIntervalSize = _timeDeltaComputation.DeltaAdd(timestamp);
                _agentInstanceContext.StatementContext.SchedulingService.Add(timeIntervalSize, _handle, _scheduleSlot);
                _callbackScheduledTime = timeIntervalSize + timestamp;
            }

            // add data points to the window
            foreach (EventBean newEvent in newData)
            {
                _currentBatch.Add(newEvent);
            }

            // forward insert stream to child views
            if (_viewUpdatedCollection != null)
            {
                _viewUpdatedCollection.Update(newData, null);
            }

            // update child views
            if (HasViews)
            {
                if (InstrumentationHelper.ENABLED)
                {
                    InstrumentationHelper.Get().QViewIndicate(this, _factory.ViewName, newData, null);
                }
                UpdateChildren(newData, null);
                if (InstrumentationHelper.ENABLED)
                {
                    InstrumentationHelper.Get().AViewIndicate();
                }
            }

            if (InstrumentationHelper.ENABLED)
            {
                InstrumentationHelper.Get().AViewProcessIRStream();
            }
        }
Ejemplo n.º 7
0
        public override void Update(EventBean[] newData, EventBean[] oldData)
        {
            using (Instrument.With(
                       i => i.QViewProcessIRStream(this, _viewFactory.ViewName, newData, oldData),
                       i => i.AViewProcessIRStream()))
            {
                EventBean[] postOldEventsArray = null;

                // Remove old data
                if (oldData != null)
                {
                    for (int i = 0; i < oldData.Length; i++)
                    {
                        var oldDataItem = oldData[i];
                        var sortValues  = GetTimestamp(oldDataItem);
                        var result      = CollectionUtil.RemoveEventByKeyLazyListMap(sortValues, oldDataItem, _sortedEvents);
                        if (result)
                        {
                            _eventCount--;
                            if (postOldEventsArray == null)
                            {
                                postOldEventsArray = oldData;
                            }
                            else
                            {
                                postOldEventsArray = CollectionUtil.AddArrayWithSetSemantics(
                                    postOldEventsArray, oldData);
                            }
                            InternalHandleRemoved(sortValues, oldDataItem);
                        }
                    }
                }

                if ((newData != null) && (newData.Length > 0))
                {
                    // figure out the current tail time
                    long engineTime     = _agentInstanceContext.StatementContext.SchedulingService.Time;
                    long windowTailTime = engineTime - _timeDeltaComputation.DeltaAdd(engineTime) + 1;
                    long oldestEvent    = long.MaxValue;
                    if (_sortedEvents.IsNotEmpty())
                    {
                        oldestEvent = (long)_sortedEvents.Keys.First();
                    }
                    bool addedOlderEvent = false;

                    // add events or post events as remove stream if already older then tail time
                    List <EventBean> postOldEvents = null;
                    for (int i = 0; i < newData.Length; i++)
                    {
                        // get timestamp of event
                        var newEvent  = newData[i];
                        var timestamp = GetTimestamp(newEvent);

                        // if the event timestamp indicates its older then the tail of the window, release it
                        if (timestamp < windowTailTime)
                        {
                            if (postOldEvents == null)
                            {
                                postOldEvents = new List <EventBean>(2);
                            }
                            postOldEvents.Add(newEvent);
                        }
                        else
                        {
                            if (timestamp < oldestEvent)
                            {
                                addedOlderEvent = true;
                                oldestEvent     = timestamp.Value;
                            }

                            // add to list
                            CollectionUtil.AddEventByKeyLazyListMapBack(timestamp, newEvent, _sortedEvents);
                            _eventCount++;
                            InternalHandleAdd(timestamp, newEvent);
                        }
                    }

                    // If we do have data, check the callback
                    if (_sortedEvents.IsNotEmpty())
                    {
                        // If we haven't scheduled a callback yet, schedule it now
                        if (!_isCallbackScheduled)
                        {
                            long callbackWait = oldestEvent - windowTailTime + 1;
                            _agentInstanceContext.StatementContext.SchedulingService.Add(
                                callbackWait, _handle, _scheduleSlot);
                            _isCallbackScheduled = true;
                        }
                        else
                        {
                            // We may need to reschedule, and older event may have been added
                            if (addedOlderEvent)
                            {
                                oldestEvent = (long)_sortedEvents.Keys.First();
                                long callbackWait = oldestEvent - windowTailTime + 1;
                                _agentInstanceContext.StatementContext.SchedulingService.Remove(_handle, _scheduleSlot);
                                _agentInstanceContext.StatementContext.SchedulingService.Add(
                                    callbackWait, _handle, _scheduleSlot);
                                _isCallbackScheduled = true;
                            }
                        }
                    }

                    if (postOldEvents != null)
                    {
                        postOldEventsArray = postOldEvents.ToArray();
                    }

                    if (_optionalSortedRandomAccess != null)
                    {
                        _optionalSortedRandomAccess.Refresh(_sortedEvents, _eventCount, _eventCount);
                    }
                }

                // Update child views
                if (HasViews)
                {
                    Instrument.With(
                        i => i.QViewIndicate(this, _viewFactory.ViewName, newData, postOldEventsArray),
                        i => i.AViewIndicate(),
                        () => UpdateChildren(newData, postOldEventsArray));
                }
            }
        }
Ejemplo n.º 8
0
        public override void Update(EventBean[] newData, EventBean[] oldData)
        {
            if (InstrumentationHelper.ENABLED)
            {
                InstrumentationHelper.Get().QViewProcessIRStream(this, _factory.ViewName, newData, oldData);
            }

            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.IsEmpty())
                {
                    // check if we need to reschedule
                    long callbackTime = timestamp + _timeDeltaComputation.DeltaAdd(timestamp);
                    if (callbackTime != _callbackScheduledTime)
                    {
                        removeSchedule = true;
                        addSchedule    = true;
                    }
                }
                else
                {
                    addSchedule = true;
                }

                if (removeSchedule)
                {
                    _agentInstanceContext.StatementContext.SchedulingService.Remove(_handle, _scheduleSlot);
                    _callbackScheduledTime = -1;
                }
                if (addSchedule)
                {
                    long timeIntervalSize = _timeDeltaComputation.DeltaAdd(timestamp);
                    _agentInstanceContext.StatementContext.SchedulingService.Add(
                        timeIntervalSize, _handle, _scheduleSlot);
                    _callbackScheduledTime = timeIntervalSize + 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)
                    {
                        var keyset = _currentBatch.Keys;
                        var 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.DeltaAdd(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)
            {
                if (InstrumentationHelper.ENABLED)
                {
                    InstrumentationHelper.Get().QViewIndicate(this, _factory.ViewName, newData, oldData);
                }
                UpdateChildren(newData, oldData);
                if (InstrumentationHelper.ENABLED)
                {
                    InstrumentationHelper.Get().AViewIndicate();
                }
            }

            if (InstrumentationHelper.ENABLED)
            {
                InstrumentationHelper.Get().AViewProcessIRStream();
            }
        }
Ejemplo n.º 9
0
 public long AbsExpiry(PatternAgentInstanceContext context)
 {
     long current = context.StatementContext.SchedulingService.Time;
     return current + _timeDeltaComputation.DeltaAdd(current);
 }