diff --git a/.gitignore b/.gitignore index 267b263d..d399c09c 100755 --- a/.gitignore +++ b/.gitignore @@ -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 @@ -32,4 +32,4 @@ Icon # Files that might appear on external disk .Spotlight-V100 .Trashes -.yarn/install-state.gz \ No newline at end of file +.yarn/install-state.gz diff --git a/v2/credentials.ts b/v2/credentials.ts index eb81b297..adeec759 100644 --- a/v2/credentials.ts +++ b/v2/credentials.ts @@ -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 = { @@ -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"; diff --git a/v2/units.ts b/v2/units.ts index a5224c4b..5b434067 100644 --- a/v2/units.ts +++ b/v2/units.ts @@ -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, @@ -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, @@ -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 @@ -577,16 +572,9 @@ 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) { @@ -594,22 +582,22 @@ module.exports = function (req, res, next, cb) { 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", ); } @@ -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("|"); } } @@ -643,7 +633,7 @@ module.exports = function (req, res, next, cb) { : ["clng", "clat"]; dbgeo.parse( - result, + result.rows, { geometryType: "ll", geometryColumn: geomAge, @@ -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) { @@ -709,12 +689,11 @@ module.exports = function (req, res, next, cb) { refs: req.query.response === "long" ? "refs" : false, }, { - data: result.rows, + data: result, }, ); } } }, ); -} - +};