Esempio n. 1
0
func TestMissingData(t *testing.T) {
	list := metrics.SimpleList{
		{
			Path: "Missing",
		},
	}
	if err := metrics.VerifyList(list); err == nil {
		t.Error("Expected error: missing value.")
	}
}
Esempio n. 2
0
func TestBadData(t *testing.T) {
	list := metrics.SimpleList{
		{
			Path:  "Bad",
			Value: 35, // plain int. Not accepted.
		},
	}
	if err := metrics.VerifyList(list); err == nil {
		t.Error("Expected error: bad value.")
	}
}
Esempio n. 3
0
func TestOk(t *testing.T) {
	list := metrics.SimpleList{
		{
			Path:  "Ok",
			Value: int64(10),
		},
	}
	if err := metrics.VerifyList(list); err != nil {
		t.Error("Expected ok. Got error")
	}
}
Esempio n. 4
0
func TestDuplicateData(t *testing.T) {
	list := metrics.SimpleList{
		{
			Path:  "Duplicate",
			Value: int64(35),
		},
		{
			Path:  "Duplicate",
			Value: int64(36),
		},
	}
	if err := metrics.VerifyList(list); err == nil {
		t.Error("Expected error: duplicate value.")
	}
}
Esempio n. 5
0
func TestSomeTimeStampsMissing(t *testing.T) {
	list := metrics.SimpleList{
		{
			Path:  "Missing timestamp",
			Value: int64(35),
		},
		{
			Path:      "Present timestamp",
			Value:     int64(36),
			TimeStamp: kNow,
		},
	}
	if err := metrics.VerifyList(list); err != nil {
		t.Error("Expected no error: Should tolerate some timestamps missing")
	}
}
Esempio n. 6
0
func TestDifferentTimeStampsSameGroup(t *testing.T) {
	list := metrics.SimpleList{
		{
			Path:      "One timestamp",
			Value:     int64(35),
			TimeStamp: kNow,
			GroupId:   0,
		},
		{
			Path:      "Another timestamp",
			Value:     int64(36),
			TimeStamp: kNow.Add(time.Minute),
			GroupId:   0,
		},
	}
	if err := metrics.VerifyList(list); err == nil {
		t.Error("Expected error: different timestamps same group.")
	}
}
Esempio n. 7
0
func TestDiffTimeStampDiffGroupOk(t *testing.T) {
	list := metrics.SimpleList{
		{
			Path:      "One timestamp",
			Value:     int64(35),
			TimeStamp: kNow,
			GroupId:   0,
		},
		{
			Path:      "Another timestamp",
			Value:     int64(36),
			TimeStamp: kNow.Add(time.Minute),
			GroupId:   1,
		},
	}
	if err := metrics.VerifyList(list); err != nil {
		t.Error("Expected no error for different timestamps and groups.")
	}
}
Esempio n. 8
0
func TestSameTimeStampDiffGroupOk(t *testing.T) {
	list := metrics.SimpleList{
		{
			Path:      "One timestamp",
			Value:     int64(35),
			TimeStamp: kNow,
			GroupId:   0,
		},
		{
			Path:      "Another timestamp",
			Value:     int64(36),
			TimeStamp: kNow,
			GroupId:   3,
		},
	}
	if err := metrics.VerifyList(list); err != nil {
		t.Error("Expected no error for same timestamps different groups.")
	}
}
Esempio n. 9
0
// LookupBatch looks up all the metrics in one go and returns the
// following:
// fetched: timeSeries already in this collection keyed by Metric.
//  values must be added to these manually.
// newOnes: timeSeries just added as a result of this lookup. Since these
// are new, the first value added automatically.
// notFetched: timeSeries in this collection but not fetched. These
// are the time series that should be marked inactive.
// ok is true if metrics can be added to this instance or false if this
// instance is inactive and closed for new metrics.
func (c *timeSeriesCollectionType) LookupBatch(
	timestamp float64, mlist metrics.List) (
	fetched map[*timeSeriesType]interface{},
	newOnes, notFetched []*timeSeriesType,
	fetchedTimeStamps map[*timestampSeriesType]float64,
	newTs []*timestampSeriesType,
	notFetchedTimeStamps []*timestampSeriesType,
	err error) {
	if err = metrics.VerifyList(mlist); err != nil {
		return
	}
	c.lock.Lock()
	defer c.lock.Unlock()
	if !c.active {
		err = ErrInactive
		return
	}
	valueByMetric := make(map[*MetricInfo]interface{})
	timestampByGroupId := make(map[int]float64)
	groupIds := make(map[int]bool)
	fetched = make(map[*timeSeriesType]interface{})
	fetchedTimeStamps = make(map[*timestampSeriesType]float64)
	mlen := mlist.Len()
	for i := 0; i < mlen; i++ {
		var avalue metrics.Value
		mlist.Index(i, &avalue)
		kind, subType := types.FromGoValueWithSubType(avalue.Value)
		id := c.metricInfoStore.Register(&avalue, kind, subType)
		if kind == types.Dist {
			distributionRollOvers := c.distributionRollOversByPath[avalue.Path]
			if distributionRollOvers == nil {
				distributionRollOvers = &distributionRollOverType{}
				c.distributionRollOversByPath[avalue.Path] = distributionRollOvers
			}
			distribution := avalue.Value.(*messages.Distribution)
			rollOverCount := distributionRollOvers.UpdateAndFetchRollOverCount(
				id.Ranges(), distribution.Generation)
			currentDistributionTotals := &DistributionTotals{
				Counts:        distExtractCounts(distribution),
				Sum:           distribution.Sum,
				RollOverCount: rollOverCount,
			}
			valueByMetric[id] = currentDistributionTotals
		} else {
			valueByMetric[id] = avalue.Value
		}
		groupIds[id.GroupId()] = true
		if !avalue.TimeStamp.IsZero() {
			timestampByGroupId[id.GroupId()] = duration.TimeToFloat(avalue.TimeStamp)
		}
	}
	// If a group ID is missing a timestamp, give it the
	// timestamp from scotty.
	for groupId := range groupIds {
		if _, ok := timestampByGroupId[groupId]; !ok {
			timestampByGroupId[groupId] = timestamp
		}
	}
	// populate notFetched
	for id, series := range c.timeSeries {
		if _, ok := valueByMetric[id]; !ok {
			notFetched = append(notFetched, series)
		}
	}
	// populate fetched and newOnes
	for id, value := range valueByMetric {
		if c.timeSeries[id] == nil {
			thisTs := timestampByGroupId[id.GroupId()]
			c.timeSeries[id] = newTimeSeriesType(
				id, thisTs, value, c.metrics)
			newOnes = append(newOnes, c.timeSeries[id])
		} else {
			fetched[c.timeSeries[id]] = value
		}
	}
	// populate notFetchedTimeStamps
	for groupId, series := range c.timestampSeries {
		if _, ok := timestampByGroupId[groupId]; !ok {
			notFetchedTimeStamps = append(
				notFetchedTimeStamps, series)
		}
	}
	// populate fetchedTimeStamps and newTs
	for groupId, ts := range timestampByGroupId {
		if c.timestampSeries[groupId] == nil {
			c.timestampSeries[groupId] = newTimeStampSeriesType(
				groupId, ts, c.metrics)
			newTs = append(newTs, c.timestampSeries[groupId])
		} else {
			fetchedTimeStamps[c.timestampSeries[groupId]] = ts
		}
	}
	return
}