|
1 | 1 | import { |
2 | | - ageInDays, |
3 | | - definedOr, |
4 | | - or, |
5 | 2 | parseLocation, |
6 | | - retry, |
7 | 3 | sampleKey |
8 | 4 | } from '../content/shared.js' |
9 | 5 |
|
10 | | -function mergeMetadata(a, b) { |
11 | | - const merged = { |
12 | | - time: Math.max(a.time, b.time), |
13 | | - snr: definedOr(Math.max, a.snr, b.snr), |
14 | | - rssi: definedOr(Math.max, a.rssi, b.rssi), |
15 | | - observed: definedOr(or, a.observed, b.observed), |
16 | | - }; |
17 | | - |
18 | | - const setA = new Set(a.path); |
19 | | - const setB = new Set(b.path); |
20 | | - merged.path = Array.from(setA.union(setB)); |
21 | | - |
22 | | - return merged; |
23 | | -} |
24 | | - |
25 | 6 | export async function onRequest(context) { |
26 | 7 | const request = context.request; |
27 | 8 | const data = await request.json(); |
28 | | - const store = context.env.SAMPLES; |
29 | 9 |
|
| 10 | + // TODO: Pass geohash directly. |
30 | 11 | const [lat, lon] = parseLocation(data.lat, data.lon); |
31 | 12 | const key = sampleKey(lat, lon); |
32 | | - const path = (data.path ?? []).map(p => p.toLowerCase()); |
33 | | - let metadata = { |
34 | | - time: Date.now(), |
35 | | - rssi: data.rssi ?? null, |
36 | | - snr: data.snr ?? null, |
37 | | - path: path, |
38 | | - observed: data.observed ?? false, |
39 | | - }; |
40 | | - |
41 | | - // KV only allows one write to a key per second. |
42 | | - // There's a strong possibility that's hit by #wardrive. |
43 | | - await retry(async () => { |
44 | | - const resp = await store.getWithMetadata(key); |
45 | | - if (resp.value !== null |
46 | | - && resp.metadata !== null |
47 | | - && ageInDays(resp.metadata.time) < 1) { |
48 | | - // Merge new information with existing if recent. |
49 | | - metadata = mergeMetadata(metadata, resp.metadata); |
50 | | - } |
51 | 13 |
|
52 | | - console.log(`PUT ${key} -> ${JSON.stringify(metadata)}`); |
53 | | - await store.put(key, "", { |
54 | | - metadata: metadata |
55 | | - }); |
56 | | - }); |
| 14 | + const time = Date.now(); |
| 15 | + const rssi = data.rssi ?? null; |
| 16 | + const snr = data.snr ?? null; |
| 17 | + const path = (data.path ?? []).map(p => p.toLowerCase()); |
| 18 | + const observed = data.observed ?? false; |
| 19 | + |
| 20 | + await context.env.DB |
| 21 | + .prepare(` |
| 22 | + INSERT INTO samples (hash, time, rssi, snr, observed, repeaters) |
| 23 | + VALUES (?, ?, ?, ?, ?, ?) |
| 24 | + ON CONFLICT(hash) DO UPDATE SET |
| 25 | + time = excluded.time, |
| 26 | + rssi = CASE |
| 27 | + WHEN samples.rssi IS NULL THEN excluded.rssi |
| 28 | + WHEN excluded.rssi IS NULL THEN samples.rssi |
| 29 | + ELSE MAX(samples.rssi, excluded.rssi) |
| 30 | + END, |
| 31 | + snr = CASE |
| 32 | + WHEN samples.snr IS NULL THEN excluded.snr |
| 33 | + WHEN excluded.snr IS NULL THEN samples.snr |
| 34 | + ELSE MAX(samples.snr, excluded.snr) |
| 35 | + END, |
| 36 | + observed = MAX(samples.observed, excluded.observed), |
| 37 | + repeaters = ( |
| 38 | + SELECT json_group_array(value) FROM ( |
| 39 | + SELECT value FROM json_each(samples.repeaters) |
| 40 | + UNION |
| 41 | + SELECT value FROM json_each(excluded.repeaters) |
| 42 | + ) |
| 43 | + ) |
| 44 | + `) |
| 45 | + .bind(key, time, rssi, snr, observed, JSON.stringify(path)) |
| 46 | + .run(); |
57 | 47 |
|
58 | 48 | return new Response('OK'); |
59 | 49 | } |
0 commit comments