github.com/thanos-io/thanos@v0.32.5/internal/cortex/querier/queryrange/compat.go (about)

     1  // Copyright (c) The Cortex Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package queryrange
     5  
     6  import (
     7  	"github.com/prometheus/common/model"
     8  )
     9  
    10  // The following code will allow us to use JSON marshal and unmarshal functions from the Prometheus common package in
    11  // query_range.go: https://github.com/prometheus/common/blob/846591a166358c7048ef197e84501ca688dda920/model/value.go
    12  // Please see the link above for more details on Sample, SampleStream, HistogramPair and SampleHistogramPair.
    13  
    14  func toModelSampleHistogramPair(s SampleHistogramPair) model.SampleHistogramPair {
    15  	return model.SampleHistogramPair{
    16  		Timestamp: model.Time(s.Timestamp),
    17  		Histogram: toModelSampleHistogram(s.Histogram),
    18  	}
    19  }
    20  
    21  func fromModelSampleHistogramPair(modelSampleHistogram model.SampleHistogramPair) (s SampleHistogramPair) {
    22  	return SampleHistogramPair{
    23  		Timestamp: int64(modelSampleHistogram.Timestamp),
    24  		Histogram: fromModelSampleHistogram(modelSampleHistogram.Histogram),
    25  	}
    26  }
    27  
    28  func fromModelSampleHistogram(modelSampleHistogram *model.SampleHistogram) (s SampleHistogram) {
    29  	buckets := make([]*HistogramBucket, len(modelSampleHistogram.Buckets))
    30  
    31  	for i, b := range modelSampleHistogram.Buckets {
    32  		buckets[i] = &HistogramBucket{
    33  			Lower:      float64(b.Lower),
    34  			Upper:      float64(b.Upper),
    35  			Count:      float64(b.Count),
    36  			Boundaries: b.Boundaries,
    37  		}
    38  	}
    39  
    40  	return SampleHistogram{
    41  		Count:   float64(modelSampleHistogram.Count),
    42  		Sum:     float64(modelSampleHistogram.Sum),
    43  		Buckets: buckets,
    44  	}
    45  }
    46  
    47  func toModelSampleHistogram(s SampleHistogram) *model.SampleHistogram {
    48  	modelBuckets := make([]*model.HistogramBucket, len(s.Buckets))
    49  
    50  	for i, b := range s.Buckets {
    51  		modelBuckets[i] = &model.HistogramBucket{
    52  			Lower:      model.FloatString(b.Lower),
    53  			Upper:      model.FloatString(b.Upper),
    54  			Count:      model.FloatString(b.Count),
    55  			Boundaries: b.Boundaries,
    56  		}
    57  	}
    58  
    59  	return &model.SampleHistogram{
    60  		Count:   model.FloatString(s.Count),
    61  		Sum:     model.FloatString(s.Sum),
    62  		Buckets: modelBuckets,
    63  	}
    64  }