From 191c30c3897b63e45fcf49e94cc7f497acde0d69 Mon Sep 17 00:00:00 2001 From: Amy Fromandi Date: Tue, 10 Sep 2024 13:58:12 -0500 Subject: [PATCH] fixed intervals and map_query_v2 --- v2/definitions/intervals.ts | 7 ++++++- v2/larkin.ts | 25 +++++++++++++------------ v2/mobile/map_query_v2.ts | 29 +++++++++++++---------------- 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/v2/definitions/intervals.ts b/v2/definitions/intervals.ts index aee1601c..8e0630f7 100644 --- a/v2/definitions/intervals.ts +++ b/v2/definitions/intervals.ts @@ -98,7 +98,12 @@ module.exports = function (req, res, next, cb) { age_bottom AS b_age, interval_type AS int_type, ${color}, - json_agg(json_build_object('timescale_id', timescales.id, 'name', timescales.timescale)) AS timescales + json_agg( + json_build_object( + 'timescale_id', + timescales.id, + 'name', + timescales.timescale)) AS timescales FROM macrostrat.intervals LEFT JOIN macrostrat.timescales_intervals ON intervals.id = macrostrat.timescales_intervals.interval_id LEFT JOIN macrostrat.timescales ON macrostrat.timescales.id = macrostrat.timescales_intervals.timescale_id diff --git a/v2/larkin.ts b/v2/larkin.ts index 5a9545fb..969f8594 100644 --- a/v2/larkin.ts +++ b/v2/larkin.ts @@ -75,13 +75,12 @@ const { Client, Pool } = require("pg"); const nameMapping = credentials.postgresDatabases ?? {}; const dbName = nameMapping[db] ?? db; - if (dbName == "geomacro") { console.warn( "In Macrostrat v2, 'geomacro' is merged with 'burwell' into the 'macrostrat' database.", ); } - let connectionDetails = credentials.pg; + let connectionDetails = {...credentials.pg}; if (dbName == "elevation") { /* Special case for elevation database (temporary) */ @@ -89,18 +88,16 @@ const { Client, Pool } = require("pg"); } const pool = new Pool(connectionDetails); + console.log(connectionDetails) pool.connect(function (err, client, done) { if (err) { larkin.log("error", "error connecting - " + err); callback(err); - } else if (typeof(params) === 'object') { - //named uses yesql to modify the params dict and sql named parameters into an array before querying PG. - //client.query can only accept numerical indices in sql syntax and an array for parameter values. - const preparedQuery = named(sql)(params); - console.log("Prepared Query Text:", preparedQuery.text); - console.log("Prepared Query Values:", preparedQuery.values); - client.query(preparedQuery.text, preparedQuery.values, function (err, result) { + } else if (Array.isArray(params)) { + console.log("Params is array: \n", sql) + console.log(params) + client.query(sql, params, function (err, result) { done(); if (err) { larkin.log("error", err); @@ -109,9 +106,13 @@ const { Client, Pool } = require("pg"); callback(null, result); } }); - } - else if (params.isArray) { - client.query(sql, params, function (err, result) { + } else if (typeof(params) === 'object') { + console.log("Params is object, using yesql: \n", sql) + console.log(params) + //named uses yesql to modify the params dict and sql named parameters into an array before querying PG. + //client.query can only accept numerical indices in sql syntax and an array for parameter values. + const preparedQuery = named(sql)(params); + client.query(preparedQuery.text, preparedQuery.values, function (err, result) { done(); if (err) { larkin.log("error", err); diff --git a/v2/mobile/map_query_v2.ts b/v2/mobile/map_query_v2.ts index a93b4d63..8337c7d1 100644 --- a/v2/mobile/map_query_v2.ts +++ b/v2/mobile/map_query_v2.ts @@ -1,5 +1,3 @@ -import {stringify} from "querystring"; - const api = require("../api"); const async = require("async"); const larkin = require("../larkin"); @@ -64,7 +62,8 @@ function getUnits(params, callback) { ` SELECT ( - SELECT json_agg(w) FROM ( + SELECT json_agg(w) + FROM ( SELECT split_part(q, '|', 1)::int AS strat_name_id, split_part(q, '|', 2) AS rank_name FROM ( SELECT unnest(array_agg(DISTINCT concat(lookup_strat_names.strat_name_id, '|', lookup_strat_names.rank_name))) q @@ -301,17 +300,13 @@ function buildLineSQL(scale) { `; } - - - - // Accepts a longitude, a latitude, and a zoom level // Returns the proper burwell data and macrostrat data module.exports = (req, res, next) => { if (Object.keys(req.query).length < 1) { return larkin.info(req, res, next); } - let params = {}; + if ( (!req.query.lng || !req.query.lat || !req.query.z) && !req.query.hasOwnProperty("sample") @@ -330,28 +325,25 @@ module.exports = (req, res, next) => { req.query.lat = 43.03; req.query.z = 10; } + req.query.lng = larkin.normalizeLng(req.query.lng); req.query.z = parseInt(req.query.z || 0); - console.log(params) - async.parallel( { elevation: (cb) => { + console.log("running elevation") require("../elevation")(req, null, null, (error, data) => { if (data && data.length) { - console.log(data) cb(null, data[0].elevation); } else { - console.log("cb is null") cb(null, null); } }); - console.log('ELEVATION IS COMPLETE') - }, lines: (cb) => { + console.log("running lines") larkin.queryPg( "burwell", buildLineSQL(scaleLookup[req.query.z]), @@ -379,6 +371,7 @@ module.exports = (req, res, next) => { }, columns: (cb) => { + console.log("running columns") larkin.queryPg( "burwell", ` @@ -403,6 +396,7 @@ module.exports = (req, res, next) => { }, regions: (cb) => { + console.log("running regions") larkin.queryPg( "burwell", ` @@ -437,6 +431,7 @@ module.exports = (req, res, next) => { }, burwell: (cb) => { + console.log("running burwell") let where = []; let params = []; @@ -466,7 +461,7 @@ module.exports = (req, res, next) => { if (error) { return cb(error); } - + //may be where the issue lies async.mapLimit( result.rows, 3, @@ -486,6 +481,7 @@ module.exports = (req, res, next) => { if (error) { return cb(error); } + console.log("getUnits function test", units) if (units.length) { mapPolygon.macrostrat = units[0]; } else if (params.strat_name_ids) { @@ -513,6 +509,7 @@ module.exports = (req, res, next) => { }, (error, data) => { if (error) return larkin.error(req, res, next, error || null); + for (let i = 0; i < data.burwell.length; i++) { data.burwell[i].lines = []; for (let j = 0; j < data.lines.length; j++) { @@ -540,6 +537,6 @@ module.exports = (req, res, next) => { }, }, ); - } + }, ); }; \ No newline at end of file