From 06162180ad5fec9c940e83541af0aa35df4f524b Mon Sep 17 00:00:00 2001 From: Julius Volz Date: Fri, 11 Jan 2013 01:19:27 +0100 Subject: [PATCH] Implement matrix range and boundary fetching from metrics store. --- rules/ast/persistence_bridge.go | 59 ++++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/rules/ast/persistence_bridge.go b/rules/ast/persistence_bridge.go index 90a558d95..e4efe4c53 100644 --- a/rules/ast/persistence_bridge.go +++ b/rules/ast/persistence_bridge.go @@ -26,17 +26,29 @@ type PersistenceBridge struct { // AST-global persistence to use. var persistence *PersistenceBridge = nil -func (p *PersistenceBridge) GetValueAtTime(labels model.LabelSet, timestamp *time.Time, stalenessPolicy *metric.StalenessPolicy) ([]*model.Sample, error) { +func (p *PersistenceBridge) getMetricsWithLabels(labels model.LabelSet) ([]*model.Metric, error) { fingerprints, err := p.persistence.GetFingerprintsForLabelSet(&labels) if err != nil { return nil, err } - samples := []*model.Sample{} + metrics := []*model.Metric{} for _, fingerprint := range fingerprints { metric, err := p.persistence.GetMetricForFingerprint(fingerprint) if err != nil { return nil, err } + metrics = append(metrics, metric) + } + return metrics, nil +} + +func (p *PersistenceBridge) GetValueAtTime(labels model.LabelSet, timestamp *time.Time, stalenessPolicy *metric.StalenessPolicy) ([]*model.Sample, error) { + metrics, err := p.getMetricsWithLabels(labels) + if err != nil { + return nil, err + } + samples := []*model.Sample{} + for _, metric := range metrics { sample, err := p.persistence.GetValueAtTime(metric, timestamp, stalenessPolicy) if err != nil { return nil, err @@ -50,11 +62,50 @@ func (p *PersistenceBridge) GetValueAtTime(labels model.LabelSet, timestamp *tim } func (p *PersistenceBridge) GetBoundaryValues(labels model.LabelSet, interval *model.Interval, stalenessPolicy *metric.StalenessPolicy) ([]*model.SampleSet, error) { - return []*model.SampleSet{}, nil // TODO real values + metrics, err := p.getMetricsWithLabels(labels) + if err != nil { + return nil, err + } + + sampleSets := []*model.SampleSet{} + for _, metric := range metrics { + // TODO: change to GetBoundaryValues() once it has the right return type. + sampleSet, err := p.persistence.GetRangeValues(metric, interval, stalenessPolicy) + if err != nil { + return nil, err + } + if sampleSet == nil { + continue + } + + // TODO remove when persistence return value is fixed. + sampleSet.Metric = *metric + sampleSets = append(sampleSets, sampleSet) + } + return sampleSets, nil } func (p *PersistenceBridge) GetRangeValues(labels model.LabelSet, interval *model.Interval, stalenessPolicy *metric.StalenessPolicy) ([]*model.SampleSet, error) { - return []*model.SampleSet{}, nil // TODO real values + metrics, err := p.getMetricsWithLabels(labels) + if err != nil { + return nil, err + } + + sampleSets := []*model.SampleSet{} + for _, metric := range metrics { + sampleSet, err := p.persistence.GetRangeValues(metric, interval, stalenessPolicy) + if err != nil { + return nil, err + } + if sampleSet == nil { + continue + } + + // TODO remove when persistence return value is fixed. + sampleSet.Metric = *metric + sampleSets = append(sampleSets, sampleSet) + } + return sampleSets, nil } func SetPersistence(p metric.MetricPersistence) {