1// Prometheus API DTOs, possibly to be autogenerated from openapi spec in the near future
2
3import { DataQuery, RelativeTimeRange } from '@grafana/data';
4
5export type Labels = Record<string, string>;
6export type Annotations = Record<string, string>;
7
8export enum PromAlertingRuleState {
9  Firing = 'firing',
10  Inactive = 'inactive',
11  Pending = 'pending',
12}
13
14export enum GrafanaAlertState {
15  Normal = 'Normal',
16  Alerting = 'Alerting',
17  Pending = 'Pending',
18  NoData = 'NoData',
19  Error = 'Error',
20}
21
22export enum PromRuleType {
23  Alerting = 'alerting',
24  Recording = 'recording',
25}
26
27interface PromRuleDTOBase {
28  health: string;
29  name: string;
30  query: string; // expr
31  evaluationTime?: number;
32  lastEvaluation?: string;
33  lastError?: string;
34}
35
36export interface PromAlertingRuleDTO extends PromRuleDTOBase {
37  alerts: Array<{
38    labels: Labels;
39    annotations: Annotations;
40    state: Exclude<PromAlertingRuleState | GrafanaAlertState, PromAlertingRuleState.Inactive>;
41    activeAt: string;
42    value: string;
43  }>;
44  labels: Labels;
45  annotations?: Annotations;
46  duration?: number; // for
47  state: PromAlertingRuleState;
48  type: PromRuleType.Alerting;
49}
50
51export interface PromRecordingRuleDTO extends PromRuleDTOBase {
52  health: string;
53  name: string;
54  query: string; // expr
55  type: PromRuleType.Recording;
56  labels?: Labels;
57}
58
59export type PromRuleDTO = PromAlertingRuleDTO | PromRecordingRuleDTO;
60
61export interface PromRuleGroupDTO {
62  name: string;
63  file: string;
64  rules: PromRuleDTO[];
65  interval: number;
66
67  evaluationTime?: number; // these 2 are not in older prometheus payloads
68  lastEvaluation?: string;
69}
70
71export interface PromResponse<T> {
72  status: 'success' | 'error' | ''; // mocks return empty string
73  data: T;
74  errorType?: string;
75  error?: string;
76  warnings?: string[];
77}
78
79export type PromRulesResponse = PromResponse<{ groups: PromRuleGroupDTO[] }>;
80
81// Ruler rule DTOs
82interface RulerRuleBaseDTO {
83  expr: string;
84  labels?: Labels;
85}
86
87export interface RulerRecordingRuleDTO extends RulerRuleBaseDTO {
88  record: string;
89}
90
91export interface RulerAlertingRuleDTO extends RulerRuleBaseDTO {
92  alert: string;
93  for?: string;
94  annotations?: Annotations;
95}
96
97export enum GrafanaAlertStateDecision {
98  Alerting = 'Alerting',
99  NoData = 'NoData',
100  KeepLastState = 'KeepLastState',
101  OK = 'OK',
102  Error = 'Error',
103}
104
105export interface AlertDataQuery extends DataQuery {
106  maxDataPoints?: number;
107  intervalMs?: number;
108}
109
110export interface AlertQuery {
111  refId: string;
112  queryType: string;
113  relativeTimeRange?: RelativeTimeRange;
114  datasourceUid: string;
115  model: AlertDataQuery;
116}
117
118export interface PostableGrafanaRuleDefinition {
119  uid?: string;
120  title: string;
121  condition: string;
122  no_data_state: GrafanaAlertStateDecision;
123  exec_err_state: GrafanaAlertStateDecision;
124  data: AlertQuery[];
125}
126export interface GrafanaRuleDefinition extends PostableGrafanaRuleDefinition {
127  id?: string;
128  uid: string;
129  namespace_uid: string;
130  namespace_id: number;
131}
132
133export interface RulerGrafanaRuleDTO {
134  grafana_alert: GrafanaRuleDefinition;
135  for: string;
136  annotations: Annotations;
137  labels: Labels;
138}
139
140export interface PostableRuleGrafanaRuleDTO {
141  grafana_alert: PostableGrafanaRuleDefinition;
142  for: string;
143  annotations: Annotations;
144  labels: Labels;
145}
146
147export type RulerRuleDTO = RulerAlertingRuleDTO | RulerRecordingRuleDTO | RulerGrafanaRuleDTO;
148
149export type PostableRuleDTO = RulerAlertingRuleDTO | RulerRecordingRuleDTO | PostableRuleGrafanaRuleDTO;
150
151export type RulerRuleGroupDTO<R = RulerRuleDTO> = {
152  name: string;
153  interval?: string;
154  rules: R[];
155};
156
157export type PostableRulerRuleGroupDTO = RulerRuleGroupDTO<PostableRuleDTO>;
158
159export type RulerRulesConfigDTO = { [namespace: string]: RulerRuleGroupDTO[] };
160