Skip to content

Commit

Permalink
merging fixes from local-run-fixes for units.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
amyfromandi committed Aug 28, 2024
2 parents f9407d9 + 5f7231d commit cedffdc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 72 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ v1/utilities/scripts/credentials.py
venv

!v2/credentials.ts
!credentials.ts
credentials.ts
!v1/credentials.ts
v2/utilities/scripts/credentials.py

Expand All @@ -32,4 +32,4 @@ Icon
# Files that might appear on external disk
.Spotlight-V100
.Trashes
.yarn/install-state.gz
.yarn/install-state.gz
7 changes: 2 additions & 5 deletions v2/credentials.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
exports.pg = {
host: process.env.host,
port: process.env.port,
user: process.env.user,
password: process.env.password,
database: process.env.database
connectionString: process.env.MACROSTRAT_DATABASE,
};

exports.postgresDatabases = {
Expand All @@ -18,4 +14,5 @@ exports.redis = {
};

// Generate a hash by running: node -e "console.log(require('uuid/v4')())"
// NOTE: this is outdated and may not be used
exports.cacheRefreshKey = "put-hash-here";
109 changes: 44 additions & 65 deletions v2/units.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,13 +498,13 @@ module.exports = function (req, res, next, cb) {
where +=
" AND units_sections.col_id = ANY(92, 488, 463, 289, 430, 481, 261, 534, 369, 798, 771, 1675) ";
}
//TODO: fix this output field in the API
params["measure_field"] =

const measureField =
"summarize_measures" in req.query
? "lookup_unit_attrs_api.measure_long"
: "lookup_unit_attrs_api.measure_short";

var shortSQL = `
var columnList = `
units.id AS unit_id,
units_sections.section_id AS section_id,
units_sections.col_id AS col_id,
Expand All @@ -524,11 +524,17 @@ module.exports = function (req, res, next, cb) {
lookup_units.pbdb_collections::integer,
lookup_units.pbdb_occurrences::integer`;

var longSQL = `${shortSQL},
if (req.query.response === "long" || cb) {
// This part deals with 'long' queries
const colRefsSubquery = `
SELECT string_agg(col_refs.ref_id::text, '|') FROM macrostrat_temp.col_refs
WHERE col_refs.col_id = cols.id`;

columnList += `,
lookup_unit_attrs_api.lith,
lookup_unit_attrs_api.environ,
lookup_unit_attrs_api.econ,
:measure_field AS measure,
${measureField} AS measure,
COALESCE(notes, '') AS notes,
lookup_units.color,
lookup_units.text_color,
Expand All @@ -543,31 +549,20 @@ module.exports = function (req, res, next, cb) {
lookup_units.b_prop,
lookup_units.units_below,
lookup_strat_names.rank_name AS strat_name_long,
STRING_AGG(col_refs.ref_id::text, '') AS refs
(${colRefsSubquery}) AS refs
`;
if ("show_position" in req.query) {
longSQL += ", position_top AS t_pos, position_bottom AS b_pos";
columnList += ", position_top AS t_pos, position_bottom AS b_pos";
}
}

var geometry =
(req.query.format && api.acceptedFormats.geo[req.query.format]) ||
req.query.response === "long"
? ", lookup_units.clat, lookup_units.clng, lookup_units.t_plat, lookup_units.t_plng, lookup_units.b_plat, lookup_units.b_plng "
: "";

let groupby = ''
if (req.query.response === "long" || cb) {
groupby = `
,lookup_unit_attrs_api.lith, lookup_unit_attrs_api.environ, lookup_unit_attrs_api.econ, lookup_units.text_color,
unit_notes.notes, lookup_units.color, lookup_units.t_int, lookup_units.t_int_name,lookup_units.t_int_age,
lookup_units.t_prop, lookup_units.units_above, lookup_units.b_int, lookup_units.b_int_name, lookup_units.b_int_age,
lookup_units.b_prop,lookup_units.units_below, lookup_strat_names.rank_name,lookup_units.clat, lookup_units.clng,
lookup_units.t_plat, lookup_units.t_plng, lookup_units.b_plat, lookup_units.b_plng
`
if ((req.query.format && api.acceptedFormats.geo[req.query.format]) ||
req.query.response === "long") {
columnList += ", lookup_units.clat, lookup_units.clng, lookup_units.t_plat, lookup_units.t_plng, lookup_units.b_plat, lookup_units.b_plng "
}

var sql = `
SELECT ${req.query.response === "long" || cb ? longSQL : shortSQL} ${geometry}
SELECT ${columnList}
FROM macrostrat_temp.units
LEFT JOIN macrostrat_temp.lookup_unit_attrs_api ON lookup_unit_attrs_api.unit_id = units.id
LEFT JOIN macrostrat_temp.lookup_units ON units.id = lookup_units.unit_id
Expand All @@ -577,39 +572,32 @@ module.exports = function (req, res, next, cb) {
LEFT JOIN macrostrat_temp.col_refs ON cols.id = col_refs.col_id
LEFT JOIN macrostrat_temp.lookup_strat_names ON lookup_strat_names.strat_name_id=unit_strat_names.strat_name_id
LEFT JOIN macrostrat_temp.unit_notes ON unit_notes.unit_id=units.id
WHERE
${where}
GROUP BY units.id, units_sections.section_id, lookup_units.t_age, units_sections.col_id,
cols.project_id, cols.col_area, unit_strat_names.strat_name_id, mbr_name, fm_name, gp_name, sgp_name,
lookup_units.b_age,lookup_units.pbdb_collections, lookup_units.pbdb_occurrences ${groupby}
WHERE ${where}
ORDER BY ${orderby.length > 0 ? orderby.join(", ") + "," : ""} lookup_units.t_age ASC
${limit}
`;


${limit}`;

larkin.queryPg("burwell", sql, params, function (error, result) {
if (error) {
console.log(error);
callback(error);
} else {
if (req.query.response === "long" || cb) {
for (var i = 0; i < result.length; i++) {
for (var i = 0; i < result.rows.length; i++) {
// These come back as JSON strings, so we need to make them real JSON
result[i].lith = JSON.parse(result[i].lith) || [];
result[i].environ = JSON.parse(result[i].environ) || [];
result[i].econ = JSON.parse(result[i].econ) || [];
result[i].measure = JSON.parse(result[i].measure) || [];
result[i].units_above = larkin.jsonifyPipes(
result[i].units_above,
result.rows[i].lith = JSON.parse(result.rows[i].lith) || [];
result.rows[i].environ = JSON.parse(result.rows[i].environ) || [];
result.rows[i].econ = JSON.parse(result.rows[i].econ) || [];
result.rows[i].measure = JSON.parse(result.rows[i].measure) || [];
result.rows[i].units_above = larkin.jsonifyPipes(
result.rows[i].units_above,
"integers",
);
result[i].units_below = larkin.jsonifyPipes(
result[i].units_below,
result.rows[i].units_below = larkin.jsonifyPipes(
result.rows[i].units_below,
"integers",
);
result[i].refs = larkin.jsonifyPipes(
result[i].refs,
result.rows[i].refs = larkin.jsonifyPipes(
result.rows[i].refs,
"integers",
);
}
Expand All @@ -620,13 +608,15 @@ module.exports = function (req, res, next, cb) {
req.query.response === "long" &&
!cb
) {
for (var i = 0; i < result.length; i++) {
result[i].units_above = result[i].units_above.join(",");
result[i].units_below = result[i].units_below.join(",");
result[i].lith = larkin.pipifyAttrs(result[i].lith);
result[i].environ = larkin.pipifyAttrs(result[i].environ);
result[i].econ = larkin.pipifyAttrs(result[i].econ);
result[i].refs = result[i].refs.join("|");
for (var i = 0; i < result.rows.length; i++) {
result.rows[i].units_above = result.rows[i].units_above.join(",");
result.rows[i].units_below = result.rows[i].units_below.join(",");

result.rows[i].lith = larkin.pipifyAttrs(result.rows[i].lith);
result.rows[i].environ = larkin.pipifyAttrs(result.rows[i].environ);
result.rows[i].econ = larkin.pipifyAttrs(result.rows[i].econ);

result.rows[i].refs = result.rows[i].refs.join("|");
}
}

Expand All @@ -643,7 +633,7 @@ module.exports = function (req, res, next, cb) {
: ["clng", "clat"];

dbgeo.parse(
result,
result.rows,
{
geometryType: "ll",
geometryColumn: geomAge,
Expand All @@ -668,23 +658,13 @@ module.exports = function (req, res, next, cb) {
},
);
} else {
callback(null, data, result);
callback(null, data, result.rows);
}
}
});
},
],
function (error, data, result) {
//console.log(result)

for (let key in result.rows) {
let obj = result.rows[key];
if (obj['refs'] == '1|1') { // or use obj.refs if 'refs' is a standard property name
console.log(obj);
}
}


if (error) {
console.log(error);
if (cb) {
Expand All @@ -709,12 +689,11 @@ module.exports = function (req, res, next, cb) {
refs: req.query.response === "long" ? "refs" : false,
},
{
data: result.rows,
data: result,
},
);
}
}
},
);
}

};

0 comments on commit cedffdc

Please sign in to comment.