· 5 years ago · Feb 10, 2021, 10:38 PM
1adapt(data:APIThreatCountOnDate, xProp?:string, yProp?, hidden?:string[]) {
2 debugger;
3 let requests = this.adaptRequestsLineSeries(data);
4 let events = this.adaptEventsBarSeries(data);
5
6 let series = requests.series.concat(events.series);
7
8 return {
9 series: series,
10 categories: requests.categories
11 }
12 }
13
14 adaptRequestsLineSeries(data):any {
15 data = data[0];
16 let chartSeries = [{
17 name: 'REQUESTS',
18 type: 'line',
19 data: []
20 }];
21 let chartCategories:string[] = [];
22 let xProp = 'timestamp';
23 let yProp = 'total_transactions';
24
25 if(data instanceof Array){
26 //if it's empty or if it's already a proper series, return it
27 if(data['series'] && data['series'].length === 0){
28 chartSeries = data;
29 } else {
30
31 //steps in which to display/hide the data in buckets
32 let step = this.adapterService.getStep(data);
33 let isHours = false;
34 let bucket = [];
35
36 //the dates in the data come to us at +00 so we must format them to display for local time
37 let firstDate = moment(data[0][xProp]).utcOffset(moment().utcOffset());
38 let lastDate = moment(data[data.length - 1][xProp]).utcOffset(moment().utcOffset());
39
40 let daysDiff = lastDate.diff(firstDate, 'days');
41 if(daysDiff < (data.length - 1)) isHours = true;
42
43 for(let x = 0; x < data.length; x++){
44 let entry = data[x];
45 let xVal;
46
47 //manage buckets if need be
48 //we need to add to buckets if we are too high in step count and
49 //we are not iterating over a multiple of the step
50 if((step > 1 && x === 0) || (step > 1 && (x % step > 0))){
51 bucket.push(data[x]);
52 } else {
53 if(bucket.length > 0){
54 //we've added to the bucket which means we're in a bucket state
55 //so we need the tallied data and special dates
56 let bucketedEntry = this.adapterService.buildBucketedEntry(entry, bucket, xProp || null, yProp || null);
57
58 entry = bucketedEntry['entry'];
59 xVal = bucketedEntry['xVal'];
60
61 //reset bucket
62 bucket = [];
63 } else {
64 // xVal = moment(entry[xProp]).utc().format('MM/DD/YYYY');
65 xVal = entry[xProp];
66 }
67
68 chartSeries[0].data.push(entry['total_transactions']);
69 chartCategories.push(xVal);
70 }
71 }
72 }
73
74 return {
75 series: chartSeries,
76 categories: chartCategories
77 };
78 } else {
79 //handle obj
80 console.error("Alerts Mixed Chart Mediator | adaptRequestsLineSeries expecting an array, has the api changed?");
81 }
82 }
83
84 adaptEventsBarSeries(data) {
85 let chartThreatTypes = ['Malicious File Upload',
86 'Remote Command Execution',
87 'SQL Injection',
88 'Illegal File Access',
89 'Open Redirect',
90 'IP Protection',
91 'Malicious Payload'
92 ];
93
94 if(FeatureToggleService.getFeatures()['enableCredentialStuffing']) chartThreatTypes.push('Credential Stuffing');
95 let chartCategories:string[] = [];
96 let chartSeries = [
97 {
98 key: 'IP Protection',
99 name: 'IP PROTECTION',
100 type: 'column',
101 data: []
102 },
103 {
104 key: 'Malicious Payload',
105 name: 'MALICIOUS PAYLOAD',
106 type: 'column',
107 data: []
108 },
109 {
110 key: 'Illegal File Access',
111 name: 'ILLEGAL FILE ACCESS',
112 type: 'column',
113 data: []
114 },
115 {
116 key: 'Remote Command Execution',
117 name: 'REMOTE COMMAND EXECUTION',
118 type: 'column',
119 data: []
120 },
121 {
122 key: 'SQL Injection',
123 name: 'SQL INJECTION',
124 type: 'column',
125 data: []
126 },
127 {
128 key: 'Open Redirect',
129 name: 'OPEN REDIRECT',
130 type: 'column',
131 data: []
132 },
133 {
134 key: 'Malicious File Upload',
135 name: 'MALICIOUS FILE UPLOAD',
136 type: 'column',
137 data: []
138 }
139 ];
140
141 if(FeatureToggleService.getFeatures()['enableCredentialStuffing']) {
142 chartSeries.push({
143 key: 'Credential Stuffing',
144 name: 'CREDENTIAL STUFFING',
145 type: 'column',
146 data: []
147 });
148 }
149
150
151 data = data[1];
152 let yProp = 'events';
153 let xProp = 'timestamp';
154
155 let series:Series = {
156 data: []
157 };
158
159 if(data instanceof Array){
160 //if it's empty or if it's already a proper series, return it
161 if(data.length === 0 || (data[0].name && (data[0].value || data[0].series))){
162 series.data = data;
163 } else {
164
165 //steps in which to display/hide the data in buckets
166 let step = this.adapterService.getStep(data);
167 let isHours = false;
168 let bucket = [];
169
170 //the dates in the data come to us at +00 so we must format them to display for local time
171 let firstDate = moment(data[0][xProp]).utcOffset(moment().utcOffset());
172 let lastDate = moment(data[data.length - 1][xProp]).utcOffset(moment().utcOffset());
173
174 let daysDiff = lastDate.diff(firstDate, 'days');
175 if(daysDiff < (data.length - 1)) isHours = true;
176
177 for(let x = 0; x < data.length; x++){
178 let entry = data[x];
179 let xVal;
180
181 //manage buckets if need be
182 //we need to add to buckets if we are too high in step count and
183 //we are not iterating over a multiple of the step
184 if((step > 1 && x === 0) || (step > 1 && (x % step > 0))){
185 bucket.push(data[x]);
186 } else {
187
188 if(bucket.length > 0){
189 //we've added to the bucket which means we're in a bucket state
190 //so we need the tallied data and special dates
191 let bucketedEntry = this.adapterService.buildBucketedEntry(entry, bucket, xProp || null, yProp || null);
192
193 entry = bucketedEntry['entry'];
194 xVal = bucketedEntry['xVal'];
195
196 //reset bucket
197 bucket = [];
198
199 } else {
200 //if this wasn't a bucket scenario, do regular dates and don't
201 //touch the y vals
202 // xVal = moment(entry[xProp]).utc().format('MM/DD/YYYY');
203 xVal = entry[xProp];
204 }
205
206 //if y obj is built already just use it
207 if(entry[yProp] && (entry[yProp].name && entry[yProp].value)){
208
209 // obj.series = entry[yProp];
210 } else {
211
212 }
213
214 for(var i=0; i<chartSeries.length; i++) {
215 chartSeries[i].data.push(entry[yProp][chartSeries[i].key] || 0)
216 }
217 chartCategories.push(xVal);
218 }
219 }
220 }
221
222 for(var i=0; i<chartSeries.length; i++) {
223 delete chartSeries[i].key;
224 }
225
226 return {
227 series: chartSeries,
228 categories: chartCategories
229 };
230
231 } else {
232 //handle obj
233 console.error("Alerts Mixed Chart Mediator | adaptEventsBarSeries expecting an array, has the api changed?");
234 }
235 }