Metrics
A Metrics
object lets you define, query, and listen to, aggregations of Cell
values within a Table
in a Store
.
This is useful for counting the number of Row
objects in a Table
, averaging Cell
values, or efficiently performing any arbitrary aggregations.
Create a Metrics
object easily with the createMetrics
function. From there, you can add new Metric
definitions (with the setMetricDefinition
method), query their values (with the getMetric
method), and add listeners for when they change (with the addMetricListener
method).
This module provides a number of predefined and self-explanatory aggregations ('sum', 'avg', 'min', and 'max'), and defaults to counting Row
objects when using the setMetricDefinition
method. However, far more complex aggregations can be configured with custom functions.
Example
This example shows a very simple lifecycle of a Metrics
object: from creation, to adding a definition, getting a Metric
, and then registering and removing a listener for it.
import {createMetrics, createStore} from 'tinybase';
const store = createStore().setTable('species', {
dog: {price: 5},
cat: {price: 4},
worm: {price: 1},
});
const metrics = createMetrics(store);
metrics.setMetricDefinition(
'highestPrice', // metricId
'species', // tableId to aggregate
'max', // aggregation
'price', // cellId to aggregate
);
console.log(metrics.getMetric('highestPrice'));
// -> 5
const listenerId = metrics.addMetricListener('highestPrice', () => {
console.log(metrics.getMetric('highestPrice'));
});
store.setCell('species', 'horse', 'price', 20);
// -> 20
metrics.delListener(listenerId);
metrics.destroy();
See also
- Using Metrics guides
- Rolling Dice demos
- Country demo
- Todo App demos
Since
v1.0.0
Getter methods
This is the collection of getter methods within the Metrics
interface. There are 5 getter methods in total.
Listener methods
This is the collection of listener methods within the Metrics
interface. There are only three listener methods, addMetricIdsListener
, addMetricListener
, and delListener
.
Configuration methods
This is the collection of configuration methods within the Metrics
interface. There are only two configuration methods, delMetricDefinition
and setMetricDefinition
.
Iterator methods
This is the collection of iterator methods within the Metrics
interface. There is only one method, forEachMetric
.
Lifecycle methods
This is the collection of lifecycle methods within the Metrics
interface. There is only one method, destroy
.
Development methods
This is the collection of development methods within the Metrics
interface. There is only one method, getListenerStats
.