TimeSeries SDK
Introduction :
A time series is a series of numerical values representing the evolution of a specific quantity over time. this SDK allows easy management of these data.
Usage :
To use the spinalTimeSeries SDK, you must :
Install :
npm install --save https://github.com/spinalcom/spinal-models-timeSeries.git
Import and instantiate
var TimeSeries = require("spinal-models-timeSeries").TimeSeries; // import var timeSeries = new TimeSeries(); // instantiate
The TimeSeries class has as attributes:
Attributes | Type | Default Value | Description |
---|---|---|---|
id | Str | Generate an uuid | a unique identifier of the timeSeries |
name | Str | TimeSeries | The Name of the Time Series, its default value is "TimeSeries" |
data | Lst | Lst | A list of TimeSeriesData , contains the dates and Values of the time series. |
frequency | Val | 5 | the frequency of creating a new value of the time series, its in seconds |
archiveTime | Val | 24 | Frequency of archiving all timeSeries data, its in hours |
archive | Ptr | Lst | A pointer to all archived data of the timeSeries |
Methods :
addToTimeSeries(value)
takes as parameter a number (data to save ) and saves an object of type {date: saveDate, value: dataToSave} in timeSeries data.
Param | Type | Description | Mandatory |
---|---|---|---|
Value | number | Value To save | yes |
getTimeSeriesCurrentValue()
this function retrieves the last time series, and returns a promise.
Below an example
var TimeSeries = require("spinal-models-timeSeries").TimeSeries; var timeSeries = new TimeSeries(); timeSeries.addToTimeSeries(20); timeSeries.addToTimeSeries(30); timeSeries.addToTimeSeries(40); timeSeries.addToTimeSeries(50); var currentTimeSeriesValuePromise = timeSeries.getTimeSeriesCurrentValue(); //function usage, it returns a promise currentTimeSeriesValuePromise.then((currentTimeSeriesValue) => { // retrieve promise value console.log( `the current TimeSeries has been created at ${currentTimeSeriesValue.date.get()}, its value is ${currentTimeSeriesValue.value.get()}` ) }) /* ------------------ OUTPUT ----------------------------- the current TimeSeries has been created at 1542106770042, its value is 50 */
getTimeSeriesBetweenDates(argBeginDate, argEndDate)
Takes as parameters two dates (in millisecond or a date string in a valid format, preferably "year-month-day hours:minutes:seconds" for example : 2018-10-25 16:26:30 ) and returns a Array of all timeSeries between the two dates.
It returns an array of all timeSeries between argBeginDate and argEndDate
Param | Type | Description | Mandatory |
---|---|---|---|
argBeginDate | Date | Must be a date in milisecond or in year-month-day hours:minutes:seconds form | yes |
argEndDate | Date | the last date in milisecond or in year-month-day hours:minutes:seconds format | yes |
var TimeSeries = require("spinal-models-timeSeries").TimeSeries; var timeSeries = new TimeSeries(); timeSeries.addToTimeSeries(20); // created on 31/12/2015 16:30:50, we'll call this number 1 timeSeries.addToTimeSeries(30); // created on 25/08/2016 23:15:40, we'll call this number 2 timeSeries.addToTimeSeries(40); // created on 13/11/2018 12:16:32, we'll call this number 3 console.log(timeSeries.getTimeSeriesBetweenDates("2015", "2016")); // return TimeSeries between 01/01/2015 01:00:00 and 01/01/2016 01:00:00, so the output will be number 1 console.log(timeSeries.getTimeSeriesBetweenDates("2016-01-30","2018-12-31 13:54")); // return TimeSeries between 30/01/2016 01:00:00 and 31/12/2018 13:54:00, so the output will be number 2 and number 3
getDateValue(argDate)
It Takes a date as params and return the data corresponding to this date, it returns an empty object if no data is associated with the date.
returns an object that contains the date and data corresponding to argDate.
Param | Type | Description | Mandatory |
---|---|---|---|
argDate | Date | Must be a date in milisecond or in year-month-day hours:minutes:seconds format | yes |
removeDate(dateToRemove)
It takes a date as a params and remove and returns the data corresponding to this date. It returns the data corresponding to this date, returns undefined if no data found.
Param | Type | Description | Mandatory |
---|---|---|---|
dateToRemove | Date | Must be a date in milisecond or in year-month-day hours:minutes:seconds format | yes |
archiveDate(beginDate, [endDate])
this function takes as parameters two date (one optional), if both dates are given it archives all date between both (they even included) else it archives the date given.
Param | Type | Description | Mandatory |
---|---|---|---|
beginDate | Date | Must be a date in milisecond or in year-month-day hours:minutes:seconds format | yes |
[endDate] | Date | Optional, must be a date in milisecond or in year-month-day hours:minutes:seconds format | No |
getDateArchived()
this function allows to get all data archived, it returns a Promise.
archiveDataPerDay()
this function allows to archive the data of the timeSeries, by changing the attribute archiveTime you change the archiving frequency.