@5minds/node-red-contrib-processcube-elasticsearch 0.3.1-feature-c8d1f6-m4l21tei → 0.3.1

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.
@@ -1,77 +1,75 @@
1
1
  <script type="text/javascript">
2
- RED.nodes.registerType('elastic-search-logger', {
2
+ RED.nodes.registerType('elastic-search-logger',{
3
3
  category: 'config',
4
4
  credentials: {
5
- username: { type: 'text' },
6
- password: { type: 'password' },
7
- index: { type: 'text' },
5
+ username: {type:"text"},
6
+ password: {type:"password"},
7
+ index: {type:"text"}
8
8
  },
9
9
  defaults: {
10
- name: { value: '' },
11
- url: { value: 'http://localhost:9200' },
12
- urlType: { value: 'str' },
13
- usernameType: { value: 'str' },
14
- passwordType: { value: 'str' },
15
- indexType: { value: 'str' },
16
- filename: { value: 'log-elastic.log', required: true },
17
- maxsize: {
18
- value: 1,
19
- required: true,
20
- validate: function (v) {
21
- return v >= 1;
22
- },
23
- },
24
- maxfiles: {
25
- value: 2,
26
- required: true,
27
- validate: function (v) {
28
- return v >= 1;
29
- },
30
- },
31
- },
32
- label: function () {
33
- return this.name;
10
+ name: { value: "" },
11
+ url: { value: "http://localhost:9200" },
12
+ urlType: { value: "str" },
13
+ usernameType: { value: "str" },
14
+ passwordType: { value: "str" },
15
+ indexType: { value: "str" },
16
+ filename: { value: "log-elastic.log", required: true },
17
+ maxsize: { value: 1, required: true, validate: function(v) { return v >= 1 } },
18
+ maxfiles: { value: 2, required: true, validate: function(v) { return v >= 1 } },
34
19
  },
20
+ label: function () { return this.name; },
35
21
  oneditprepare: function () {
36
22
  // URL
37
- $('#node-config-input-url').typedInput({
23
+ $("#node-config-input-url").typedInput({
38
24
  type: this.urlType,
39
- types: ['str', 'cred', 'env'],
40
- typeField: '#node-config-input-urlType',
41
- value: this.url,
42
- });
25
+ types: [
26
+ "str",
27
+ "global"
28
+ ],
29
+ typeField: "#node-config-input-urlType",
30
+ value: this.url
31
+ })
43
32
 
44
33
  // Username
45
- $('#node-config-input-username').typedInput({
34
+ $("#node-config-input-username").typedInput({
46
35
  type: this.usernameType,
47
- types: ['str', 'cred', 'env'],
48
- typeField: '#node-config-input-usernameType',
49
- value: this.credentials.username,
50
- });
36
+ types:[
37
+ "str",
38
+ "global"
39
+ ],
40
+ typeField: "#node-config-input-usernameType",
41
+ value: this.credentials.username
42
+ })
51
43
 
52
44
  // Password
53
- $('#node-config-input-password').typedInput({
45
+ $("#node-config-input-password").typedInput({
54
46
  type: this.passwordType,
55
- types: ['str', 'cred', 'env'],
56
- typeField: '#node-config-input-passwordType',
57
- value: this.credentials.password,
58
- });
47
+ types:[
48
+ "str",
49
+ "global"
50
+ ],
51
+ typeField: "#node-config-input-passwordType",
52
+ value: this.credentials.password
53
+ })
59
54
 
60
55
  // Index
61
- $('#node-config-input-index').typedInput({
56
+ $("#node-config-input-index").typedInput({
62
57
  type: this.indexType,
63
- types: ['str', 'cred', 'env'],
64
- typeField: '#node-config-input-indexType',
65
- value: this.credentials.index,
66
- });
67
- },
68
- });
58
+ types:[
59
+ "str",
60
+ "global"
61
+ ],
62
+ typeField: "#node-config-input-indexType",
63
+ value: this.credentials.index
64
+ })
65
+ }
66
+ });
69
67
  </script>
70
68
 
71
69
  <script type="text/html" data-template-name="elastic-search-logger">
72
70
  <div class="form-row">
73
71
  <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
74
- <input type="text" id="node-config-input-name" placeholder="Name" />
72
+ <input type="text" id="node-config-input-name" placeholder="Name">
75
73
  </div>
76
74
  <div id="elkfields">
77
75
  <div class="form-row">
@@ -79,28 +77,29 @@
79
77
  </div>
80
78
  <div class="form-row" style="padding-left:20px;">
81
79
  <label for="node-config-input-url"><i class="fa fa-plug"></i> Elastic URL</label>
