From 9cc8a09b4a2a7777051564193d1f61bae34ed4e3 Mon Sep 17 00:00:00 2001
From: Alberto Monterroso <14013679+Albermonte@users.noreply.github.com>
Date: Wed, 6 Sep 2023 10:02:41 -0400
Subject: [PATCH] Parallelization in saving computed clusters
---
.vscode/settings.json | 1 +
README.md | 2 +-
.../generate-locations-clusters-set/index.ts | 12 +++++++-----
3 files changed, 9 insertions(+), 6 deletions(-)
diff --git a/.vscode/settings.json b/.vscode/settings.json
index 36b6a88e..fbf5be67 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -2,6 +2,7 @@
"cSpell.words": [
"grecaptcha"
],
+ "deno.enable": true,
"deno.enablePaths": ["./supabase", "./bot"],
"deno.lint": true,
"deno.unstable": true,
diff --git a/README.md b/README.md
index 15f63f44..8742961f 100644
--- a/README.md
+++ b/README.md
@@ -100,7 +100,7 @@ supabase secrets list
And deploy them with:
```bash
-supabase functions deploy generate-locations-clusters-set
+supabase functions deploy generate-locations-clusters-set --import-map supabase/import_map.json
```
## Data flow
diff --git a/supabase/functions/generate-locations-clusters-set/index.ts b/supabase/functions/generate-locations-clusters-set/index.ts
index 597441f8..1838bca1 100644
--- a/supabase/functions/generate-locations-clusters-set/index.ts
+++ b/supabase/functions/generate-locations-clusters-set/index.ts
@@ -1,5 +1,3 @@
-///
-
/* eslint-disable no-console */
import { flushClusterTable, insertLocationsClusterSet } from '../../../database/functions.ts'
@@ -35,14 +33,18 @@ const locations = await getLocations(dbArgs, bbox)
const minZoom = Number(Deno.env.get('MIN_ZOOM'))
const maxZoom = Number(Deno.env.get('MAX_ZOOM'))
+const promises: Promise[] = []
for (let zoom = minZoom; zoom <= maxZoom; zoom++) {
const res = computeCluster(algorithm, locations, { zoom, boundingBox: bbox })
const singles: InsertLocationsClustersSetParamsItem[] = (res.singles as Location[]).map(({ lng, lat, uuid }) => ({ lat, lng, count: 1, locationUuid: uuid }))
- await insertLocationsClusterSet(dbArgs, {
+ promises.push(insertLocationsClusterSet(dbArgs, {
zoom_level: zoom,
items: singles.concat(res.clusters as Cluster[]),
- })
+ }))
console.log(
- `Added ${clusters.length} clusters and ${singles.length} singles at zoom level ${zoom}`,
+ `Added ${res.clusters.length} clusters and ${singles.length} singles at zoom level ${zoom}`,
)
}
+
+const res = await Promise.allSettled(promises)
+console.log(res.filter(({ status }) => status === 'rejected'))