Skip to content

Commit

Permalink
[items] TimeSeries: Add support for using Instant as timestamp (#342)
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
  • Loading branch information
florian-h05 authored Jun 9, 2024
1 parent 2791284 commit 104bbfc
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ The following example shows how to create a `TimeSeries`:

```javascript
var timeSeries = new items.TimeSeries('ADD'); // Create a new TimeSeries with policy ADD
timeSeries.add(now.minusMinutes(10), Quantity('5 m')).add(now.minusMinutes(5), Quantity('3 m')).add(now.minusMinutes(1), Quantity('1 m'));
timeSeries.add(time.toZDT('2024-01-01T14:53'), Quantity('5 m')).add(time.toZDT().minusMinutes(2), Quantity('0 m')).add(time.toZDT().plusMinutes(5), Quantity('5 m'));
console.log(ts); // Let's have a look at the TimeSeries
items.getItem('MyDistanceItem').persistence.persist(timeSeries, 'influxdb'); // Persist the TimeSeries for the Item 'MyDistanceItem' using the InfluxDB persistence service
```
Expand Down
10 changes: 8 additions & 2 deletions src/items/time-series.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
const time = require('../time');
const { _isInstant } = require('../helpers');

/**
* @typedef { import('@js-joda/core').Instant} time.Instant
* @private
*/
/**
* @typedef { import('@js-joda/core').ZonedDateTime} time.ZonedDateTime
* @private
*/
/**
* @typedef {import('../quantity').Quantity} Quantity
* @private
Expand Down Expand Up @@ -82,12 +87,13 @@ class TimeSeries {
*
* Elements can be added in an arbitrary order and are sorted chronologically.
*
* @param {*} timestamp a timestamp for the given state (uses {@link time.toZDT} for creating a {@link time.ZonedDateTime})
* @param {(time.Instant|time.ZonedDateTime|string|Date)} timestamp a timestamp for the given state
* @param {string|number|Quantity|HostState} state the state at the given timestamp
* @returns {TimeSeries} this TimeSeries instance
*/
add (timestamp, state) {
this.#states.push([time.toZDT(timestamp).toInstant(), state]);
const ts = _isInstant(timestamp) ? timestamp : time.toZDT(timestamp).toInstant();
this.#states.push([ts, state]);
return this;
}

Expand Down
13 changes: 11 additions & 2 deletions types/items/time-series.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ export = TimeSeries;
* @typedef { import('@js-joda/core').Instant} time.Instant
* @private
*/
/**
* @typedef { import('@js-joda/core').ZonedDateTime} time.ZonedDateTime
* @private
*/
/**
* @typedef {import('../quantity').Quantity} Quantity
* @private
Expand Down Expand Up @@ -58,16 +62,21 @@ declare class TimeSeries {
*
* Elements can be added in an arbitrary order and are sorted chronologically.
*
* @param {*} timestamp a timestamp for the given state (uses {@link time.toZDT} for creating a {@link time.ZonedDateTime})
* @param {(time.Instant|time.ZonedDateTime|string|Date)} timestamp a timestamp for the given state
* @param {string|number|Quantity|HostState} state the state at the given timestamp
* @returns {TimeSeries} this TimeSeries instance
*/
add(timestamp: any, state: string | number | Quantity | HostState): TimeSeries;
add(timestamp: (time.Instant | time.ZonedDateTime | string | Date), state: string | number | Quantity | HostState): TimeSeries;
toString(): string;
#private;
}
declare namespace TimeSeries {
export { Quantity };
}
declare namespace time {
type Instant = import('@js-joda/core').Instant;
type ZonedDateTime = import('@js-joda/core').ZonedDateTime;
}
import time = require("../time");
type Quantity = import('../quantity').Quantity;
//# sourceMappingURL=time-series.d.ts.map
2 changes: 1 addition & 1 deletion types/items/time-series.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 104bbfc

Please sign in to comment.