Each row of the readings table is the difference since the row before it, at most one a minute and only when something changed, so a range total is one indexed query. Per-language counts, not-cacheable reasons and build observations have their own tables; what the last poll and the last row saw is remembered in the state table, so differences survive a restart. The five views are computed once per poll and kept; /api/view answers from them and never touches the database, which only the polling task holds. /api/zero wakes that task instead of reading the counters itself. Build observations: after a poll in which every build that finished was of one language, one row with the difference in compilations and compile time, for the build-time histogram. Time saved uses their per-language average. Views fill the api's build_times and interval_90 as empty/None.
164 lines
5.0 KiB
Rust
164 lines
5.0 KiB
Rust
//! `sccache-dash`: serves the dashboard, or installs itself as a user service.
|
|
|
|
use std::process::{Command as Process, Stdio};
|
|
use std::sync::Arc;
|
|
|
|
use sccache_dash::app::{App, Config};
|
|
use sccache_dash::{http, systemd};
|
|
|
|
const USAGE: &str = "\
|
|
sccache-dash [--port N] [--no-open] serve the dashboard on http://127.0.0.1:8226 and open it
|
|
sccache-dash install start it at login, as a systemd user service
|
|
sccache-dash uninstall remove that service again";
|
|
|
|
fn main() {
|
|
let args: Vec<String> = std::env::args().skip(1).collect();
|
|
if let Err(e) = run(&args) {
|
|
eprintln!("sccache-dash: {e}");
|
|
std::process::exit(1);
|
|
}
|
|
}
|
|
|
|
fn run(args: &[String]) -> Result<(), String> {
|
|
match parse(args)? {
|
|
Command::Help => println!("{USAGE}"),
|
|
Command::Install => systemd::install()?,
|
|
Command::Uninstall => systemd::uninstall()?,
|
|
Command::Serve { port, open } => serve(port, open)?,
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// What the command line asks for.
|
|
#[derive(Debug)]
|
|
enum Command {
|
|
Serve { port: u16, open: bool },
|
|
Install,
|
|
Uninstall,
|
|
Help,
|
|
}
|
|
|
|
fn parse(args: &[String]) -> Result<Command, String> {
|
|
let mut serve = Command::Serve {
|
|
port: sccache_dash_api::DEFAULT_PORT,
|
|
open: true,
|
|
};
|
|
let mut i = 0;
|
|
while let Some(arg) = args.get(i) {
|
|
i += 1;
|
|
match arg.as_str() {
|
|
"--no-open" => {
|
|
if let Command::Serve { open, .. } = &mut serve {
|
|
*open = false;
|
|
}
|
|
}
|
|
"--help" | "-h" => return Ok(Command::Help),
|
|
"install" => return Ok(Command::Install),
|
|
"uninstall" => return Ok(Command::Uninstall),
|
|
a if a == "--port" || a.starts_with("--port=") => {
|
|
let value = match a.strip_prefix("--port=") {
|
|
Some(value) => value.to_owned(),
|
|
None => {
|
|
i += 1;
|
|
args.get(i - 1)
|
|
.ok_or("--port needs a number, as in --port 8226")?
|
|
.clone()
|
|
}
|
|
};
|
|
let port = value
|
|
.parse()
|
|
.map_err(|_| format!("`{value}` is not a port number"))?;
|
|
if let Command::Serve { port: p, .. } = &mut serve {
|
|
*p = port;
|
|
}
|
|
}
|
|
a => return Err(format!("unknown argument `{a}`\n\n{USAGE}")),
|
|
}
|
|
}
|
|
Ok(serve)
|
|
}
|
|
|
|
fn serve(port: u16, open: bool) -> Result<(), String> {
|
|
let app = Arc::new(App::new(Config::from_env()?));
|
|
let server = http::listen(port)?;
|
|
let port = http::port_of(&server);
|
|
let url = format!("http://127.0.0.1:{port}");
|
|
println!(
|
|
"sccache-dash: serving {url}, history in {}",
|
|
app.cfg.history.display()
|
|
);
|
|
app.clone().keep_updated();
|
|
if open {
|
|
// Left to finish on its own: a browser that will not open is not worth stopping for.
|
|
let browser = Process::new("xdg-open")
|
|
.arg(&url)
|
|
.stdin(Stdio::null())
|
|
.stdout(Stdio::null())
|
|
.stderr(Stdio::null())
|
|
.spawn();
|
|
if let Ok(mut browser) = browser {
|
|
std::thread::spawn(move || {
|
|
let _ = browser.wait();
|
|
});
|
|
}
|
|
}
|
|
http::answer(server, app);
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
fn parse_args(args: &[&str]) -> Result<Command, String> {
|
|
parse(&args.iter().map(|a| a.to_string()).collect::<Vec<_>>())
|
|
}
|
|
|
|
#[test]
|
|
fn the_command_line() {
|
|
assert!(matches!(
|
|
parse_args(&[]),
|
|
Ok(Command::Serve {
|
|
port: 8226,
|
|
open: true
|
|
})
|
|
));
|
|
assert!(matches!(
|
|
parse_args(&["--port", "18226"]),
|
|
Ok(Command::Serve {
|
|
port: 18226,
|
|
open: true
|
|
})
|
|
));
|
|
assert!(matches!(
|
|
parse_args(&["--port=9000", "--no-open"]),
|
|
Ok(Command::Serve {
|
|
port: 9000,
|
|
open: false
|
|
})
|
|
));
|
|
assert!(matches!(
|
|
parse_args(&["--no-open", "--port", "1"]),
|
|
Ok(Command::Serve {
|
|
port: 1,
|
|
open: false
|
|
})
|
|
));
|
|
assert!(matches!(parse_args(&["install"]), Ok(Command::Install)));
|
|
assert!(matches!(parse_args(&["uninstall"]), Ok(Command::Uninstall)));
|
|
assert!(matches!(parse_args(&["--help"]), Ok(Command::Help)));
|
|
assert!(parse_args(&["18226"])
|
|
.unwrap_err()
|
|
.starts_with("unknown argument `18226`"));
|
|
assert!(parse_args(&["--port"])
|
|
.unwrap_err()
|
|
.contains("--port needs a number"));
|
|
assert!(parse_args(&["--port", "x"])
|
|
.unwrap_err()
|
|
.contains("`x` is not a port number"));
|
|
assert!(parse_args(&["--port", "70000"])
|
|
.unwrap_err()
|
|
.contains("is not a port number"));
|
|
}
|
|
}
|