Computed measurement
This page describes computed measurement in detail.
Contents
Overview
As described here, computed measurement is a type of data that is generated from other data sources.
... describe in detail ... ... include a diagram to describe the parts that the developer needs to know ... ... describe the lifecycle of those parts ...
Design
The first thing to do when designing a computed measurement is to decide what are the inputs and what to output. A computed measurement can specify multiple measurementsas input. Metrics and tags in those measurement will then be available to the function that is responsible for generating the output.
Input Measurements
Any measurement measurements that that are available to you in the CMS can be used an input measurements for your computed measurement.
You should choose only the measurement(s) with metric(s) that are needed for you to calculate your computed measurement as input measurements.
Output Measurements
Any data that you want to create with your computed measurement should be packaged in an output measurement. An output measurement has the same composition (metrics, tags, timestamp) as other measurements in Senfi.
When designing the output measurement metrics and tags, should consider the following:
- Metrics: The metrics that you want and how to calculate them from your input measurement(s).
- Tags: What tags that allow you to differentiate computed measurements derived from different sensors. You can re-use the tags from input measurements or attach different tags depending on the outcome of your computed measurement's computation.
- Timestamp: Whether to use the input measurement's timestamp, or the time the computed measurement was output.
Implementation
... describe what functions the developer must override, and what is the expected behaviour of those functions ... ... Junhao feedback that it is unclear that you must output a valid measurement. he thought he has to always output something, like NULL during initial invocation ...
Testing
... how to test ... ... limitations of the test environment ...
Execution
... describe when the compute function will be run. ... ... describe caveats when using multiple measurements - eg. measurement 1 and measurement 2 can come in at different times, and compute will be called twice ...
Examples
Example 1: Temperature Scale Conversion
This example shows a simple usage of computed measurement.
Imagine you have a temperature sensor that sends raw temperature values in degrees Farenheit (℉) and you want to show the temperature in degrees Celsius (°C) instead. This is the temperature measurement from the temperature sensor that you have as input:
{ "tm_source": xxxxxxxxxx, "site_id" xxxxxxxx, "tag1": "xxxxxxxx", "tag2": "xxxxxxxx", "temperatureF": xxxxxxxxx }
And this is the computed measurement that you want to output:
{ "tm_source": xxxxxxxxxx, "site_id" xxxxxxxx, "tag1": "xxxxxxxx", "tag2": "xxxxxxxx", "temperatureC": xxxxxxxxx }
You can use the following script to perform the temperature metric conversion:
/** * @name: init * @description: Perform one-time initialization of your script * param {string} **/ async function init() { // No initialization tasks required for this simple example } /** * @name: compute * @description: Perform computation on watching measurements. This function is called when new measurements arrive * @param {string} measurement - The source measurement name * @param {Array.<object>} data - The measurement array data **/ function compute(measurement, data) { // Loop through the array of incoming measurements, // and convert 'temperatureF' to 'temperatureC' for (let i = 0; i < data.length; i++) { const inputMeasurement = data[i]; const temperatureF = inputMeasurement.temperatureF; // Calculate the temperature in C const temperatureC = (temperatureF − 32) * 5/9; const outputMeasurement = { tm_source: inputMeasurement.tm_source, site_id: inputMeasurement.site_id, tag1: inputMeasurement.tag1, tag2: inputMeasurement.tag2, temperatureC: temperatureC, }; // Output computed measurement output(outputMeasurement); } } /** * @function output * @description: User-invoked function to output a computed measurement * @param {Object} outputData - Object containing output metrics (required tags, optional tags, measurement metrics) * @param {string} outputData.tm_source - Timestamp (Same as input measurement) * @param {string} outputData.site_id - Site Id (Same as input measurement) * @param {string} outputData.tag1 - Tag1 (Same as input measurement) * @param {number} outputData.tag2 - Tag2 (Same as input measurement) * @param {boolean} outputData.temperatureC - Calculated temperature in C **/
Example 2
... give a more complex example ...