TinyBase logoTinyBase

MetricAggregateRemove

The MetricAggregateRemove type describes a function that can be used to optimize a custom MetricAggregate by providing a shortcut for when a single value is removed from the input values.

(
  metric: Metric,
  remove: number,
  length: number,
): Metric | undefined
TypeDescription
metricMetric

The current value of the Metric.

removenumber

The number being removed from the Metric's aggregation.

lengthnumber

The length of the array of numbers in the Metric's aggregation.

returnsMetric | undefined

The new value of the Metric.

Some aggregation functions do not need to recalculate the aggregation of the whole set when one value changes. For example, when removing a number from a series, the new sum of the series is the new value subtracted from the previous sum.

If it is not possible to shortcut the aggregation based on just one value being removed, return undefined and the Metric will be completely recalculated. One example might be if you were taking the minimum of the values, and the previous minimum is being removed. The whole of the rest of the list will need to be re-scanned to find a new minimum.

Where possible, if you are providing a custom MetricAggregate, seek an implementation of an MetricAggregateRemove function that can reduce the complexity cost of shrinking the input data set. See the setMetricDefinition method for more examples.