@sedni/cloud_common 1.0.4 → 1.0.6

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.
@@ -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
- timestamp: {
22
- type: Number,
23
- required: true,
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
@@ -127,7 +152,7 @@ channeldataBucketSchema.statics.insertDataPoint = async function(timestamp, valu
127
152
  date.setMilliseconds(0);
128
153
 
129
154
  // Upsert the data point
130
- const bucket = await this.updateOne(
155
+ const bucket = await this.findOneAndUpdate(
131
156
  {
132
157
  start_date: date
133
158
  },
@@ -153,7 +178,8 @@ channeldataBucketSchema.statics.insertDataPoint = async function(timestamp, valu
153
178
  }
154
179
  },
155
180
  {
156
- upsert: true
181
+ upsert: true,
182
+ new: true
157
183
  }
158
184
  );
159
185
 
@@ -222,7 +248,27 @@ channeldataBucketSchema.statics.getData = async function(start_date, end_date, p
222
248
  }
223
249
  });
224
250
 
225
- return plain ? data.flatMap(bucket => bucket.data) : data;
251
+ if(!data || data.length === 0)
252
+ {
253
+ return [];
254
+ }
255
+
256
+ // Remove the data inside the bucket that is not in the specified date range
257
+ data[0].removeDataPointsOutsideDateRange(start_date, end_date);
258
+
259
+ if(data.length > 1)
260
+ {
261
+ data[1].removeDataPointsOutsideDateRange(start_date, end_date);
262
+ }
263
+
264
+ return plain ? data.flatMap(bucket => {
265
+ return bucket.data.map(data_point => {
266
+ return {
267
+ timestamp: data_point.timestamp,
268
+ value: data_point.value
269
+ };
270
+ });
271
+ }) : data;
226
272
  };
227
273
 
228
274
 
@@ -42,6 +42,10 @@ const historySchema = new mongoose.Schema({
42
42
  type: String,
43
43
  required: false,
44
44
  },
45
+ alarm_data: {
46
+ type: Object,
47
+ required: false,
48
+ },
45
49
  }, {
46
50
  timestamps: {
47
51
  createdAt: false,
@@ -55,13 +59,13 @@ const historySchema = new mongoose.Schema({
55
59
  ret.id = ret._id;
56
60
  delete ret._id;
57
61
  delete ret.__v;
58
- ret.alarm_timestamp = ret.alarm_timestamp.getTime();
62
+ ret.alarm_timestamp = new Date(ret.alarm_timestamp).getTime();
59
63
  ret.alarm = {
60
- priority: ret.alarm_priority,
61
- state: ret.alarm_original_state ?? "Inactive",
62
- type: ret.alarm_type,
63
- value: ret.alarm_value === null ? "inf" : `${ret.alarm_value}`,
64
- timestamp: new Date(ret.alarm_timestamp).getTime()
64
+ alarm_priority: ret.alarm_priority,
65
+ alarm_state: ret.alarm_original_state ?? "Inactive",
66
+ alarm_type: ret.alarm_type,
67
+ alarm_value: ret.alarm_value === null ? "inf" : `${ret.alarm_value}`,
68
+ alarm_timestamp: new Date(ret.alarm_timestamp).getTime()
65
69
  };
66
70
  },
67
71
  },
@@ -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: true,
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
+ };
@@ -1,12 +1,13 @@
1
1
  const EventCategories =
2
2
  {
3
- ACCESS_CONTROL: "ACCESS_CONTROL",
4
- REQUEST_FAILURE: "REQUEST_FAILURE",
5
- OS: "OS",
6
- CONTROL: "CONTROL",
7
- BACKUP: "BACKUP",
8
- CONFIG_CHANGE: "CONFIG_CHANGE",
9
- LOGS: "LOGS"
3
+ ACCESS_CONTROL: "AccessControl",
4
+ REQUEST_ERROR: "RequestErrors",
5
+ OS: "OperatingSystemEvents",
6
+ CONTROL: "ControlSystemEvents",
7
+ BACKUP: "BackupAndRestoreEvents",
8
+ CONFIG_CHANGE: "ConfigurationChanges",
9
+ LOGS: "AuditLogEvents",
10
+ POTENTIAL_ATTACK: "PotentialAttackActivity",
10
11
  };
11
12
 
12
13
  module.exports = {
@@ -0,0 +1,15 @@
1
+ const UnitTypes =
2
+ {
3
+ AIM18 : "Aim18",
4
+ DIM36 : "Dim36",
5
+ DIOM24 : "Diom24",
6
+ KLIM : "Klim",
7
+ LUM : "Lum",
8
+ PMM : "Pmm",
9
+ SLIM : "Slim",
10
+ TIM28 : "Tim28",
11
+ };
12
+
13
+ module.exports = {
14
+ UnitTypes
15
+ };
package/index.js CHANGED
@@ -38,6 +38,7 @@ const Types = {
38
38
  * EXTERNAL
39
39
  */
40
40
  const mongoose = require("mongoose");
41
+ const mongooseAutopopulate = require("mongoose-autopopulate");
41
42
  const mongoosePaginate = require("mongoose-paginate-v2");
42
43
  const mongooseAggregatePaginate = require("mongoose-aggregate-paginate-v2");
43
44
 
@@ -48,4 +49,5 @@ module.exports = {
48
49
  mongoose,
49
50
  mongoosePaginate,
50
51
  mongooseAggregatePaginate,
52
+ mongooseAutopopulate,
51
53
  };
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "@sedni/cloud_common",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
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": "^5.1.1",
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/eslint.config.js DELETED
@@ -1,38 +0,0 @@
1
- const globals = require("globals");
2
- const pluginJs = require("@eslint/js");
3
- const nodePlugin = require("eslint-plugin-n");
4
-
5
- module.exports = [
6
- pluginJs.configs.recommended,
7
- nodePlugin.configs["flat/recommended-script"],
8
- {
9
- files: ["app/**/*.js", "index.js"],
10
- languageOptions: {
11
- "ecmaVersion": "latest",
12
- "sourceType": "commonjs",
13
- "globals": globals.node,
14
- },
15
- rules: {
16
- "prefer-const": ["error"],
17
- "no-var": ["error"],
18
- "semi": ["error", "always"],
19
- "indent": ["error", 4],
20
- "quotes": ["error", "double"],
21
- "no-unused-vars": [
22
- "error",
23
- {
24
- "varsIgnorePattern": "should|expect|supertest|assert",
25
- },
26
- ],
27
- "curly": ["error", "all"],
28
- "brace-style": ["error", "allman"],
29
- "n/no-missing-require" : "off",
30
- "n/no-unpublished-require": [
31
- "error",
32
- {
33
- "allowModules": ["swagger-ui-express"],
34
- },
35
- ],
36
- }
37
- },
38
- ];