Skip to content

Commit

Permalink
Parallelization in saving computed clusters
Browse files Browse the repository at this point in the history
  • Loading branch information
Albermonte committed Sep 6, 2023
1 parent ce21874 commit 9cc8a09
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"cSpell.words": [
"grecaptcha"
],
"deno.enable": true,
"deno.enablePaths": ["./supabase", "./bot"],
"deno.lint": true,
"deno.unstable": true,
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions supabase/functions/generate-locations-clusters-set/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/// <reference lib="deno.ns" />

/* eslint-disable no-console */

import { flushClusterTable, insertLocationsClusterSet } from '../../../database/functions.ts'
Expand Down Expand Up @@ -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<unknown>[] = []
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'))

0 comments on commit 9cc8a09

Please sign in to comment.