コード例 #1
0
        internal void Process(long newTime, double newValue)
        {
            Header header      = parentDb.Header;
            long   step        = header.Step;
            long   oldTime     = header.LastUpdateTime;
            long   startTime   = Util.Normalize(oldTime, step);
            long   endTime     = startTime + step;
            double oldValue    = lastValue.Get();
            double updateValue = CalculateUpdateValue(oldTime, oldValue, newTime, newValue);

            if (newTime < endTime)
            {
                Accumulate(oldTime, newTime, updateValue);
            }
            else
            {
                // should store something
                long boundaryTime = Util.Normalize(newTime, step);
                Accumulate(oldTime, boundaryTime, updateValue);
                double value = CalculateTotal(startTime, boundaryTime);
                // how many updates?
                long numSteps = (boundaryTime - endTime) / step + 1L;
                // ACTION!
                parentDb.Archive(this, value, numSteps);
                // cleanup
                nanSeconds.Set(0);
                accumValue.Set(0.0);
                Accumulate(boundaryTime, newTime, updateValue);
            }
        }
コード例 #2
0
        private double CalculateUpdateValue(long oldTime, double oldValue, long newTime, double newValue)
        {
            double updateValue = Double.NaN;

            if (newTime - oldTime <= heartbeat.Get())
            {
                string type = dsType.Get();
                if (type.Equals("GAUGE"))
                {
                    updateValue = newValue;
                }
                else if (type.Equals("ABSOLUTE"))
                {
                    if (!Double.IsNaN(newValue))
                    {
                        updateValue = newValue / (newTime - oldTime);
                    }
                }
                else if (type.Equals("DERIVE"))
                {
                    if (!Double.IsNaN(newValue) && !Double.IsNaN(oldValue))
                    {
                        updateValue = (newValue - oldValue) / (newTime - oldTime);
                    }
                }
                else if (type.Equals("COUNTER"))
                {
                    if (!Double.IsNaN(newValue) && !Double.IsNaN(oldValue))
                    {
                        double diff     = newValue - oldValue;
                        double max32bit = Math.Pow(2, 32);
                        double max64bit = Math.Pow(2, 64);
                        if (diff < 0)
                        {
                            diff += max32bit;
                        }
                        if (diff < 0)
                        {
                            diff += max64bit - max32bit;
                        }
                        if (diff >= 0)
                        {
                            updateValue = diff / (newTime - oldTime);
                        }
                    }
                }
                if (!Double.IsNaN(updateValue))
                {
                    double minVal = minValue.Get();
                    double maxVal = maxValue.Get();
                    if (!Double.IsNaN(minVal) && updateValue < minVal)
                    {
                        updateValue = Double.NaN;
                    }
                    if (!Double.IsNaN(maxVal) && updateValue > maxVal)
                    {
                        updateValue = Double.NaN;
                    }
                }
            }
            lastValue.Set(newValue);
            return(updateValue);
        }