Merge pull request #170 from prometheus/refactor/readability/business-logic-types

Extract dto.SampleValueSeries into model.Values.
This commit is contained in:
Matt T. Proud 2013-04-22 04:38:15 -07:00
commit 8a17ee843d
4 changed files with 37 additions and 34 deletions

View file

@ -17,6 +17,7 @@ import (
"bytes"
"code.google.com/p/goprotobuf/proto"
"fmt"
dto "github.com/prometheus/prometheus/model/generated"
"sort"
"time"
)
@ -135,6 +136,17 @@ func (v Values) InsideInterval(t time.Time) (s bool) {
return true
}
func NewValuesFromDTO(dto *dto.SampleValueSeries) (v Values) {
for _, value := range dto.Value {
v = append(v, SamplePair{
Timestamp: time.Unix(*value.Timestamp, 0),
Value: SampleValue(*value.Value),
})
}
return
}
type SampleSet struct {
Metric Metric
Values Values

View file

@ -656,9 +656,14 @@ func extractSampleKey(i leveldb.Iterator) (key model.SampleKey, err error) {
return
}
func extractSampleValues(i leveldb.Iterator) (v *dto.SampleValueSeries, err error) {
v = &dto.SampleValueSeries{}
func extractSampleValues(i leveldb.Iterator) (values model.Values, err error) {
v := &dto.SampleValueSeries{}
err = proto.Unmarshal(i.Value(), v)
if err != nil {
return
}
values = model.NewValuesFromDTO(v)
return
}

View file

@ -216,17 +216,11 @@ func levelDBGetRangeValues(l *LevelDBMetricPersistence, fp model.Fingerprint, i
break
}
retrievedValue, err := extractSampleValues(iterator)
retrievedValues, err := extractSampleValues(iterator)
if err != nil {
return nil, err
}
for _, value := range retrievedValue.Value {
samples = append(samples, model.SamplePair{
Value: model.SampleValue(*value.Value),
Timestamp: time.Unix(*value.Timestamp, 0),
})
}
samples = append(samples, retrievedValues...)
}
return
@ -463,15 +457,15 @@ func StochasticTests(persistenceMaker func() (MetricPersistence, test.Closer), t
samples, err = levelDBGetRangeValues(persistence, fp, interval)
if err != nil {
t.Fatal(err)
return
}
if len(samples) < 2 {
t.Fatalf("expected sample count greater than %d, got %d", 2, len(samples))
}
default:
samples = p.GetRangeValues(fp, interval)
}
if len(samples) < 2 {
t.Errorf("expected sample count less than %d, got %d", 2, len(samples))
return
if len(samples) < 2 {
t.Fatalf("expected sample count greater than %d, got %d", 2, len(samples))
}
}
}
}

View file

@ -472,8 +472,8 @@ func (t *tieredStorage) loadChunkAroundTime(iterator leveldb.Iterator, frontier
targetKey = &dto.SampleKey{
Fingerprint: fingerprint.ToDTO(),
}
foundKey model.SampleKey
foundValue *dto.SampleValueSeries
foundKey model.SampleKey
foundValues model.Values
)
// Limit the target key to be within the series' keyspace.
@ -510,9 +510,9 @@ func (t *tieredStorage) loadChunkAroundTime(iterator leveldb.Iterator, frontier
rewound = true
}
foundValue, err = extractSampleValues(iterator)
foundValues, err = extractSampleValues(iterator)
if err != nil {
panic(err)
return
}
// If we rewound, but the target time is still past the current block, return
@ -520,33 +520,25 @@ func (t *tieredStorage) loadChunkAroundTime(iterator leveldb.Iterator, frontier
if rewound {
foundKey, err = extractSampleKey(iterator)
if err != nil {
panic(err)
return
}
currentChunkLastTime := foundKey.LastTimestamp
if ts.After(currentChunkLastTime) {
sampleCount := len(foundValue.Value)
chunk = append(chunk, model.SamplePair{
Timestamp: time.Unix(*foundValue.Value[sampleCount-1].Timestamp, 0),
Value: model.SampleValue(*foundValue.Value[sampleCount-1].Value),
})
sampleCount := len(foundValues)
chunk = append(chunk, foundValues[sampleCount-1])
// We know there's a next block since we have rewound from it.
iterator.Next()
foundValue, err = extractSampleValues(iterator)
foundValues, err = extractSampleValues(iterator)
if err != nil {
panic(err)
return
}
}
}
// Now append all the samples of the currently seeked block to the output.
for _, sample := range foundValue.Value {
chunk = append(chunk, model.SamplePair{
Timestamp: time.Unix(*sample.Timestamp, 0),
Value: model.SampleValue(*sample.Value),
})
}
chunk = append(chunk, foundValues...)
return
}