diff --git a/v2/definitions/projects.ts b/v2/definitions/projects.ts index effc416b..93e139dd 100644 --- a/v2/definitions/projects.ts +++ b/v2/definitions/projects.ts @@ -7,22 +7,44 @@ module.exports = function (req, res, next, cb) { } //There will be a discrepancy with a key in production. Updated in_proccess_cols to in_process_cols key. Values are //still the same. - var sql = `WITH RECURSIVE in_proc AS (SELECT count(distinct id) c,project_id from macrostrat.cols where status_code='in process' group by project_id), - obs AS (SELECT count(distinct id) co,project_id from macrostrat.cols where status_code='obsolete' group by project_id) - SELECT projects.id AS project_id, - project, - descrip, - timescale_id, - COUNT(DISTINCT units_sections.col_id)::integer AS t_cols, - coalesce(c,0)::integer as in_process_cols, - coalesce(co,0)::integer as obsolete_cols, - COUNT(DISTINCT units_sections.unit_id)::integer AS t_units, - round(SUM(cols.col_area)::integer,0)::integer as area + var sql = `WITH in_proc AS ( + SELECT COUNT(DISTINCT id) AS c, project_id + FROM macrostrat.cols + WHERE status_code = 'in process' + GROUP BY project_id + ), + obs AS ( + SELECT COUNT(DISTINCT id) AS co, project_id + FROM macrostrat.cols + WHERE status_code = 'obsolete' + GROUP BY project_id + ), + col_area_sum AS ( + SELECT project_id, SUM(col_area) AS total_area + FROM macrostrat.cols + WHERE status_code = 'active' + GROUP BY project_id + ) + + + SELECT + projects.id AS project_id, + projects.project, + projects.descrip, + projects.timescale_id, + COUNT(DISTINCT units_sections.col_id)::integer AS t_cols, + COALESCE(c, 0)::integer AS in_process_cols, + COALESCE(co, 0)::integer AS obsolete_cols, + COUNT(DISTINCT units_sections.unit_id)::integer AS t_units, + COALESCE(ROUND(total_area), 0)::integer AS area + + FROM macrostrat.projects LEFT JOIN macrostrat.cols ON projects.id = cols.project_id LEFT JOIN macrostrat.units_sections ON units_sections.col_id = cols.id - LEFT JOIN in_proc using (project_id) - left join obs using (project_id) + LEFT JOIN in_proc USING (project_id) + LEFT JOIN obs USING (project_id) + LEFT JOIN col_area_sum ON projects.id = col_area_sum.project_id `; var where = []; @@ -35,7 +57,14 @@ module.exports = function (req, res, next, cb) { if (where.length) { sql += ` WHERE ${where.join(" AND ")}`; } - sql += "\nGROUP BY projects.id, in_proc.c, obs.co"; + sql += `\nGROUP BY + projects.id, + projects.project, + projects.descrip, + projects.timescale_id, + c, + co, + total_area;`; larkin.queryPg("burwell", sql, params, function (error, data) { if (error) { diff --git a/v2/definitions/strat_name_concepts.ts b/v2/definitions/strat_name_concepts.ts index fb2ca115..a542c794 100644 --- a/v2/definitions/strat_name_concepts.ts +++ b/v2/definitions/strat_name_concepts.ts @@ -26,15 +26,16 @@ module.exports = function (req, res, next, cb) { JOIN macrostrat.refs ON snm.ref_id = refs.id */ }), - params = {}; + params = {}, + where = []; if ("all" in req.query) { // do nothing } else if ("concept_name" in req.query) { - sql += " WHERE name = ANY(:concept_name)"; + where.push("name = ANY(:concept_name)"); params["concept_name"] = req.query.concept_name; } else if (req.query.concept_id || req.query.strat_name_concept_id) { - sql += " WHERE concept_id = ANY(:concept_id)"; + where.push("concept_id = ANY(:concept_id)"); if (req.query.concept_id) { params["concept_id"] = larkin.parseMultipleIds(req.query.concept_id); } else { @@ -43,15 +44,28 @@ module.exports = function (req, res, next, cb) { ); } } else if (req.query.strat_name_id) { - sql += - " WHERE concept_id IN (SELECT concept_id FROM macrostrat.lookup_strat_names WHERE strat_name_id IN (:strat_name_ids))"; + where.push("concept_id IN (SELECT concept_id FROM macrostrat.lookup_strat_names WHERE strat_name_id IN (:strat_name_ids))"); params["strat_name_ids"] = larkin.parseMultipleIds(req.query.strat_name_id); } + // pagination + const lastId = req.query.last_id ? parseInt(req.query.last_id, 10) : null; + const pageSize = req.query.page_size ? parseInt(req.query.page_size, 10) : 5; // defaults to 5 + + if (req.query.last_id) { + where.push("concept_id > :last_id"); + params["last_id"] = lastId; + } + + if (where.length > 0) { + sql += " WHERE " + where.join(" AND "); + } + sql += " GROUP BY concept_id, author ORDER BY concept_id"; - if ("sample" in req.query) { - sql += " LIMIT 5"; + if ("sample" in req.query || req.query.last_id) { + sql += " LIMIT :page_size"; + params["page_size"] = pageSize; } larkin.queryPg("burwell", sql, params, function (error, result) { @@ -71,6 +85,9 @@ module.exports = function (req, res, next, cb) { if (cb) { cb(null, result.rows); } else { + const rows = result.rows; + const lastIdOut = rows.length > 0 ? rows[rows.length - 1].concept_id : null; + larkin.sendData( req, res, @@ -84,6 +101,7 @@ module.exports = function (req, res, next, cb) { }, { data: result.rows, + last_id: lastIdOut, }, ); } diff --git a/v2/definitions/strat_names.ts b/v2/definitions/strat_names.ts index 99a584ac..49ee3cc4 100644 --- a/v2/definitions/strat_names.ts +++ b/v2/definitions/strat_names.ts @@ -8,7 +8,8 @@ module.exports = function (req, res, next, cb) { } var where = [], - params = {}; + params = {}, + orderBy = []; if (req.query.rule) { if (req.query.rule === "down") { @@ -123,12 +124,27 @@ module.exports = function (req, res, next, cb) { FROM macrostrat.lookup_strat_names l `; + // pagination + const lastId = req.query.last_id ? parseInt(req.query.last_id, 10) : null; + const pageSize = req.query.page_size ? parseInt(req.query.page_size, 10) : 5; // defaults to 5 + + if (req.query.last_id) { + where.push("strat_name_id > :last_id"); + params["last_id"] = lastId; + orderBy.push("strat_name_id ASC"); + } + if (where.length > 0) { sql += " WHERE " + where.join(" AND "); } - if ("sample" in req.query) { - sql += " LIMIT 5"; + if (orderBy.length > 0) { + sql += " ORDER BY " + orderBy.join(", "); + } + + if ("sample" in req.query || req.query.last_id) { + sql += " LIMIT :page_size"; + params["page_size"] = pageSize; } larkin.queryPg("burwell", sql, params, function (error, response) { @@ -140,6 +156,9 @@ module.exports = function (req, res, next, cb) { larkin.error(req, res, next, "Something went wrong"); } } else { + const rows = response.rows; + const lastIdOut = rows.length > 0 ? rows[rows.length - 1].strat_name_id : null; + if (cb) { cb(null, response.rows); } else { @@ -157,6 +176,7 @@ module.exports = function (req, res, next, cb) { }, { data: response.rows, + last_id: lastIdOut, }, ); } diff --git a/v2/larkin.ts b/v2/larkin.ts index 75b76bbf..c1530619 100644 --- a/v2/larkin.ts +++ b/v2/larkin.ts @@ -243,6 +243,10 @@ const { Client, Pool } = require("pg"); .send(JSON.stringify(outgoing.data, null, 0)); } + if (options.last_id) { + outgoing.last_id = options.last_id; + } + if (options.refs) { larkin.getRefs(options.refs, outgoing.data, function (refs) { outgoing.refs = refs; @@ -266,6 +270,10 @@ const { Client, Pool } = require("pg"); responseObject.success["refs"] = outgoing.refs; } + if (outgoing.last_id) { + responseObject.success["last_id"] = outgoing.last_id; + } + if ((options && options.compact) || outgoing.data.length <= 5) { return res .set("Content-type", "application/json; charset=utf-8")