-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
298 lines (249 loc) · 10.1 KB
/
script.js
File metadata and controls
298 lines (249 loc) · 10.1 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
let currentRun = [];
let allRuns = JSON.parse(localStorage.getItem('runs')) || [];
let currentTagIndex = 0;
const CSVFiles = {
"line2east": "BDEB_Tags.csv",
"line2west": "BDWB_Tags.csv"
}
// Fetch and parse CSV data
function fetchCSV(filePath) {
return fetch(filePath)
.then(response => response.text())
.then(data => {
let parsedData = data.split('\n').map(row => row.split(','));
// Remove header row if exists
if (parsedData[0][0] === 'tag_id') parsedData.shift();
return parsedData;
});
}
function startRun() {
let runDateElem = document.getElementById('runDate');
if (!runDateElem) return; // Exit early if we're not on the main page
let runDate = runDateElem.value;
let lineNumber = document.getElementById('lineNumber').value;
let bound = document.getElementById('bound').value;
currentRun = {
date: runDate,
line: lineNumber,
bound: bound,
measurements: []
};
currentTagIndex = 0;
showTag();
}
function showTag() {
let csvFile = CSVFiles[currentRun.line + currentRun.bound];
fetchCSV(csvFile).then(tags => {
if (currentTagIndex < tags.length) {
let tag = tags[currentTagIndex];
document.getElementById('currentTagID').innerText = tag[0];
document.getElementById('currentTagPosition').innerText = tag[8];
document.getElementById('tagEntrySection').style.display = 'block';
} else {
saveAndExit();
}
});
}
function saveMeasurement() {
let reportedPos = document.getElementById('reportedPosition').value;
let tagID = document.getElementById('currentTagID').innerText;
currentRun.measurements.push({
tag: tagID,
reportedPosition: reportedPos
});
currentTagIndex++;
showTag();
document.getElementById('reportedPosition').value = '';
}
function skipTag() {
currentTagIndex++;
showTag();
}
function previousTag() {
if (currentTagIndex > 0) {
currentTagIndex--;
showTag();
}
}
function saveAndExit() {
let existingRunIndex = allRuns.findIndex(run => run.date === currentRun.date && run.line === currentRun.line && run.bound === currentRun.bound);
if (existingRunIndex > -1) {
allRuns[existingRunIndex] = currentRun; // Update the existing run
} else {
allRuns.push(currentRun); // Add as a new run
}
localStorage.setItem('runs', JSON.stringify(allRuns));
document.getElementById('tagEntrySection').style.display = 'none';
// Show the 'Add New Run' section
document.getElementById('addRunSection').style.display = 'block';
loadRuns();
}
function calculateAverageOffset(measurements) {
let totalOffset = 0;
let validEntries = 0;
measurements.forEach(measurement => {
// Ensure we have both reportedPosition and databasePosition
if (!measurement.reportedPosition || !measurement.databasePosition) return;
let reported = parseFloat(measurement.reportedPosition);
let database = parseFloat(measurement.databasePosition);
// Ensure both values are valid numbers
if (!isNaN(reported) && !isNaN(database)) {
const offset = (database - reported) * 100;
totalOffset += offset;
validEntries++;
}
});
// If no valid entries, return 'N/A'
if (validEntries === 0) return 'N/A';
// Calculate the average
const averageOffset = totalOffset / validEntries;
// Return the average rounded to two decimal places
return averageOffset.toFixed(2);
}
function loadRuns() {
const runsTableBody = document.getElementById('runsTable').querySelector('tbody');
runsTableBody.innerHTML = ''; // Clear existing rows
allRuns.forEach((run, index) => {
const csvFile = CSVFiles[run.line + run.bound];
fetchCSV(csvFile).then(tags => {
// Map the parsed CSV tags with measurements
run.measurements.forEach(measurement => {
const tagData = tags.find(tag => tag[0] === measurement.tag);
if (tagData) {
measurement.databasePosition = tagData[8];
}
});
const row = runsTableBody.insertRow();
const dateCell = row.insertCell(0);
dateCell.textContent = run.date;
const lineNumberCell = row.insertCell(1);
const logoImg = document.createElement('img');
logoImg.src = 'line_2_logo.png';
logoImg.alt = 'Line Logo';
logoImg.className = 'logo';
lineNumberCell.appendChild(logoImg);
const boundCell = row.insertCell(2);
if (run.bound === 'east') {
boundCell.textContent = 'Eastbound';
} else if (run.bound === 'west') {
boundCell.textContent = 'Westbound';
}
const actionsCell = row.insertCell(3);
const continueButton = document.createElement('button');
continueButton.textContent = '❗'; // yellow exclamation point
continueButton.onclick = function() {
continueRun(index);
};
const avgOffsetCell = row.insertCell(4); // Assuming this is the 5th column
avgOffsetCell.textContent = calculateAverageOffset(run.measurements);
actionsCell.appendChild(continueButton);
const deleteButton = document.createElement('button');
deleteButton.textContent = '❌'; // red x
deleteButton.onclick = function() {
if (confirm('Are you sure you want to delete this run?')) {
deleteRun(index);
loadRuns(); // Refresh the table
}
};
actionsCell.appendChild(deleteButton);
// Add Download Button
const downloadButton = document.createElement('button');
downloadButton.textContent = '⬇️'; // download symbol
downloadButton.onclick = function() {
downloadRunAsCSV(index);
};
actionsCell.appendChild(downloadButton);
});
});
}
function sortTable() {
console.log("Sorting runs..."); // Debugging line
allRuns.sort((a, b) => {
const dateA = new Date(a.date);
const dateB = new Date(b.date);
if (dateA < dateB) return -1;
if (dateA > dateB) return 1;
return 0;
});
loadRuns(); // Refresh the table with sorted data
}
function continueRun(index) {
localStorage.setItem('continueRunIndex', index);
window.location.href = 'index.html';
}
function deleteRun(index) {
if (confirm('Are you sure?')) {
allRuns.splice(index, 1);
localStorage.setItem('runs', JSON.stringify(allRuns));
loadRuns();
}
}
function downloadRunAsCSV(index) {
const run = allRuns[index];
let csvContent = "data:text/csv;charset=utf-8,Tag ID,Reported Position\n";
fetchCSV(CSVFiles[run.line + run.bound]).then(tags => {
tags.forEach(tag => {
const measurement = run.measurements.find(measurement => measurement.tag === tag[0]);
const reportedPosition = measurement ? measurement.reportedPosition : '';
csvContent += `${tag[0]},${reportedPosition}\n`;
});
const encodedUri = encodeURI(csvContent);
const link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", `${run.date}_${run.line}_${run.bound}_run.csv`);
document.body.appendChild(link); // Required for FF
link.click();
document.body.removeChild(link);
});
}
function loadAnalysis() {
allRuns = JSON.parse(localStorage.getItem('runs')) || [];
// Define the CSV files for Eastbound and Westbound
const eastCSVFile = CSVFiles["line2east"];
const westCSVFile = CSVFiles["line2west"];
function analyzeRuns(direction, csvFile) {
let problematicTags = [];
let tagOffsets = {};
// Fetch and parse the CSV file
fetchCSV(csvFile).then(tags => {
allRuns.filter(run => run.bound === direction).forEach(run => {
run.measurements.forEach(measurement => {
// Check if reported position is empty or not a number
if (!measurement.reportedPosition || isNaN(parseFloat(measurement.reportedPosition))) {
return; // Skip this measurement
}
const tagData = tags.find(tag => tag[0] === measurement.tag);
if (tagData) {
const offset = Math.abs((parseFloat(tagData[8]) - parseFloat(measurement.reportedPosition)) * 100);
if (tagOffsets[measurement.tag]) {
tagOffsets[measurement.tag].push(offset);
} else {
tagOffsets[measurement.tag] = [offset];
}
}
});
});
for (let tag in tagOffsets) {
let avgOffset = tagOffsets[tag].reduce((acc, val) => acc + val, 0) / tagOffsets[tag].length;
problematicTags.push({ tag: tag, avgOffset: avgOffset });
}
problematicTags.sort((a, b) => b.avgOffset - a.avgOffset);
return problematicTags.slice(0, 5);
}).then(problematicTags => {
const containerId = direction === 'east' ? 'eastboundProblems' : 'westboundProblems';
if (problematicTags.length === 0) {
document.getElementById(containerId).textContent = 'No runs at this time';
} else {
problematicTags.forEach(tagData => {
const tagInfo = document.createElement('p');
tagInfo.textContent = `Tag: ${tagData.tag}, Average Offset: ${tagData.avgOffset.toFixed(2)} ft`;
document.getElementById(containerId).appendChild(tagInfo);
});
}
});
}
// Analyze for Eastbound
analyzeRuns('east', eastCSVFile);
// Analyze for Westbound
analyzeRuns('west', westCSVFile);
}