1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
use crate::error::Error;
use serde::Deserialize;
use std::fmt;

/// A helper type that provides label matching logic for e.g. aggregations like `sum`.<br>
///
/// See the [Prometheus reference](https://prometheus.io/docs/prometheus/latest/querying/operators/) for details.
#[derive(Debug)]
pub enum Aggregate<'a> {
    By(&'a [&'a str]),
    Without(&'a [&'a str]),
}

impl<'a> fmt::Display for Aggregate<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Aggregate::By(list) => write!(f, "by ({})", list.join(",")),
            Aggregate::Without(list) => write!(f, "without ({})", list.join(",")),
        }
    }
}

/// A helper type that provides label matching logic for e.g. binary operations (between instant vectors).<br>
///
/// See the [Prometheus reference](https://prometheus.io/docs/prometheus/latest/querying/operators/) for details.
#[derive(Debug)]
pub enum Match<'a> {
    On(&'a [&'a str]),
    Ignoring(&'a [&'a str]),
}

impl<'a> fmt::Display for Match<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Match::On(list) => write!(f, "on ({})", list.join(",")),
            Match::Ignoring(list) => write!(f, "ignoring ({})", list.join(",")),
        }
    }
}

/// A helper type that provides grouping logic for e.g. vector matching.<br>
///
/// See the [Prometheus reference](https://prometheus.io/docs/prometheus/latest/querying/operators/) for details.
#[derive(Debug)]
pub enum Group<'a> {
    Left(&'a [&'a str]),
    Right(&'a [&'a str]),
}

impl<'a> fmt::Display for Group<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Group::Left(list) => write!(f, "group_left ({})", list.join(",")),
            Group::Right(list) => write!(f, "group_right ({})", list.join(",")),
        }
    }
}

/// A helper type to filter targets by state.
#[derive(Debug)]
pub enum TargetState {
    Active,
    Dropped,
    Any,
}

impl fmt::Display for TargetState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            TargetState::Active => write!(f, "active"),
            TargetState::Dropped => write!(f, "dropped"),
            TargetState::Any => write!(f, "any"),
        }
    }
}

/// A helper type to represent possible target health states.
#[derive(Debug, Copy, Clone, Deserialize)]
pub enum TargetHealth {
    #[serde(alias = "up")]
    Up,
    #[serde(alias = "down")]
    Down,
    #[serde(alias = "unknown")]
    Unknown,
}

impl fmt::Display for TargetHealth {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            TargetHealth::Up => write!(f, "up"),
            TargetHealth::Down => write!(f, "down"),
            TargetHealth::Unknown => write!(f, "unknown"),
        }
    }
}

/// A helper type to represent possible rule health states.
#[derive(Debug, Copy, Clone, Deserialize)]
pub enum RuleHealth {
    #[serde(alias = "ok")]
    Good,
    #[serde(alias = "err")]
    Bad,
    #[serde(alias = "unknown")]
    Unknown,
}

impl fmt::Display for RuleHealth {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            RuleHealth::Good => write!(f, "ok"),
            RuleHealth::Bad => write!(f, "err"),
            RuleHealth::Unknown => write!(f, "unknown"),
        }
    }
}

/// A helper type to represent possible rule health states.
#[derive(Debug, Copy, Clone, Deserialize)]
pub enum AlertState {
    #[serde(alias = "inactive")]
    Inactive,
    #[serde(alias = "pending")]
    Pending,
    #[serde(alias = "firing")]
    Firing,
}

impl fmt::Display for AlertState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            AlertState::Inactive => write!(f, "inactive"),
            AlertState::Pending => write!(f, "pending"),
            AlertState::Firing => write!(f, "firing"),
        }
    }
}

/// A helper type to filter rules by type.
#[derive(Debug)]
pub enum RuleType {
    Alert,
    Record,
}

impl fmt::Display for RuleType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            RuleType::Alert => write!(f, "alert"),
            RuleType::Record => write!(f, "record"),
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub(crate) enum Label<'c> {
    With((&'c str, &'c str)),
    Without((&'c str, &'c str)),
    Matches((&'c str, &'c str)),
    Clashes((&'c str, &'c str)),
}

#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq)]
pub(crate) enum Unit {
    Milliseconds,
    Seconds,
    Minutes,
    Hours,
    Days,
    Weeks,
    Years,
}

