Use this file to discover all available pages before exploring further.
Historical queries let you retrieve aggregated values over past time ranges. Unlike live queries that stream updates, historical queries execute once and return a single aggregate value.
Historical queries typically specify a time range:
// Last hourquery_history("AVG:temp:[sensor=1]:[1h]")?;// Last 30 minutesquery_history("SUM:requests:[api=v1]:[30min]")?;// Last dayquery_history("MAX:cpu:[host=prod]:[1d]")?;// Last weekquery_history("COUNT:errors:[]:[1week]")?;
query_history("AVG:cpu:[host=prod AND region=us-west]:[1h]")?;query_history("SUM:requests:[status=200 OR status=201]:[1d]")?;query_history("MAX:latency:[NOT region=test]:[30min]")?;
use slung::prelude::*;#[main]fn main() -> Result<()> { // Query high-resolution data in chunks let ranges = vec![ "[0h,1h]", "[1h,2h]", "[2h,3h]", "[3h,4h]", ]; for range in ranges { let query = format!("AVG:temp:[sensor=1]:{}", range); let avg = query_history(&query)?; // Write downsampled data write_event( unix_micros(), avg, vec!["series=temp_hourly".to_string(), "sensor=1".to_string()] )?; } Ok(())}
Use historical queries to initialize state, then maintain it with live queries:
use slung::prelude::*;struct State { max_24h: f64, avg_7d: f64,}#[main]fn main() -> Result<()> { // Initialize state with historical data let state = State { max_24h: query_history("MAX:temp:[sensor=1]:[1d]")?, avg_7d: query_history("AVG:temp:[sensor=1]:[1week]")?, }; println!("24h max: {}, 7d avg: {}", state.max_24h, state.avg_7d); // Subscribe to live updates let handle = query_live("AVG:temp:[sensor=1]")?; poll_handle(handle, on_event, state)?; Ok(())}fn on_event(event: Event, state: State) -> Result<()> { if event.value > state.max_24h * 1.1 { println!("Temperature exceeds 24h max by 10%"); } if event.value > state.avg_7d * 2.0 { println!("Temperature is 2x the weekly average"); } Ok(())}
Historical queries scan stored data. Larger time ranges and broader tag filters increase query cost:
// Cheaper: narrow time range and specific tagsquery_history("AVG:cpu:[host=prod,region=us-west]:[1h]")?;// More expensive: wide time range and broad tagsquery_history("AVG:cpu:[host=prod]:[1week]")?;
The query_history() function returns 0.0 for both:
Valid aggregate results that are actually zero
Host-side failures
There’s no way to distinguish between these cases in the current API. Handle this by:
let result = query_history("SUM:bytes:[app=api]:[1h]")?;if result == 0.0 { // Could be: // - No events in the time range (valid) // - Host query failure // - Sum actually equals zero println!("Warning: zero result (may indicate no data)");}