Skip to content

Commit

Permalink
fixed intervals and map_query_v2
Browse files Browse the repository at this point in the history
  • Loading branch information
amyfromandi committed Sep 10, 2024
1 parent 16385dd commit 191c30c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
7 changes: 6 additions & 1 deletion v2/definitions/intervals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 13 additions & 12 deletions v2/larkin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,32 +75,29 @@ 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) */
connectionDetails.database = 'elevation'
}

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);
Expand All @@ -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);
Expand Down
29 changes: 13 additions & 16 deletions v2/mobile/map_query_v2.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {stringify} from "querystring";

const api = require("../api");
const async = require("async");
const larkin = require("../larkin");
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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]),
Expand Down Expand Up @@ -379,6 +371,7 @@ module.exports = (req, res, next) => {
},

columns: (cb) => {
console.log("running columns")
larkin.queryPg(
"burwell",
`
Expand All @@ -403,6 +396,7 @@ module.exports = (req, res, next) => {
},

regions: (cb) => {
console.log("running regions")
larkin.queryPg(
"burwell",
`
Expand Down Expand Up @@ -437,6 +431,7 @@ module.exports = (req, res, next) => {
},

burwell: (cb) => {
console.log("running burwell")
let where = [];
let params = [];

Expand Down Expand Up @@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -540,6 +537,6 @@ module.exports = (req, res, next) => {
},
},
);
}
},
);
};

0 comments on commit 191c30c

Please sign in to comment.