use crate::error::{Error, ParseUrlError};
use mime::Mime;
use reqwest::header::HeaderValue;
use serde::Deserialize;
use std::{fmt, str::FromStr};
use time::Duration as TimeDuration;
use url::Url;
fn time_duration_to_string(mut duration: TimeDuration) -> String {
let mut s = String::new();
if duration.is_negative() {
s.push('-');
}
{
let d = TimeDuration::weeks(duration.whole_weeks());
let years = (d / TimeDuration::weeks(12)).floor() as i64;
if years >= 1 {
let d = TimeDuration::weeks(12 * years);
s.push_str(&d.to_string());
s.push('y');
duration -= d;
}
}
{
let d = TimeDuration::weeks(duration.whole_weeks());
if !d.is_zero() {
s.push_str(&d.whole_weeks().to_string());
s.push('w');
duration -= d;
}
}
{
let d = TimeDuration::days(duration.whole_days());
if !d.is_zero() {
s.push_str(&d.whole_days().to_string());
s.push('d');
duration -= d;
}
}
{
let d = TimeDuration::hours(duration.whole_hours());
if !d.is_zero() {
s.push_str(&d.whole_hours().to_string());
s.push('h');
duration -= d;
}
}
{
let d = TimeDuration::minutes(duration.whole_minutes());
if !d.is_zero() {
s.push_str(&d.whole_minutes().to_string());
s.push('m');
duration -= d;
}
}
{
let d = TimeDuration::seconds(duration.whole_seconds());
if !d.is_zero() {
s.push_str(&d.whole_seconds().to_string());
s.push('s');
duration -= d;
}
}
{
let d = TimeDuration::milliseconds(duration.whole_milliseconds() as i64);
if !d.is_zero() {
s.push_str(&d.whole_milliseconds().to_string());
s.push_str("ms");
duration -= d;
}
}
s
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Duration {
pub(crate) inner: TimeDuration,
raw: String,
}
impl FromStr for Duration {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut total_milliseconds: i64 = 0;
let mut raw_num = String::new();
let mut duration_iter = s.chars().peekable();
let is_neg = duration_iter.next_if_eq(&'-').is_some();
while let Some(item) = duration_iter.next() {
if ('0'..='9').contains(&item) {
raw_num.push(item);
continue;
}
let num = raw_num.parse::<i64>().map_err(Error::ParseInt)?;
let factor = match item {
'y' => 1000 * 60 * 60 * 24 * 365,
'w' => 1000 * 60 * 60 * 24 * 7,
'd' => 1000 * 60 * 60 * 24,
'h' => 1000 * 60 * 60,
'm' => {
if duration_iter.next_if_eq(&'s').is_some() {
1
} else {
1000 * 60
}
}
's' => 1000,
_ => return Err(Error::Parse(format!("invalid time duration: {}", s))),
};
if is_neg {
total_milliseconds -= num * factor;
} else {
total_milliseconds += num * factor;
}
raw_num.clear();
}
Ok(Self {
inner: TimeDuration::milliseconds(total_milliseconds),
raw: s.to_string(),
})
}
}
impl fmt::Display for Duration {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.raw)
}
}
impl TryFrom<String> for Duration {
type Error = Error;
fn try_from(s: String) -> Result<Self, Self::Error> {
Self::from_str(&s)
}
}
impl From<f64> for Duration {
fn from(f: f64) -> Self {
let inner = TimeDuration::saturating_seconds_f64(f);
let raw = time_duration_to_string(inner);
Self { raw, inner }
}
}
impl Into<f64> for Duration {
fn into(self) -> f64 {
self.inner.as_seconds_f64()
}
}
impl From<i64> for Duration {
fn from(s: i64) -> Self {
let inner = TimeDuration::seconds(s);
let raw = time_duration_to_string(inner);
Self { raw, inner }
}
}
impl Into<i64> for Duration {
fn into(self) -> i64 {
self.inner.whole_seconds()
}
}
#[derive(Clone, Copy, Debug)]
pub enum TargetState {
Active,
Dropped,
Any,
}
impl fmt::Display for TargetState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Active => f.write_str("active"),
Self::Dropped => f.write_str("dropped"),
Self::Any => f.write_str("any"),
}
}
}
impl FromStr for TargetState {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"active" => Ok(Self::Active),
"dropped" => Ok(Self::Dropped),
"any" => Ok(Self::Any),
_ => Err(Error::Parse(format!(
"failed to parse target state from '{}', must be one of 'active', 'dropped', 'any'",
s
))),
}
}
}
#[derive(Debug, Copy, Clone, Deserialize, Eq, PartialEq)]
pub enum TargetHealth {
#[serde(alias = "up")]
Up,
#[serde(alias = "down")]
Down,
#[serde(alias = "unknown")]
Unknown,
}
impl TargetHealth {
pub fn is_up(&self) -> bool {
*self == Self::Up
}
pub fn is_down(&self) -> bool {
*self == Self::Down
}
pub fn is_unknown(&self) -> bool {
*self == Self::Unknown
}
}
impl fmt::Display for TargetHealth {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Up => f.write_str("up"),
Self::Down => f.write_str("down"),
Self::Unknown => f.write_str("unknown"),
}
}
}
#[derive(Debug, Copy, Clone, Deserialize, Eq, PartialEq)]
pub enum RuleHealth {
#[serde(alias = "ok")]
Good,
#[serde(alias = "err")]
Bad,
#[serde(alias = "unknown")]
Unknown,
}
impl RuleHealth {
pub fn is_good(&self) -> bool {
*self == Self::Good
}
pub fn is_bad(&self) -> bool {
*self == Self::Bad
}
pub fn is_unknown(&self) -> bool {
*self == Self::Unknown
}
}
impl fmt::Display for RuleHealth {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Good => f.write_str("ok"),
Self::Bad => f.write_str("err"),
Self::Unknown => f.write_str("unknown"),
}
}
}
#[derive(Debug, Copy, Clone, Deserialize, Eq, PartialEq)]
pub enum AlertState {
#[serde(alias = "inactive")]
Inactive,
#[serde(alias = "pending")]
Pending,
#[serde(alias = "firing")]
Firing,
}
impl AlertState {
pub fn is_inactive(&self) -> bool {
*self == Self::Inactive
}
pub fn is_pending(&self) -> bool {
*self == Self::Pending
}
pub fn is_firing(&self) -> bool {
*self == Self::Firing
}
}
impl fmt::Display for AlertState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Inactive => f.write_str("inactive"),
Self::Pending => f.write_str("pending"),
Self::Firing => f.write_str("firing"),
}
}
}
#[derive(Copy, Clone, Debug)]
pub enum RuleKind {
Alerting,
Recording,
}
impl RuleKind {
pub(crate) fn to_query_param(&self) -> String {
match self {
Self::Alerting => String::from("alert"),
Self::Recording => String::from("record"),
}
}
}
impl fmt::Display for RuleKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Alerting => f.write_str("alerting"),
Self::Recording => f.write_str("recording"),
}
}
}
impl FromStr for RuleKind {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"alert"| "alerting" => Ok(Self::Alerting),
"record"| "recording" => Ok(Self::Recording),
_ => Err(Error::Parse(format!("failed to parse rule type from '{}', must be one of 'alert', 'alerting', 'record', 'recording'", s)))
}
}
}
#[allow(clippy::enum_variant_names)]
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum Label<'a> {
Equal((&'a str, &'a str)),
NotEqual((&'a str, &'a str)),
RegexEqual((&'a str, &'a str)),
RegexNotEqual((&'a str, &'a str)),
}
impl<'a> fmt::Display for Label<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
match self {
Self::Equal((k, v)) => write!(f, "{}=\"{}\"", k, v),
Self::NotEqual((k, v)) => write!(f, "{}!=\"{}\"", k, v),
Self::RegexEqual((k, v)) => write!(f, "{}=~\"{}\"", k, v),
Self::RegexNotEqual((k, v)) => write!(f, "{}!~\"{}\"", k, v),
}
}
}
pub(crate) trait ToBaseUrl {
fn to_base_url(self) -> Result<Url, Error>;
}
impl ToBaseUrl for &str {
fn to_base_url(self) -> Result<Url, Error> {
Url::parse(self).map_err(|source| {
Error::ParseUrl(ParseUrlError {
message: "failed to build Prometheus server base URL",
source,
})
})
}
}
impl ToBaseUrl for String {
fn to_base_url(self) -> Result<Url, Error> {
Url::parse(&self).map_err(|source| {
Error::ParseUrl(ParseUrlError {
message: "failed to build Prometheus server base URL",
source,
})
})
}
}
pub(crate) fn build_final_url(mut url: Url, path: &str) -> Url {
let base_path = url.path();
match base_path {
"/" => return url.join(path).unwrap(),
_ => {
let p = format!("{}/{}", base_path, path);
url.set_path(&p);
}
}
url
}
pub(crate) fn is_json(v: Option<&HeaderValue>) -> bool {
match v
.and_then(|h| h.to_str().ok())
.and_then(|h| h.parse::<Mime>().ok())
{
Some(mime) => match (mime.type_(), mime.subtype()) {
(mime::APPLICATION, mime::JSON) => true,
_ => false,
},
None => false,
}
}
#[cfg(test)]
mod tests {
use super::{build_final_url, is_json, Duration, ToBaseUrl};
use std::str::FromStr;
use time::Duration as TimeDuration;
#[test]
fn test_simple_str_to_url() {
let s = "http://127.0.0.1:9090";
let url = s.to_base_url().unwrap();
assert_eq!("http://127.0.0.1:9090/", url.as_str());
}
#[test]
fn test_proxied_str_to_url() {
let s = "http://proxy.example.com/prometheus";
let url = s.to_base_url().unwrap();
assert_eq!("http://proxy.example.com/prometheus", url.as_str());
}
#[test]
fn test_simple_string_to_url() {
let s = String::from("http://127.0.0.1:9090");
let url = s.to_base_url().unwrap();
assert_eq!("http://127.0.0.1:9090/", url.as_str());
}
#[test]
fn test_proxied_string_to_url() {
let s = String::from("http://proxy.example.com/prometheus");
let url = s.to_base_url().unwrap();
assert_eq!("http://proxy.example.com/prometheus", url.as_str());
}
#[test]
fn test_simple_url_finalization() {
let s = String::from("http://127.0.0.1:9090");
let url = s.to_base_url().unwrap();
let final_url = build_final_url(url, "api/v1/targets");
assert_eq!("http://127.0.0.1:9090/api/v1/targets", final_url.as_str());
}
#[test]
fn test_proxied_url_finalization() {
let s = String::from("http://proxy.example.com/prometheus");
let url = s.to_base_url().unwrap();
let final_url = build_final_url(url, "api/v1/targets");
assert_eq!(
"http://proxy.example.com/prometheus/api/v1/targets",
final_url.as_str()
);
}
#[test]
fn test_is_json() {
let header = reqwest::header::HeaderValue::from_static("application/json");
assert!(is_json(Some(&header)));
}
#[test]
fn test_is_json_with_charset() {
let header = reqwest::header::HeaderValue::from_static("application/json; charset=utf-8");
assert!(is_json(Some(&header)));
}
#[test]
fn test_duration_from_str() -> Result<(), anyhow::Error> {
{
let s = "1s";
let expected = Duration {
inner: TimeDuration::seconds(1),
raw: "1s".to_string(),
};
assert_eq!(Duration::from_str(s)?, expected);
assert_eq!(Into::<f64>::into(expected.clone()), 1.0);
assert_eq!(Into::<i64>::into(expected), 1);
}
{
let s = "-1s";
let expected = Duration {
inner: TimeDuration::seconds(-1),
raw: "-1s".to_string(),
};
assert_eq!(Duration::from_str(s)?, expected);
assert_eq!(Into::<f64>::into(expected.clone()), -1.0);
assert_eq!(Into::<i64>::into(expected), -1);
}
{
let s = "2m";
let expected = Duration {
inner: TimeDuration::seconds(120),
raw: "2m".to_string(),
};
assert_eq!(Duration::from_str(s)?, expected);
assert_eq!(Into::<f64>::into(expected.clone()), 120.0);
assert_eq!(Into::<i64>::into(expected), 120);
}
{
let s = "-2m";
let expected = Duration {
inner: TimeDuration::seconds(-120),
raw: "-2m".to_string(),
};
assert_eq!(Duration::from_str(s)?, expected);
assert_eq!(Into::<f64>::into(expected.clone()), -120.0);
assert_eq!(Into::<i64>::into(expected), -120);
}
{
let s = "1ms";
let expected = Duration {
inner: TimeDuration::milliseconds(1),
raw: "1ms".to_string(),
};
assert_eq!(Duration::from_str(s)?, expected);
assert_eq!(Into::<f64>::into(expected.clone()), 0.001);
assert_eq!(Into::<i64>::into(expected), 0);
}
{
let s = "-1ms";
let expected = Duration {
inner: TimeDuration::milliseconds(-1),
raw: "-1ms".to_string(),
};
assert_eq!(Duration::from_str(s)?, expected);
assert_eq!(Into::<f64>::into(expected.clone()), -0.001);
assert_eq!(Into::<i64>::into(expected), 0);
}
{
let s = "-2h";
let expected = Duration {
inner: TimeDuration::seconds(-7200),
raw: "-2h".to_string(),
};
assert_eq!(Duration::from_str(s)?, expected);
assert_eq!(Into::<f64>::into(expected.clone()), -7200.0);
assert_eq!(Into::<i64>::into(expected), -7200);
}
{
let s = "1h30m";
let expected = Duration {
inner: TimeDuration::seconds(5400),
raw: "1h30m".to_string(),
};
assert_eq!(Duration::from_str(s)?, expected);
assert_eq!(Into::<f64>::into(expected.clone()), 5400.0);
assert_eq!(Into::<i64>::into(expected), 5400);
}
{
let s = "12h34m56s";
let expected = Duration {
inner: TimeDuration::seconds(45296),
raw: "12h34m56s".to_string(),
};
assert_eq!(Duration::from_str(s)?, expected);
assert_eq!(Into::<f64>::into(expected.clone()), 45296.0);
assert_eq!(Into::<i64>::into(expected), 45296);
}
{
let s = "54s321ms";
let expected = Duration {
inner: TimeDuration::saturating_seconds_f64(54.321),
raw: "54s321ms".to_string(),
};
assert_eq!(Duration::from_str(s)?, expected);
assert_eq!(Into::<f64>::into(expected.clone()), 54.321);
assert_eq!(Into::<i64>::into(expected), 54);
}
Ok(())
}
#[test]
fn test_duration_from_f64() -> Result<(), anyhow::Error> {
{
let f = 50.312;
let expected = Duration {
inner: TimeDuration::saturating_seconds_f64(50.312),
raw: "50s312ms".to_string(),
};
assert_eq!(Duration::from(f), expected);
assert_eq!(Into::<f64>::into(expected.clone()), 50.312);
assert_eq!(Into::<i64>::into(expected), 50);
}
{
let f = 1000.0;
let expected = Duration {
inner: TimeDuration::saturating_seconds_f64(1000.0),
raw: "16m40s".to_string(),
};
assert_eq!(Duration::from(f), expected);
assert_eq!(Into::<f64>::into(expected.clone()), 1000.0);
assert_eq!(Into::<i64>::into(expected), 1000);
}
{
let f = 2650034.1;
let expected = Duration {
inner: TimeDuration::saturating_seconds_f64(2650034.1),
raw: "4w2d16h7m14s100ms".to_string(),
};
assert_eq!(Duration::from(f), expected);
assert_eq!(Into::<f64>::into(expected.clone()), 2650034.1);
assert_eq!(Into::<i64>::into(expected), 2650034);
}
Ok(())
}
}