func (self *DataCompacter) Filter(group string, seriesCommands []*net.SeriesCommand) []*net.SeriesCommand {
	self.Lock()
	defer self.Unlock()
	output := []*net.SeriesCommand{}

	if _, ok := self.buffer[group]; ok {
		for _, seriesCommand := range seriesCommands {
			if seriesCommand.Timestamp() != nil {
				timestamp := *seriesCommand.Timestamp()
				var newSc *net.SeriesCommand
				for metric, val := range seriesCommand.Metrics() {
					key := getKey(seriesCommand.Entity(), metric, seriesCommand.Tags())
					if _, ok := self.buffer[group][key]; !ok ||
						hasChangedEnough(self.buffer[group][key].Value, val, self.groupParams[group].Threshold) ||
						time.Duration(timestamp-self.buffer[group][key].Time)*time.Millisecond >= self.groupParams[group].Interval ||
						time.Duration(timestamp-self.buffer[group][key].Time)*time.Millisecond < 0 {

						if newSc == nil {
							newSc = net.NewSeriesCommand(seriesCommand.Entity(), metric, val).SetTimestamp(timestamp)
							for name, val := range seriesCommand.Tags() {
								newSc.SetTag(name, val)
							}
						} else {
							newSc.SetMetricValue(metric, val)
						}

						if _, ok := self.buffer[group][key]; !ok ||
							time.Duration(timestamp-self.buffer[group][key].Time)*time.Millisecond > 0 {

							self.buffer[group][key] = sample{Time: timestamp, Value: val}
						}
					}
				}
				if newSc != nil {
					output = append(output, newSc)
				}
			} else {
				output = append(output, seriesCommand)
			}
		}
	} else {
		output = append(output, seriesCommands...)
	}
	return output
}
Esempio n. 2
0
func (self *MemStore) getKey(sc *net.SeriesCommand) string {
	key := sc.Entity()
	metrics := []string{}
	for metricName := range sc.Metrics() {
		metrics = append(metrics, metricName)
	}
	sort.Strings(metrics)
	for i := range metrics {
		key += metrics[i]
	}

	tags := []string{}
	for tagName, tagValue := range sc.Tags() {
		tags = append(tags, tagName+"="+tagValue)
	}
	sort.Strings(tags)
	for i := range tags {
		key += tags[i]
	}

	return key
}