-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
merge-trees-to-database #15
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { runMerge } from "../src/index.js"; | ||
await runMerge(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* https://github.com/vitaly-t/pg-promise/wiki/Connection-Syntax | ||
*/ | ||
export const dbConfig = { | ||
host: process.env.POSTGRES_HOST, | ||
port: process.env.POSTGRES_PORT, | ||
database: process.env.POSTGRES_DB, | ||
user: process.env.POSTGRES_USER, | ||
password: process.env.POSTGRES_PASSWORD, | ||
}; | ||
|
||
// console.log('dbConfig',dbConfig, process.env); | ||
|
||
//module.exports = dbConfig; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import pgp from 'pg-promise'; | ||
import { dbConfig } from './db-config.js'; | ||
import { pgPromiseConfig } from './pg-promise-config.js'; | ||
|
||
export const pgPromise = pgp(pgPromiseConfig); | ||
export const db = pgPromise(dbConfig); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* eslint-disable guard-for-in */ | ||
/* eslint-disable no-restricted-syntax */ | ||
import pgp from 'pg-promise'; | ||
|
||
/** | ||
* source: | ||
* https://github.com/vitaly-t/pg-promise/issues/78#issuecomment-171951303 | ||
*/ | ||
function camelizeColumns(data) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a note, we have this function replicated here: https://github.com/waterthetrees/wtt_server/blob/68e22644d31da834a69b4435373e8b9aeac9ad5e/server/db/pg-promise-config.js Probably not worth calling between repos tho since its a common utility function. |
||
const tmp = data[0]; | ||
for (const prop in tmp) { | ||
const camel = pgp.utils.camelize(prop); | ||
if (!(camel in tmp)) { | ||
for (let i = 0; i < data.length; i++) { | ||
const d = data[i]; | ||
d[camel] = d[prop]; | ||
delete d[prop]; | ||
} | ||
} | ||
} | ||
} | ||
|
||
export const pgPromiseConfig = { | ||
capSQL: true, | ||
receive: (data) => camelizeColumns(data), | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { db } from '../db/index.js'; | ||
import { exec } from 'child_process'; | ||
import { dbConfig } from '../db/db-config.js'; | ||
import * as utils from "../core/utils.js"; | ||
|
||
|
||
export const mergeSource = async (source) => { | ||
console.log('source.id', source.id); | ||
if ( | ||
!source.destinations || | ||
!source.destinations.geojson || | ||
!source.destinations.normalized | ||
) { | ||
throw new Error(`No destinations for source: "${source}"`); | ||
} | ||
|
||
const normalizedExists = await utils.asyncFileExists( | ||
source.destinations.normalized.path | ||
); | ||
if (!normalizedExists) { | ||
console.log( | ||
`The normalized file '${source.destinations.normalized.path}' does not exists. Skipping...` | ||
); | ||
return `NO FILE for ${source.id}`; // Early Return | ||
} | ||
|
||
console.log(`Running for ${source.destinations.normalized.path}`); | ||
// FIXME use the async version of exec, but that means a new dependecy | ||
const command = `ogr2ogr -f "PostgreSQL" PG:"host=${dbConfig.host} user=${dbConfig.user} password=${dbConfig.password} dbname=${dbConfig.database}" ${source.destinations.normalized.path} -nln tree_staging -geomfield geom -append` | ||
exec(command, async (error, stdout, stderr) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer spawn here as it has larger buffer for longer running processes. Could use spawnSync. |
||
if (error) { | ||
console.log(`error: ${error.message}`); | ||
return; | ||
} | ||
if (stderr) { | ||
console.log(`stderr: ${stderr}`); | ||
return; | ||
} | ||
|
||
console.log(`Running the stored proc for ${source.id}`); | ||
const result = await db.proc('public.merge_treedata', [source.id]); | ||
console.log(result); | ||
console.log(`stdout: ${stdout}`); | ||
}); | ||
/* | ||
*/ | ||
|
||
const context = { | ||
source, | ||
nullGeometry: 0, | ||
invalidGeometry: 0, | ||
}; | ||
|
||
return context; | ||
}; | ||
|
||
|
||
export const mergeSources = async (list) => { | ||
if (process.argv.length > 2) { | ||
list = list.filter(m => m.id == process.argv[2]); | ||
} | ||
|
||
const limit = pLimit(5); | ||
const promises = list.map((source) => | ||
limit(() => mergeSource(source)) | ||
); | ||
const results = await Promise.allSettled(promises); | ||
console.log("Finished uploading to the database..."); | ||
results.forEach((l) => { | ||
if (l && l.forEach) { | ||
l.forEach(console.log); | ||
} else { | ||
console.log(l); | ||
} | ||
}); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Merge will get rid of the need for the save step I think. Do you want to handle that or should I assign myself?