Skip to content

topk

Input Stack:
k: Int
stat: String
expr: TimeSeriesExpr
Output Stack:
TimeSeriesExpr
 
 

Select the top K time series based on a summary statistic. This operation keeps only the K time series with the largest values for the specified statistic, effectively filtering out all but the highest-performing series according to the chosen criteria.

Parameters

  • expr: The grouped time series expression to select from
  • stat: The summary statistic to rank by (max, min, avg, count, total, last)
  • k: Number of highest-ranking series to keep (positive integer)

Behavior

  1. Statistical evaluation: Computes the specified statistic for each time series over the query window
  2. Ranking: Sorts all series by the statistic values in descending order (largest first)
  3. Selection: Keeps only the first K series (those with the largest statistic values)
  4. Output: Returns the selected time series with their original data, not the statistic values

Examples

Selecting the 2 clusters with the highest maximum values:

BeforeAfter
name,sps,:eq,
(,nf.cluster,),:by
name,sps,:eq,
(,nf.cluster,),:by,
max,2,:topk

Others Aggregation

For including aggregate summaries of the excluded series, use the :topk-others-* operators:

  • :bottomk - Select bottom K time series (smallest values)
  • :limit - Limit results after sorting (different ranking criteria)
  • :sort - Sort time series by statistics
  • :filter - Filter based on statistical criteria
  • :stat - Compute statistics used for ranking

Since: 1.7