Skip to content

Commit 811de1b

Browse files
committed
unify interface
1 parent 6fd5849 commit 811de1b

6 files changed

Lines changed: 141 additions & 66 deletions

File tree

dist/index.browser.esm.js

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2524,8 +2524,9 @@ const Predictor = class Predictor {
25242524
* @param {string[]} labels
25252525
* @param {{ center: { [featureName: string]: number }, scale: { [featureName: string]: number } }} scaler
25262526
* @param {{ windowingMode: "time" | "sample" }} options
2527+
* @param {boolean} useDeviceTime - True if you want to use timestamps generated by the server
25272528
*/
2528-
constructor(predictor, sensors, windowSize, labels, scaler, { windowingMode = "sample" } = {}) {
2529+
constructor(predictor, sensors, windowSize, labels, scaler, { windowingMode = "sample", useDeviceTime = false } = {}) {
25292530
/** @type {(input: number[]) => number[]} */
25302531
this.predictor = predictor;
25312532
/** @type {string[]} */
@@ -2539,6 +2540,9 @@ const Predictor = class Predictor {
25392540

25402541
/** @type {boolean} */
25412542
this.windowModeMs = windowSize < 0 || windowingMode === "time";
2543+
/** @type {boolean} */
2544+
this.useDeviceTime = useDeviceTime;
2545+
25422546
this.lastPruneTime = 0;
25432547
this.lastAddTime = 0;
25442548

@@ -2550,15 +2554,24 @@ const Predictor = class Predictor {
25502554
}
25512555

25522556
/**
2553-
* addDatapoint
2557+
* addDataPoint
2558+
* @param {number} time - The timestamp assigned to the datapoint
25542559
* @param {string} sensorName
25552560
* @param {number} value
2556-
* @param {number | null} time use a predefined timestamp, or null if the timestamp should be generated
25572561
*/
2558-
addDatapoint = (sensorName, value, time = null) => {
2562+
addDataPoint = (time, sensorName, value) => {
25592563
if (typeof value !== 'number') throw new TypeError('Datapoint is not a number');
25602564
if (!this.sensors.includes(sensorName)) throw new TypeError('Sensor is not valid');
2561-
if (time === null) time = Date.now();
2565+
// TODO: see #16 use if (time === null) time = Date.now()
2566+
if (!this.useDeviceTime && typeof time !== "number") {
2567+
throw new Error("Provide a valid timestamp");
2568+
}
2569+
if (this.useDeviceTime) {
2570+
time = Date.now();
2571+
}
2572+
2573+
//TODO: see #17 no clue why see Collector
2574+
value = Math.round(value * 100) / 100;
25622575

25632576
this.lastAddTime = time;
25642577
this.store[sensorName].push([time, value]);
@@ -2597,6 +2610,9 @@ const Predictor = class Predictor {
25972610
let window;
25982611
if (this.windowModeMs) {
25992612
window = Predictor._sliceByTime(samples, this.lastAddTime - this.windowSize);
2613+
if (window.length < 1 ) {
2614+
throw new PredictorError("Not enough samples")
2615+
}
26002616
} else {
26012617
window = samples.slice(-this.windowSize);
26022618
if (window.length < this.windowSize) {
@@ -2800,15 +2816,14 @@ async function datasetCollector(
28002816
var timeSeries = timeSeries;
28012817

28022818
/**
2803-
* Uploads a vlaue for a specific timestamp to a datasets timeSeries with name sensorName
2804-
* @param {string} name - The name of the timeSeries to upload the value to
2819+
* Uploads a value for a specific timestamp to a dataset's timeSeries with name sensorName
2820+
* @param {string} sensorName - The name of the timeSeries to upload the value to
28052821
* @param {number} value - The datapoint to upload
28062822
* @param {number} time - The timestamp assigned to the datapoint
2807-
* @returns A Promise indicating success or failure of upload
28082823
*/
2809-
function addDataPoint(time, name, value) {
2824+
function addDataPoint(time, sensorName, value) {
28102825

2811-
if (!timeSeries.includes(name)) {
2826+
if (!timeSeries.includes(sensorName)) {
28122827
throw Error("invalid time-series name")
28132828
}
28142829
if (typeof value !== "number") {
@@ -2824,14 +2839,14 @@ async function datasetCollector(
28242839

28252840
value = Math.round(value * 100) / 100;
28262841

2827-
if (dataStore.data.every((elm) => elm.name !== name)) {
2842+
if (dataStore.data.every((elm) => elm.name !== sensorName)) {
28282843
dataStore.data.push({
2829-
name: name,
2844+
name: sensorName,
28302845
data: [[time, value]],
28312846
});
28322847
} else {
28332848
const idx = dataStore.data.findIndex(
2834-
(elm) => elm.name === name
2849+
(elm) => elm.name === sensorName
28352850
);
28362851
dataStore.data[idx].data.push([time, value]);
28372852

dist/index.browser.js

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2529,8 +2529,9 @@ var edgeML = (function (require$$0, require$$1) {
25292529
* @param {string[]} labels
25302530
* @param {{ center: { [featureName: string]: number }, scale: { [featureName: string]: number } }} scaler
25312531
* @param {{ windowingMode: "time" | "sample" }} options
2532+
* @param {boolean} useDeviceTime - True if you want to use timestamps generated by the server
25322533
*/
2533-
constructor(predictor, sensors, windowSize, labels, scaler, { windowingMode = "sample" } = {}) {
2534+
constructor(predictor, sensors, windowSize, labels, scaler, { windowingMode = "sample", useDeviceTime = false } = {}) {
25342535
/** @type {(input: number[]) => number[]} */
25352536
this.predictor = predictor;
25362537
/** @type {string[]} */
@@ -2544,6 +2545,9 @@ var edgeML = (function (require$$0, require$$1) {
25442545

25452546
/** @type {boolean} */
25462547
this.windowModeMs = windowSize < 0 || windowingMode === "time";
2548+
/** @type {boolean} */
2549+
this.useDeviceTime = useDeviceTime;
2550+
25472551
this.lastPruneTime = 0;
25482552
this.lastAddTime = 0;
25492553

@@ -2555,15 +2559,24 @@ var edgeML = (function (require$$0, require$$1) {
25552559
}
25562560

25572561
/**
2558-
* addDatapoint
2562+
* addDataPoint
2563+
* @param {number} time - The timestamp assigned to the datapoint
25592564
* @param {string} sensorName
25602565
* @param {number} value
2561-
* @param {number | null} time use a predefined timestamp, or null if the timestamp should be generated
25622566
*/
2563-
addDatapoint = (sensorName, value, time = null) => {
2567+
addDataPoint = (time, sensorName, value) => {
25642568
if (typeof value !== 'number') throw new TypeError('Datapoint is not a number');
25652569
if (!this.sensors.includes(sensorName)) throw new TypeError('Sensor is not valid');
2566-
if (time === null) time = Date.now();
2570+
// TODO: see #16 use if (time === null) time = Date.now()
2571+
if (!this.useDeviceTime && typeof time !== "number") {
2572+
throw new Error("Provide a valid timestamp");
2573+
}
2574+
if (this.useDeviceTime) {
2575+
time = Date.now();
2576+
}
2577+
2578+
//TODO: see #17 no clue why see Collector
2579+
value = Math.round(value * 100) / 100;
25672580

25682581
this.lastAddTime = time;
25692582
this.store[sensorName].push([time, value]);
@@ -2602,6 +2615,9 @@ var edgeML = (function (require$$0, require$$1) {
26022615
let window;
26032616
if (this.windowModeMs) {
26042617
window = Predictor._sliceByTime(samples, this.lastAddTime - this.windowSize);
2618+
if (window.length < 1 ) {
2619+
throw new PredictorError("Not enough samples")
2620+
}
26052621
} else {
26062622
window = samples.slice(-this.windowSize);
26072623
if (window.length < this.windowSize) {
@@ -2805,15 +2821,14 @@ var edgeML = (function (require$$0, require$$1) {
28052821
var timeSeries = timeSeries;
28062822

28072823
/**
2808-
* Uploads a vlaue for a specific timestamp to a datasets timeSeries with name sensorName
2809-
* @param {string} name - The name of the timeSeries to upload the value to
2824+
* Uploads a value for a specific timestamp to a dataset's timeSeries with name sensorName
2825+
* @param {string} sensorName - The name of the timeSeries to upload the value to
28102826
* @param {number} value - The datapoint to upload
28112827
* @param {number} time - The timestamp assigned to the datapoint
2812-
* @returns A Promise indicating success or failure of upload
28132828
*/
2814-
function addDataPoint(time, name, value) {
2829+
function addDataPoint(time, sensorName, value) {
28152830

2816-
if (!timeSeries.includes(name)) {
2831+
if (!timeSeries.includes(sensorName)) {
28172832
throw Error("invalid time-series name")
28182833
}
28192834
if (typeof value !== "number") {
@@ -2829,14 +2844,14 @@ var edgeML = (function (require$$0, require$$1) {
28292844

28302845
value = Math.round(value * 100) / 100;
28312846

2832-
if (dataStore.data.every((elm) => elm.name !== name)) {
2847+
if (dataStore.data.every((elm) => elm.name !== sensorName)) {
28332848
dataStore.data.push({
2834-
name: name,
2849+
name: sensorName,
28352850
data: [[time, value]],
28362851
});
28372852
} else {
28382853
const idx = dataStore.data.findIndex(
2839-
(elm) => elm.name === name
2854+
(elm) => elm.name === sensorName
28402855
);
28412856
dataStore.data[idx].data.push([time, value]);
28422857

dist/index.esm.js

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17033,8 +17033,9 @@ const Predictor = class Predictor {
1703317033
* @param {string[]} labels
1703417034
* @param {{ center: { [featureName: string]: number }, scale: { [featureName: string]: number } }} scaler
1703517035
* @param {{ windowingMode: "time" | "sample" }} options
17036+
* @param {boolean} useDeviceTime - True if you want to use timestamps generated by the server
1703617037
*/
17037-
constructor(predictor, sensors, windowSize, labels, scaler, { windowingMode = "sample" } = {}) {
17038+
constructor(predictor, sensors, windowSize, labels, scaler, { windowingMode = "sample", useDeviceTime = false } = {}) {
1703817039
/** @type {(input: number[]) => number[]} */
1703917040
this.predictor = predictor;
1704017041
/** @type {string[]} */
@@ -17048,6 +17049,9 @@ const Predictor = class Predictor {
1704817049

1704917050
/** @type {boolean} */
1705017051
this.windowModeMs = windowSize < 0 || windowingMode === "time";
17052+
/** @type {boolean} */
17053+
this.useDeviceTime = useDeviceTime;
17054+
1705117055
this.lastPruneTime = 0;
1705217056
this.lastAddTime = 0;
1705317057

@@ -17059,15 +17063,24 @@ const Predictor = class Predictor {
1705917063
}
1706017064

1706117065
/**
17062-
* addDatapoint
17066+
* addDataPoint
17067+
* @param {number} time - The timestamp assigned to the datapoint
1706317068
* @param {string} sensorName
1706417069
* @param {number} value
17065-
* @param {number | null} time use a predefined timestamp, or null if the timestamp should be generated
1706617070
*/
17067-
addDatapoint = (sensorName, value, time = null) => {
17071+
addDataPoint = (time, sensorName, value) => {
1706817072
if (typeof value !== 'number') throw new TypeError('Datapoint is not a number');
1706917073
if (!this.sensors.includes(sensorName)) throw new TypeError('Sensor is not valid');
17070-
if (time === null) time = Date.now();
17074+
// TODO: see #16 use if (time === null) time = Date.now()
17075+
if (!this.useDeviceTime && typeof time !== "number") {
17076+
throw new Error("Provide a valid timestamp");
17077+
}
17078+
if (this.useDeviceTime) {
17079+
time = Date.now();
17080+
}
17081+
17082+
//TODO: see #17 no clue why see Collector
17083+
value = Math.round(value * 100) / 100;
1707117084

1707217085
this.lastAddTime = time;
1707317086
this.store[sensorName].push([time, value]);
@@ -17106,6 +17119,9 @@ const Predictor = class Predictor {
1710617119
let window;
1710717120
if (this.windowModeMs) {
1710817121
window = Predictor._sliceByTime(samples, this.lastAddTime - this.windowSize);
17122+
if (window.length < 1 ) {
17123+
throw new PredictorError("Not enough samples")
17124+
}
1710917125
} else {
1711017126
window = samples.slice(-this.windowSize);
1711117127
if (window.length < this.windowSize) {
@@ -17309,15 +17325,14 @@ async function datasetCollector(
1730917325
var timeSeries = timeSeries;
1731017326

1731117327
/**
17312-
* Uploads a vlaue for a specific timestamp to a datasets timeSeries with name sensorName
17313-
* @param {string} name - The name of the timeSeries to upload the value to
17328+
* Uploads a value for a specific timestamp to a dataset's timeSeries with name sensorName
17329+
* @param {string} sensorName - The name of the timeSeries to upload the value to
1731417330
* @param {number} value - The datapoint to upload
1731517331
* @param {number} time - The timestamp assigned to the datapoint
17316-
* @returns A Promise indicating success or failure of upload
1731717332
*/
17318-
function addDataPoint(time, name, value) {
17333+
function addDataPoint(time, sensorName, value) {
1731917334

17320-
if (!timeSeries.includes(name)) {
17335+
if (!timeSeries.includes(sensorName)) {
1732117336
throw Error("invalid time-series name")
1732217337
}
1732317338
if (typeof value !== "number") {
@@ -17333,14 +17348,14 @@ async function datasetCollector(
1733317348

1733417349
value = Math.round(value * 100) / 100;
1733517350

17336-
if (dataStore.data.every((elm) => elm.name !== name)) {
17351+
if (dataStore.data.every((elm) => elm.name !== sensorName)) {
1733717352
dataStore.data.push({
17338-
name: name,
17353+
name: sensorName,
1733917354
data: [[time, value]],
1734017355
});
1734117356
} else {
1734217357
const idx = dataStore.data.findIndex(
17343-
(elm) => elm.name === name
17358+
(elm) => elm.name === sensorName
1734417359
);
1734517360
dataStore.data[idx].data.push([time, value]);
1734617361

dist/index.js

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17049,8 +17049,9 @@ const Predictor = class Predictor {
1704917049
* @param {string[]} labels
1705017050
* @param {{ center: { [featureName: string]: number }, scale: { [featureName: string]: number } }} scaler
1705117051
* @param {{ windowingMode: "time" | "sample" }} options
17052+
* @param {boolean} useDeviceTime - True if you want to use timestamps generated by the server
1705217053
*/
17053-
constructor(predictor, sensors, windowSize, labels, scaler, { windowingMode = "sample" } = {}) {
17054+
constructor(predictor, sensors, windowSize, labels, scaler, { windowingMode = "sample", useDeviceTime = false } = {}) {
1705417055
/** @type {(input: number[]) => number[]} */
1705517056
this.predictor = predictor;
1705617057
/** @type {string[]} */
@@ -17064,6 +17065,9 @@ const Predictor = class Predictor {
1706417065

1706517066
/** @type {boolean} */
1706617067
this.windowModeMs = windowSize < 0 || windowingMode === "time";
17068+
/** @type {boolean} */
17069+
this.useDeviceTime = useDeviceTime;
17070+
1706717071
this.lastPruneTime = 0;
1706817072
this.lastAddTime = 0;
1706917073

@@ -17075,15 +17079,24 @@ const Predictor = class Predictor {
1707517079
}
1707617080

1707717081
/**
17078-
* addDatapoint
17082+
* addDataPoint
17083+
* @param {number} time - The timestamp assigned to the datapoint
1707917084
* @param {string} sensorName
1708017085
* @param {number} value
17081-
* @param {number | null} time use a predefined timestamp, or null if the timestamp should be generated
1708217086
*/
17083-
addDatapoint = (sensorName, value, time = null) => {
17087+
addDataPoint = (time, sensorName, value) => {
1708417088
if (typeof value !== 'number') throw new TypeError('Datapoint is not a number');
1708517089
if (!this.sensors.includes(sensorName)) throw new TypeError('Sensor is not valid');
17086-
if (time === null) time = Date.now();
17090+
// TODO: see #16 use if (time === null) time = Date.now()
17091+
if (!this.useDeviceTime && typeof time !== "number") {
17092+
throw new Error("Provide a valid timestamp");
17093+
}
17094+
if (this.useDeviceTime) {
17095+
time = Date.now();
17096+
}
17097+
17098+
//TODO: see #17 no clue why see Collector
17099+
value = Math.round(value * 100) / 100;
1708717100

1708817101
this.lastAddTime = time;
1708917102
this.store[sensorName].push([time, value]);
@@ -17122,6 +17135,9 @@ const Predictor = class Predictor {
1712217135
let window;
1712317136
if (this.windowModeMs) {
1712417137
window = Predictor._sliceByTime(samples, this.lastAddTime - this.windowSize);
17138+
if (window.length < 1 ) {
17139+
throw new PredictorError("Not enough samples")
17140+
}
1712517141
} else {
1712617142
window = samples.slice(-this.windowSize);
1712717143
if (window.length < this.windowSize) {
@@ -17325,15 +17341,14 @@ async function datasetCollector(
1732517341
var timeSeries = timeSeries;
1732617342

1732717343
/**
17328-
* Uploads a vlaue for a specific timestamp to a datasets timeSeries with name sensorName
17329-
* @param {string} name - The name of the timeSeries to upload the value to
17344+
* Uploads a value for a specific timestamp to a dataset's timeSeries with name sensorName
17345+
* @param {string} sensorName - The name of the timeSeries to upload the value to
1733017346
* @param {number} value - The datapoint to upload
1733117347
* @param {number} time - The timestamp assigned to the datapoint
17332-
* @returns A Promise indicating success or failure of upload
1733317348
*/
17334-
function addDataPoint(time, name, value) {
17349+
function addDataPoint(time, sensorName, value) {
1733517350

17336-
if (!timeSeries.includes(name)) {
17351+
if (!timeSeries.includes(sensorName)) {
1733717352
throw Error("invalid time-series name")
1733817353
}
1733917354
if (typeof value !== "number") {
@@ -17349,14 +17364,14 @@ async function datasetCollector(
1734917364

1735017365
value = Math.round(value * 100) / 100;
1735117366

17352-
if (dataStore.data.every((elm) => elm.name !== name)) {
17367+
if (dataStore.data.every((elm) => elm.name !== sensorName)) {
1735317368
dataStore.data.push({
17354-
name: name,
17369+
name: sensorName,
1735517370
data: [[time, value]],
1735617371
});
1735717372
} else {
1735817373
const idx = dataStore.data.findIndex(
17359-
(elm) => elm.name === name
17374+
(elm) => elm.name === sensorName
1736017375
);
1736117376
dataStore.data[idx].data.push([time, value]);
1736217377

0 commit comments

Comments
 (0)