github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/util/chunkcompat/compat.go (about)

     1  package chunkcompat
     2  
     3  import (
     4  	"bytes"
     5  
     6  	"github.com/prometheus/common/model"
     7  	"github.com/prometheus/prometheus/pkg/labels"
     8  
     9  	"github.com/cortexproject/cortex/pkg/chunk"
    10  	prom_chunk "github.com/cortexproject/cortex/pkg/chunk/encoding"
    11  	"github.com/cortexproject/cortex/pkg/cortexpb"
    12  	"github.com/cortexproject/cortex/pkg/ingester/client"
    13  	"github.com/cortexproject/cortex/pkg/util"
    14  )
    15  
    16  // StreamsToMatrix converts a slice of QueryStreamResponse to a model.Matrix.
    17  func StreamsToMatrix(from, through model.Time, responses []*client.QueryStreamResponse) (model.Matrix, error) {
    18  	result := model.Matrix{}
    19  	for _, response := range responses {
    20  		series, err := SeriesChunksToMatrix(from, through, response.Chunkseries)
    21  		if err != nil {
    22  			return nil, err
    23  		}
    24  
    25  		result = append(result, series...)
    26  	}
    27  	return result, nil
    28  }
    29  
    30  // SeriesChunksToMatrix converts slice of []client.TimeSeriesChunk to a model.Matrix.
    31  func SeriesChunksToMatrix(from, through model.Time, serieses []client.TimeSeriesChunk) (model.Matrix, error) {
    32  	if serieses == nil {
    33  		return nil, nil
    34  	}
    35  
    36  	result := model.Matrix{}
    37  	for _, series := range serieses {
    38  		metric := cortexpb.FromLabelAdaptersToMetric(series.Labels)
    39  		chunks, err := FromChunks("", cortexpb.FromLabelAdaptersToLabels(series.Labels), series.Chunks)
    40  		if err != nil {
    41  			return nil, err
    42  		}
    43  
    44  		samples := []model.SamplePair{}
    45  		for _, chunk := range chunks {
    46  			ss, err := chunk.Samples(from, through)
    47  			if err != nil {
    48  				return nil, err
    49  			}
    50  			samples = util.MergeSampleSets(samples, ss)
    51  		}
    52  
    53  		result = append(result, &model.SampleStream{
    54  			Metric: metric,
    55  			Values: samples,
    56  		})
    57  	}
    58  	return result, nil
    59  }
    60  
    61  // FromChunks converts []client.Chunk to []chunk.Chunk.
    62  func FromChunks(userID string, metric labels.Labels, in []client.Chunk) ([]chunk.Chunk, error) {
    63  	out := make([]chunk.Chunk, 0, len(in))
    64  	for _, i := range in {
    65  		o, err := prom_chunk.NewForEncoding(prom_chunk.Encoding(byte(i.Encoding)))
    66  		if err != nil {
    67  			return nil, err
    68  		}
    69  
    70  		if err := o.UnmarshalFromBuf(i.Data); err != nil {
    71  			return nil, err
    72  		}
    73  
    74  		firstTime, lastTime := model.Time(i.StartTimestampMs), model.Time(i.EndTimestampMs)
    75  		// As the lifetime of this chunk is scopes to this request, we don't need
    76  		// to supply a fingerprint.
    77  		out = append(out, chunk.NewChunk(userID, 0, metric, o, firstTime, lastTime))
    78  	}
    79  	return out, nil
    80  }
    81  
    82  // ToChunks converts []chunk.Chunk to []client.Chunk.
    83  func ToChunks(in []chunk.Chunk) ([]client.Chunk, error) {
    84  	out := make([]client.Chunk, 0, len(in))
    85  	for _, i := range in {
    86  		wireChunk := client.Chunk{
    87  			StartTimestampMs: int64(i.From),
    88  			EndTimestampMs:   int64(i.Through),
    89  			Encoding:         int32(i.Data.Encoding()),
    90  		}
    91  
    92  		buf := bytes.NewBuffer(make([]byte, 0, prom_chunk.ChunkLen))
    93  		if err := i.Data.Marshal(buf); err != nil {
    94  			return nil, err
    95  		}
    96  
    97  		wireChunk.Data = buf.Bytes()
    98  		out = append(out, wireChunk)
    99  	}
   100  	return out, nil
   101  }