func (p *movingAverageProcessor) calculateMovingAverage(m plugin.MetricType, logger *log.Logger) (float64, error) { namespace := concatNameSpace(m.Namespace().Strings()) switch v := m.Data().(type) { default: logger.Warnln(fmt.Sprintf("Unknown data received: Type %T", v)) return 0.0, nil case int: if _, ok := p.movingAverageMap[namespace]; ok { counter, err := p.getCounter(namespace) counterCurrent := counter % p.movingBufLength err = p.addBufferData(counterCurrent, m.Data(), namespace) if err != nil { return 0.0, err } sum := int(0) //Initial Counter is used to give correct average for initial iterations ie when the buffer is not full initialCounter := 0 for i := 0; i < p.movingBufLength; i++ { if p.getBufferData(i, namespace) != nil { initialCounter++ sum += p.getBufferData(i, namespace).(int) } } movingAvg := float64(sum) / float64(initialCounter) counterCurrent++ p.setCounter(namespace, counterCurrent) return movingAvg, err } else { //Since map doesnot have an entry of this namespace, it's creating an entry for the namespace. //Also m.data value is inserted into 0th position of the buffer because we know that this buffer is being used for the first time p.movingAverageMap[namespace] = newmovingAverage(p.getBufferLength()) err := p.addBufferData(0, m.Data(), namespace) if err != nil { return 0.0, err } sum := p.getBufferData(0, namespace).(int) p.setCounter(namespace, 1) return float64(sum), nil } case float64: if _, ok := p.movingAverageMap[namespace]; ok { counter, err := p.getCounter(namespace) counterCurrent := counter % p.movingBufLength err = p.addBufferData(counterCurrent, m.Data(), namespace) if err != nil { return 0.0, err } sum := float64(0) initialCounter := 0 for i := 0; i < p.movingBufLength; i++ { if p.getBufferData(i, namespace) != nil { initialCounter++ sum += p.getBufferData(i, namespace).(float64) } } movingAvg := float64(sum) / float64(initialCounter) counterCurrent++ p.setCounter(namespace, counterCurrent) return movingAvg, err } p.movingAverageMap[namespace] = newmovingAverage(p.getBufferLength()) err := p.addBufferData(0, m.Data(), namespace) if err != nil { return 0.0, err } sum := p.getBufferData(0, namespace).(float64) p.setCounter(namespace, 1) return float64(sum), nil case float32: if _, ok := p.movingAverageMap[namespace]; ok { counter, err := p.getCounter(namespace) counterCurrent := counter % p.movingBufLength err = p.addBufferData(counterCurrent, m.Data(), namespace) if err != nil { return 0.0, err } sum := float32(0) initialCounter := 0 for i := 0; i < p.movingBufLength; i++ { if p.getBufferData(i, namespace) != nil { initialCounter++ sum += p.getBufferData(i, namespace).(float32) } } movingAvg := float64(sum) / float64(initialCounter) p.setCounter(namespace, counterCurrent) return movingAvg, err } p.movingAverageMap[namespace] = newmovingAverage(p.getBufferLength()) err := p.addBufferData(0, m.Data(), namespace) if err != nil { return 0.0, err } sum := p.getBufferData(0, namespace).(float32) p.setCounter(namespace, 1) return float64(sum), nil case uint32: if _, ok := p.movingAverageMap[namespace]; ok { counter, err := p.getCounter(namespace) counterCurrent := counter % p.movingBufLength err = p.addBufferData(counterCurrent, m.Data(), namespace) if err != nil { return 0.0, err } sum := uint32(0) initialCounter := 0 for i := 0; i < p.movingBufLength; i++ { if p.getBufferData(i, namespace) != nil { initialCounter++ sum += p.getBufferData(i, namespace).(uint32) } } movingAvg := float64(sum) / float64(initialCounter) counterCurrent++ p.setCounter(namespace, counterCurrent) return movingAvg, err } p.movingAverageMap[namespace] = newmovingAverage(p.getBufferLength()) err := p.addBufferData(0, m.Data(), namespace) if err != nil { return 0.0, err } sum := p.getBufferData(0, namespace).(uint32) p.setCounter(namespace, 1) return float64(sum), nil case uint64: if _, ok := p.movingAverageMap[namespace]; ok { counter, err := p.getCounter(namespace) counterCurrent := counter % p.movingBufLength err = p.addBufferData(counterCurrent, m.Data(), namespace) if err != nil { return 0.0, err } sum := uint64(0) initialCounter := 0 for i := 0; i < p.movingBufLength; i++ { if p.getBufferData(i, namespace) != nil { initialCounter++ sum += p.getBufferData(i, namespace).(uint64) } } movingAvg := float64(sum) / float64(initialCounter) counterCurrent++ p.setCounter(namespace, counterCurrent) return movingAvg, err } p.movingAverageMap[namespace] = newmovingAverage(p.getBufferLength()) err := p.addBufferData(0, m.Data(), namespace) if err != nil { return 0.0, err } sum := p.getBufferData(0, namespace).(uint64) p.setCounter(namespace, 1) return float64(sum), nil } }