Skip to content

Commit

Permalink
add git commit script
Browse files Browse the repository at this point in the history
  • Loading branch information
viet nguyen committed Jan 14, 2024
1 parent 9832436 commit 52c5edf
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 2 deletions.
22 changes: 22 additions & 0 deletions export-crag-data.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

if [ -z ${GITHUB_ACCESS_TOKEN} ]
then
echo "GITHUB_ACCESS_TOKEN not defined."
exit 1
fi

echo "cloning openbeta-export repository"
git clone --depth 1 --branch production https://ob-bot-user:${GITHUB_ACCESS_TOKEN}@github.com/OpenBeta/openbeta-export || exit 1
git config user.name "db-export-bot"
git config user.email "db-export-bot@noreply"
cd ..

echo "start exporting CRAG data..."
yarn export-crags

echo "... finished export. Committing data..."

git add -A
git commit -am "export crag data"
git push origin production
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"export-prod": "./export.sh",
"prepare": "husky install",
"import-users": "tsc ; node build/db/utils/jobs/migration/CreateUsersCollection.js",
"export-crags": "tsc ; node build/db/export/CragGeojson/index.js"
"export-crags": "tsc ; node build/db/utils/jobs/CragGeojson/index.js"
},
"standard": {
"plugins": [
Expand All @@ -105,4 +105,4 @@
"engines": {
"node": ">=16.14.0"
}
}
}
140 changes: 140 additions & 0 deletions src/db/utils/jobs/CragGeojson/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { createWriteStream } from 'node:fs'
import { point, feature, featureCollection, Feature, Point, Polygon } from '@turf/helpers'
import os from 'node:os'
import { MUUID } from 'uuid-mongodb'

import { connectDB, gracefulExit, getAreaModel } from '../../../index.js'
import { logger } from '../../../../logger.js'

/**
* Export leaf areas as Geojson. Leaf areas are crags/boulders that have climbs.
*/
async function exportLeafCrags (): Promise<void> {
const model = getAreaModel()

const stream = createWriteStream('crags.geojson', { encoding: 'utf-8' })

const features: Array<Feature<Point, {
name: string
id: string
}>> = []

for await (const doc of model.find({ 'metadata.leaf': true }).lean()) {
const { metadata, area_name: areaName, pathTokens, ancestors } = doc

const ancestorArray = ancestors.split(',')
const pointFeature = point(doc.metadata.lnglat.coordinates, {
id: metadata.area_id.toUUID().toString(),
name: areaName,
type: 'crag',
parent: {
id: ancestorArray[ancestorArray.length - 2],
name: pathTokens[doc.pathTokens.length - 2]
}
})
features.push(pointFeature)
}
stream.write(JSON.stringify(featureCollection(features)))
stream.close()
}

/**
* Export crag groups as Geojson. Crag groups are immediate parent of leaf areas (crags/boulders).
*/
async function exportCragGroups (): Promise<void> {
const model = getAreaModel()
const stream = createWriteStream('crag-groups.geojson', { encoding: 'utf-8' })

interface CragGroup {
uuid: MUUID
name: string
polygon: Polygon
childAreaList: Array<{
name: string
uuid: MUUID
leftRightIndex: number
}>
}

const rs: CragGroup[] = await model.aggregate([
{ $match: { 'metadata.leaf': true } },
{
$lookup: {
from: 'areas',
localField: '_id',
foreignField: 'children',
as: 'parentCrags'
}
},
{
$match: {
$and: [
{ parentCrags: { $type: 'array', $ne: [] } }
]
}
},
{
$unwind: '$parentCrags'
},
{
$addFields: {
parentCrags: {
childId: '$metadata.area_id'
}
}
},
{
$group: {
_id: {
uuid: '$parentCrags.metadata.area_id',
name: '$parentCrags.area_name',
polygon: '$parentCrags.metadata.polygon'
},
childAreaList: {
$push: {
leftRightIndex: '$metadata.leftRightIndex',
uuid: '$metadata.area_id',
name: '$area_name'
}
}
}
},
{
$project: {
_id: 0,
uuid: '$_id.uuid',
name: '$_id.name',
polygon: '$_id.polygon',
childAreaList: 1
}
}
])

const features: Array<Feature<Polygon, {
name: string
id: string
}>> = []

for await (const doc of rs) {
const polygonFeature = feature(doc.polygon, {
type: 'crag-group',
name: doc.name,
id: doc.uuid.toUUID().toString(),
children: doc.childAreaList.map(({ uuid, name, leftRightIndex }) => (
{ id: uuid.toUUID().toString(), name, lr: leftRightIndex }))
})
features.push(polygonFeature)
}

stream.write(JSON.stringify(featureCollection(features)) + os.EOL)
stream.close()
}

async function onDBConnected (): Promise<void> {
logger.info('Start exporting crag data as Geojson')
await exportLeafCrags()
await exportCragGroups()
await gracefulExit()
}

void connectDB(onDBConnected)

0 comments on commit 52c5edf

Please sign in to comment.