mean calculation
This commit is contained in:
parent
f29e6b760e
commit
b3f52e84e4
|
@ -29,7 +29,7 @@ type InfluxDB struct {
|
||||||
WriteConsistency string
|
WriteConsistency string
|
||||||
Timeout internal.Duration
|
Timeout internal.Duration
|
||||||
UDPPayload int `toml:"udp_payload"`
|
UDPPayload int `toml:"udp_payload"`
|
||||||
DS *DS
|
Downsampler *Downsampling
|
||||||
|
|
||||||
// Path to CA file
|
// Path to CA file
|
||||||
SSLCA string `toml:"ssl_ca"`
|
SSLCA string `toml:"ssl_ca"`
|
||||||
|
@ -192,7 +192,7 @@ func (i *InfluxDB) Write(metrics []telegraf.Metric) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
i.DS.Add(metrics)
|
i.Downsampler.Add(metrics)
|
||||||
|
|
||||||
for _, metric := range metrics {
|
for _, metric := range metrics {
|
||||||
bp.AddPoint(metric.Point())
|
bp.AddPoint(metric.Point())
|
||||||
|
@ -222,50 +222,93 @@ func (i *InfluxDB) Write(metrics []telegraf.Metric) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
influxdb := &InfluxDB{
|
||||||
|
Timeout: internal.Duration{Duration: time.Second * 5},
|
||||||
|
Downsampler: new(Downsampling),
|
||||||
|
}
|
||||||
|
outputs.Add("influxdb", func() telegraf.Output {
|
||||||
|
return influxdb
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// Downsampling
|
// Downsampling
|
||||||
type Downsampling struct {
|
type Downsampling struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
Metrics []telegraf.Metric
|
Name string
|
||||||
Since time.Time
|
Metrics []telegraf.Metric
|
||||||
TimeRange time.Duration
|
TimeRange time.Duration
|
||||||
|
Aggrations Aggregation
|
||||||
}
|
}
|
||||||
|
|
||||||
type Aggregation struct {
|
type Aggregation map[string]string
|
||||||
Fieldname string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Downsampling) Add(metrics []telegraf.Metric) error {
|
func (d *Downsampling) Add(metrics []telegraf.Metric) error {
|
||||||
d.Lock()
|
d.Lock()
|
||||||
d.Metrics = append(d.Metrics, metrics...)
|
d.Metrics = append(d.Metrics, metrics...)
|
||||||
after := metrics[len(metrics)-1].Time()
|
|
||||||
if d.Since.Sub(after) >= d.TimeRange {
|
|
||||||
d.Aggregate()
|
|
||||||
}
|
|
||||||
d.Unlock()
|
d.Unlock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Downsampling) Run() {
|
func (d *Downsampling) Run() {
|
||||||
for {
|
for {
|
||||||
|
select {
|
||||||
|
case <-time.After(d.TimeRange):
|
||||||
|
aggrData := d.Aggregate()
|
||||||
|
fmt.Printf("%+v\n", aggrData)
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Aggregate calculates the mean value of fields by given time
|
// Aggregate calculates the mean value of fields by given time
|
||||||
func (d *Downsampling) Aggregate(fields ...string) []telegraf.Metric {
|
func (d *Downsampling) Aggregate() []telegraf.Metric {
|
||||||
for _, metric := range d.Metrics {
|
|
||||||
fmt.Printf("%+v\n", metric.Fields())
|
|
||||||
fmt.Printf("%+v\n", metric.Point())
|
|
||||||
fmt.Printf("%+v\n", metric.Time())
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func (d *Downsampling) Mean(fields ...string) (telegraf.Metric, error) {
|
||||||
influxdb := &InfluxDB{
|
var (
|
||||||
Timeout: internal.Duration{Duration: time.Second * 5},
|
aggrMetric telegraf.Metric
|
||||||
DS: new(DS),
|
sums = make(map[string]interface{})
|
||||||
|
size = len(d.Metrics)
|
||||||
|
)
|
||||||
|
|
||||||
|
// initialize sums map
|
||||||
|
for _, field := range fields {
|
||||||
|
sums[field] = 0
|
||||||
}
|
}
|
||||||
outputs.Add("influxdb", func() telegraf.Output {
|
|
||||||
return influxdb
|
d.RLock()
|
||||||
})
|
for _, metric := range d.Metrics {
|
||||||
|
for fieldName, value := range metric.Fields() {
|
||||||
|
oldVal := sums[fieldName]
|
||||||
|
switch value := value.(type) {
|
||||||
|
case int:
|
||||||
|
sums[fieldName] = oldVal.(int) + value
|
||||||
|
case int32:
|
||||||
|
sums[fieldName] = oldVal.(int32) + value
|
||||||
|
case int64:
|
||||||
|
sums[fieldName] = oldVal.(int64) + value
|
||||||
|
case float32:
|
||||||
|
sums[fieldName] = oldVal.(float32) + value
|
||||||
|
case float64:
|
||||||
|
sums[fieldName] = oldVal.(float64) + value
|
||||||
|
default:
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
d.RUnlock()
|
||||||
|
|
||||||
|
for i := range sums {
|
||||||
|
sums[i] = sums[i] / size
|
||||||
|
}
|
||||||
|
|
||||||
|
aggrMetric, err := telegraf.NewMetric(
|
||||||
|
d.Name,
|
||||||
|
map[string]string{},
|
||||||
|
fields,
|
||||||
|
time.Now(),
|
||||||
|
)
|
||||||
|
return aggrMetric, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,6 +41,7 @@ func TestHTTPInflux(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
func TestInfluxDS(t *testing.T) {
|
func TestInfluxDS(t *testing.T) {
|
||||||
downsampler := &DS{
|
downsampler := &DS{
|
||||||
TimeRange: time.Minute,
|
TimeRange: time.Minute,
|
||||||
|
@ -55,3 +56,4 @@ func TestInfluxDS(t *testing.T) {
|
||||||
|
|
||||||
i.DS.Add(testutil.MockMetrics())
|
i.DS.Add(testutil.MockMetrics())
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
Loading…
Reference in New Issue