82
- <input type="text" id="node-config-input-url" />
83
- <input type="hidden" id="node-config-input-urlType" />
80
+ <input type="text" id="node-config-input-url">
81
+ <input type="hidden" id="node-config-input-urlType">
84
82
  </div>
85
83
  <div class="form-row" style="padding-left:20px;">
86
84
  <label for="node-config-input-username"><i class="fa fa-user"></i> Username</label>
87
- <input type="text" id="node-config-input-username" />
88
- <input type="hidden" id="node-config-input-usernameType" />
85
+ <input type="text" id="node-config-input-username">
86
+ <input type="hidden" id="node-config-input-usernameType">
89
87
  </div>
90
88
  <div class="form-row" style="padding-left:20px;">
91
89
  <label for="node-config-input-password"><i class="fa fa-key"></i> Password</label>
92
- <input type="password" id="node-config-input-password" />
93
- <input type="hidden" id="node-config-input-passwordType" />
90
+ <input type="password" id="node-config-input-password">
91
+ <input type="hidden" id="node-config-input-passwordType">
94
92
  </div>
95
93
  <div class="form-row" style="padding-left:20px;">
96
94
  <label for="node-config-input-index"><i class="fa fa-tag"></i> Index</label>
97
- <input type="text" id="node-config-input-index" />
98
- <input type="hidden" id="node-config-input-indexType" />
95
+ <input type="text" id="node-config-input-index">
96
+ <input type="hidden" id="node-config-input-indexType">
99
97
  </div>
