Do more processing in Deno, than in SQLite

This commit is contained in:
Marcin Czop 2025-01-30 05:26:29 +01:00
parent 7ed8f8d85b
commit 05fae4285d
No known key found for this signature in database
GPG key ID: 44BCC84471234D0D

View file

@ -14,10 +14,20 @@ app.use("/", serveStatic({ root: "./public" }));
app.post("/api/v1/geosubmit", async (c) => { app.post("/api/v1/geosubmit", async (c) => {
const enconding = await c.req.header("Content-Encoding"); const enconding = await c.req.header("Content-Encoding");
if (enconding !== "gzip") { if (enconding && enconding !== "gzip") {
return c.json({ status: 400, message: "Bad Request" }); console.log(enconding);
return c.json({ status: 400, message: "Bad Request" }, 400);
} }
const body = await c.req.arrayBuffer(); let json: Geosubmit;
if (enconding === "gzip") {
const body = await c.req.arrayBuffer();
const arr = new Uint8Array(body);
const data = await gunzip(arr);
json = JSON.parse(new TextDecoder().decode(data)) as Geosubmit;
} else {
json = (await c.req.json()) as Geosubmit;
}
const body = gzip(new TextEncoder().encode(JSON.stringify(json)));
const ftch = await fetch("https://api.beacondb.net/v2/geosubmit", { const ftch = await fetch("https://api.beacondb.net/v2/geosubmit", {
method: "POST", method: "POST",
@ -28,13 +38,9 @@ app.post("/api/v1/geosubmit", async (c) => {
body: body, body: body,
}); });
if (ftch.status !== 200) { if (ftch.status !== 200) {
return c.json({ status: ftch.status, message: "Bad Request" }); return c.json({ status: ftch.status, message: "Bad Request" }, 400);
} }
let json: Geosubmit;
const arr = new Uint8Array(body);
const data = await gunzip(arr);
json = JSON.parse(new TextDecoder().decode(data));
json.items.forEach((item) => { json.items.forEach((item) => {
const timestamp = Math.floor(item.timestamp / 1000); const timestamp = Math.floor(item.timestamp / 1000);
const hex = h3.latLngToCell(item.position.latitude, item.position.longitude, 11); const hex = h3.latLngToCell(item.position.latitude, item.position.longitude, 11);
@ -64,8 +70,11 @@ app.post("/api/v1/geosubmit", async (c) => {
}); });
} }
const isHexInDb = db.prepare("SELECT hex_id FROM hexes WHERE hex_id = ?").get(hex); const hexInDb = db.prepare("SELECT * FROM hexes WHERE hex_id = ?").get(hex) as { hex_id: string; wifi: boolean; gsm: boolean; wcdma: boolean; lte: boolean; ble: boolean; last_update: number } | undefined;
if (isHexInDb) {
if (hexInDb && hexInDb.last_update >= timestamp && hexInDb.wifi === hasWifi && hexInDb.gsm === hasGsm && hexInDb.wcdma === hasWcdma && hexInDb.lte === hasLte && hexInDb.ble === hasBle) return;
if (hexInDb) {
db.prepare( db.prepare(
` `
UPDATE hexes UPDATE hexes