Skip to content

Services Communication

Observables

The Kelvin SDK Client uses Observables, with RxJS, to expose our services to the application. This approach offers to us significant benefits, like asynchronous programming, handling data streams, easy cancellation, streams combination, etc...

ServiceName.getSomething()
    .pipe(
        // operations or transformations can be done here
    )
    .subscribe(
        (onSuccess) => {},
        (onError) => {},
        (onComplete) => {}
    );

Promises

We don't offer a native integration to use Promises, but RxJS offers a utility operator toPromise that converts an observable into an ES2015 promise. In both solutions it is necessary to install theRxJS npm dependency.

ServiceName.getSomething().toPromise()
    .then((onSuccess) => {})
    .catch((onError) => {});

Services

For the services and methods available in the SDK Client, check the documentation here.

Storage Interaction

The StorageService provides the possibility to read metrics data. All subsequent approaches return StorageData model, either in list or single object, depending of the request. The model has the following properties:

Property Description Example
assetName Name of the Asset asset-name-1
name Name of the metric node1.metric1
payload The model in which the value was stored { "value": 212.0312 }
timestamp Time in microseconds that this value is recorded 2020-06-24T00:00:00.000000Z
_insertionTimestamp Time in microseconds that this value is stored 2020-06-24T00:00:00.000000Z

Last Values

Returns the last written storage value for a given Asset.

import { StorageService } from '@kelvininc/js-client-sdk';

StorageService.getHistorianMetricLast({
    assetName: 'asset-name-1',
    name: 'node1.metric1'
}).subscribe((data: StorageData) => doSomething(data));


If you want to request the last values of multiple Assets use getHistorianMetricLastAdvanced method.

import { StorageService } from '@kelvininc/js-client-sdk';

StorageService.getHistorianMetricLastAdvanced({
    query: [
        {
            asset_name: 'asset-name-1',
            name: 'node1.metric1'
        },
        ...
    ]
}).subscribe((list: StorageData[]) => doSomething(list));

Between Ranges

Returns the data for an Asset in a given time range.

import { StorageService } from '@kelvininc/js-client-sdk';

StorageService.getHistorianMetricRange({
    assetName: 'my-asset-name-1',
    name: 'node1.metric1',
    startTime: '2020-08-05T23:00:00.000Z', // time in microseconds
    endTime: '2020-08-06T15:47:04.627Z',
    agg: 'mean', // optional, possible values: none, count, distinct, integral, mean, median, mode, spread, stddev, sum
    timeBucket: '1h', // optional, aggregate the data in blocks of 1 hour
    fill: '-', // optional, filling for empty time buckets
    order: 'ASC', // optional, order the data list
}).subscribe((list: StorageData[]) => doSomething(list));


If you want to request a range getRangePOSTStorage of multiples Assets use getLastPOSTStorage method.

import { StorageService } from 'kelvin-js-sdk';

StorageService.getLastPOSTStorage({
    query: [
        {
            startTime: '2020-08-05T23:00:00.000Z',
            endTime: '2020-08-06T15:47:04.627Z',
            agg: 'none',
            selectors: [
                {
                    asset_name: 'asset-name-1',
                    name: 'node1.metric1',
                    fields: ['value']
                },
                {
                    asset_name: 'asset-name-2',
                    name: 'node2.metric2'
                },
                ...
            ]
        }
    ]
}).subscribe((list: StorageData[]) => doSomething(list));