Revision control
Copy as Markdown
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
use crate::schema::{FieldConfigOverrideColor, FieldConfigOverrideProperty};
pub fn slug(text: &str) -> String {
text.replace(|ch: char| !ch.is_alphanumeric(), "-")
.to_ascii_lowercase()
}
pub struct UrlBuilder {
base_url: String,
params: Vec<String>,
}
impl UrlBuilder {
pub fn new_dashboard(dashboard_uid: String) -> Self {
Self {
params: vec![],
}
}
pub fn with_param(mut self, name: impl Into<String>, val: impl Into<String>) -> Self {
self.params.push(format!("{}={}", name.into(), val.into()));
self
}
pub fn with_time_range_param(mut self) -> Self {
self.params.push("${__url_time_range}".into());
self
}
pub fn build(self) -> String {
if self.params.is_empty() {
self.base_url.clone()
} else {
format!("{}?{}", self.base_url, self.params.join("&"))
}
}
}
/// Used to implement `join()` for an iterator
pub trait Join {
fn join(self, sep: &str) -> String;
}
impl<T, I> Join for T
where
T: Iterator<Item = I>,
I: Into<String>,
{
fn join(self, sep: &str) -> String {
self.map(I::into).collect::<Vec<String>>().join(sep)
}
}
/// Generate a color name for one of the dashboard count panels
pub fn dashboard_count_color(index: usize, for_unique_users: bool) -> FieldConfigOverrideProperty {
// Cycle through the 10 pre-defined colors.
//
// The base colors were generated by bdk using coolors.com
// The `for_unique_users` colors are lightened versions of the base.
let color = match (index % 10, for_unique_users) {
// Primary Scarlet
(0, false) => "#DC1D1A",
(0, true) => "#F18A88",
// Bright Ocean
(1, false) => "#1B88D0",
(1, true) => "#85C5EF",
// Golden Glow
(2, false) => "#F1DD25",
(2, true) => "#F8EE93",
// Jungle Green
(3, false) => "#3CB575",
(3, true) => "#9ADDB9",
// Rebecca Purple
(4, false) => "#6F3AA4",
(4, true) => "#B895DA",
// Reddish Brown
(5, false) => "#A44A3F",
(5, true) => "#D8A19A",
// Deep Saffron
(6, false) => "#FE9000",
(6, true) => "#FFC880",
// Radioactive Grass
(7, false) => "#84dd63",
(7, true) => "#C2EEB2",
// Violet
(8, false) => "#E980FC",
(8, true) => "#F4C1FD",
// Shadow Gray
(9, false) => "#242331",
(9, true) => "#8482A6",
_ => unreachable!(),
};
FieldConfigOverrideProperty::Color {
value: FieldConfigOverrideColor::Fixed {
fixed_color: color.into(),
},
}
}