@5minds/node-red-contrib-processcube-elasticsearch 0.2.0 → 0.3.0-add-update-cycle-4db609-m3r9rkdh

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.
@@ -8,39 +8,92 @@
8
8
  },
9
9
  defaults: {
10
10
  name: { value: "" },
11
- url: {value:"http://localhost:9200"},
11
+ url: { value: "http://localhost:9200" },
12
+ urlType: { value: "str" },
13
+ usernameType: { value: "str" },
14
+ passwordType: { value: "str" },
15
+ indexType: { value: "str" },
12
16
  filename: { value: "log-elastic.log", required: true },
13
17
  maxsize: { value: 1, required: true, validate: function(v) { return v >= 1 } },
14
18
  maxfiles: { value: 2, required: true, validate: function(v) { return v >= 1 } },
15
19
  },
16
- label: function() { return this.name; },
20
+ label: function () { return this.name; },
21
+ oneditprepare: function () {
22
+ // URL
23
+ $("#node-config-input-url").typedInput({
24
+ type: this.urlType,
25
+ types: [
26
+ "str",
27
+ "global"
28
+ ],
29
+ typeField: "#node-config-input-urlType",
30
+ value: this.url
31
+ })
32
+
33
+ // Username
34
+ $("#node-config-input-username").typedInput({
35
+ type: this.usernameType,
36
+ types:[
37
+ "str",
38
+ "global"
39
+ ],
40
+ typeField: "#node-config-input-usernameType",
41
+ value: this.credentials.username
42
+ })
43
+
44
+ // Password
45
+ $("#node-config-input-password").typedInput({
46
+ type: this.passwordType,
47
+ types:[
48
+ "str",
49
+ "global"
50
+ ],
51
+ typeField: "#node-config-input-passwordType",
52
+ value: this.credentials.password
53
+ })
54
+
55
+ // Index
56
+ $("#node-config-input-index").typedInput({
57
+ type: this.indexType,
58
+ types:[
59
+ "str",
60
+ "global"
61
+ ],
62
+ typeField: "#node-config-input-indexType",
63
+ value: this.credentials.index
64
+ })
65
+ }
17
66
  });
18
- </script>
19
-
20
- <script type="text/html" data-template-name="elastic-search-logger">
21
- <div class="form-row">
22
- <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
23
- <input type="text" id="node-config-input-name" placeholder="Name">
24
- </div>
25
- <div id="elkfields">
67
+ </script>
68
+
69
+ <script type="text/html" data-template-name="elastic-search-logger">
70
+ <div class="form-row">
71
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
72
+ <input type="text" id="node-config-input-name" placeholder="Name">
73
+ </div>
74
+ <div id="elkfields">
26
75
  <div class="form-row">
27
- <label style="width:100%;"><span>ElasticSearch options</span></label>
76
+ <label style="width:100%;"><span>ElasticSearch options</span></label>
28
77
  </div>
29
78
  <div class="form-row" style="padding-left:20px;">
30
- <label for="node-config-input-url"><i class="fa fa-plug"></i> Elastic URL</label>
31
- <input type="text" id="node-config-input-url">
79
+ <label for="node-config-input-url"><i class="fa fa-plug"></i> Elastic URL</label>
80
+ <input type="text" id="node-config-input-url">
81
+ <input type="hidden" id="node-config-input-urlType">
32
82
  </div>
33
83
  <div class="form-row" style="padding-left:20px;">
34
- <label for="node-config-input-username"><i class="fa fa-user"></i> Username</label>
35
- <input type="text" id="node-config-input-username">
84
+ <label for="node-config-input-username"><i class="fa fa-user"></i> Username</label>
85
+ <input type="text" id="node-config-input-username">
86
+ <input type="hidden" id="node-config-input-usernameType">
36
87
  </div>
37
88
  <div class="form-row" style="padding-left:20px;">
