forked from googlemaps/fleet-debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocalStorage.js
More file actions
377 lines (330 loc) · 11.7 KB
/
localStorage.js
File metadata and controls
377 lines (330 loc) · 11.7 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// src/localStorage.js
import JSZip from "jszip";
import { DEFAULT_API_KEY } from "./constants";
import _ from "lodash";
import { log } from "./Utils";
const DB_NAME = "FleetDebuggerDB";
const STORE_NAME = "uploadedData";
const TOS_RESTRICTED_ATTRIBUTES = [
"currentRouteSegment",
"waypoints",
"currentRouteSegmentEndPoint",
"pickupPoint",
"intermediateDestinations",
"dropoffPoint",
"remainingWaypoints",
"vehicleWaypoints",
];
/**
* Recursively sorts the keys of an object or objects within an array,
* ensuring a consistent order for display and comparison.
* @param {*} data The object or array to sort.
* @returns {*} The sorted object or array.
*/
export function sortObjectKeysRecursively(data) {
const _sort = (obj) => {
if (obj === null || typeof obj !== "object") {
return obj;
}
if (Array.isArray(obj)) {
return obj.map(_sort);
}
return Object.keys(obj)
.sort()
.reduce((sorted, key) => {
sorted[key] = _sort(obj[key]);
return sorted;
}, {});
};
return _sort(data);
}
async function openDB() {
return new Promise((resolve, reject) => {
const request = indexedDB.open(DB_NAME, 1);
request.onerror = () => reject(request.error);
request.onsuccess = () => resolve(request.result);
request.onupgradeneeded = (event) => {
event.target.result.createObjectStore(STORE_NAME, { keyPath: "id" });
};
});
}
export async function uploadFile(file, index) {
console.log(`Importing file: ${file.name}`);
let parsedData;
if (file.name.endsWith(".zip")) {
log("uploadFile: Processing ZIP file.");
parsedData = await processZipFile(file);
} else if (file.name.endsWith(".json")) {
log("uploadFile: Processing JSON file.");
parsedData = await processJsonFile(file);
} else {
throw new Error("Unsupported file format. Please upload a ZIP or JSON file.");
}
parsedData = ensureCorrectFormat(parsedData);
await saveToIndexedDB(parsedData, index);
log("File imported and stored successfully");
}
export async function uploadCloudLogs(logs, index) {
try {
if (!logs || !Array.isArray(logs) || logs.length === 0) {
console.warn("No logs to upload - skipping upload");
throw new Error("No logs to upload. Please adjust your search criteria and try again.");
}
const formattedData = ensureCorrectFormat(logs);
await saveToIndexedDB(formattedData, index);
return formattedData;
} catch (error) {
console.error("Error processing cloud logs:", error);
throw error;
}
}
export async function saveDatasetAsJson(index) {
try {
log(`Attempting to save dataset ${index} as JSON`);
const data = await getUploadedData(index);
if (!data || !data.rawLogs || !Array.isArray(data.rawLogs) || data.rawLogs.length === 0) {
throw new Error("No data available to save");
}
const jsonContent = JSON.stringify(data, null, 2);
const blob = new Blob([jsonContent], { type: "application/json" });
// Create a temporary download link
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
// Set the filename based on the dataset number and current date
const date = new Date().toISOString().split("T")[0];
link.download = `dataset_${index + 1}_${date}.json`;
document.body.appendChild(link);
link.click();
setTimeout(() => {
document.body.removeChild(link);
URL.revokeObjectURL(url);
}, 100);
log(`Dataset ${index} saved successfully`);
return true;
} catch (error) {
console.error(`Error saving dataset ${index}:`, error);
throw error;
}
}
async function processZipFile(file) {
const zip = new JSZip();
const contents = await zip.loadAsync(file);
const jsonFile = Object.values(contents.files).find((file) => file.name.endsWith(".json"));
if (!jsonFile) {
throw new Error("No JSON file found in the ZIP archive");
}
const jsonContent = await jsonFile.async("string");
return processJsonFile(new Blob([jsonContent], { type: "application/json" }));
}
async function processJsonFile(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = (event) => {
try {
const content = event.target.result;
const parsedData = parseJsonContent(content);
resolve(parsedData);
} catch (error) {
reject(error);
}
};
reader.onerror = () => reject(new Error("Error reading JSON file"));
reader.readAsText(file);
});
}
export function parseJsonContent(content) {
log("Parsing JSON content");
const processJsonObject = (obj) => {
if (obj === null || typeof obj !== "object") return obj;
if (Array.isArray(obj)) return obj.map(processJsonObject);
return Object.keys(obj).reduce((result, key) => {
let value = obj[key];
if (value === null || value === undefined) {
return result;
}
const newKey = key.replace(/_/g, "");
// Check if this is a value object with only a 'value' property and flatten
if (
value !== null &&
typeof value === "object" &&
!Array.isArray(value) &&
Object.keys(value).length === 1 &&
"value" in value
) {
value = value.value;
if (value === null || value === undefined) {
return result;
}
} else if (typeof value === "object" && value !== null) {
// Recursively process nested objects
value = processJsonObject(value);
// Skip empty objects (those with no properties after processing)
if (typeof value === "object" && !Array.isArray(value) && Object.keys(value).length === 0) {
return result;
}
}
result[newKey] = value;
return result;
}, {});
};
try {
const parsed = JSON.parse(content);
const processedData = processJsonObject(parsed);
log("Processed JSON data: removed underscores, flattened value objects, and pruned null/undefined fields");
return sortObjectKeysRecursively(processedData);
} catch (error) {
log("Initial JSON parsing failed, attempting to wrap in array");
try {
const parsed = JSON.parse(`[${content}]`);
const processedData = processJsonObject(parsed);
log("Processed JSON data in array format");
return sortObjectKeysRecursively(processedData);
} catch (innerError) {
console.error("JSON parsing error:", innerError);
throw new Error(`Invalid JSON content: ${innerError.message}`);
}
}
}
export function removeEmptyObjects(obj) {
Object.keys(obj).forEach((key) => {
if (obj[key] && typeof obj[key] === "object") {
if (Object.keys(obj[key]).length === 0) {
delete obj[key];
} else {
removeEmptyObjects(obj[key]);
}
}
});
return obj;
}
function isRestrictedLog(row) {
return row.jsonPayload?.["@type"]?.includes("Restricted") || false;
}
export function ensureCorrectFormat(data) {
//Handle if data is not array (like when reading a file).
if (!Array.isArray(data)) {
// If it's already in the correct format, return it as is.
if (data && data.rawLogs && Array.isArray(data.rawLogs)) {
return {
...data,
APIKEY: data.APIKEY || DEFAULT_API_KEY,
};
}
// If it's not an array and not in expected format, throw an error.
throw new Error("Invalid input data. Expected an array or an object with a rawLogs property.");
}
const logsArray = data; // It's already an array of logs.
const restrictedLogsMap = new Map();
logsArray.forEach((row) => {
if (isRestrictedLog(row)) {
removeEmptyObjects(row.jsonPayload);
restrictedLogsMap.set(row.jsonPayload.parentInsertId, row);
}
});
// Filter out restricted logs while merging their TOS-restricted attributes into their parent logs.
const mergedLogs = logsArray.filter((row) => {
if (isRestrictedLog(row)) {
return false;
}
const restrictedLog = restrictedLogsMap.get(row.insertId)?.jsonPayload;
if (restrictedLog) {
["request", "response"].forEach((section) => {
if (restrictedLog[section] && row.jsonPayload[section]) {
TOS_RESTRICTED_ATTRIBUTES.forEach((attr) => {
if (restrictedLog[section][attr] !== undefined) {
row.jsonPayload[section][attr] = restrictedLog[section][attr];
}
if (restrictedLog[section].vehicle?.[attr] !== undefined) {
row.jsonPayload[section].vehicle = row.jsonPayload[section].vehicle || {};
row.jsonPayload[section].vehicle[attr] = restrictedLog[section].vehicle[attr];
}
if (restrictedLog[section].trip?.[attr] !== undefined) {
row.jsonPayload[section].trip = row.jsonPayload[section].trip || {};
row.jsonPayload[section].trip[attr] = restrictedLog[section].trip[attr];
}
});
}
});
}
return true;
});
mergedLogs.forEach((row) => {
if (row.jsonPayload) {
row.jsonPayload = sortObjectKeysRecursively(row.jsonPayload);
}
});
// Determine the solution type based on the presence of _delivery_vehicle logs
const isLMFS = mergedLogs.some((row) => row.logName?.includes("_delivery_vehicle"));
const solutionType = isLMFS ? "LMFS" : "ODRD";
console.log(`Determined solution type: ${solutionType}`);
const bounds = {
north: -90,
south: 90,
east: -180,
west: 180,
};
let hasPoints = false;
mergedLogs.forEach((row) => {
const lat =
_.get(row, "jsonPayload.response.lastLocation.rawLocation.latitude") ||
_.get(row, "jsonPayload.response.lastlocation.rawlocation.latitude");
const lng =
_.get(row, "jsonPayload.response.lastLocation.rawLocation.longitude") ||
_.get(row, "jsonPayload.response.lastlocation.rawlocation.longitude");
if (lat != null && lng != null) {
if (!hasPoints) {
bounds.north = lat;
bounds.south = lat;
bounds.east = lng;
bounds.west = lng;
hasPoints = true;
} else {
bounds.north = Math.max(bounds.north, lat);
bounds.south = Math.min(bounds.south, lat);
bounds.east = Math.max(bounds.east, lng);
bounds.west = Math.min(bounds.west, lng);
}
}
});
if (!hasPoints) log("Bounds Calculation Failed: Could not find vehicle location data in any row.");
return {
APIKEY: DEFAULT_API_KEY,
vehicle: "",
projectId: "",
logSource: "Direct Cloud Logging",
solutionType: solutionType,
rawLogs: mergedLogs,
bounds: hasPoints ? bounds : null,
};
}
export async function saveToIndexedDB(data, index) {
const db = await openDB();
const transaction = db.transaction(STORE_NAME, "readwrite");
const store = transaction.objectStore(STORE_NAME);
return new Promise((resolve, reject) => {
const request = store.put({ id: `uploadedData${index}`, data: data });
request.onerror = () => reject(request.error);
request.onsuccess = () => resolve();
});
}
export async function getUploadedData(index) {
const db = await openDB();
const transaction = db.transaction(STORE_NAME, "readonly");
const store = transaction.objectStore(STORE_NAME);
return new Promise((resolve, reject) => {
const request = store.get(`uploadedData${index}`);
request.onerror = () => reject(request.error);
request.onsuccess = () => resolve(request.result ? request.result.data : null);
});
}
export async function deleteUploadedData(index) {
const db = await openDB();
const transaction = db.transaction(STORE_NAME, "readwrite");
const store = transaction.objectStore(STORE_NAME);
return new Promise((resolve, reject) => {
const request = store.delete(`uploadedData${index}`);
request.onerror = () => reject(request.error);
request.onsuccess = () => resolve();
});
}