// SPDX-License-Identifier: AGPL-3.0-only
// Provenance-includes-location: https://github.com/cortexproject/cortex/blob/master/pkg/ruler/ruler.proto
// Provenance-includes-license: Apache-2.0
// Provenance-includes-copyright: The Cortex Authors.
// Ruler Service Representation
// This service is used to retrieve the current state of rules running across
// all Rulers in a cluster. It allows mimir to fully serve the `/api/v1/{rules|alerts}`
// Prometheus API
syntax = "proto3";
package ruler;

import "github.com/grafana/mimir/pkg/mimirpb/mimir.proto";
import "github.com/grafana/mimir/pkg/ruler/rulespb/rules.proto";
import "gogoproto/gogo.proto";
import "google/protobuf/duration.proto";
import "google/protobuf/timestamp.proto";

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

service Ruler {
  // Rules returns the currently loaded on the ruler instance for the authenticated tenant.
  rpc Rules(RulesRequest) returns (RulesResponse) {}

  // SyncRules requests a ruler to asynchronously re-synchronize the rules of 1+ tenants.
  rpc SyncRules(SyncRulesRequest) returns (SyncRulesResponse) {}
}

message RulesRequest {
  enum RuleType {
    AnyRule = 0;
    AlertingRule = 1;
    RecordingRule = 2;
  }
  RuleType filter = 1;
  repeated string rule_name = 2;
  repeated string rule_group = 3;
  repeated string file = 4;
  bool exclude_alerts = 5;
  int32 max_groups = 6;
  string next_token = 7;
}

message RulesResponse {
  repeated GroupStateDesc groups = 1;
  repeated string warnings = 2;
}

// SyncRulesRequest is the message sent to request a ruler to re-synchronize the rules of 1+ tenants.
message SyncRulesRequest {
  // The user / tenant IDs for which the rules should be synced.
  repeated string user_ids = 1;
}

// SyncRulesResponse is the message received back for a SyncRulesRequest.
message SyncRulesResponse {}

// GroupStateDesc is a proto representation of a mimir rule group
message GroupStateDesc {
  rules.RuleGroupDesc group = 1;
  repeated RuleStateDesc active_rules = 2;
  google.protobuf.Timestamp evaluationTimestamp = 3 [
    (gogoproto.nullable) = false,
    (gogoproto.stdtime) = true
  ];
  google.protobuf.Duration evaluationDuration = 4 [
    (gogoproto.nullable) = false,
    (gogoproto.stdduration) = true
  ];
}

// RuleStateDesc is a proto representation of a Prometheus Rule
message RuleStateDesc {
  rules.RuleDesc rule = 1;
  string state = 2;
  string health = 3;
  string lastError = 4;
  repeated AlertStateDesc alerts = 5;
  google.protobuf.Timestamp evaluationTimestamp = 6 [
    (gogoproto.nullable) = false,
    (gogoproto.stdtime) = true
  ];
  google.protobuf.Duration evaluationDuration = 7 [
    (gogoproto.nullable) = false,
    (gogoproto.stdduration) = true
  ];
}

message AlertStateDesc {
  string state = 1;
  repeated cortexpb.LabelPair labels = 2 [
    (gogoproto.nullable) = false,
    (gogoproto.customtype) = "github.com/grafana/mimir/pkg/mimirpb.LabelAdapter"
  ];
  repeated cortexpb.LabelPair annotations = 3 [
    (gogoproto.nullable) = false,
    (gogoproto.customtype) = "github.com/grafana/mimir/pkg/mimirpb.LabelAdapter"
  ];
  double value = 4;
  google.protobuf.Timestamp active_at = 5 [
    (gogoproto.nullable) = false,
    (gogoproto.stdtime) = true
  ];
  google.protobuf.Timestamp fired_at = 6 [
    (gogoproto.nullable) = false,
    (gogoproto.stdtime) = true
  ];
  google.protobuf.Timestamp resolved_at = 7 [
    (gogoproto.nullable) = false,
    (gogoproto.stdtime) = true
  ];
  google.protobuf.Timestamp last_sent_at = 8 [
    (gogoproto.nullable) = false,
    (gogoproto.stdtime) = true
  ];
  google.protobuf.Timestamp valid_until = 9 [
    (gogoproto.nullable) = false,
    (gogoproto.stdtime) = true
  ];
  google.protobuf.Timestamp keep_firing_since = 10 [
    (gogoproto.nullable) = false,
    (gogoproto.stdtime) = true
  ];
}
