Registering a Live Query
Usequery_live() to register a subscription:
filter: &str- Query in the formatOP:SERIES:[TAGS]:[RANGE]
Ok(QueryHandle)- A handle to poll for events (typeu64)Ok(0)- Host registration failed (0 is the sentinel value)
Region containing UTF-8 filter bytes. The host returns a query handle or 0 on failure.
Query Syntax
Operators
AVG- Running averageMIN- Minimum valueMAX- Maximum valueSUM- Sum of valuesCOUNT- Event count
Tag Filters
Use boolean operators to filter:Time Ranges
Optional range specification:sec, min, hour, day, week (singular and plural forms)
You can also use microsecond timestamps:
Polling for Events
Once you have a handle, poll it for events.Continuous Polling
Usepoll_handle() to poll forever:
handle: QueryHandle- Handle fromquery_live()callback: F- Function called for each eventargs: A- Argument passed to the callback (must beClone)
Region containing JSON event data, or 0 if no event is available.
Example with State
Single Poll
Usepoll_handle_state() to poll once:
Ok(Some(PollState))- Aggregate state if availableOk(None)- No event available yet
Event Structure
Polled events have this structure:Field Descriptions
- timestamp: Unix timestamp in microseconds when the event occurred
- value: The aggregated value (e.g., running average, sum, max)
- tags: String tags attached to the event
- producers: WebSocket connection IDs that contributed to this aggregate. Use these for writeback.
Aggregate States
ThePollState enum represents different aggregate types:
AVG queries, you get both sum and count to compute the average:
Freeing Handles
When you’re done with a handle, free it:Multiple Query Example
You can register multiple queries and poll them independently:Best Practices
- Handle errors: Always check return values.
query_live()returns 0 on failure. - Free handles: Call
free_handle()when done to release resources. - Keep queries specific: Use tag filters to reduce event volume.
- Use appropriate aggregates: Choose the operator that matches your use case.
- Clone args carefully: The
argsparameter must beCloneand is cloned on each event.
Next Steps
Historical Queries
Query aggregated historical data
Writeback
Send results to WebSocket or HTTP endpoints