Enhance visit filtering and batching for remote data submission
This commit is contained in:
1 parent
0ca53e634c
commit
6badceaf46
1 file changed
+31
-9
@@ -24,16 +24,38 @@ async function archiveLocally(visits) {
|
||||
async function sendToCollector(visits) {
|
||||
// Empty settings deliberately disable sending; the local SQLite archive still works.
|
||||
if (!CONFIG.collectorUrl || !CONFIG.apiToken) return true;
|
||||
const remoteVisits = visits
|
||||
.filter((visit) => {
|
||||
if (typeof visit?.url !== "string") return false;
|
||||
try {
|
||||
const protocol = new URL(visit.url).protocol;
|
||||
return protocol === "http:" || protocol === "https:";
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
.map((visit) => ({
|
||||
...visit,
|
||||
title: String(visit.title ?? "").slice(0, 1000)
|
||||
}));
|
||||
try {
|
||||
const response = await fetch(CONFIG.collectorUrl, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": `Bearer ${CONFIG.apiToken}`
|
||||
},
|
||||
body: JSON.stringify({ device_id: CONFIG.deviceId, visits })
|
||||
});
|
||||
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
||||
for (let offset = 0; offset < remoteVisits.length; offset += 100) {
|
||||
const response = await fetch(CONFIG.collectorUrl, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": `Bearer ${CONFIG.apiToken}`
|
||||
},
|
||||
body: JSON.stringify({
|
||||
device_id: CONFIG.deviceId,
|
||||
visits: remoteVisits.slice(offset, offset + 100)
|
||||
})
|
||||
});
|
||||
if (!response.ok) {
|
||||
const detail = (await response.text()).trim().slice(0, 1000);
|
||||
throw new Error(`HTTP ${response.status}${detail ? `: ${detail}` : ""}`);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.warn("Collector unavailable", error.message);
|
||||
|
||||
Reference in new issue
Block a user