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
// (C) Copyright 2019-2020 Hewlett Packard Enterprise Development LP

use std::fmt;

/// A byte-index tuple representing a span of characters in a string
///
/// Note that spans refer to the position in the input string as read by the
/// parser rather than the output of an expression's `Display` impl.
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub struct Span {
  pub start: usize,
  pub end: usize
}

impl From<(usize, usize)> for Span {
  fn from(tup: (usize, usize)) -> Span {
    Span::new(tup.0, tup.1)
  }
}

impl Span {
  pub fn new(start: usize, end: usize) -> Self {
    Span { start, end }
  }

  pub(crate) fn from_node(node: &crate::parser::Node) -> Self {
    let span = node.as_span();
    Span {
      start: span.start(),
      end: span.end()
    }
  }
}

impl fmt::Display for Span {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    write!(f, "({}, {})", self.start, self.end)
  }
}

/// A Prometheus duration
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum PromDuration {
  Seconds(u64),
  Minutes(u64),
  Hours(u64),
  Days(u64),
  Weeks(u64),
  Years(u64)
}

type StringResult<T> = std::result::Result<T, String>;

impl PromDuration {
  pub fn from_pair(unit: &str, value: u64) -> StringResult<PromDuration> {
    Ok(match unit {
      "s" => PromDuration::Seconds(value),
      "m" => PromDuration::Minutes(value),
      "h" => PromDuration::Hours(value),
      "d" => PromDuration::Days(value),
      "w" => PromDuration::Weeks(value),
      "y" => PromDuration::Years(value),
      u => return Err(format!("invalid duration unit: {:?}", u))
    })
  }

  pub fn as_char(self) -> char {
    match self {
      PromDuration::Seconds(_) => 's',
      PromDuration::Minutes(_) => 'm',
      PromDuration::Hours(_) => 'h',
      PromDuration::Days(_) => 'd',
      PromDuration::Weeks(_) => 'w',
      PromDuration::Years(_) => 'y'
    }
  }
}

impl fmt::Display for PromDuration {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let v = match self {
      PromDuration::Seconds(v) => v,
      PromDuration::Minutes(v) => v,
      PromDuration::Hours(v) => v,
      PromDuration::Days(v) => v,
      PromDuration::Weeks(v) => v,
      PromDuration::Years(v) => v
    };

    write!(f, "{}{}", v, self.as_char())
  }
}

/// A Subquery which converts an instant vector to a range vector by repeatedly
/// evaluating it at set intervals into the relative past
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Subquery {
  /// Duration back in time to begin the subquery
  pub range: PromDuration,

  /// Optional step size. If unset, uses the global/query default at runtime.
  pub resolution: Option<PromDuration>,

  pub span: Option<Span>
}

impl Subquery {
  pub fn new(range: PromDuration) -> Self {
    Subquery {
      range,
      resolution: None,
      span: None
    }
  }

  pub fn resolution(mut self, res: PromDuration) -> Self {
    self.resolution = Some(res);
    self
  }

  pub fn clear_resolution(mut self) -> Self {
    self.resolution = None;
    self
  }

  pub fn span<S: Into<Span>>(mut self, span: S) -> Self {
    self.span = Some(span.into());
    self
  }
}

impl fmt::Display for Subquery {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    if let Some(res) = self.resolution {
      write!(f, "[{}:{}]", self.range, res)
    } else {
      write!(f, "[{}:]", self.range)
    }
  }
}