Custom Normalization
When you do not want to normalize data based on existing processes, you can define a custom normalization procedure.
Create a custom normalization
Step 1: Create a Type that mixes Normalizer. Then, define a method normalizer using the following syntax, which overrides a custom implementation.
/**
* Simple normalizer where it returns the same values as in the data points
*/
type IdentityNormalizer mixes Normalizer {
/**
* Function that needs to be overriden
*/
normalize: function(objs: stream<TSDataPoint>, spec: TSNormalizationSpec): [Timeseries] js-server
}Step 2: Implement the normalization logic. There are several helper functions that can be used to help normalize the data.
Implementation of the normalizer method follows the same rules as the normal method implementation rules. The file name should be the Type name the method is defined on, and the extension is .js,.py, or .r, depending on the implementation language.
A sample implementation is shown below:
/**
* IdentityNormalizer.js
*
* This is a custom normalizer. This function should look at the
* data points and then create one Timeseries per month
*/
function normalize(objs, spec) {
var results = [];
var i = 0;
while(objs.hasNext()) {
var current = objs.next();
if (current.value == null)
continue;
var ts = Timeseries.fromValue(TimeInfo.make({
start: current.start,
end: current.end,
interval: "MONTH" }), current.value, current.unit);
results[i++] = ts;
}
return results;
}Step 3: Annotate the fields that are going to be normalized using a custom procedure. The annotation @ts with the normalizer parameter can be used to indicate the use of a custom normalizer:
@db(datastore='kv', partitionKeyField='parent')
entity type SmartBulbMeasurement mixes IntervalDataPoint<SmartBulbMeasurementSeries> schema name 'SMRT_BLB_MSRMNT' {
@ts(normalizer='IdentityNormalizer')
quantity: !~
…
}