// This basically does the same as the Go implementation in
// https://github.com/prometheus/common/blob/00591a3ea9c0d18f6bc983818a23901d4154077f/model/time.go#L190
// However there is no reason to store the duration in another type
// as it is posted to the HTTP API as-is anyway.
// Thus the duration string is only validated.
pub(crate) fn validate_duration(mut duration: &str, allow_negative: bool) -> Result<(), Error> {
    if duration.is_empty() {
        return Err(Error::InvalidTimeDuration);
    }

    // Note: As per the Prometheus documentation negative durations are only allowed
    // in the context of an offset.
    if allow_negative {
        duration = duration.strip_prefix('-').unwrap_or(duration);
    }

    // Remember the last encountered unit to check for duplicates and proper ordering.
    // Actually checking for proper ordering is sufficient here because the following
    // unit must be lesser than the leading unit, i.e. the leading unit must be
    // greater or equal the following unit.
    let mut last_unit: Option<Unit> = None;

    // In the go implementation the whole duration string is converted to an
    // time.Duration that is constructed from an int64. Thus the total number
    // of nanoseconds (when each unit is converted to nanoseconds) may not exceed
    // i64::MAX.
    let mut total_nanos: i64 = 0;

    // Each unit is converted to nanoseconds. As "ms" is the most precise unit
    // that can be used, we need to multiply _every_ unit, "ms" and above, by
    // this amount to convert it to nanoseconds.
    const MULTIPLIER: i64 = 1000 * 1000;

    // Add each number character to a string until a unit character is encountered.
    // This string is then cleared to process the next number + unit.
    let mut raw_num = String::new();

    // Iterate the duration string, convert each unit to nanoseconds and add
    // it to the total.
    let mut duration_iter = duration.chars().peekable();
    while let Some(item) = duration_iter.next() {
        if ('0'..='9').contains(&item) {
            raw_num.push(item);
            continue;
        }

        // If the next character is not in 0..=9, it must be a unit. However
        // a unit must be preceded by a number, so at least one index must
        // be stored.
        if raw_num.is_empty() {
            return Err(Error::InvalidTimeDuration);
        }

        let num = raw_num
            .parse::<i64>()
            .map_err(|_| Error::InvalidTimeDuration)?;

        let unit = match item {
            'y' => {
                total_nanos = num
                    .checked_mul(1000 * 60 * 60 * 24 * 365 * MULTIPLIER)
                    .and_then(|n| total_nanos.checked_add(n))
                    .ok_or(Error::InvalidTimeDuration)?;
                Unit::Years
            }
            'w' => {
                total_nanos = num
                    .checked_mul(1000 * 60 * 60 * 24 * 7 * MULTIPLIER)
                    .and_then(|n| total_nanos.checked_add(n))
                    .ok_or(Error::InvalidTimeDuration)?;
                Unit::Weeks
            }
            'd' => {
                total_nanos = num
                    .checked_mul(1000 * 60 * 60 * 24 * MULTIPLIER)
                    .and_then(|n| total_nanos.checked_add(n))
                    .ok_or(Error::InvalidTimeDuration)?;
                Unit::Days
            }
            'h' => {
                total_nanos = num
                    .checked_mul(1000 * 60 * 60 * MULTIPLIER)
                    .and_then(|n| total_nanos.checked_add(n))
                    .ok_or(Error::InvalidTimeDuration)?;
                Unit::Hours
            }
            'm' => {
                if duration_iter.next_if_eq(&'s').is_some() {
                    total_nanos = num
                        .checked_mul(1000 * 60 * 60 * MULTIPLIER)
                        .and_then(|n| total_nanos.checked_add(n))
                        .ok_or(Error::InvalidTimeDuration)?;
                    Unit::Milliseconds
                } else {
                    total_nanos = num
                        .checked_mul(1000 * 60 * MULTIPLIER)
                        .and_then(|n| total_nanos.checked_add(n))
                        .ok_or(Error::InvalidTimeDuration)?;
                    Unit::Minutes
                }
            }
            's' => {
                total_nanos = num
                    .checked_mul(1000 * MULTIPLIER)
                    .and_then(|n| total_nanos.checked_add(n))
                    .ok_or(Error::InvalidTimeDuration)?;
                Unit::Seconds
            }
            _ => return Err(Error::InvalidTimeDuration),
        };

        raw_num.clear();

        // Check for duplicates and ordering.
        if matches!(last_unit, Some(x) if x <= unit) {
            return Err(Error::InvalidTimeDuration);
        } else {
            last_unit = Some(unit);
        }
    }

    // When the whole duration string has been iterated over this "buffer"
    // should be empty, i.e. no number should be left that is not followed
    // by a unit.
    if !raw_num.is_empty() {
        return Err(Error::InvalidTimeDuration);
    }

    if total_nanos < 0 {
        return Err(Error::InvalidTimeDuration);
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_validate_duration() {
        // Large duration, but still in range.
        let input = "292y";
        assert!(validate_duration(input, false).is_ok());

        // Large duration, but still in range.
        let input = "9223372036s";
        assert!(validate_duration(input, false).is_ok());

        // Out of range (greater than i64::MAX when converted to ns).
        let input = "293y";
        assert!(validate_duration(input, false).is_err());

        // Out of range (greater than i64::MAX when converted to ns).
        let input = "9223372037s";
        assert!(validate_duration(input, false).is_err());

        // Normal range with multiple units and in proper order.
        let input = "2y5m30s";
        assert!(validate_duration(input, false).is_ok());

        // Normal range with multiple units and in proper order.
        let input = "2y5m30s15ms";
        assert!(validate_duration(input, false).is_ok());

        // Same as the prior but negative.
        let input = "-2y5m30s";
        assert!(validate_duration(input, true).is_ok());

        // Same as the prior but negative is not allowed.
        let input = "-2y5m30s";
        assert!(validate_duration(input, false).is_err());

        // Only exactly one minus is stripped.
        let input = "--2y5m30s";
        assert!(validate_duration(input, true).is_err());

        // Wrong order.
        let input = "2y5m1h30s";
        assert!(validate_duration(input, false).is_err());

        // Duplicate.
        let input = "2y5m30s1s";
        assert!(validate_duration(input, false).is_err());

        // No unit at all.
        let input = "200";
        assert!(validate_duration(input, false).is_err());

        // Missing unit in trailing number.
        let input = "1h30";
        assert!(validate_duration(input, false).is_err());
    }
}