-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHistoricalData.js
196 lines (171 loc) · 6.6 KB
/
HistoricalData.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
let allLocations = [];
let filteredLocations = [];
let map;
let markersLayer = new L.LayerGroup();
let currentPage = 1;
const rowsPerPage = 10; // You can adjust this number as needed
function displayLocations(locations) {
const table = document.getElementById("locationData");
table.innerHTML = ""; // Clear existing data
const startIndex = (currentPage - 1) * rowsPerPage;
const endIndex = startIndex + rowsPerPage;
const PageLocations = locations.slice(startIndex, endIndex);
PageLocations.forEach(function(location) {
var row = table.insertRow();
row.insertCell(0).textContent = location.time;
row.insertCell(1).textContent = location.latitude.toFixed(4);
row.insertCell(2).textContent = location.longitude.toFixed(4);
row.insertCell(3).textContent = location.district;
row.insertCell(4).textContent = location.state;
row.insertCell(5).textContent = location.status;
});
updatePaginationControls(locations.length);
}
function updatePaginationControls(totalItems) {
const totalPages = Math.ceil(totalItems / rowsPerPage);
const pageInfo = document.getElementById('page-info');
const prevButton = document.getElementById('prev-page');
const nextButton = document.getElementById('next-page');
pageInfo.textContent = `Page ${currentPage} of ${totalPages}`;
prevButton.disabled = currentPage === 1;
nextButton.disabled = currentPage === totalPages;
}
function goToNextPage() {
if (currentPage < Math.ceil(filteredLocations.length / rowsPerPage)) {
currentPage++;
displayLocations(filteredLocations);
}
}
function goToPrevPage() {
if (currentPage > 1) {
currentPage--;
displayLocations(filteredLocations);
}
}
// Add event listeners for pagination buttons
document.getElementById('next-page').addEventListener('click', goToNextPage);
document.getElementById('prev-page').addEventListener('click', goToPrevPage);
// Generate device options from 1 to 35 (as per your example)
const deviceSelect = document.getElementById('device-select');
for (let i = 1; i <= 45; i++) {
const option = document.createElement('option');
option.value = i;
option.textContent = `Device ${i}`;
deviceSelect.appendChild(option);
}
function fetchLocations() {
var xhr = new XMLHttpRequest();
var url = `/api/locations?device_id=${deviceSelect.value}`;
xhr.open("GET", url, true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
var locations = JSON.parse(xhr.responseText);
allLocations = locations;
applyTimeFilter();
}
};
deviceSelect.addEventListener('change', function() {
currentPage = 1; // Reset to first page when changing device
fetchLocations();
});
xhr.send();
}
function applyTimeFilter() {
const startTime = document.getElementById('start-time').value;
const endTime = document.getElementById('end-time').value;
if (startTime || endTime) {
filteredLocations = allLocations.filter(location => {
const locationTime = new Date(location.time);
const isAfterStart = !startTime || locationTime >= new Date(startTime);
const isBeforeEnd = !endTime || locationTime <= new Date(endTime);
return isAfterStart && isBeforeEnd;
});
} else {
filteredLocations = allLocations;
}
filteredLocations.sort((a, b) => new Date(b.time) - new Date(a.time));
currentPage = 1; // Reset to first page when applying filter
displayLocations(filteredLocations);
}
function filterLastDay() {
const now = new Date();
const lastDay = new Date(now.getTime() - 24 * 60 * 60 * 1000);
setFilterTimes(lastDay, now);
}
function filterLastWeek() {
const now = new Date();
const lastWeek = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
setFilterTimes(lastWeek, now);
}
function filterLastMonth() {
const now = new Date();
const lastMonth = new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000);
setFilterTimes(lastMonth, now);
}
function setFilterTimes(startTime, endTime) {
document.getElementById('start-time').value = startTime.toISOString().slice(0, 16);
document.getElementById('end-time').value = '' ;
applyTimeFilter();
}
function filterLocationsByTime(startTime, endTime) {
filteredLocations = allLocations.filter(location => {
const locationTime = new Date(location.time);
return locationTime >= startTime && locationTime <= endTime;
});
currentPage = 1; // Reset to first page when applying filter
displayLocations(filteredLocations);
}
deviceSelect.addEventListener('change', fetchLocations);
function exportTableToCSV() {
const csv = [["Time", "Latitude", "Longitude", "District", "State"]];
filteredLocations.forEach(location => {
csv.push([
location.time,
location.latitude.toFixed(4),
location.longitude.toFixed(4),
location.district,
location.state
]);
});
const csvContent = csv.map(row => row.join(",")).join("\n");
const csvFile = new Blob([csvContent], { type: "text/csv" });
const downloadLink = document.createElement("a");
downloadLink.download = `device_${deviceSelect.value}_data.csv`;
downloadLink.href = window.URL.createObjectURL(csvFile);
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
function exportAllDataToCSV() {
const wb = XLSX.utils.book_new();
let fetchCount = 0;
for (let i = 1; i <= 35; i++) {
const xhr = new XMLHttpRequest();
const url = `/api/locations?device_id=${i}`;
xhr.open("GET", url, true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
const locations = JSON.parse(xhr.responseText);
const sheetData = locations.map(location => [
location.time,
location.latitude,
location.longitude,
location.district,
location.state
]);
sheetData.unshift(["Time", "Latitude", "Longitude", "District", "State"]);
const ws = XLSX.utils.aoa_to_sheet(sheetData);
XLSX.utils.book_append_sheet(wb, ws, `Device ${i}`);
fetchCount++;
if (fetchCount === 35) {
XLSX.writeFile(wb, 'all_devices_data.xlsx');
}
}
};
xhr.send();
}
}
document.addEventListener('DOMContentLoaded', function () {
fetchLocations();
setInterval(fetchLocations, 45000); // Refresh data every 15 seconds
});