-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.js
194 lines (167 loc) · 7.12 KB
/
parse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* This contains all event listeners to make the page function as expected. */
// Keep track of which log is being processed
document.requestLogs = true;
// Work the "change input" button
document.getElementById('change-input').addEventListener('click', function () {
console.log('ok');
document.requestLogs = !document.requestLogs;
// Change title text
document.getElementById('input-title').innerHTML = 'Input '+(document.requestLogs ? 'Request' : 'Response')+' Logs';
// Change button text
document.getElementById('change-input').innerHTML = 'Change to '+(document.requestLogs ? 'Response' : 'Request')+' Parsing';
});
// Work the "submit" button
document.getElementById('submit').addEventListener('click', function() {
var inputText = document.getElementById('input-text').value;
var outputText = parseRequestResponse(document.requestLogs, inputText);
document.getElementById('output-text').value = outputText;
});
// Actually handle the log parsing
function parseRequestResponse(requestsOnly, inputText) {
console.log(requestsOnly);
var logs = inputText.split('\n');
// Run parsing functions
var calls = requestsOnly ? callsFromLogsReq(logs) : callsFromLogsRes(logs);
// // Print it like a CSV
// var outputFormat = "timestamp,ucid,dnis,ani,concertSessionId" + (requestsOnly ? "" : ",statusCode,tenantId,anonymousId,sessionId" )
// var output = calls.reduce(function(output,call){
// return output+'\n'+call.timestamp+","+(call.ucid||"")+","+(call.dnis||"")+","+(call.ani||"")+","+(call.concertSessionId||"")+(requestsOnly ? "" : ","+(call.status||"")+","+(call.tenantId||"")+","+(call.anonymousId||"")+","+(call.sessionId||""))
// },outputFormat);
// /* Now we create a CSV output */
// // Select the output columns
// var outputFields = ["timestamp","ucid","dnis","ani","concertSessionId"]
// // Add more for responses
// if (!requestsOnly) {
// Array.prototype.push.apply(outputFields, ["statusCode","tenantId","anonymousId","sessionId"])
// } else {
// // Nothing happens
// }
// // Establish the header row
// var outputFormat = outputFields.reduce(function(columns,column,index) {
// return columns+(index!==0?",":"")+column
// }, "");
// // Map it to an output
// var outputString = calls.reduce(function(finalOutput,call) {
// // Now we'll do some crazy shit to return each line of the CSV
// return finalOutput+'\n'+outputFields.map(function(outputField) {
// // Return an array of the call Object's value for each key specified in outputFields
// return call[outputField] || ""
// }).reduce(function(outputLine,callField,index) {
// // Reduce this array to a comma separated string
// return outputLine+(index!==0?",":"")+callField
// },"")
// }, outputFormat)
return formatOutput(requestsOnly, calls)
}
// Extract calls from the request format
function callsFromLogsReq(logs) {
return logs.map(function(log) {
// Break the line into words
var req = log.split(' ');
var reqObj = req.filter(function(part) {
// Only consider words that are key=value pairs
return part.indexOf('=') !== -1
}).reduce(function(obj, keyVal){
// Break apart keys & values
keyVal = keyVal.split('=');
var key = keyVal[0],
val = keyVal[1];
// Store them in the object
obj[key]=val;
// Return an object for each log
return obj
}, {})
// Rename session ID to be more specific
reqObj.concertSessionId = reqObj.sessionId;
delete reqObj.sessionId;
// Reconstruct the timestamp
reqObj.timestamp = req[0]+" "+req[6].replace(",",".");
return reqObj
});
}
// Extract calls from the response format
function callsFromLogsRes(logs) {
return logs.map(function(log) {
// Grab just the requestData object
var req = log.substring(log.indexOf('requestData='),log.length);
// Turn it into something we can parse into a javascript object
req = req.substring(req.indexOf('{'),req.indexOf('}')+1).replace('callId','ucid');
// Do that
try {
var callObj = JSON.parse(req);
} catch (e) {
console.log(req);
console.error(e);
var callObj = {"ucid": "thisOneSucks"};
}
callObj.log = log;
// Also get the response data
var response = log.substring(log.indexOf('responseData=')+'responseData='.length,log.indexOf(',request'));
try {
var responseObj = JSON.parse(response);
// Add response data to call Object
callObj.tenantId = responseObj.tenantId || '';
callObj.anonymousId = responseObj.anonymousId || '';
callObj.sessionId = responseObj.sessionId || '';
// Also get any Paid Search Permalease data
if (responseObj.metadata) {
callObj.adGroupName = responseObj.metadata.adGroupName || '';
callObj.adGroupId = responseObj.metadata.adGroupId || '';
callObj.searchProvider = responseObj.metadata.searchProvider || '';
callObj.campaignName = responseObj.metadata.campaignName || '';
callObj.campaignId = responseObj.metadata.campaignId || '';
}
console.log(callObj)
// Add a successful response status if one was not logged
callObj.statusCode = responseObj.status || '200';
} catch (e) {
console.log(response);
console.error(e);
}
// Also get the timestamp, concert session ID, and concert user
try {
callObj.timestamp = log.split(' ').slice(0,2).join(' ').replace(',','.');
callObj.concertSessionId = log.match(/sessionId=\S+/)[0].replace('sessionId=','');
callObj.concertUser = log.match(/user=\S+/)[0].replace('user=','');
} catch(e) {
console.error(e);
}
// Also get the IP addresses
try {
log.match(/[A-Za-z]+IpAddress="\d+\.\d+\.\d+\.\d+/g).forEach(function(match) {
var keyVal = match.split('="');
callObj[keyVal[0]] = keyVal[1];
});
} catch(e) {
console.error(e);
}
// Return the results
return callObj
});
}
function formatOutput(requestsOnly, calls) {
/* Now we create a CSV output */
// Select the output columns
var outputFields = ["timestamp","ucid","dnis","ani","concertSessionId"]
// Add more for responses
if (!requestsOnly) {
Array.prototype.push.apply(outputFields, ["statusCode","tenantId","anonymousId","sessionId","clientIpAddress","publicIpAddress", "concertUser", "adGroupId", "adGroupName", "campaignId","campaignName","searchProvider"])
} else {
// Nothing happens
}
// Establish the header row
var outputFormat = outputFields.reduce(function(columns,column,index) {
return columns+(index!==0?",":"")+column
}, "");
// Map it to an output
return calls.reduce(function(finalOutput,call) {
// Now we'll do some crazy shit to return each line of the CSV
return finalOutput+'\n'+outputFields.map(function(outputField) {
// Return an array of the call Object's value for each key specified in outputFields
return call[outputField] || ""
}).reduce(function(outputLine,callField,index) {
// Reduce this array to a comma separated string
return outputLine+(index!==0?",":"")+callField
},"")
}, outputFormat)
}