100
- </div>
101
- </script>
102
-
103
- <script type="text/x-red" data-help-name="elastic-search-logger">
104
- <p>The configuration for the elastic-search-logger.</p>
105
- <p>Configurations like the requested URL to Elastic, Username & Password and the Elastic Index.</p>
106
- </script>
98
+ </div>
99
+ </script>
100
+
101
+ <script type="text/x-red" data-help-name="elastic-search-logger">
102
+ <p>The configuration for the elastic-search-logger.</p>
103
+ <p>Configurations like the requested URL to Elastic, Username & Password and the Elastic Index.</p>
104
+ </script>
105
+
@@ -1,6 +1,8 @@
1
1
  module.exports = function (RED) {
2
2
  'use strict';
3
3
 
4
+ let lock = false;
5
+
4
6
  function LogElasticLoggerNode(config) {
5
7
  let winston = require('winston');
6
8
  let winstonElasticSearch = require('winston-elasticsearch');
@@ -9,73 +11,113 @@ module.exports = function (RED) {
9
11
  this.logger = null;
10
12
  let transports = [];
11
13
 
12
- // Elastic settings
13
- let url = RED.util.evaluateNodeProperty(config.url, config.urlType, this);
14
- if (url == '') {
15
- this.error('Elastic search url is not set');
16
- }
14
+ const stopRefreshing = periodicallyRefreshElasticSettings(10000);
15
+
16
+ function periodicallyRefreshElasticSettings(timeOut) {
17
+ let url, user, password, index;
18
+
19
+ function refresh() {
20
+ if (lock) return;
21
+
22
+ // Elastic settings
23
+ let newUrl = RED.util.evaluateNodeProperty(config.url, config.urlType, this);
24
+ if (newUrl == '') {
25
+ this.error('Elastic search url is not set');
26
+ return;
27
+ }
28
+
29
+ let newUser = RED.util.evaluateNodeProperty(this.credentials.username, config.usernameType, this);
30
+ if (newUser == '') {
31
+ this.error('Elastic search username is not set');
32
+ return;
33
+ }
34
+
35
+ let newPassword = RED.util.evaluateNodeProperty(this.credentials.password, config.passwordType, this);
36
+ if (newPassword == '') {
37
+ this.error('Elastic search password is not set');
38
+ return;
39
+ }
40
+
41
+ let newIndex = RED.util.evaluateNodeProperty(this.credentials.index, config.indexType, this);
42
+ if (newIndex == '') {
43
+ this.error('Elastic search index is not set');
44
+ return;
45
+ }
46
+
47
+ if (newUrl === url && newUser === user && newPassword === password && newIndex === index) {
48
+ return;
49
+ }
50
+
51
+ url = newUrl;
52
+ user = newUser;
53
+ password = newPassword;
54
+ index = newIndex;
55
+
56
+ index = index.toLowerCase();
57
+ if (url) {
58
+ const elasticSearchTransport = new winstonElasticSearch.ElasticsearchTransport({
59
+ clientOpts: {
60
+ node: url,
61
+ auth: {
62
+ username: user,
63
+ password: password,
64
+ },
65
+ ssl: {
66
+ // accept any
67
+ rejectUnauthorized: false,
68
+ },
69
+ },
70
+ transformer: (logData) => setElasticFields(logData, this),
71
+ index: index,
72
+ });
73
+
74
+ transports = [elasticSearchTransport];
75
+
76
+ elasticSearchTransport.on('error', (error) => {
77
+ this.error(`Error in elasticSearchTransport caught: ${error.message}`);
78
+ console.error('Error in elasticSearchTransport caught', error);
79
+ });
80
+ }
81
+
82
+ let logLevels = {
83
+ levels: {
84
+ Error: 0,
85
+ Warning: 1,
86
+ Information: 2,
87
+ Debug: 3,
88
+ },
89
+ };
17
90
 
18
- let user = RED.util.evaluateNodeProperty(this.credentials.username, config.usernameType, this);
19
- if (user == '') {
20
- this.error('Elastic search username is not set');
21
- }
91
+ const newLogger = new winston.createLogger({
92
+ exitOnError: false,
93
+ level: 'Debug',
94
+ levels: logLevels.levels,
95
+ transports: transports,
96
+ });
22
97
 
23
- let password = RED.util.evaluateNodeProperty(this.credentials.password, config.passwordType, this);
24
- if (password == '') {
25
- this.error('Elastic search password is not set');
26
- }
98
+ lock = true;
27
99
 
28
- let index = RED.util.evaluateNodeProperty(this.credentials.index, config.indexType, this);
29
- if (index == '') {
30
- this.error('Elastic search index is not set');
31
- }
32
-
33
- index = index.toLowerCase();
34
- if (url) {
35
- const elasticSearchTransport = new winstonElasticSearch.ElasticsearchTransport({
36
- clientOpts: {
37
- node: url,
38
- auth: {
39
- username: user,
40
- password: password,
41
- },
42
- ssl: {
43
- // accept any
44
- rejectUnauthorized: false,
45
- },
46
- },
47
- transformer: (logData) => setElasticFields(logData, this),
48
- index: index,
49
- });
100
+ try {
101
+ if (this.logger) this.logger.close();
50
102
 
51
- transports.push(elasticSearchTransport);
103
+ this.logger = newLogger;
104
+ } finally {
105
+ lock = false;
106
+ }
52
107
 
53
- elasticSearchTransport.on('error', (error) => {
54
- this.error(`Error in elasticSearchTransport caught: ${error.message}`);
55
- console.error('Error in elasticSearchTransport caught', error);
56
- });
57
- }
108
+ this.debug('elastic-search logger created');
109
+ }
58
110
 
59
- let logLevels = {
60
- levels: {
61
- Error: 0,
62
- Warning: 1,
63
- Information: 2,
64
- Debug: 3,
65
- },
66
- };
67
- this.logger = new winston.createLogger({
68
- exitOnError: false,
69
- level: 'Debug',
70
- levels: logLevels.levels,
71
- transports: transports,
72
- });
111
+ refresh();
112
+ const intervalId = setInterval(refresh, timeOut);
73
113
 
74
- this.debug('elastic-search logger created');
114
+ return () => clearInterval(intervalId);
115
+ }
75
116
 
76
117
  this.on('close', function (removed, done) {
118
+ stopRefreshing();
77
119
  // close logger
78
- if (this.loggger) {
120
+ if (this.logger) {
79
121
  this.logger.close();
80
122
  }
81
123
 
@@ -110,7 +152,20 @@ module.exports = function (RED) {
110
152
  },
111
153
  });
112
154
 
113
- LogElasticLoggerNode.prototype.addToLog = function addTolog(loglevel, msg) {
114
- this.logger.log(loglevel, msg.payload.message, msg.payload.meta);
155
+ LogElasticLoggerNode.prototype.addToLog = function addToLog(loglevel, msg) {
156
+ let attempt = 0;
157
+
158
+ const tryLog = () => {
159
+ if (!lock && this.logger) {
160
+ this.logger.log(loglevel, msg.payload.message, msg.payload.meta);
161
+ } else if (attempt < 5) {
162
+ attempt++;
163
+ setTimeout(tryLog, 10);
164
+ } else {
165
+ this.error('Failed to log message after multiple attempts due to logger lock.');
166
+ }
167
+ };
168
+
169
+ tryLog();
115
170
  };
116
171
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@5minds/node-red-contrib-processcube-elasticsearch",
3
- "version": "0.3.1-feature-c8d1f6-m4l21tei",
3
+ "version": "0.3.1",
4
4
  "license": "MIT",
5
5
  "description": "Node-RED nodes for Elasticsearch",
6
6
  "scripts": {