Rule Queries

The query that defines what an Analytic Rule detects, and how its columns become the content of a detection.

The Query tab holds the StroomQl query that defines the rule. It is the same query editor used by the Query document, so you can write a query, run it and inspect the results while you are developing the rule.

Every row the query returns becomes one detection. This is the single most important thing to understand about a rule. A query that returns a million rows will raise a million detections, so it is worth running the query and looking at the row count before enabling the rule.

Choosing a Data Source

A rule query can use any data source that a Query document can use, but the choice interacts with the processing type.

  • A Scheduled Query rule can query anything, including Searchables and Lucene indexes.
  • A Streaming rule must query a View , because the View’s filter is what decides which streams are fed through the rule.

Time Ranges and Relative Times

A rule that is going to run repeatedly should express its time range relative to when it runs, not as absolute dates.

When a scheduled rule runs, Stroom sets the reference time for the query to the rule’s effective execution time rather than to the current clock time. Every relative date expression in the query and in the time range is then resolved against that effective time.

This is what makes it possible for a rule to work through history. A rule whose query says where EventTime >= day() - 1d will, when it runs for an effective time of last Tuesday, select last Monday’s data. The same query run for an effective time of today selects yesterday’s data.

Time Windows

A query with a window clause gives a row for each window of time, such as a count for each user for each day. A rule judges a window only when it has all of the window’s events, so that a window is judged once and is not judged on part of its events.

A Scheduled Query rule judges each window once, in the run whose time range holds the end of the window. A row that uses lead() is judged in the run whose time range holds the end of the last window it reads. Where the query gives no end to its time range, the range ends at the effective execution time.

The rule searches back before the start of its time range as far as it needs to, for the start of the first window it judges and for the windows that it reads with lag(). For example, a rule run once a day with windows of a day, a time range from day() - 1d to day() and lag(count(), 1) judges yesterday, and searches the day before as well so that it can compare the two.

Give each run a time range that follows on from the last one, so that every window ends in one of them. The range does not need to line up with the windows: a range from now() - 1d to now() judges each day in the run after it ends.

The search back applies to the whole query, including any join, so a joined data source is searched over the same longer range. A limit in the query applies to the windows the rule judges, and not to the windows it searches only for lag().

Columns

The columns your query selects become the content of each detection. Columns that Stroom adds for its own use, such as the ones behind a having expression or a window, are not added.

Every column is added to the detection as a name/value pair, using the column’s name as the key, and is available to email templates through the values dictionary. Two column names are treated specially.

Column Treatment
StreamId Used as the streamId of the detection’s linked event rather than added to values.
EventId Used as the eventId of the detection’s linked event rather than added to values.

Including StreamId and EventId in the query is what allows a detection to link back to the Event that caused it. Without them the detection still carries all its other values, but there is nothing to click through to.

Table Preferences

Some things about a result table cannot be expressed in StroomQl , such as which columns are hidden, how values are formatted and how rows are sorted. These are set by interacting with the results table in the UI and are stored on the rule alongside the query.

For an Analytic Rule these preferences affect only what you see while developing the query. For a Report they also affect the file that is produced, because the report is written from the same table.

Parameters

A rule can define parameters in the same way as a Query document, and refer to them in the query. Parameters are stored on the rule, so a scheduled rule always runs with the values held against it.