-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvScript.js
More file actions
321 lines (276 loc) · 9.44 KB
/
csvScript.js
File metadata and controls
321 lines (276 loc) · 9.44 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
var mainData = [];
//These are references to the column names
const TIME = 1;
const DURATION = 2;
const CALLSTATUS = 5;
//This section deals with reading in the CSV
function handleFiles (files){
// Check for the various File API support.
if (window.File && window.FileReader) {
// Great success! All the File APIs are supported.
getAsText(files[0]);
} else {
alert('The File APIs are not fully supported in this browser.');
}
}
function getAsText(fileToRead){
var reader = new FileReader();
// Read file into memory as UTF-8
reader.readAsText(fileToRead);
//Handle errors load
reader.onload = loadHandler;
reader.onerror = errorHandler;
}
function loadHandler(event){
var csv = event.target.result;
processData(csv);
}
function processData(csv){
var allTextLines = csv.split(/\r\n|\n/)
for (var i=0; i<allTextLines.length; i++){
var data = allTextLines[i].split(',');
var tarr = [];
for (var j=0; j<data.length; j++){
tarr.push(data[j]);
}
mainData.push(tarr);
}
console.log(mainData);
//Run a quick check over data before it gets wrecked by the reverse.
getData(mainData);
mainData = [];
}
function errorHandler(evt){
if(evt.target.error.name == "NotReadableError"){
alert("Cannot read file!");
}
}
//Get Data Output
function getData(rawData){
//Run a quick check over data before it gets wrecked by the reverse.
let totalCallsMade = callsMade(rawData);
let getWrapTime = wrapUpTime(rawData);
let callData = callDuration(rawData, totalCallsMade);
//For efficiency callDuration returns an object with the other data. So group this up for simplicity
let callDataObject = {
wrapTime: getWrapTime,
avgDuration: callData.avgDuration,
avgTime: callData.avgTime,
avgCalls: callData.avgCalls,
totalCalls:callData.totalCalls
};
makeTableRow(callDataObject);
//Reset the data array
document.getElementById("fileUpload").value = "";
mainData = [];
}
//Create the row on end of the table
function makeTableRow(rowData){
let table = document.getElementById('mainTable');
let row = document.createElement('tr');
let nameData = document.createElement('td', {dataID: "cellName"});
let durationData = document.createElement('td', {dataID: "cellAvgDuration"});
let wrapData = document.createElement('td', {dataID: "cellWrap"});
let timeData = document.createElement('td', {dataID: "cellAvgTime"});
let callsData = document.createElement('td', {dataID: "cellAvgCalls"});
let totalData = document.createElement('td', {dataID: "cellTotalCalls"});
nameData.innerHTML = "";
durationData.innerHTML = rowData.avgDuration;
wrapData.innerHTML = rowData.wrapTime;
timeData.innerHTML = rowData.avgTime;
callsData.innerHTML = rowData.avgCalls;
totalData.innerHTML = rowData.totalCalls;
row.appendChild(nameData);
row.appendChild(durationData);
row.appendChild(wrapData);
row.appendChild(timeData);
row.appendChild(callsData);
row.appendChild(totalData);
table.appendChild(row);
}
//creates datetime arguments
function formatDate(date){
let split = date.split(" ");
let dateSplit = split[0].split("/");
let timeSplit = split[1].split(":");
//check for 24 hour
if(split[2] == "PM" && timeSplit[0] != "12"){
let twentyFour = parseInt(timeSplit[0]) + 12;
timeSplit[0] = twentyFour.toString();
}
//concatanate it all into a string
let dateString = dateSplit[2] + "-" + dateSplit[1] + "-" + dateSplit[0] + "T" +
timeSplit[0] + ':' + timeSplit[1] + ':' + timeSplit[2];
return dateString
}
//Removes lines we dont want and reverses the data
//THIS AFFECTS THE DATA PERMENANTLY ANYTHING IN THE THREAD AFTER THIS WILL SEE THIS DATA INSTEAD
//ONLY NEEDS TO BE RUN ONCE
function removeLines(data){
data.splice(0,1);
newData = data.reverse();
for(let i=0; i < newData.length; i++){
if (newData[i][DURATION] < 20 || newData[i][CALLSTATUS] == "INBOUND UNANSWERED") {
newData.splice(i,1);
i--;
}
}
console.log(newData);
return newData;
}
//breaks down milliseconds into useful time format
function getTimeBreakdown(milliseconds){
let seconds = Math.floor(milliseconds / 1000);
let minuets;
let hours;
// work out the total duration broken down
if(seconds > 60){
minuets = Math.floor(seconds / 60);
seconds = seconds % 60;
if(minuets > 60){
hours = Math.floor(minuets / 60);
minuets = minuets % 60;
} else {
hours = 0;
}
} else {
minuets = 0;
hours = 0;
}
//Add leading 0 if lower than 10
if(seconds < 10){
seconds.toString();
seconds = "0" + seconds;
}
if(minuets < 10){
minuets.toString();
minuets = "0" + minuets;
}
if(hours < 10){
hours.toString();
hours = "0" + hours;
}
return hours + ":" + minuets + ":" + seconds;
}
// This is where the data processing functions are
//*** WRAP UP TIME ***
function wrapUpTime(data){
const DATA = removeLines(data);
let dayTracker = new Date(formatDate(DATA[0][TIME]));
let wrapUpTimeSum = 0;
let dayCount = 1;
//begin main loop
for(let i=0; i<DATA.length - 1; i++){
let skipCall = 1;
let currentDay = new Date(formatDate(DATA[i][TIME]));
let nextCall = new Date(formatDate(DATA[i+1][TIME]))
//This is the time at end of call in ms
let endOfCall = currentDay.getTime() + (DATA[i][DURATION] * 1000)
//Is the call the same day? if not update the dayTracker and run same check
if(currentDay.getDate() == dayTracker.getDate()){
// check that the call isnt longer than the next action (this would mean its not the salespersons call) & is on the same day
while(true){
if(endOfCall < nextCall.getTime() && currentDay.getDate() == nextCall.getDate()){
wrapUpTimeSum += nextCall.getTime() - endOfCall;
skipCall -= 1;
i += skipCall;
break;
} else {
//check we have not gone to next day because of last call
if(currentDay.getDate() != nextCall.getDate()){
break;
}
//If we are here its because we need to check the next next call for end of call and skip the calls that werent the agents
if(i + skipCall == DATA.length - 1){
break;
} else {
skipCall += 1;
nextCall = new Date(formatDate(DATA[i+skipCall][TIME]));
}
}
}
} else {
dayTracker.setDate(currentDay.getDate());
dayCount += 1;
// check that the call isnt longer than the next action (this would mean its not the salespersons call) & is on the same day
while(true){
if(endOfCall < nextCall.getTime() && currentDay.getDate() == nextCall.getDate()){
wrapUpTimeSum += nextCall.getTime() - endOfCall;
skipCall -= 1;
i += skipCall;
break;
} else {
//check we have not gone to next day because of last call
if(currentDay.getDate() != nextCall.getDate()){
break;
}
//If we are here its because we need to check the next next call for end of call and skip the calls that werent the agents
if(i + skipCall == DATA.length - 1){
break;
} else {
skipCall += 1;
nextCall = new Date(formatDate(DATA[i+skipCall][TIME]));
}
}
}
}
}
if(wrapUpTimeSum == 0){
console.log("Error No Calls to Parse");
console.log(DATA);
} else {
console.log("Average Wrap Up time is: " + getTimeBreakdown(Math.floor(wrapUpTimeSum / DATA.length))); // Dividing by calls not days
return(getTimeBreakdown(Math.floor(wrapUpTimeSum / DATA.length)));
}
}
//*** AVG CALL DURATION & AVG TIME ON PHONE PER DAY***
//Note: this does not take into account time travel calls (not taken by agent)
function callDuration(data, callsMade){
const DATA = data;
let callDataObject = {
avgDuration: 0,
avgTime: 0,
avgCalls: 0,
totalCalls:0
};
let durationSum = 0;
let dayTracker = new Date(formatDate(DATA[0][TIME]));
let dayCount = 1;
for(let i=0; i < DATA.length; i++){
let currentDay = new Date(formatDate(DATA[i][TIME]));
if(currentDay.getDate() == dayTracker.getDate()){
durationSum += parseInt(DATA[i][DURATION]);
} else {
durationSum += parseInt(DATA[i][DURATION]);
dayTracker = currentDay;
dayCount += 1;
}
}
if(durationSum == 0){
console.log("Error No Calls to Parse");
console.log(DATA);
} else {
console.log("Average Duration is: " + getTimeBreakdown(Math.floor(durationSum / DATA.length)* 1000));
console.log("Average Time on Phone is: " + getTimeBreakdown(Math.floor(durationSum / dayCount)* 1000));
console.log("Average Calls Made per day is: " + Math.floor(callsMade / dayCount));
console.log("Calls made this month: " + callsMade);
callDataObject.avgDuration = getTimeBreakdown(Math.floor(durationSum / DATA.length)* 1000);
callDataObject.avgTime = getTimeBreakdown(Math.floor(durationSum / dayCount)* 1000);
callDataObject.avgCalls = Math.floor(callsMade / dayCount);
callDataObject.totalCalls = callsMade;
return callDataObject;
}
}
//*** AVG CALLS PER DAY & CALLS MADE PER MONTH ***
//Note this is only to work out the calls made before data is ruined.
//Note: this does not take into account time travel calls (not taken by agent)
function callsMade(data){
const DATA = data;
let callCount = 0;
for(let i=0; i<DATA.length; i++){
if(DATA[i][CALLSTATUS] != "INBOUND UNANSWERED" && DATA[i][CALLSTATUS] != "INBOUND ANSWERED"){
callCount +=1;
}
}
return callCount;
}