@alertlogic/al-collector-js 3.0.0 → 3.0.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.
package/al_log_filter.js CHANGED
@@ -7,16 +7,22 @@
7
7
  * @end
8
8
  * -----------------------------------------------------------------------------
9
9
  */
10
- const where = require('lodash.where');
10
+ const lodashFilter = require('lodash.filter');
11
+ const lodashRemove = require('lodash.remove');
12
+ const lodashcloneDeep = require('lodash.clonedeep');
11
13
 
12
14
  /**
13
15
  * @function initializes JSON filter
14
- *
15
- * @param filter - string or object, for example, '{"a": 1}', {a:1}
16
- *
16
+ *
17
+ * @param filter - string or object,
18
+ * for example, string '{"a": 1}',
19
+ * object {a:1},
20
+ * deep object '{"a": "firstlevelChild": 1}', '{"a": "firstlevelChild": "secondlevelChild": 2}', '{"a": "...nth levelChild": n},
21
+ * array object '[{"a": 1}, {"b": 2}]',
22
+ *
17
23
  * @return filter - inited Json filter or null if json is incorrect
18
24
  */
19
- var initJsonFilter = function(filter) {
25
+ var initJsonFilter = function (filter) {
20
26
  if (typeof filter === 'string') {
21
27
  try {
22
28
  return JSON.parse(filter);
@@ -38,10 +44,24 @@ var initJsonFilter = function(filter) {
38
44
  * @NOTE: If json filter is bad then 'messages' is returned unfiltered.
39
45
  */
40
46
 
41
- var filterJson = function(messages, filter) {
47
+ var filterJson = function (messages, filter) {
42
48
  const filterJ = initJsonFilter(filter);
43
49
  if (filterJ) {
44
- return where(messages, filterJ);
50
+ let result = [];
51
+ let clonedMsgs = lodashcloneDeep(messages);
52
+ if (Array.isArray(filterJ)) {
53
+ // Iterate over filterJson and push filtered items into array
54
+ filterJ.forEach(function (e) {
55
+ result = result.concat(lodashRemove(clonedMsgs, function (t) {
56
+ // filter messages with filterJson element
57
+ return lodashFilter([t], e).length;
58
+ }));
59
+ });
60
+ } else {
61
+ result = result.concat(lodashFilter(clonedMsgs, filterJ));
62
+ }
63
+ // Return filtered items
64
+ return result;
45
65
  } else {
46
66
  return messages;
47
67
  }
@@ -54,7 +74,7 @@ var filterJson = function(messages, filter) {
54
74
  *
55
75
  * @return filter - inited regexp or null if regexp is incorrect
56
76
  */
57
- var initRegExpFilter = function(filter) {
77
+ var initRegExpFilter = function (filter) {
58
78
  if (typeof filter === 'string') {
59
79
  try {
60
80
  return new RegExp(filter);
@@ -76,10 +96,12 @@ var initRegExpFilter = function(filter) {
76
96
  * @NOTE: If regexp filter is bad then 'messages' is returned unfiltered.
77
97
  */
78
98
 
79
- var filterRegExp = function(messages, filter) {
99
+ var filterRegExp = function (messages, filter) {
80
100
  const re = initRegExpFilter(filter);
81
101
  if (re) {
82
- return messages.filter(function(m) { return re.test(m); });
102
+ return messages.filter(function (m) {
103
+ return re.test(m);
104
+ });
83
105
  } else {
84
106
  return messages;
85
107
  }
@@ -87,9 +109,9 @@ var filterRegExp = function(messages, filter) {
87
109
 
88
110
 
89
111
  module.exports = {
90
- filterJson : filterJson,
91
- filterRegExp : filterRegExp,
92
- initRegExpFilter : initRegExpFilter,
93
- initJsonFilter : initJsonFilter
112
+ filterJson: filterJson,
113
+ filterRegExp: filterRegExp,
114
+ initRegExpFilter: initRegExpFilter,
115
+ initJsonFilter: initJsonFilter
94
116
  };
95
117
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alertlogic/al-collector-js",
3
- "version": "3.0.0",
3
+ "version": "3.0.1",
4
4
  "license": "MIT",
5
5
  "description": "Alert Logic Collector Common Library",
6
6
  "repository": {
@@ -32,7 +32,9 @@
32
32
  "dependencies": {
33
33
  "async": "3.1.0",
34
34
  "debug": "4.1.1",
35
- "lodash.where": "^3.1.0",
35
+ "lodash.filter": "^4.6.0",
36
+ "lodash.remove": "^4.7.0",
37
+ "lodash.clonedeep": "^4.5.0",
36
38
  "moment": "2.24.0",
37
39
  "protobufjs": "6.8.8",
38
40
  "request": "2.88.0",
@@ -11,15 +11,9 @@
11
11
  const assert = require('assert');
12
12
  const alLogFilter = require('../al_log_filter');
13
13
 
14
-
15
14
  describe('Unit Tests', function() {
16
15
  describe('Filter Messages', function() {
17
-
18
- const msgJson = [
19
- { message1: 1, text: 'test1' },
20
- { message2: 2, text: 'test12'},
21
- { messageA: "a", text: 'testa'},
22
- ];
16
+ let msgJson = [];
23
17
  const msgString = [
24
18
  'message1',
25
19
  'message2',
@@ -27,6 +21,27 @@ describe('Unit Tests', function() {
27
21
  ];
28
22
 
29
23
  before(function(){
24
+ msgJson = [
25
+ { message1: 1, text: 'test1' },
26
+ { message2: 2, text: 'test12'},
27
+ { messageA: "a", text: 'testa'},
28
+ { messageB: {
29
+ childTestMsg: 'childTest',
30
+ childTestValue: 'childValue'
31
+ }, text: 'testb'},
32
+ { messageC: {
33
+ childTestMsg: 'childTest',
34
+ childTestValue: {
35
+ messageC: "c"
36
+ }
37
+ }, text: 'testc'},
38
+ { messageD: {
39
+ childTestMsg: 'childTestD',
40
+ childTestValue: {
41
+ messageC: "c"
42
+ }
43
+ }, text: 'testd'}
44
+ ];
30
45
  });
31
46
  after(function() {
32
47
  });
@@ -48,11 +63,97 @@ describe('Unit Tests', function() {
48
63
  return done();
49
64
  });
50
65
 
51
- it('Undefined filters', function(done) {
66
+ it('Undefined filters', function (done) {
52
67
  assert.deepEqual(msgString, alLogFilter.filterRegExp(msgString, null));
53
68
  assert.deepEqual(msgJson, alLogFilter.filterJson(msgJson));
54
69
  return done();
55
70
  });
56
-
71
+
72
+ it('Should filter array based on object child property', function (done) {
73
+ assert.deepEqual([{
74
+ messageB: {
75
+ childTestMsg: 'childTest',
76
+ childTestValue: 'childValue'
77
+ },
78
+ text: 'testb'
79
+ }], alLogFilter.filterJson(msgJson, '{"messageB":{"childTestMsg":"childTest"}}'));
80
+ return done();
81
+ });
82
+
83
+ it('Should filter array based on object child of child property', function (done) {
84
+ assert.deepEqual([{
85
+ messageC: {
86
+ childTestMsg: 'childTest',
87
+ childTestValue: {
88
+ messageC: "c"
89
+ }
90
+ },
91
+ text: 'testc'
92
+ }], alLogFilter.filterJson(msgJson, '{"messageC":{"childTestMsg": "childTest","childTestValue":{"messageC":"c"}}}'));
93
+ return done();
94
+ });
95
+
96
+ it('Should filter array based on array of object with AND case', function (done) {
97
+ assert.deepEqual([{
98
+ messageB: {
99
+ childTestMsg: 'childTest',
100
+ childTestValue: 'childValue'
101
+ },
102
+ text: 'testb'
103
+ }, {
104
+ messageC: {
105
+ childTestMsg: 'childTest',
106
+ childTestValue: {
107
+ messageC: "c"
108
+ }
109
+ },
110
+ text: 'testc'
111
+ }], alLogFilter.filterJson(msgJson, '[{"messageB":{"childTestMsg":"childTest"}}, {"messageC":{"childTestMsg": "childTest","childTestValue":{"messageC":"c"}}}]'));
112
+ return done();
113
+ });
114
+
115
+ it('Should filter array based on array of object with OR case', function (done) {
116
+ assert.deepEqual([{
117
+ messageB: {
118
+ childTestMsg: 'childTest',
119
+ childTestValue: 'childValue'
120
+ },
121
+ text: 'testb'
122
+ }], alLogFilter.filterJson(msgJson, '[{"messageB":{"childTestMsg":"childTest"}}, {"messageC":{"childTestMsgT": "childTest","childTestValue":{"messageC":"c"}}}]'));
123
+ return done();
124
+ });
125
+
126
+ it('Two condition match of same object for array based filtering on array of object with OR case', function (done) {
127
+ assert.deepEqual([{
128
+ messageB: {
129
+ childTestMsg: 'childTest',
130
+ childTestValue: 'childValue'
131
+ },
132
+ text: 'testb'
133
+ }], alLogFilter.filterJson(msgJson, '[{"messageB":{"childTestMsg":"childTest"}}, {"messageB":{"childTestValue":"childValue"}}]'));
134
+ return done();
135
+ });
136
+
137
+ it('three condition match of same object for array based filtering on array of object with OR case', function (done) {
138
+ assert.deepEqual([{
139
+ messageB: {
140
+ childTestMsg: 'childTest',
141
+ childTestValue: 'childValue'
142
+ },
143
+ text: 'testb'
144
+ }], alLogFilter.filterJson(msgJson, '[{"messageB":{"childTestMsg":"childTest"}}, {"messageB":{"childTestValue":"childValue"}}, {"text": "testb"}]'));
145
+ return done();
146
+ });
147
+
148
+ it('negative case for array based filtering on array of object with OR case', function (done) {
149
+ assert.deepEqual([{
150
+ messageB: {
151
+ childTestMsg: 'childTest',
152
+ childTestValue: 'childValue'
153
+ },
154
+ text: 'testb'
155
+ }], alLogFilter.filterJson(msgJson, '[{"messageB":{"childTestMsg":"childTest"}}, {"messageB":{"childTestValue":"childValue1"}}]'));
156
+ return done();
157
+ });
57
158
  });
58
159
  });