Esempio n. 1
0
func (self *PercentileAggregator) AggregatePoint(series string, group interface{}, p *protocol.Point) error {
	v, err := datastore.GetValue(self.value, self.columns, p)
	if err != nil {
		return err
	}

	value := 0.0
	if v.Int64Value != nil {
		value = float64(*v.Int64Value)
	} else if v.DoubleValue != nil {
		value = *v.DoubleValue
	} else {
		return nil
	}

	values := self.float_values[series]
	if values == nil {
		values = map[interface{}][]float64{}
		self.float_values[series] = values
	}

	values[group] = append(values[group], value)

	return nil
}
Esempio n. 2
0
func (self *ModeAggregator) AggregatePoint(series string, group interface{}, p *protocol.Point) error {
	seriesCounts := self.counts[series]
	if seriesCounts == nil {
		seriesCounts = make(map[interface{}]map[float64]int)
		self.counts[series] = seriesCounts
	}

	groupCounts := seriesCounts[group]
	if groupCounts == nil {
		groupCounts = make(map[float64]int)
	}

	point, err := datastore.GetValue(self.value, self.columns, p)
	if err != nil {
		return err
	}

	var value float64
	if point.Int64Value != nil {
		value = float64(*point.Int64Value)
	} else if point.DoubleValue != nil {
		value = *point.DoubleValue
	} else {
		return nil
	}

	count := groupCounts[value]
	count += 1
	groupCounts[value] = count
	seriesCounts[group] = groupCounts

	return nil
}
Esempio n. 3
0
func (self *MeanAggregator) AggregatePoint(series string, group interface{}, p *protocol.Point) error {
	means := self.means[series]
	counts := self.counts[series]

	if means == nil && counts == nil {
		means = make(map[interface{}]float64)
		self.means[series] = means

		counts = make(map[interface{}]int)
		self.counts[series] = counts
	}

	currentMean := means[group]
	currentCount := counts[group] + 1

	fieldValue, err := datastore.GetValue(self.value, self.columns, p)
	if err != nil {
		return err
	}

	var value float64
	if ptr := fieldValue.Int64Value; ptr != nil {
		value = float64(*ptr)
	} else if ptr := fieldValue.DoubleValue; ptr != nil {
		value = *ptr
	}

	currentMean = currentMean*float64(currentCount-1)/float64(currentCount) + value/float64(currentCount)

	means[group] = currentMean
	counts[group] = currentCount
	return nil
}
Esempio n. 4
0
func (self *HistogramAggregator) AggregatePoint(series string, group interface{}, p *protocol.Point) error {
	groups := self.histograms[series]
	if groups == nil {
		groups = make(map[interface{}]map[int]int)
		self.histograms[series] = groups
	}

	buckets := groups[group]
	if buckets == nil {
		buckets = make(map[int]int)
		groups[group] = buckets
	}

	fieldValue, err := datastore.GetValue(self.value, self.columns, p)
	if err != nil {
		return err
	}

	var value float64
	if ptr := fieldValue.Int64Value; ptr != nil {
		value = float64(*ptr)
	} else if ptr := fieldValue.DoubleValue; ptr != nil {
		value = *ptr
	}

	bucket := int(value / self.bucketSize)
	buckets[bucket] += 1

	return nil
}
Esempio n. 5
0
func (self *StandardDeviationAggregator) AggregatePoint(series string, group interface{}, p *protocol.Point) error {
	fieldValue, err := datastore.GetValue(self.value, self.columns, p)
	if err != nil {
		return err
	}

	var value float64
	if ptr := fieldValue.Int64Value; ptr != nil {
		value = float64(*ptr)
	} else if ptr := fieldValue.DoubleValue; ptr != nil {
		value = *ptr
	} else {
		// else ignore this point
		return nil
	}

	running := self.running[series]
	if running == nil {
		running = make(map[interface{}]*StandardDeviationRunning)
		self.running[series] = running
	}

	r := running[group]
	if r == nil {
		r = &StandardDeviationRunning{}
		running[group] = r
	}

	r.count++
	r.totalX += value
	r.totalX2 += value * value
	return nil
}
Esempio n. 6
0
func (self *QueryEngine) executeArithmeticQuery(user common.User, database string, query *parser.SelectQuery,
	yield func(*protocol.Series) error) error {

	names := map[string]*parser.Value{}
	for idx, v := range query.GetColumnNames() {
		switch v.Type {
		case parser.ValueSimpleName:
			names[v.Name] = v
		case parser.ValueFunctionCall:
			names[v.Name] = v
		case parser.ValueExpression:
			names["expr"+strconv.Itoa(idx)] = v
		}
	}

	return self.distributeQuery(user, database, query, func(series *protocol.Series) error {
		if len(series.Points) == 0 {
			yield(series)
			return nil
		}

		newSeries := &protocol.Series{
			Name: series.Name,
		}

		// create the new column names
		for name, _ := range names {
			newSeries.Fields = append(newSeries.Fields, name)
		}

		for _, point := range series.Points {
			newPoint := &protocol.Point{
				Timestamp:      point.Timestamp,
				SequenceNumber: point.SequenceNumber,
			}
			for _, field := range newSeries.Fields {
				value := names[field]
				v, err := datastore.GetValue(value, series.Fields, point)
				if err != nil {
					return err
				}
				newPoint.Values = append(newPoint.Values, v)
			}
			newSeries.Points = append(newSeries.Points, newPoint)
		}

		yield(newSeries)

		return nil
	})
}
Esempio n. 7
0
func (self *FirstOrLastAggregator) AggregatePoint(series string, group interface{}, p *protocol.Point) error {
	values := self.values[series]
	if values == nil {
		values = make(map[interface{}]*protocol.FieldValue)
		self.values[series] = values
	}
	if values[group] == nil || !self.isFirst {
		value, err := datastore.GetValue(self.value, self.columns, p)
		if err != nil {
			return err
		}

		values[group] = value
	}
	return nil
}
Esempio n. 8
0
func (self *CumulativeArithmeticAggregator) AggregatePoint(series string, group interface{}, p *protocol.Point) error {
	values := self.values[series]
	if values == nil {
		values = make(map[interface{}]float64)
		self.values[series] = values
	}
	currentValue, ok := values[group]
	if !ok {
		currentValue = self.initialValue
	}
	value, err := datastore.GetValue(self.value, self.columns, p)
	if err != nil {
		return err
	}
	values[group] = self.operation(currentValue, value)
	return nil
}
Esempio n. 9
0
func (self *DerivativeAggregator) AggregatePoint(series string, group interface{}, p *protocol.Point) error {
	fieldValue, err := datastore.GetValue(self.value, self.columns, p)
	if err != nil {
		return err
	}

	var value float64
	if ptr := fieldValue.Int64Value; ptr != nil {
		value = float64(*ptr)
	} else if ptr := fieldValue.DoubleValue; ptr != nil {
		value = *ptr
	} else {
		// else ignore this point
		return nil
	}

	newValue := &protocol.Point{
		Timestamp: p.Timestamp,
		Values:    []*protocol.FieldValue{&protocol.FieldValue{DoubleValue: &value}},
	}

	firstValues := self.firstValues[series]
	if firstValues == nil {
		firstValues = make(map[interface{}]*protocol.Point)
		self.firstValues[series] = firstValues
	}

	if _, ok := firstValues[group]; !ok {
		firstValues[group] = newValue
		return nil
	}

	lastValues := self.lastValues[series]
	if lastValues == nil {
		lastValues = make(map[interface{}]*protocol.Point)
		self.lastValues[series] = lastValues
	}

	lastValues[group] = newValue
	return nil
}