@@ -7,62 +7,83 @@ module.exports = function (req, res, next, cb) {
77 }
88 //There will be a discrepancy with a key in production. Updated in_proccess_cols to in_process_cols key. Values are
99 //still the same.
10- var sql = `WITH in_proc AS (
11- SELECT COUNT(DISTINCT id) AS c, project_id
12- FROM macrostrat.cols
13- WHERE status_code = 'in process'
14- GROUP BY project_id
15- ),
16- obs AS (
17- SELECT COUNT(DISTINCT id) AS co, project_id
18- FROM macrostrat.cols
19- WHERE status_code = 'obsolete'
20- GROUP BY project_id
21- ),
22- col_area_sum AS (
23- SELECT project_id, SUM(col_area) AS total_area
24- FROM macrostrat.cols
25- WHERE status_code = 'active'
26- GROUP BY project_id
27- )
28-
29- SELECT
30- projects.id AS project_id,
31- projects.project,
32- projects.descrip,
33- projects.timescale_id,
34- COUNT(DISTINCT units_sections.col_id)::integer AS t_cols,
35- COALESCE(c, 0)::integer AS in_process_cols,
36- COALESCE(co, 0)::integer AS obsolete_cols,
37- COUNT(DISTINCT units_sections.unit_id)::integer AS t_units,
38- COALESCE(ROUND(total_area), 0)::integer AS area
39-
40- FROM macrostrat.projects
41- LEFT JOIN macrostrat.cols ON projects.id = cols.project_id
42- LEFT JOIN macrostrat.units_sections ON units_sections.col_id = cols.id
43- LEFT JOIN in_proc USING (project_id)
44- LEFT JOIN obs USING (project_id)
45- LEFT JOIN col_area_sum ON projects.id = col_area_sum.project_id
46- ` ;
4710
48- var where = [ ] ;
49- var params = { } ;
11+ const where = [ ] ;
12+ let params = { } ;
5013
5114 if ( req . query . project_id ) {
5215 where . push ( "projects.id = ANY(:project_id)" ) ;
5316 params [ "project_id" ] = larkin . parseMultipleIds ( req . query . project_id ) ;
5417 }
55- if ( where . length ) {
56- sql += ` WHERE ${ where . join ( " AND " ) } ` ;
18+
19+ const whereStatement = where . length > 0 ? where . join ( " AND " ) : "true" ;
20+
21+ let sql = `
22+ SELECT
23+ p.id AS project_id,
24+ p.project,
25+ p.descrip,
26+ p.timescale_id,
27+ count(DISTINCT units_sections.col_id)::integer AS t_cols,
28+ count(DISTINCT cols.id) FILTER ( WHERE cols.status_code = 'active' )::integer AS active_cols,
29+ count(DISTINCT cols.id) FILTER ( WHERE cols.status_code = 'in process' )::integer AS in_process_cols,
30+ count(DISTINCT cols.id) FILTER ( WHERE cols.status_code = 'obsolete' )::integer AS obsolete_cols,
31+ count(DISTINCT units_sections.unit_id)::integer AS t_units,
32+ coalesce(round(sum(DISTINCT cols.col_area) FILTER ( WHERE cols.status_code = 'active')), 0) AS area
33+ FROM macrostrat.projects p
34+ LEFT JOIN macrostrat.cols ON p.id = cols.project_id
35+ LEFT JOIN macrostrat.units_sections ON units_sections.col_id = cols.id
36+ WHERE ${ whereStatement }
37+ GROUP BY
38+ p.id,
39+ p.project,
40+ p.descrip,
41+ p.timescale_id
42+ ` ;
43+
44+ if ( larkin . hasCapability ( "composite-projects" ) ) {
45+ /** Progressive enhancement for composite projects **/
46+ sql = `
47+ WITH composite_tree AS (
48+ SELECT pt.parent_id, array_agg(pt.child_id) children, jsonb_agg(to_jsonb(p)) AS members
49+ FROM macrostrat.projects_tree pt
50+ JOIN LATERAL (
51+ SELECT p.id, p.slug, p.project name
52+ FROM macrostrat.projects p
53+ WHERE p.id = pt.child_id
54+ ) AS p ON true
55+ GROUP BY pt.parent_id
56+ )
57+ SELECT
58+ p.id AS project_id,
59+ p.slug,
60+ p.project,
61+ p.descrip,
62+ p.timescale_id,
63+ ct.members,
64+ count(DISTINCT units_sections.col_id)::integer AS t_cols,
65+ count(DISTINCT cols.id) FILTER ( WHERE cols.status_code = 'active' )::integer AS active_cols,
66+ count(DISTINCT cols.id) FILTER ( WHERE cols.status_code = 'in process' )::integer AS in_process_cols,
67+ count(DISTINCT cols.id) FILTER ( WHERE cols.status_code = 'obsolete' )::integer AS obsolete_cols,
68+ count(DISTINCT units_sections.unit_id)::integer AS t_units,
69+ coalesce(round(sum(DISTINCT cols.col_area) FILTER ( WHERE cols.status_code = 'active')), 0) AS area
70+ FROM macrostrat.projects p
71+ LEFT JOIN composite_tree ct
72+ ON ct.parent_id = p.id
73+ LEFT JOIN macrostrat.cols ON p.id = cols.project_id
74+ OR (p.is_composite AND cols.project_id = ANY(ct.children))
75+ LEFT JOIN macrostrat.units_sections ON units_sections.col_id = cols.id
76+ WHERE ${ whereStatement }
77+ GROUP BY
78+ p.id,
79+ p.project,
80+ p.descrip,
81+ p.timescale_id,
82+ p.slug,
83+ ct.children,
84+ ct.members
85+ ` ;
5786 }
58- sql += `\nGROUP BY
59- projects.id,
60- projects.project,
61- projects.descrip,
62- projects.timescale_id,
63- c,
64- co,
65- total_area;` ;
6687
6788 larkin . queryPg ( "burwell" , sql , params , function ( error , data ) {
6889 if ( error ) {
0 commit comments