// SPDX-License-Identifier: AGPL-3.0-only
// Provenance-includes-location: https://github.com/cortexproject/cortex/blob/master/pkg/querier/queryrange/queryrange.proto
// Provenance-includes-license: Apache-2.0
// Provenance-includes-copyright: The Cortex Authors.

syntax = "proto3";

// Uses queryrange package to offer backwards compatibility with cached extents.
package queryrange;

option go_package = "querymiddleware";

import "github.com/gogo/protobuf/gogoproto/gogo.proto";
import "google/protobuf/duration.proto";
import "github.com/grafana/mimir/pkg/mimirpb/mimir.proto";
import "google/protobuf/any.proto";

option (gogoproto.marshaler_all) = true;
option (gogoproto.unmarshaler_all) = true;

message PrometheusRangeQueryRequest {
  string path = 1;
  int64 start = 2;
  int64 end = 3;
  int64 step = 4;
  google.protobuf.Duration timeout = 5 [(gogoproto.stdduration) = true, (gogoproto.nullable) = false];
  string query = 6;
  Options options = 7 [(gogoproto.nullable) = false];

  // ID of the request used by splitAndCacheMiddleware to correlate downstream requests and responses.
  int64 id = 8;

  // Hints that could be optionally attached to the request to pass down the stack.
  // These hints can be used to optimize the query execution.
  Hints hints = 9 [(gogoproto.nullable) = true];
}

message PrometheusInstantQueryRequest {
  string path = 1;
  int64 time = 2;
  string query = 3;
  Options options = 4 [(gogoproto.nullable) = false];

  // ID of the request used by splitAndCacheMiddleware to correlate downstream requests and responses.
  int64 id = 5;

  // Hints that could be optionally attached to the request to pass down the stack.
  // These hints can be used to optimize the query execution.
  Hints hints = 6 [(gogoproto.nullable) = true];
}

message PrometheusResponseHeader {
  string Name = 1 [(gogoproto.jsontag) = "-"];
  repeated string Values = 2 [(gogoproto.jsontag) = "-"];
}

message PrometheusResponse {
  string Status = 1 [(gogoproto.jsontag) = "status"];
  PrometheusData Data = 2 [(gogoproto.jsontag) = "data,omitempty"];
  string ErrorType = 3 [(gogoproto.jsontag) = "errorType,omitempty"];
  string Error = 4 [(gogoproto.jsontag) = "error,omitempty"];
  repeated PrometheusResponseHeader Headers = 5 [(gogoproto.jsontag) = "-"];
}

message PrometheusData {
  string ResultType = 1 [(gogoproto.jsontag) = "resultType"];
  repeated SampleStream Result = 2 [(gogoproto.nullable) = false, (gogoproto.jsontag) = "result"];
}

message SampleStream {
  repeated cortexpb.LabelPair labels = 1 [(gogoproto.nullable) = false, (gogoproto.jsontag) = "metric", (gogoproto.customtype) = "github.com/grafana/mimir/pkg/mimirpb.LabelAdapter"];
  repeated cortexpb.Sample samples = 2 [(gogoproto.nullable) = false, (gogoproto.jsontag) = "values"];
}

message CachedResponse  {
	string key = 1 [(gogoproto.jsontag) = "key"];

	// List of cached responses; non-overlapping and in order.
	repeated Extent extents = 2 [(gogoproto.nullable) = false, (gogoproto.jsontag) = "extents"];
}

message Extent  {
	int64 start = 1 [(gogoproto.jsontag) = "start"];
  int64 end = 2 [(gogoproto.jsontag) = "end"];
  // reserved the previous key to ensure cache transition
  reserved 3;
  string trace_id = 4 [(gogoproto.jsontag) = "-"];
  google.protobuf.Any response = 5 [(gogoproto.jsontag) = "response"];
}

message Options {
  bool CacheDisabled = 1;
  bool ShardingDisabled = 2;
  int32 TotalShards = 3;
  bool InstantSplitDisabled = 4;
  // Instant split by time interval unit stored in nanoseconds (time.Duration unit in int64)
  int64 InstantSplitInterval = 5;
}

message Hints {
  // Total number of queries that are expected to to be executed to serve the original request.
  int32 TotalQueries = 1;
}
