@sedni/cloud_common 1.0.4 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/app/models/ChannelDataBucket.js +48 -10
- package/app/models/Unit.js +3 -1
- package/app/types/channel.types.js +43 -0
- package/app/types/unit.types.js +15 -0
- package/eslint.config.js +2 -0
- package/package.json +7 -4
- package/playground.js +11 -0
|
@@ -3,6 +3,19 @@ const mongooseAutopopulate = require("mongoose-autopopulate");
|
|
|
3
3
|
const mongoosePaginate = require("mongoose-paginate-v2");
|
|
4
4
|
const mongooseAggregatePaginate = require("mongoose-aggregate-paginate-v2");
|
|
5
5
|
|
|
6
|
+
const dataPointSchema = new mongoose.Schema({
|
|
7
|
+
timestamp: {
|
|
8
|
+
type: Number,
|
|
9
|
+
required: true,
|
|
10
|
+
},
|
|
11
|
+
value: {
|
|
12
|
+
type: Number,
|
|
13
|
+
required: true,
|
|
14
|
+
}
|
|
15
|
+
}, {
|
|
16
|
+
_id: false,
|
|
17
|
+
});
|
|
18
|
+
|
|
6
19
|
const channeldataBucketSchema = new mongoose.Schema({
|
|
7
20
|
start_date: {
|
|
8
21
|
type: Date,
|
|
@@ -17,16 +30,11 @@ const channeldataBucketSchema = new mongoose.Schema({
|
|
|
17
30
|
return new Date(Date.now() + 1000 * 60 * 60); // 1 hour from now
|
|
18
31
|
}
|
|
19
32
|
},
|
|
20
|
-
data :
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
value: {
|
|
26
|
-
type: Number,
|
|
27
|
-
required: true,
|
|
28
|
-
}
|
|
29
|
-
}],
|
|
33
|
+
data : {
|
|
34
|
+
type: [dataPointSchema],
|
|
35
|
+
required: true,
|
|
36
|
+
default: [],
|
|
37
|
+
},
|
|
30
38
|
size : {
|
|
31
39
|
type: Number,
|
|
32
40
|
required: true,
|
|
@@ -99,6 +107,23 @@ channeldataBucketSchema.methods.insertDataPoint = async function(timestamp, valu
|
|
|
99
107
|
await this.save();
|
|
100
108
|
};
|
|
101
109
|
|
|
110
|
+
/**
|
|
111
|
+
* Remove data points outside the specified date range within the bucket.
|
|
112
|
+
*
|
|
113
|
+
* IT WILL NOT SAVE THE BUCKET, YOU NEED TO CALL save() AFTER THIS FUNCTION TO SAVE THE BUCKET
|
|
114
|
+
* @param {Number | Date} start_date The start date of the date range
|
|
115
|
+
* @param {Number | Date} end_date The end date of the date range
|
|
116
|
+
*/
|
|
117
|
+
channeldataBucketSchema.methods.removeDataPointsOutsideDateRange = function(start_date, end_date)
|
|
118
|
+
{
|
|
119
|
+
start_date = start_date instanceof Date ? start_date.getTime() : start_date;
|
|
120
|
+
end_date = end_date instanceof Date ? end_date.getTime() : end_date;
|
|
121
|
+
|
|
122
|
+
this.data = this.data.filter(data_point => data_point.timestamp >= start_date && data_point.timestamp < end_date);
|
|
123
|
+
this.size = this.data.length;
|
|
124
|
+
this.sum = this.data.reduce((sum, data_point) => sum + data_point.value, 0);
|
|
125
|
+
};
|
|
126
|
+
|
|
102
127
|
/**
|
|
103
128
|
* Check if the bucket is closed, meaning that it is no longer accepting data points.
|
|
104
129
|
* @param {Number} timestamp The timestamp to check if the bucket is closed
|
|
@@ -222,6 +247,19 @@ channeldataBucketSchema.statics.getData = async function(start_date, end_date, p
|
|
|
222
247
|
}
|
|
223
248
|
});
|
|
224
249
|
|
|
250
|
+
if(!data || data.length === 0)
|
|
251
|
+
{
|
|
252
|
+
return [];
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// Remove the data inside the bucket that is not in the specified date range
|
|
256
|
+
data[0].data.removeDataPointsOutsideDateRange(start_date, end_date);
|
|
257
|
+
|
|
258
|
+
if(data.length > 1)
|
|
259
|
+
{
|
|
260
|
+
data[1].data.removeDataPointsOutsideDateRange(start_date, end_date);
|
|
261
|
+
}
|
|
262
|
+
|
|
225
263
|
return plain ? data.flatMap(bucket => bucket.data) : data;
|
|
226
264
|
};
|
|
227
265
|
|
package/app/models/Unit.js
CHANGED
|
@@ -2,6 +2,7 @@ const mongoose = require("mongoose");
|
|
|
2
2
|
const mongooseAutopopulate = require("mongoose-autopopulate");
|
|
3
3
|
const mongoosePaginate = require("mongoose-paginate-v2");
|
|
4
4
|
const mongooseAggregatePaginate = require("mongoose-aggregate-paginate-v2");
|
|
5
|
+
const { UnitTypes } = require("../types/unit.types");
|
|
5
6
|
|
|
6
7
|
const unitSchema = new mongoose.Schema({
|
|
7
8
|
unit_id: {
|
|
@@ -16,6 +17,7 @@ const unitSchema = new mongoose.Schema({
|
|
|
16
17
|
unit_type: {
|
|
17
18
|
type: String,
|
|
18
19
|
required: true,
|
|
20
|
+
enum : Object.values(UnitTypes),
|
|
19
21
|
},
|
|
20
22
|
unit_internal_description: {
|
|
21
23
|
type: String,
|
|
@@ -23,7 +25,7 @@ const unitSchema = new mongoose.Schema({
|
|
|
23
25
|
},
|
|
24
26
|
unit_cabinet_id: {
|
|
25
27
|
type: String,
|
|
26
|
-
required:
|
|
28
|
+
required: false,
|
|
27
29
|
},
|
|
28
30
|
}, {
|
|
29
31
|
timestamps: true,
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
const ChannelBaseTypes =
|
|
2
|
+
{
|
|
3
|
+
VIRTUAL_ANALOG : "VirtualAnalog",
|
|
4
|
+
VIRTUAL_DIGITAL : "VirtualDigital",
|
|
5
|
+
VIRTUAL_STRING : "VirtualString",
|
|
6
|
+
WIRED_ANALOG : "WiredAnalog",
|
|
7
|
+
WIRED_DIGITAL : "WiredDigital",
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const ChannelSpecificTypes =
|
|
11
|
+
{
|
|
12
|
+
// Wired types
|
|
13
|
+
DIGITAL_INPUT : "DigitalInput",
|
|
14
|
+
DIGITAL_OUTPUT : "DigitalOutput",
|
|
15
|
+
ANALOG_INPUT : "AnalogInput",
|
|
16
|
+
ANALOG_OUTPUT : "AnalogOutput",
|
|
17
|
+
|
|
18
|
+
// Virtual types
|
|
19
|
+
ANALOG_SOFTWARE : "AnalogSoftware",
|
|
20
|
+
DIGITAL_SOFTWARE : "DigitalSoftware",
|
|
21
|
+
VIRTUAL_STRING : "VirtualString",
|
|
22
|
+
COMMAND : "Command",
|
|
23
|
+
|
|
24
|
+
// Virtual logic types
|
|
25
|
+
BROADCAST_COMMAND : "BroadcastCommand",
|
|
26
|
+
|
|
27
|
+
// To be deleted in the near future
|
|
28
|
+
SOFTWARE: "Software",
|
|
29
|
+
|
|
30
|
+
ANALOG_TIMER : "AnalogTimer",
|
|
31
|
+
|
|
32
|
+
// Serial types
|
|
33
|
+
SERIAL_DIGITAL_INPUT : "SerialDigitalInput",
|
|
34
|
+
SERIAL_DIGITAL_OUTPUT : "SerialDigitalOutput",
|
|
35
|
+
SERIAL_LINE_COMMAND : "SerialLineCommand",
|
|
36
|
+
SERIAL_ANALOG_INPUT : "SerialAnalogInput",
|
|
37
|
+
SERIAL_ANALOG_OUTPUT : "SerialAnalogOutput",
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
module.exports = {
|
|
41
|
+
ChannelBaseTypes,
|
|
42
|
+
ChannelSpecificTypes,
|
|
43
|
+
};
|
package/eslint.config.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
const globals = require("globals");
|
|
2
2
|
const pluginJs = require("@eslint/js");
|
|
3
3
|
const nodePlugin = require("eslint-plugin-n");
|
|
4
|
+
const mochaPlugin = require("eslint-plugin-mocha");
|
|
4
5
|
|
|
5
6
|
module.exports = [
|
|
7
|
+
mochaPlugin.configs.recommended,
|
|
6
8
|
pluginJs.configs.recommended,
|
|
7
9
|
nodePlugin.configs["flat/recommended-script"],
|
|
8
10
|
{
|
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sedni/cloud_common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "Common package for all types, resources and tools of Diamar Cloud",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "mocha",
|
|
7
|
+
"test": "cross-env NODE_ENV=internal_test mocha --recursive --exit app/test/*",
|
|
8
8
|
"coverage": "nyc --include 'app/**/*controller.js' mocha",
|
|
9
9
|
"lint": "eslint app/*",
|
|
10
|
-
"lint:fix": "eslint --fix app/*"
|
|
10
|
+
"lint:fix": "eslint --fix app/*",
|
|
11
|
+
"playground": "node playground.js"
|
|
11
12
|
},
|
|
12
13
|
"author": "Jose Luis Silvestre García",
|
|
13
14
|
"license": "ISC",
|
|
@@ -19,12 +20,14 @@
|
|
|
19
20
|
},
|
|
20
21
|
"devDependencies": {
|
|
21
22
|
"@eslint/js": "^9.12.0",
|
|
22
|
-
"chai": "^
|
|
23
|
+
"chai": "^4.0.0",
|
|
23
24
|
"eslint": "^9.12.0",
|
|
24
25
|
"eslint-plugin-n": "^17.11.0",
|
|
25
26
|
"globals": "^15.11.0",
|
|
26
27
|
"mocha": "^10.7.3",
|
|
27
28
|
"nyc": "^17.0.0",
|
|
29
|
+
"proxyquire": "^2.1.3",
|
|
30
|
+
"sinon": "^19.0.2",
|
|
28
31
|
"supertest": "^7.0.0"
|
|
29
32
|
}
|
|
30
33
|
}
|
package/playground.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
|
|
2
|
+
// Get the timestamp before importing the models
|
|
3
|
+
const start = Date.now();
|
|
4
|
+
|
|
5
|
+
const ChannelDataBucket = require("./app/test/data/ChannelDataBucket");
|
|
6
|
+
|
|
7
|
+
// Get the timestamp after importing the models
|
|
8
|
+
const end = Date.now();
|
|
9
|
+
|
|
10
|
+
// Print the difference
|
|
11
|
+
console.log(`Time taken to import the models: ${end - start}ms`);
|