Skip to content

Commit

Permalink
feat: have mapeo-migrate cli for migrating mapeo data
Browse files Browse the repository at this point in the history
  • Loading branch information
luandro committed Oct 24, 2024
1 parent cf976ea commit 6674c2b
Show file tree
Hide file tree
Showing 6 changed files with 358 additions and 29 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/mapeo-migrate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Publish Mapeo Migrator

on:
push:
branches:
- 'migration-tool'

jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '16'
registry-url: 'https://registry.npmjs.org'

- name: Install dependencies
run: npm ci

- name: Build package
run: npm run build

- name: Publish to npm
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
129 changes: 123 additions & 6 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,31 @@
"name": "mapeo-bridge",
"version": "1.0.1",
"description": "sync and storage service for mapeo maps",
"bin": {
"mapeo-migrate": "./src/bin/migrate.js"
},
"main": "index.js",
"scripts": {
"start": "node .",
"lint": "standard --fix"
"lint": "standard --fix",
"build": "npm run lint"
},
"files": [
"src",
"index.js"
],
"license": "ISC",
"dependencies": {
"@fastify/cors": "^8.1.1",
"@fastify/static": "^6.5.0",
"@mapeo/core": "git+https://github.com/digidem/mapeo-core.git#ws-sync",
"commander": "^12.1.0",
"fastify": "^4.9.2",
"hypercore-crypto": "^1.0.0",
"jsonexport": "^3.2.0",
"level": "^6.0.1",
"mkdirp": "^1.0.4",
"node-fetch": "^2.7.0",
"osm-p2p": "^5.1.0",
"pino": "^6.8.0",
"safe-fs-blob-store": "^1.0.6"
Expand Down
2 changes: 1 addition & 1 deletion public/assets/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function degr2rad(degr) { return degr * Math.PI / 180; }
* pairs in degrees. e.g. [[latitude1, longtitude1], [latitude2
* [longtitude2] ...]
*
* @return array with the center latitude longtitude pairs in
* @return array with the center latitude longtitude pairs in
* degrees.
*/
function getLatLngCenter(latLngInDegr) {
Expand Down
64 changes: 43 additions & 21 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,27 @@ module.exports = (mapeo, filteredType) => {
reply.sendFile('index.html') // serving path.join(__dirname, 'public', 'myHtml.html') directly
})
fastify.get('/mapeo', (req, reply) => {
const { category, name } = req.query
mapeo.observationList(null, (err, data) => {
if (err) return console.error(err)
reply.send(data)
if (err) {
console.error(err)
return reply.status(500).send({ error: 'Internal Server Error' })
}
let filteredData = data
if (category) {
const categories = Array.isArray(category) ? category : [category]
filteredData = filteredData.filter(obs => {
return obs.tags && obs.tags.categoryId && categories.includes(obs.tags.categoryId)
})
}
if (name) {
const lowercaseName = name.toLowerCase();
filteredData = filteredData.filter(obs => {
return obs.tags && obs.tags.name && obs.tags.name.toLowerCase().includes(lowercaseName);
});
}
reply.send(filteredData);
})

})
fastify.post('/mapeo', (req, reply) => {
const { lat, lng } = req.body
Expand All @@ -29,26 +45,32 @@ module.exports = (mapeo, filteredType) => {
reply.send(data)
})
})
fastify.put('/mapeo', (req, reply) => {
const { observationId, observationVersion, nodeHostname, nodeModel } = req.body
const obs = {
version: observationVersion,
id: observationId,
type: 'observation',
tags: {
categoryId: nodeModel,
hostname: nodeHostname,
type: 'network'
}
}
mapeo.observationUpdate(obs, (err, data) => {
console.log('data', data)
if (err) {
console.error(err)
reply.err(err)
fastify.put('/mapeo', async (req, reply) => {
try {
const { observationId, observationVersion, nodeHostname, nodeModel } = req.body
const obs = {
version: observationVersion,
id: observationId,
type: 'observation',
tags: {
categoryId: nodeModel,
hostname: nodeHostname,
type: 'network'
}
}
console.log('Updating observation:', obs)
const data = await new Promise((resolve, reject) => {
mapeo.observationUpdate(obs, (err, result) => {
if (err) reject(err)
else resolve(result)
})
})
console.log('Update successful:', data)
reply.send(data)
})
} catch (error) {
console.error('Error updating observation:', error)
reply.status(500).send({ error: 'Internal Server Error' })
}
})
fastify.delete('/mapeo', (req, reply) => {
console.log('req.body', req.body)
Expand Down
Loading

0 comments on commit 6674c2b

Please sign in to comment.