38
- <label for="node-config-input-password"><i class="fa fa-key"></i> Password</label>
39
- <input type="password" id="node-config-input-password">
89
+ <label for="node-config-input-password"><i class="fa fa-key"></i> Password</label>
90
+ <input type="password" id="node-config-input-password">
91
+ <input type="hidden" id="node-config-input-passwordType">
40
92
  </div>
41
93
  <div class="form-row" style="padding-left:20px;">
42
- <label for="node-config-input-index"><i class="fa fa-tag"></i> Index</label>
43
- <input type="text" id="node-config-input-index">
94
+ <label for="node-config-input-index"><i class="fa fa-tag"></i> Index</label>
95
+ <input type="text" id="node-config-input-index">
96
+ <input type="hidden" id="node-config-input-indexType">
44
97
  </div>
45
98
  </div>
46
99
  </script>
@@ -1,201 +1,145 @@
1
1
  module.exports = function (RED) {
2
- "use strict";
3
-
4
- function LogElasticLoggerNode(config) {
5
- let winston = require('winston');
6
- let winstonElasticSearch = require('winston-elasticsearch');
7
- let os = require("os");
8
- this.hostname = os.hostname();
9
-
10
- RED.nodes.createNode(this, config);
11
- this.logger = null;
12
- let transports = [];
13
-
14
- // Elastic settings
15
- let url = config.url;
16
- let user = this.credentials.username || '';
17
- let password = this.credentials.password || '';
18
- let index = this.credentials.index || '';
19
- if (index == '') {
20
- this.error("Elastic search index is not set");
21
- }
22
- index = index.toLowerCase();
23
- if (url) {
24
- const elasticSearchTransport = new winstonElasticSearch.ElasticsearchTransport({
25
- clientOpts: {
26
- node: url,
27
- auth: {
28
- username: user,
29
- password: password
30
- },
31
- ssl: {
32
- // accept any
33
- rejectUnauthorized: false
34
- }
35
- },
36
- transformer: (logData) => setElasticFields(logData, this),
37
- index: index
38
- })
2
+ 'use strict';
3
+
4
+ function LogElasticLoggerNode(config) {
5
+ let winston = require('winston');
6
+ let winstonElasticSearch = require('winston-elasticsearch');
7
+
8
+ RED.nodes.createNode(this, config);
9
+ this.logger = null;
10
+ let transports = [];
11
+
12
+ const stopRefreshing = periodicallyRefreshElasticSettings(10000);
13
+
14
+ function periodicallyRefreshElasticSettings(timeOut) {
15
+ let url, user, password, index;
16
+
17
+ function refresh() {
18
+ // Elastic settings
19
+ let newUrl = RED.util.evaluateNodeProperty(config.url, config.urlType, this);
20
+ if (newUrl == '') {
21
+ this.error('Elastic search url is not set');
22
+ return;
23
+ }
24
+
25
+ let newUser = RED.util.evaluateNodeProperty(this.credentials.username, config.usernameType, this);
26
+ if (newUser == '') {
27
+ this.error('Elastic search username is not set');
28
+ return;
29
+ }
30
+
31
+ let newPassword = RED.util.evaluateNodeProperty(this.credentials.password, config.passwordType, this);
32
+ if (newPassword == '') {
33
+ this.error('Elastic search password is not set');
34
+ return;
35
+ }
36
+
37
+ let newIndex = RED.util.evaluateNodeProperty(this.credentials.index, config.indexType, this);
38
+ if (newIndex == '') {
39
+ this.error('Elastic search index is not set');
40
+ return;
41
+ }
42
+
43
+ if (newUrl === url && newUser === user && newPassword === password && newIndex === index) {
44
+ return;
45
+ }
46
+
47
+ if (this.logger) this.logger.close();
48
+
49
+ url = newUrl;
50
+ user = newUser;
51
+ password = newPassword;
52
+ index = newIndex;
53
+
54
+ index = index.toLowerCase();
55
+ if (url) {
56
+ const elasticSearchTransport = new winstonElasticSearch.ElasticsearchTransport({
57
+ clientOpts: {
58
+ node: url,
59
+ auth: {
60
+ username: user,
61
+ password: password,
62
+ },
63
+ ssl: {
64
+ // accept any
65
+ rejectUnauthorized: false,
66
+ },
67
+ },
68
+ transformer: (logData) => setElasticFields(logData, this),
69
+ index: index,
70
+ });
71
+
72
+ transports = [elasticSearchTransport];
73
+
74
+ elasticSearchTransport.on('error', (error) => {
75
+ this.error(`Error in elasticSearchTransport caught: ${error.message}`);
76
+ console.error('Error in elasticSearchTransport caught', error);
77
+ });
78
+ }
79
+
80
+ let logLevels = {
81
+ levels: {
82
+ Error: 0,
83
+ Warning: 1,
84
+ Information: 2,
85
+ Debug: 3,
86
+ },
87
+ };
88
+ this.logger = new winston.createLogger({
89
+ exitOnError: false,
90
+ level: 'Debug',
91
+ levels: logLevels.levels,
92
+ transports: transports,
93
+ });
94
+
95
+ this.debug('elastic-search logger created');
96
+ }
97
+
98
+ refresh();
99
+ const intervalId = setInterval(refresh, timeOut);
100
+
101
+ return () => clearInterval(intervalId);
102
+ }
39
103
 
40
- transports.push(elasticSearchTransport);
104
+ this.on('close', function (removed, done) {
105
+ stopRefreshing();
106
+ // close logger
107
+ if (this.logger) {
108
+ this.logger.close();
109
+ }
41
110
 
42
- elasticSearchTransport.on('error', (error) => {
43
- this.error(`Error in elasticSearchTransport caught: ${error.message}`);
44
- console.error('Error in elasticSearchTransport caught', error);
45
- });
46
- }
111
+ this.debug('elastic-search logger closed');
47
112
 
48
- let logLevels = {
49
- levels: {
50
- Error: 0,
51
- Warning: 1,
52
- Information: 2,
53
- Debug: 3
54
- }
113
+ if (done) done();
114
+ });
55
115
  }
56
- this.logger = new winston.createLogger({
57
- exitOnError: false,
58
- level: 'Debug',
59
- levels: logLevels.levels,
60
- transports: transports
61
- });
62
-
63
- this.debug("elastic-search logger created");
64
116
 
65
- this.on('close', function (removed, done) {
66
- // close logger
67
- if (this.loggger) {
68
- this.logger.close();
69
- }
70
-
71
- this.debug("elastic-search logger closed");
72
-
73
- if (done) done();
74
- });
75
- }
76
-
77
- function setElasticFields(logData, node) {
78
- logData = mergeFieldsIntoMessage(logData, node);
79
-
80
- let { ElasticsearchTransformer } = require('winston-elasticsearch');
81
- const transformed = ElasticsearchTransformer(logData);
82
- transformed['@timestamp'] = logData.timestamp ? logData.timestamp : new Date().toISOString();
83
- transformed.message = logData.message;
84
- transformed.messageTemplate = logData.messageTemplate;
85
- transformed.severity = logData.level;
86
- transformed.level = logData.level;
87
- transformed.fields = logData.meta;
88
-
89
- if (logData.meta['transaction.id']) transformed.transaction = { id: logData.meta['transaction.id'] };
90
- if (logData.meta['trace.id']) transformed.trace = { id: logData.meta['trace.id'] };
91
- if (logData.meta['span.id']) transformed.span = { id: logData.meta['span.id'] };
92
-
93
- return transformed;
94
- };
95
-
96
- RED.nodes.registerType("elastic-search-logger", LogElasticLoggerNode,
97
- {
98
- credentials: {
99
- username: { type: "text" },
100
- password: { type: "password" },
101
- index: { type: "text" }
102
- }
103
- });
104
-
105
- LogElasticLoggerNode.prototype.addToLog = function addTolog(loglevel, msg) {
106
- this.logger.log(loglevel, msg.payload.message, msg.payload.meta);
107
- }
108
-
109
- // Extract {fields} from message and add them to meta.
110
- // Store original message in messageTemplate.
111
- // Replace {fields} with values in message.
112
- function mergeFieldsIntoMessage(logData, node) {
113
- try {
114
- if (logData.message == null) {
115
- node.error("Message is null");
116
- return logData;
117
- }
118
-
119
- var fieldNames = extractAllFieldNames(logData);
120
- addAllFieldsToMeta(logData, fieldNames);
121
- replaceFieldsInMessage(logData, fieldNames);
122
- addFixedFieldsToLogData(logData, node);
123
- } catch (error) {
124
- const serializedLogData = serializeData(logData);
125
- node.error(`Error merging fields into message: ${error.message}\nlogData: ${serializedLogData}\nStack trace: ${error.stack}`);
117
+ function setElasticFields(logData, node) {
118
+ let { ElasticsearchTransformer } = require('winston-elasticsearch');
119
+ const transformed = ElasticsearchTransformer(logData);
120
+ transformed['@timestamp'] = logData.timestamp ? logData.timestamp : new Date().toISOString();
121
+ transformed.message = logData.message;
122
+ transformed.messageTemplate = logData.messageTemplate;
123
+ transformed.severity = logData.level;
124
+ transformed.level = logData.level;
125
+ transformed.fields = logData.meta;
126
+
127
+ if (logData.meta['transaction.id']) transformed.transaction = { id: logData.meta['transaction.id'] };
128
+ if (logData.meta['trace.id']) transformed.trace = { id: logData.meta['trace.id'] };
129
+ if (logData.meta['span.id']) transformed.span = { id: logData.meta['span.id'] };
130
+
131
+ return transformed;
126
132
  }
127
133
 
128
- return logData;
129
- }
130
-
131
- function extractAllFieldNames(logData) {
132
- var fieldNames = [];
133
- var message = logData.message;
134
-
135
- var index = 0;
136
- while (index != -1 && fieldNames.length < logData.meta?.fields?.length) {
137
- index = message.indexOf("{", index);
138
- if (index != -1) {
139
- var endIndex = message.indexOf("}", index);
140
- var fieldName = message.substring(index + 1, endIndex);
141
- fieldNames.push(fieldName);
142
- index = endIndex;
143
- }
144
- }
145
-
146
- return fieldNames;
147
- }
148
-
149
- function addAllFieldsToMeta(logData, fieldNames) {
150
- for (var i = 0; i < fieldNames.length; i++) {
151
- var fieldName = fieldNames[i];
152
- var fieldValue = logData.meta.fields[i];
153
-
154
- if (typeof fieldValue === 'string') {
155
- if (fieldValue.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i)) {
156
- logData.meta[fieldName + "_Guid"] = fieldValue;
157
- } else {
158
- logData.meta[fieldName + "_String"] = fieldValue;
159
- }
160
- } else if (typeof fieldValue === 'number') {
161
- logData.meta[fieldName + "_Number"] = fieldValue;
162
- } else if (typeof fieldValue === 'boolean') {
163
- logData.meta[fieldName + "_Boolean"] = fieldValue;
164
- } else if (typeof fieldValue === 'object' && fieldValue instanceof Date) {
165
- logData.meta[fieldName + "_DateTime"] = fieldValue;
166
- } else {
167
- logData.meta[fieldName] = fieldValue;
168
- }
169
- }
170
- }
171
-
172
- function replaceFieldsInMessage(logData, fieldNames) {
173
- // Replace all field names in message with values from fields
174
- logData.messageTemplate = logData.message.slice();
175
- for (var i = 0; i < fieldNames.length; i++) {
176
- var fieldName = fieldNames[i];
177
- var fieldValue = logData.meta.fields[i];
178
- var replacement = typeof fieldValue === 'string' ? "'" + fieldValue + "'" : fieldValue;
179
- logData.message = logData.message.replace("{" + fieldName + "}", replacement);
180
- }
181
-
182
- // Remove fields from meta
183
- delete logData.meta.fields;
184
- }
185
-
186
- function addFixedFieldsToLogData(logData, node) {
187
- logData.meta['Origin_String'] = 'Node-RED';
188
- logData.meta['MachineName_String'] = node.hostname;
189
- }
134
+ RED.nodes.registerType('elastic-search-logger', LogElasticLoggerNode, {
135
+ credentials: {
136
+ username: { type: 'text' },
137
+ password: { type: 'password' },
138
+ index: { type: 'text' },
139
+ },
140
+ });
190
141
 
191
- function serializeData(data) {
192
- return JSON.stringify(data, (key, value) => {
193
- if (typeof value === 'object' && value !== null && value !== undefined) {
194
- if (value instanceof Date) {
195
- return value.toISOString();
196
- }
197
- }
198
- return value;
199
- }, 2);
200
- }
142
+ LogElasticLoggerNode.prototype.addToLog = function addTolog(loglevel, msg) {
143
+ this.logger.log(loglevel, msg.payload.message, msg.payload.meta);
144
+ };
201
145
  };
package/elastic-search.js CHANGED
@@ -1,20 +1,20 @@
1
1
  module.exports = function (RED) {
2
- "use strict";
2
+ 'use strict';
3
3
 
4
- const Error = "Error"
5
- const Warning = "Warning"
6
- const Information = "Information"
7
- const Debug = "Debug"
4
+ const Error = 'Error';
5
+ const Warning = 'Warning';
6
+ const Information = 'Information';
7
+ const Debug = 'Debug';
8
8
 
9
9
  function LogElasticNode(config) {
10
10
  RED.nodes.createNode(this, config);
11
11
  const node = this;
12
12
 
13
- this.logger = RED.nodes.getNode(config.logger);
13
+ this.on('input', function (msg, send, done) {
14
+ node.logger = RED.nodes.getNode(config.logger);
14
15
 
15
- let loglevel = config.loglevel || "";
16
+ let loglevel = config.loglevel || '';
16
17
 
17
- this.on('input', function (msg, send, done) {
18
18
  if (node.logger) {
19
19
  let level;
20
20
  if (loglevel === Error || loglevel === Warning || loglevel === Information || loglevel === Debug) {
@@ -22,8 +22,9 @@ module.exports = function (RED) {
22
22
  level = loglevel;
23
23
  } else {
24
24
  // get loglevel from message
25
- try { level = RED.util.getMessageProperty(msg, loglevel); }
26
- catch (err) {
25
+ try {
26
+ level = RED.util.getMessageProperty(msg, loglevel);
27
+ } catch (err) {
27
28
  level = Debug;
28
29
  }
29
30
 
@@ -40,5 +41,5 @@ module.exports = function (RED) {
40
41
  });
41
42
  }
42
43
 
43
- RED.nodes.registerType("elastic-search", LogElasticNode);
44
- };
44
+ RED.nodes.registerType('elastic-search', LogElasticNode);
45
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@5minds/node-red-contrib-processcube-elasticsearch",
3
- "version": "0.2.0",
3
+ "version": "0.3.0-add-update-cycle-4db609-m3r9rkdh",
4
4
  "license": "MIT",
5
5
  "description": "Node-RED nodes for Elasticsearch",
6
6
  "scripts": {
@@ -27,14 +27,14 @@
27
27
  "node-red": {
28
28
  "version": ">=3.1.9",
29
29
  "nodes": {
30
- "elastic-search": "elastic-search.js",
31
- "elastic-search-logger": "elastic-search-logger.js"
30
+ "elastic-search": "elastic-search.js",
31
+ "elastic-search-logger": "elastic-search-logger.js"
32
32
  },
33
33
  "examples": "examples"
34
34
  },
35
35
  "dependencies": {
36
- "winston": "^3.17.0",
37
- "winston-elasticsearch": "^0.19.0"
36
+ "winston": "^3.17.0",
37
+ "winston-elasticsearch": "^0.19.0"
38
38
  },
39
39
  "keywords": [
40
40
  "node-red",