-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
222 lines (178 loc) · 7.3 KB
/
script.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
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
"use strict";
let XMLFiles = [
'xml/bookeye1.xml',
'xml/bookeye2.xml',
'xml/bookeye3.xml'
]; //Add file paths here
getXML(createSummary);
let detailBTN = document.getElementById('Detail'); //button
let summaryBTN = document.getElementById('Summary'); //button
let mainDIV = document.getElementById('Stats'); //div
//Adds event listeners for buttons
detailBTN.addEventListener('click', function() {
removeChildren(mainDIV);
getXML(createDailyDetailTable);
});
summaryBTN.addEventListener('click', function() {
removeChildren(mainDIV);
getXML(createSummary);
});
function appendDataToRow(tableElem, data, tagName) {
let newTableRow = document.createElement('tr');
tableElem.appendChild(newTableRow);
data.forEach(function(datum) {
let newElem = document.createElement(tagName);
newElem.textContent = datum;
newTableRow.appendChild(newElem);
});
} //Appends TR and THs or TDs
function appendTable(elem) {
let newTable = document.createElement('table');
newTable.align = 'center';
elem.appendChild(newTable);
return newTable;
} //Appends and returns table element
function getAvg(array) {
let runningTotal = 0;
for (let i = 0; i < array.length; i++) {
let number = parseInt(array[i].textContent);
runningTotal += number;
}
return Math.round(runningTotal / array.length);
} //Returns avg number of scans for a particular KIC
function getData(XHR, tagName) {
return XHR.responseXML.getElementsByTagName(tagName);
} //Returns array of data from XML
function getFriendlyDate(date) {
let day = date.getDate();
let month = date.getMonth() + 1;
let year = date.getFullYear();
return month + "/" + day + "/" + year;
} //Returns reader friendly date
function getHighestNum(array) {
let parsedData = [];
for (let i = 0; i < array.length; i++) {
parsedData.push(parseInt(array[i].textContent));
}
return Math.max(...parsedData);
} //Returns highest number in passed array
function getRunningTotal(array) {
let runningTotal = 0;
for (let i = 0; i < array.length; i++) {
let number = parseInt(array[i].textContent);
runningTotal += number;
}
return runningTotal;
} //Returns total scans for a particular KIC
function getXML(funct) {
XMLFiles.forEach(function(XMLFile) {
let XHR = new XMLHttpRequest();
XHR.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 404) {
alert("Something went wrong. See Web Console for more details.");
} else if (this.readyState === 4 && this.status === 200) {
funct(XHR);
}
}
XHR.open('GET', XMLFile);
XHR.send();
}); //This may cause the script to create tables out of order in some browsers
} //Use AJAX to get XML data from each file in the XMLFiles array and run passed function
function removeChildren(elem) {
while (elem.firstChild) {
elem.removeChild(elem.firstChild);
}
} //Removes all elements nested within passed element
//Main functions
function createDailyDetailTable(XHR) {
let newTable = appendTable(mainDIV);
newTable.addEventListener('click', (e) => {
if (e.target.nodeName == 'TD') {
if (e.target.parentNode.style.backgroundColor == 'yellow') {
e.target.parentNode.style.backgroundColor = '';
} else {
e.target.parentNode.style.backgroundColor = 'yellow';
}
}
}); //Add event listener to table to highlight clicked table rows
let compName = getData(XHR, 'ComputerName')[0].textContent;
let newCaption = document.createElement('caption');
newCaption.textContent = compName;
newTable.appendChild(newCaption);
let summaryTableHeaders = [
/*Column 1*/ "Date",
/*Column 2*/ "Data Sent (MBs)",
/*Column 3*/ "Total Scans",
/*Column 4*/ "Sessions",
/*Column 5*/ "Avg Scans/Session"
]; //Detailed table header row created by createDailyDetailTable()
appendDataToRow(newTable, summaryTableHeaders, 'th');
let dates = getData(XHR, 'Date');
let sizes = getData(XHR, 'Size');
let scans = getData(XHR, 'Scans');
let sessions = getData(XHR, 'Ss');
let dateTracker = new Date(dates[0].textContent);
for (let i = 0; i < scans.length; i++) {
let rowDate = new Date(dates[i].textContent);
while (rowDate > dateTracker) {
let tableEmptyData = [
/*Column 1*/ getFriendlyDate(dateTracker),
/*Column 2*/ "*",
/*Column 3*/ "*",
/*Column 4*/ "*",
/*Column 5*/ "*"
]; //Daily detail empty data row created by createDailyDetailTable()
appendDataToRow(newTable, tableEmptyData, 'td');
dateTracker = new Date(dateTracker.getFullYear(), dateTracker.getMonth(), dateTracker.getDate() + 1);
} //Add rows with no data
let date = getFriendlyDate(rowDate);
let size = parseInt(sizes[i].textContent);
let sizeMB = Math.round(size / 100);
let totalScans = parseInt(scans[i].textContent);
let totalSes = sessions[i].textContent;
let avgScansPerSes = Math.round(totalScans / totalSes);
let tableDetailData = [
/*Column 1*/ date,
/*Column 2*/ sizeMB,
/*Column 3*/ totalScans,
/*Column 4*/ totalSes,
/*Column 5*/ avgScansPerSes
]; //Daily detail data row created by createDailyDetailTable()
appendDataToRow(newTable, tableDetailData, 'td');
dateTracker = new Date(dateTracker.getFullYear(), dateTracker.getMonth(), dateTracker.getDate() + 1);
} //Each iteration of this loop creates a row in the table
}
function createSummary(XHR) {
let newTable = appendTable(mainDIV);
let compName = getData(XHR, 'ComputerName')[0].textContent;
let dates = getData(XHR, 'Date');
let firstDate = new Date(dates[0].textContent);
let lastDate = new Date(dates[dates.length - 1].textContent);
let dateRange = getFriendlyDate(firstDate) + " - " + getFriendlyDate(lastDate);
let newCaption = document.createElement('caption');
newCaption.textContent = compName + " (" + dateRange + ")";
newTable.appendChild(newCaption);
let summaryTableHeaders = [
/*Column 1*/ "Data Sent (MBs)",
/*Column 2*/ "Avg Data/Day (MBs)",
/*Column 3*/ "Total Scans",
/*Column 4*/ "Avg Scans/Day",
/*Column 5*/ "Highest # of Scans in a Day"
]; //Summary table header row created by createSummary()
appendDataToRow(newTable, summaryTableHeaders, 'th');
let sizes = getData(XHR, 'Size');
let dataSentMBs = Math.round(getRunningTotal(sizes) / 100);
let avgDataDayMBs = Math.round(getAvg(sizes) / 100);
let scans = getData(XHR, 'Scans');
let totalScans = getRunningTotal(scans);
let avgScansDay = getAvg(scans);
let highestScans = getHighestNum(scans);
let summaryData = [
/*Column 1*/ dataSentMBs,
/*Column 2*/ avgDataDayMBs,
/*Column 3*/ totalScans,
/*Column 4*/ avgScansDay,
/*Column 5*/ highestScans
]; //Summary table data row created by createSummary()
appendDataToRow(newTable, summaryData, 'td');
}