use std::collections::BTreeMap;
use regex::Regex;
use crate::ast::{Arg, Command};
use crate::query::{FieldSel, QueryOp};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WidgetKind {
Text,
Tail,
List,
Gauge,
Bars,
Histo,
Spark,
Chart,
Table,
Tabs,
Block,
Frame,
}
impl WidgetKind {
fn from(verb: &str) -> Option<WidgetKind> {
Some(match verb {
"text" => WidgetKind::Text,
"tail" => WidgetKind::Tail,
"list" => WidgetKind::List,
"gauge" => WidgetKind::Gauge,
"bars" => WidgetKind::Bars,
"histo" => WidgetKind::Histo,
"spark" => WidgetKind::Spark,
"chart" => WidgetKind::Chart,
"table" => WidgetKind::Table,
"tabs" => WidgetKind::Tabs,
"block" => WidgetKind::Block,
"frame" => WidgetKind::Frame,
_ => return None,
})
}
pub fn label(&self) -> &'static str {
match self {
WidgetKind::Text => "text",
WidgetKind::Tail => "tail",
WidgetKind::List => "list",
WidgetKind::Gauge => "gauge",
WidgetKind::Bars => "bars",
WidgetKind::Histo => "histo",
WidgetKind::Spark => "spark",
WidgetKind::Chart => "chart",
WidgetKind::Table => "table",
WidgetKind::Tabs => "tabs",
WidgetKind::Block => "block",
WidgetKind::Frame => "frame",
}
}
}
#[derive(Debug, Clone)]
pub struct Source {
pub pipeline: Vec<QueryOp>,
}
#[derive(Debug, Clone)]
pub struct Widget {
pub path: String,
pub kind: WidgetKind,
pub opts: BTreeMap<String, String>,
pub source: Option<Source>,
pub grid: Option<(usize, usize)>,
}
#[derive(Debug, Default)]
pub struct Spec {
pub widgets: Vec<Widget>,
pub out: Option<Vec<QueryOp>>,
}
pub fn build(cmds: &[Command]) -> Result<Spec, String> {
let mut spec = Spec::default();
build_into(&mut spec, cmds, 0)?;
Ok(spec)
}
fn build_into(spec: &mut Spec, cmds: &[Command], depth: usize) -> Result<(), String> {
if depth > 16 {
return Err("import: module nesting too deep (cycle?)".into());
}
for c in cmds {
if c.name == "import" {
let name = c
.args
.first()
.and_then(Arg::as_str)
.ok_or("import: missing module name")?;
let src = resolve_module(name)?;
let sub = crate::parser::parse(&src)?;
build_into(spec, &sub, depth + 1)?;
} else if let Some(kind) = WidgetKind::from(&c.name) {
let path = c
.args
.first()
.and_then(Arg::as_str)
.ok_or_else(|| format!("{}: missing widget path", c.name))?;
if !path.starts_with('.') {
return Err(format!(
"{}: widget path must start with '.', got `{path}`",
c.name
));
}
spec.widgets.push(Widget {
path: path.to_string(),
kind,
opts: parse_opts(&c.args[1..]),
source: None,
grid: None,
});
} else if c.name == "source" {
let path = c
.args
.first()
.and_then(Arg::as_str)
.ok_or("source: missing path")?;
let body = match c.args.get(1) {
Some(Arg::Block(b)) => b,
_ => return Err("source: expected `{ body }`".into()),
};
let pipeline = pipeline_from_body(body)?;
set_source(spec, path, Source { pipeline })?;
} else if c.name == "out" {
let body = match c.args.first() {
Some(Arg::Block(b)) => b,
_ => return Err("out: expected `{ body }`".into()),
};
spec.out = Some(pipeline_from_body(body)?);
} else if c.name == "grid" {
let path = c
.args
.first()
.and_then(Arg::as_str)
.ok_or("grid: missing path")?;
let o = parse_opts(&c.args[1..]);
let cell = |k| {
o.get(k)
.and_then(|s: &String| s.parse::<usize>().ok())
.unwrap_or(0)
};
set_grid(spec, path, (cell("row"), cell("col")))?;
} else if c.name.starts_with('.') {
if c.args.first().and_then(Arg::as_str) == Some("<-")
&& c.args.get(1).and_then(Arg::as_str) == Some("in")
{
let path = c.name.clone();
set_source(spec, &path, Source { pipeline: vec![] })?;
}
}
}
Ok(())
}
fn resolve_module(name: &str) -> Result<String, String> {
let local = format!("{name}.arb");
if let Ok(s) = std::fs::read_to_string(&local) {
return Ok(s);
}
if let Some(home) = std::env::var_os("HOME") {
let p = std::path::Path::new(&home)
.join(".arb/lib")
.join(format!("{name}.arb"));
if let Ok(s) = std::fs::read_to_string(p) {
return Ok(s);
}
}
bundled_module(name)
.map(str::to_string)
.ok_or_else(|| format!("import: module `{name}` not found"))
}
pub const STDLIB_NAMES: &[&str] = &[
"nums", "logs", "http", "json", "table", "top", "docker", "k8s", "nginx", "git", "systemd",
"redis", "postgres", "mysql", "mongodb", "kafka", "prometheus", "elasticsearch", "rabbitmq",
"apache", "haproxy", "journalctl", "dmesg", "ps", "htop", "iostat", "vmstat", "ss", "dig",
"curl", "gh", "terraform", "aws", "gcloud", "azure", "ansible", "consul", "vault", "etcd",
"nomad", "envoy", "memcached", "varnish", "pgbouncer", "celery", "sidekiq", "gunicorn",
"supervisor", "fail2ban", "iptables", "conntrack", "sar",
];
fn bundled_module(name: &str) -> Option<&'static str> {
Some(match name {
"nums" => include_str!("../stdlib/nums.arb"),
"logs" => include_str!("../stdlib/logs.arb"),
"http" => include_str!("../stdlib/http.arb"),
"json" => include_str!("../stdlib/json.arb"),
"table" => include_str!("../stdlib/table.arb"),
"top" => include_str!("../stdlib/top.arb"),
"docker" => include_str!("../stdlib/docker.arb"),
"k8s" => include_str!("../stdlib/k8s.arb"),
"nginx" => include_str!("../stdlib/nginx.arb"),
"git" => include_str!("../stdlib/git.arb"),
"systemd" => include_str!("../stdlib/systemd.arb"),
"redis" => include_str!("../stdlib/redis.arb"),
"postgres" => include_str!("../stdlib/postgres.arb"),
"mysql" => include_str!("../stdlib/mysql.arb"),
"mongodb" => include_str!("../stdlib/mongodb.arb"),
"kafka" => include_str!("../stdlib/kafka.arb"),
"prometheus" => include_str!("../stdlib/prometheus.arb"),
"elasticsearch" => include_str!("../stdlib/elasticsearch.arb"),
"rabbitmq" => include_str!("../stdlib/rabbitmq.arb"),
"apache" => include_str!("../stdlib/apache.arb"),
"haproxy" => include_str!("../stdlib/haproxy.arb"),
"journalctl" => include_str!("../stdlib/journalctl.arb"),
"dmesg" => include_str!("../stdlib/dmesg.arb"),
"ps" => include_str!("../stdlib/ps.arb"),
"htop" => include_str!("../stdlib/htop.arb"),
"iostat" => include_str!("../stdlib/iostat.arb"),
"vmstat" => include_str!("../stdlib/vmstat.arb"),
"ss" => include_str!("../stdlib/ss.arb"),
"dig" => include_str!("../stdlib/dig.arb"),
"curl" => include_str!("../stdlib/curl.arb"),
"gh" => include_str!("../stdlib/gh.arb"),
"terraform" => include_str!("../stdlib/terraform.arb"),
"aws" => include_str!("../stdlib/aws.arb"),
"gcloud" => include_str!("../stdlib/gcloud.arb"),
"azure" => include_str!("../stdlib/azure.arb"),
"ansible" => include_str!("../stdlib/ansible.arb"),
"consul" => include_str!("../stdlib/consul.arb"),
"vault" => include_str!("../stdlib/vault.arb"),
"etcd" => include_str!("../stdlib/etcd.arb"),
"nomad" => include_str!("../stdlib/nomad.arb"),
"envoy" => include_str!("../stdlib/envoy.arb"),
"memcached" => include_str!("../stdlib/memcached.arb"),
"varnish" => include_str!("../stdlib/varnish.arb"),
"pgbouncer" => include_str!("../stdlib/pgbouncer.arb"),
"celery" => include_str!("../stdlib/celery.arb"),
"sidekiq" => include_str!("../stdlib/sidekiq.arb"),
"gunicorn" => include_str!("../stdlib/gunicorn.arb"),
"supervisor" => include_str!("../stdlib/supervisor.arb"),
"fail2ban" => include_str!("../stdlib/fail2ban.arb"),
"iptables" => include_str!("../stdlib/iptables.arb"),
"conntrack" => include_str!("../stdlib/conntrack.arb"),
"sar" => include_str!("../stdlib/sar.arb"),
_ => return None,
})
}
pub fn list_presets() -> Vec<(String, String)> {
let mut out: Vec<(String, String)> = STDLIB_NAMES
.iter()
.map(|n| {
(
n.to_string(),
first_comment(bundled_module(n).unwrap_or("")),
)
})
.collect();
if let Some(home) = std::env::var_os("HOME") {
let dir = std::path::Path::new(&home).join(".arb/lib");
if let Ok(entries) = std::fs::read_dir(dir) {
for e in entries.flatten() {
let path = e.path();
if path.extension().and_then(|s| s.to_str()) == Some("arb") {
if let Some(stem) = path.file_stem().and_then(|s| s.to_str()) {
let src = std::fs::read_to_string(&path).unwrap_or_default();
out.push((stem.to_string(), first_comment(&src)));
}
}
}
}
}
out
}
fn first_comment(src: &str) -> String {
src.lines()
.find_map(|l| l.trim().strip_prefix('#'))
.map(|s| s.trim().to_string())
.unwrap_or_default()
}
fn parse_opts(args: &[Arg]) -> BTreeMap<String, String> {
let mut m = BTreeMap::new();
let mut i = 0;
while i < args.len() {
if let Some(flag) = args[i].as_str().and_then(|w| w.strip_prefix('-')) {
let val = args
.get(i + 1)
.and_then(Arg::as_str)
.unwrap_or("")
.to_string();
m.insert(flag.to_string(), val);
i += 2;
} else {
i += 1;
}
}
m
}
fn pipeline_from_body(cmds: &[Command]) -> Result<Vec<QueryOp>, String> {
let mut ops = Vec::new();
let mut saw_in = false;
for c in cmds {
match c.name.as_str() {
"in" | "in.json" | "in.html" | "in.xml" | "in.logfmt" => saw_in = true,
"in.csv" => {
saw_in = true;
ops.push(QueryOp::Csv);
}
"in.tsv" => {
saw_in = true;
ops.push(QueryOp::Tsv);
}
"in.yaml" | "in.yml" => {
saw_in = true;
ops.push(QueryOp::Yaml);
}
"in.toml" => {
saw_in = true;
ops.push(QueryOp::Toml);
}
"sel" => {
let words: Vec<&str> = c.args.iter().filter_map(Arg::as_str).collect();
let mut css_parts = Vec::new();
let mut attr = None;
let mut i = 0;
while i < words.len() {
if words[i] == "-attr" {
attr = words.get(i + 1).map(|s| s.to_string());
i += 2;
} else {
css_parts.push(words[i]);
i += 1;
}
}
let css = css_parts.join(" ");
if css.trim().is_empty() {
return Err("sel: expected a CSS selector".into());
}
ops.push(QueryOp::Sel { css, attr });
}
"find" => {
let css = c
.args
.iter()
.filter_map(Arg::as_str)
.collect::<Vec<_>>()
.join(" ");
if css.trim().is_empty() {
return Err("find: expected a tag/selector".into());
}
ops.push(QueryOp::Find(css));
}
"attr" => {
let name = str_arg(c);
if name.is_empty() {
return Err("attr: expected an attribute name".into());
}
ops.push(QueryOp::Attr(name));
}
"text" => ops.push(QueryOp::Text),
"match" | "grep" => ops.push(QueryOp::Match(regex_arg(c)?)),
"reject" | "grepv" => ops.push(QueryOp::Reject(regex_arg(c)?)),
"field" => ops.push(QueryOp::Field(field_sel(&c.args)?)),
"each" => ops.push(QueryOp::Each),
"count" => ops.push(QueryOp::Count),
"rate" => ops.push(QueryOp::Rate),
"tally" => ops.push(QueryOp::Tally),
"sum" => ops.push(QueryOp::Sum),
"min" => ops.push(QueryOp::Min),
"max" => ops.push(QueryOp::Max),
"avg" => ops.push(QueryOp::Avg),
"keys" => ops.push(QueryOp::Keys),
"vals" => ops.push(QueryOp::Vals),
"pick" => {
let keys: Vec<String> = c
.args
.iter()
.filter_map(Arg::as_str)
.map(str::to_string)
.collect();
if keys.is_empty() {
return Err("pick: expected one or more key names".into());
}
ops.push(QueryOp::Pick(keys));
}
"sort" => {
let flags: Vec<&str> = c.args.iter().filter_map(Arg::as_str).collect();
ops.push(QueryOp::Sort {
numeric: flags.contains(&"-n"),
reverse: flags.contains(&"-r"),
});
}
"uniq" => ops.push(QueryOp::Uniq),
"rev" => ops.push(QueryOp::Rev),
"first" => ops.push(QueryOp::First),
"last" => ops.push(QueryOp::Last),
"upper" => ops.push(QueryOp::Upper),
"lower" => ops.push(QueryOp::Lower),
"trim" => ops.push(QueryOp::Trim),
"replace" => {
let re = regex_arg(c)?;
let to = c
.args
.get(1)
.and_then(Arg::as_str)
.unwrap_or("")
.to_string();
ops.push(QueryOp::Replace(re, to));
}
"join" => {
let sep = c
.args
.first()
.and_then(Arg::as_str)
.unwrap_or(" ")
.to_string();
ops.push(QueryOp::Join(sep));
}
"nth" => ops.push(QueryOp::Nth(count_arg(c, "nth")?)),
"take" => ops.push(QueryOp::Take(count_arg(c, "take")?)),
"drop" => ops.push(QueryOp::Drop(count_arg(c, "drop")?)),
"calc" => {
let src = c
.args
.iter()
.filter_map(Arg::as_str)
.collect::<Vec<_>>()
.join(" ");
ops.push(QueryOp::Calc(crate::expr::parse(&src)?));
}
"where" => {
let src = c
.args
.iter()
.filter_map(Arg::as_str)
.collect::<Vec<_>>()
.join(" ");
ops.push(QueryOp::Where(crate::expr::parse(&src)?));
}
"map" => {
let src = c
.args
.iter()
.filter_map(Arg::as_str)
.collect::<Vec<_>>()
.join(" ");
ops.push(QueryOp::Map(crate::expr::parse(&src)?));
}
"sort_by" => {
let field = str_arg(c);
if field.is_empty() {
return Err("sort_by: expected a field name".into());
}
ops.push(QueryOp::SortBy(field));
}
"unique_by" => {
let field = str_arg(c);
if field.is_empty() {
return Err("unique_by: expected a field name".into());
}
ops.push(QueryOp::UniqueBy(field));
}
"count_by" => {
let field = str_arg(c);
if field.is_empty() {
return Err("count_by: expected a field name".into());
}
ops.push(QueryOp::CountBy(field));
}
"min_by" => {
let field = str_arg(c);
if field.is_empty() {
return Err("min_by: expected a field name".into());
}
ops.push(QueryOp::MinBy(field));
}
"max_by" => {
let field = str_arg(c);
if field.is_empty() {
return Err("max_by: expected a field name".into());
}
ops.push(QueryOp::MaxBy(field));
}
"has" => {
let key = str_arg(c);
if key.is_empty() {
return Err("has: expected a key name".into());
}
ops.push(QueryOp::Has(key));
}
"entries" => {
if !c.args.is_empty() { return Err("entries: takes no arguments".into()); }
ops.push(QueryOp::Entries);
}
"flatten" => ops.push(QueryOp::Flatten),
"add" => ops.push(QueryOp::Add),
"over" => {
let n = c
.args
.first()
.and_then(Arg::as_str)
.and_then(|s| s.parse::<f64>().ok())
.ok_or_else(|| "over: expected a numeric threshold".to_string())?;
ops.push(QueryOp::Over(n));
}
"under" => {
let n: f64 = str_arg(c)
.parse()
.map_err(|_| "under: expected a number".to_string())?;
ops.push(QueryOp::Under(n));
}
"between" => {
let lo = c
.args
.first()
.and_then(Arg::as_str)
.and_then(|s| s.parse::<f64>().ok())
.ok_or_else(|| "between: expected two numbers A B".to_string())?;
let hi = c
.args
.get(1)
.and_then(Arg::as_str)
.and_then(|s| s.parse::<f64>().ok())
.ok_or_else(|| "between: expected two numbers A B".to_string())?;
ops.push(QueryOp::Between(lo, hi));
}
"enumerate" => {
ops.push(QueryOp::Enumerate);
}
"words" => {
if !c.args.is_empty() { return Err("words: takes no arguments".into()); }
ops.push(QueryOp::Words);
}
"dedup" => ops.push(QueryOp::Dedup),
"tailn" => ops.push(QueryOp::Tailn(count_arg(c, "tailn")?)),
"pad" => {
let n = count_arg(c, "pad")?;
ops.push(QueryOp::Pad(n));
}
"lpad" => ops.push(QueryOp::Lpad(count_arg(c, "lpad")?)),
"grepf" => {
let field = c
.args
.first()
.and_then(Arg::as_str)
.ok_or_else(|| "grepf: expected FIELD and /re/".to_string())?
.to_string();
let raw = c
.args
.get(1)
.and_then(Arg::as_str)
.ok_or_else(|| "grepf: expected a pattern".to_string())?;
let pat = raw
.strip_prefix('/')
.and_then(|s| s.strip_suffix('/'))
.unwrap_or(raw);
let re = regex::Regex::new(pat).map_err(|e| format!("grepf: bad regex: {e}"))?;
ops.push(QueryOp::Grepf(field, re));
}
"flip" => {
ops.push(QueryOp::Flip);
}
"b64" => {
ops.push(QueryOp::B64);
}
"b64d" => {
ops.push(QueryOp::B64d);
}
"hex" => ops.push(QueryOp::Hex),
"unhex" => {
ops.push(QueryOp::Unhex);
}
"urlenc" => {
ops.push(QueryOp::Urlenc);
}
"urldec" => {
ops.push(QueryOp::Urldec);
}
"extract" => {
ops.push(QueryOp::Extract(regex_arg(c)?));
}
"split" => {
let delim = str_arg(c);
if delim.is_empty() { return Err("split: expected a non-empty delimiter".into()); }
ops.push(QueryOp::Split(delim));
}
"substr" => {
let args: Vec<usize> = c
.args
.iter()
.filter_map(Arg::as_str)
.filter_map(|s| s.parse::<usize>().ok())
.collect();
if args.len() != 2 {
return Err("substr: expected two non-negative integer args A B".into());
}
ops.push(QueryOp::Substr(args[0], args[1]));
}
"chars" => ops.push(QueryOp::Chars),
"title" => {
ops.push(QueryOp::Title);
}
"repeat" => {
let n = count_arg(c, "repeat")?;
ops.push(QueryOp::Repeat(n));
}
"set" => {
let key = str_arg(c);
if key.is_empty() { return Err("set: expected key and value".into()); }
let val = c.args.iter().filter_map(Arg::as_str).nth(1).unwrap_or("").to_string();
ops.push(QueryOp::Set(key, val));
}
"del" => {
let key = str_arg(c);
if key.is_empty() { return Err("del: expected a key name".into()); }
ops.push(QueryOp::Del(key));
}
"rename" => {
let args: Vec<String> = c.args.iter().filter_map(Arg::as_str).map(str::to_string).collect();
if args.len() != 2 || args[0].is_empty() || args[1].is_empty() {
return Err("rename: expected OLD NEW key names".into());
}
ops.push(QueryOp::Rename(args[0].clone(), args[1].clone()));
}
"default" => {
let args: Vec<String> = c.args.iter().filter_map(Arg::as_str).map(str::to_string).collect();
if args.len() != 2 {
return Err("default: expected exactly two args: key value".into());
}
ops.push(QueryOp::Default(args[0].clone(), args[1].clone()));
}
"merge" => {
ops.push(QueryOp::Merge);
}
"floor" => {
ops.push(QueryOp::Floor);
}
"ceil" => {
ops.push(QueryOp::Ceil);
}
"clamp" => {
let mut it = c.args.iter().filter_map(Arg::as_str);
let lo = it.next().and_then(|s| s.parse::<f64>().ok());
let hi = it.next().and_then(|s| s.parse::<f64>().ok());
match (lo, hi) {
(Some(lo), Some(hi)) => ops.push(QueryOp::Clamp(lo, hi)),
_ => return Err("clamp: expected LO HI numeric args".into()),
}
}
"contains" => ops.push(QueryOp::Contains(str_arg(c))),
"starts" => ops.push(QueryOp::Starts(str_arg(c))),
"ends" => ops.push(QueryOp::Ends(str_arg(c))),
"nonempty" => ops.push(QueryOp::Nonempty),
"numeric" => ops.push(QueryOp::Numeric),
"len" => ops.push(QueryOp::Len),
"wc" => ops.push(QueryOp::Wc),
"abs" => ops.push(QueryOp::Abs),
"round" => ops.push(QueryOp::Round),
"prepend" => ops.push(QueryOp::Prepend(str_arg(c))),
"append" => ops.push(QueryOp::Append(str_arg(c))),
"cut" => {
let delim = str_arg(c);
let n = c
.args
.get(1)
.and_then(Arg::as_str)
.and_then(|s| s.parse::<usize>().ok())
.unwrap_or(0);
ops.push(QueryOp::Cut(delim, n));
}
"median" => ops.push(QueryOp::Median),
"stddev" => ops.push(QueryOp::Stddev),
"p95" => ops.push(QueryOp::P95),
"range" => ops.push(QueryOp::Range),
"product" => ops.push(QueryOp::Product),
"distinct" => ops.push(QueryOp::Distinct),
"sample" => ops.push(QueryOp::Sample(count_arg(c, "sample")?)),
"bins" => ops.push(QueryOp::Bins(count_arg(c, "bins")?)),
"slice" => {
let a = c
.args
.first()
.and_then(Arg::as_str)
.and_then(|s| s.parse::<usize>().ok())
.unwrap_or(1);
let b = c
.args
.get(1)
.and_then(Arg::as_str)
.and_then(|s| s.parse::<usize>().ok())
.unwrap_or(usize::MAX);
ops.push(QueryOp::Slice(a, b));
}
other => return Err(format!("source: unknown verb `{other}`")),
}
}
if !saw_in {
return Err("source: pipeline must start with `in`".into());
}
Ok(ops)
}
fn regex_arg(c: &Command) -> Result<Regex, String> {
let raw = c
.args
.first()
.and_then(Arg::as_str)
.ok_or_else(|| format!("{}: expected a pattern", c.name))?;
let pat = raw
.strip_prefix('/')
.and_then(|s| s.strip_suffix('/'))
.unwrap_or(raw);
Regex::new(pat).map_err(|e| format!("{}: bad regex: {e}", c.name))
}
fn str_arg(c: &Command) -> String {
c.args
.first()
.and_then(Arg::as_str)
.unwrap_or("")
.to_string()
}
fn count_arg(c: &Command, verb: &str) -> Result<usize, String> {
c.args
.first()
.and_then(Arg::as_str)
.and_then(|s| s.parse::<usize>().ok())
.ok_or_else(|| format!("{verb}: expected a count"))
}
fn field_sel(args: &[Arg]) -> Result<FieldSel, String> {
let words: Vec<&str> = args.iter().filter_map(Arg::as_str).collect();
if words.is_empty() {
return Err("field: expected a column number or key path".into());
}
if words.len() == 1 {
if let Ok(n) = words[0].parse::<usize>() {
return Ok(FieldSel::Col(n));
}
}
Ok(FieldSel::Key(words.iter().map(|s| s.to_string()).collect()))
}
fn set_source(spec: &mut Spec, path: &str, src: Source) -> Result<(), String> {
for w in &mut spec.widgets {
if w.path == path {
w.source = Some(src);
return Ok(());
}
}
Err(format!("source: no widget named `{path}`"))
}
fn set_grid(spec: &mut Spec, path: &str, cell: (usize, usize)) -> Result<(), String> {
for w in &mut spec.widgets {
if w.path == path {
w.grid = Some(cell);
return Ok(());
}
}
Err(format!("grid: no widget named `{path}`"))
}