Skip to content

Commit 3eaf608

Browse files
committed
Bring in the changes from upstream PR 1835
1 parent a2ca4e5 commit 3eaf608

1 file changed

Lines changed: 274 additions & 0 deletions

File tree

  • moped-database/migrations/default/1777661669380_speed_up_project_list_view

moped-database/migrations/default/1777661669380_speed_up_project_list_view/up.sql

Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
DROP VIEW IF EXISTS exploded_component_arcgis_online_view;
2+
DROP VIEW IF EXISTS component_arcgis_online_view;
3+
DROP VIEW IF EXISTS project_list_view;
4+
15
CREATE OR REPLACE VIEW project_list_view AS
26
WITH project_person_list_lookup AS (
37
SELECT
@@ -301,3 +305,273 @@ LEFT JOIN LATERAL (
301305
LIMIT 1
302306
) proj_status_update ON true
303307
WHERE mp.is_deleted = false;
308+
309+
310+
-- Most recent migration: moped-database/migrations/default/1769119215514_combined_funding_toggle/up.sql
311+
312+
CREATE OR REPLACE VIEW component_arcgis_online_view AS WITH work_types AS (
313+
SELECT
314+
mpcwt.project_component_id,
315+
string_agg(mwt.name, ', '::text) AS work_types
316+
FROM moped_proj_component_work_types mpcwt
317+
LEFT JOIN moped_work_types mwt ON mpcwt.work_type_id = mwt.id
318+
WHERE mpcwt.is_deleted = false
319+
GROUP BY mpcwt.project_component_id
320+
),
321+
322+
council_districts AS (
323+
SELECT
324+
features.component_id AS project_component_id,
325+
string_agg(DISTINCT features_council_districts.council_district_id::text, ', '::text) AS council_districts,
326+
string_agg(DISTINCT lpad(features_council_districts.council_district_id::text, 2, '0'::text), ', '::text) AS council_districts_searchable
327+
FROM features_council_districts
328+
LEFT JOIN features ON features_council_districts.feature_id = features.id
329+
WHERE features.is_deleted = false
330+
GROUP BY features.component_id
331+
),
332+
333+
comp_geography AS (
334+
SELECT
335+
feature_union.component_id AS project_component_id,
336+
string_agg(DISTINCT feature_union.id::text, ', '::text) AS feature_ids,
337+
st_asgeojson(st_multi(st_union(array_agg(feature_union.geography))))::json AS geometry,
338+
st_asgeojson(st_multi(st_union(array_agg(feature_union.line_geography))))::json AS line_geometry,
339+
string_agg(DISTINCT feature_union.signal_id::text, ', '::text) AS signal_ids,
340+
sum(feature_union.length_feet) AS length_feet_total
341+
FROM (
342+
SELECT
343+
feature_signals.id,
344+
feature_signals.component_id,
345+
feature_signals.geography::geometry AS geography,
346+
st_exteriorring(st_buffer(feature_signals.geography, 7::double precision)::geometry) AS line_geography,
347+
feature_signals.signal_id,
348+
null::integer AS length_feet
349+
FROM feature_signals
350+
WHERE feature_signals.is_deleted = false
351+
UNION ALL
352+
SELECT
353+
feature_street_segments.id,
354+
feature_street_segments.component_id,
355+
feature_street_segments.geography::geometry AS geography,
356+
feature_street_segments.geography::geometry AS line_geography,
357+
null::integer AS signal_id,
358+
feature_street_segments.length_feet
359+
FROM feature_street_segments
360+
WHERE feature_street_segments.is_deleted = false
361+
UNION ALL
362+
SELECT
363+
feature_intersections.id,
364+
feature_intersections.component_id,
365+
feature_intersections.geography::geometry AS geography,
366+
st_exteriorring(st_buffer(feature_intersections.geography, 7::double precision)::geometry) AS line_geography,
367+
null::integer AS signal_id,
368+
null::integer AS length_feet
369+
FROM feature_intersections
370+
WHERE feature_intersections.is_deleted = false
371+
UNION ALL
372+
SELECT
373+
feature_drawn_points.id,
374+
feature_drawn_points.component_id,
375+
feature_drawn_points.geography::geometry AS geography,
376+
st_exteriorring(st_buffer(feature_drawn_points.geography, 7::double precision)::geometry) AS line_geography,
377+
null::integer AS signal_id,
378+
null::integer AS length_feet
379+
FROM feature_drawn_points
380+
WHERE feature_drawn_points.is_deleted = false
381+
UNION ALL
382+
SELECT
383+
feature_drawn_lines.id,
384+
feature_drawn_lines.component_id,
385+
feature_drawn_lines.geography::geometry AS geography,
386+
feature_drawn_lines.geography::geometry AS line_geography,
387+
null::integer AS signal_id,
388+
feature_drawn_lines.length_feet
389+
FROM feature_drawn_lines
390+
WHERE feature_drawn_lines.is_deleted = false
391+
UNION ALL
392+
SELECT
393+
feature_school_beacons.id,
394+
feature_school_beacons.component_id,
395+
feature_school_beacons.geography::geometry AS geography,
396+
st_exteriorring(st_buffer(feature_school_beacons.geography, 7::double precision)::geometry) AS line_geography,
397+
null::integer AS signal_id,
398+
null::integer AS length_feet
399+
FROM feature_school_beacons
400+
WHERE feature_school_beacons.is_deleted = false
401+
) feature_union
402+
GROUP BY feature_union.component_id
403+
),
404+
405+
subcomponents AS (
406+
SELECT
407+
mpcs.project_component_id,
408+
string_agg(ms.subcomponent_name, ', '::text) AS subcomponents
409+
FROM moped_proj_components_subcomponents mpcs
410+
LEFT JOIN moped_subcomponents ms ON mpcs.subcomponent_id = ms.subcomponent_id
411+
WHERE mpcs.is_deleted = false
412+
GROUP BY mpcs.project_component_id
413+
),
414+
415+
component_tags AS (
416+
SELECT
417+
mpct.project_component_id,
418+
string_agg(mct.full_name, ', '::text) AS component_tags
419+
FROM moped_proj_component_tags mpct
420+
LEFT JOIN moped_component_tags mct ON mpct.component_tag_id = mct.id
421+
WHERE mpct.is_deleted = false
422+
GROUP BY mpct.project_component_id
423+
),
424+
425+
related_projects AS (
426+
SELECT
427+
pmp.project_id,
428+
concat_ws(', '::text, pmp.project_id, string_agg(cmp.project_id::text, ', '::text)) AS related_project_ids_with_self,
429+
concat_ws(', '::text, lpad(pmp.project_id::text, 5, '0'::text), string_agg(lpad(cmp.project_id::text, 5, '0'::text), ', '::text)) AS related_project_ids_searchable_with_self
430+
FROM moped_project pmp
431+
LEFT JOIN moped_project cmp ON pmp.project_id = cmp.parent_project_id
432+
WHERE cmp.is_deleted = false
433+
GROUP BY pmp.project_id
434+
),
435+
436+
latest_public_meeting_date AS (
437+
SELECT
438+
mpm.project_id,
439+
coalesce(max(mpm.date_actual), max(mpm.date_estimate)) AS latest
440+
FROM moped_proj_milestones mpm
441+
WHERE mpm.milestone_id = 65 AND mpm.is_deleted = false
442+
GROUP BY mpm.project_id
443+
),
444+
445+
earliest_active_or_construction_phase_date AS (
446+
SELECT
447+
mpp.project_id,
448+
min(mpp.phase_start) AS earliest
449+
FROM moped_proj_phases mpp
450+
LEFT JOIN moped_phases mp ON mpp.phase_id = mp.phase_id
451+
WHERE (mp.phase_name_simple = any(ARRAY['Active'::text, 'Construction'::text])) AND mpp.is_deleted = false
452+
GROUP BY mpp.project_id
453+
)
454+
455+
SELECT
456+
mpc.project_id,
457+
mpc.project_component_id,
458+
comp_geography.feature_ids,
459+
mpc.component_id,
460+
comp_geography.geometry,
461+
comp_geography.line_geometry,
462+
comp_geography.signal_ids,
463+
council_districts.council_districts,
464+
council_districts.council_districts_searchable,
465+
NOT coalesce(council_districts.council_districts IS null OR council_districts.council_districts = ''::text, false) AS is_within_city_limits,
466+
comp_geography.length_feet_total,
467+
round(comp_geography.length_feet_total::numeric / 5280::numeric, 2) AS length_miles_total,
468+
mc.component_name,
469+
mc.component_subtype,
470+
mc.component_name_full,
471+
CASE
472+
WHEN mc.line_representation = true THEN 'Line'::text
473+
ELSE 'Point'::text
474+
END AS geometry_type,
475+
CASE
476+
WHEN comp_geography.geometry IS null THEN false
477+
ELSE true
478+
END AS is_mapped,
479+
subcomponents.subcomponents AS component_subcomponents,
480+
work_types.work_types AS component_work_types,
481+
component_tags.component_tags,
482+
mpc.description AS component_description,
483+
mpc.interim_project_component_id,
484+
CASE
485+
WHEN mpc.phase_id IS null THEN plv.substantial_completion_date
486+
WHEN mpc.phase_id IS NOT null AND mpc.completion_date IS null THEN null::timestamp with time zone
487+
ELSE mpc.completion_date
488+
END AS substantial_completion_date,
489+
plv.substantial_completion_date_estimated,
490+
mpc.srts_id,
491+
mpc.location_description AS component_location_description,
492+
plv.project_name,
493+
plv.project_name_secondary,
494+
plv.project_name_full,
495+
plv.project_description,
496+
plv.ecapris_subproject_id,
497+
plv.project_website,
498+
plv.updated_at AS project_updated_at,
499+
plv.date_added AS project_created_at,
500+
mpc.phase_id AS component_phase_id,
501+
mph.phase_name AS component_phase_name,
502+
mph.phase_name_simple AS component_phase_name_simple,
503+
current_phase.phase_id AS project_phase_id,
504+
current_phase.phase_name AS project_phase_name,
505+
current_phase.phase_name_simple AS project_phase_name_simple,
506+
coalesce(mph.phase_name, current_phase.phase_name) AS current_phase_name,
507+
coalesce(mph.phase_name_simple, current_phase.phase_name_simple) AS current_phase_name_simple,
508+
plv.project_team_members,
509+
plv.project_sponsor,
510+
plv.project_lead,
511+
plv.public_process_status,
512+
plv.interim_project_id,
513+
plv.project_partners,
514+
plv.task_order_names,
515+
plv.funding_source_and_program_names AS funding_sources,
516+
plv.project_status_update,
517+
plv.project_status_update_date_created,
518+
to_char(timezone('US/Central'::text, plv.construction_start_date), 'YYYY-MM-DD'::text) AS construction_start_date,
519+
plv.project_inspector,
520+
plv.project_designer,
521+
plv.project_tags,
522+
plv.workgroup_contractors,
523+
plv.contract_numbers,
524+
plv.parent_project_id,
525+
plv.parent_project_name,
526+
plv.parent_project_url,
527+
plv.parent_project_name AS parent_project_name_full,
528+
rp.related_project_ids_with_self AS related_project_ids,
529+
rp.related_project_ids_searchable_with_self AS related_project_ids_searchable,
530+
plv.knack_project_id AS knack_data_tracker_project_record_id,
531+
plv.project_url,
532+
(plv.project_url || '?tab=map&project_component_id='::text) || mpc.project_component_id::text AS component_url,
533+
get_project_development_status(lpmd.latest::timestamp with time zone, eaocpd.earliest, coalesce(mpc.completion_date, plv.substantial_completion_date), plv.substantial_completion_date_estimated, coalesce(mph.phase_name_simple, current_phase.phase_name_simple)) AS project_development_status,
534+
project_development_status_date.result AS project_development_status_date,
535+
to_char(project_development_status_date.result, 'YYYY'::text)::integer AS project_development_status_date_calendar_year,
536+
to_char(project_development_status_date.result, 'FMMonth YYYY'::text) AS project_development_status_date_calendar_year_month,
537+
to_char(project_development_status_date.result, 'YYYY-MM'::text) AS project_development_status_date_calendar_year_month_numeric,
538+
date_part('quarter'::text, project_development_status_date.result)::text AS project_development_status_date_calendar_year_quarter,
539+
CASE
540+
WHEN date_part('quarter'::text, project_development_status_date.result) = 4::double precision THEN (to_char(project_development_status_date.result, 'YYYY'::text)::integer + 1)::text
541+
ELSE to_char(project_development_status_date.result, 'YYYY'::text)
542+
END AS project_development_status_date_fiscal_year,
543+
CASE
544+
WHEN date_part('quarter'::text, project_development_status_date.result) = 4::double precision THEN 1::double precision
545+
ELSE date_part('quarter'::text, project_development_status_date.result) + 1::double precision
546+
END::text AS project_development_status_date_fiscal_year_quarter,
547+
plv.added_by AS project_added_by
548+
FROM moped_proj_components mpc
549+
LEFT JOIN comp_geography ON mpc.project_component_id = comp_geography.project_component_id
550+
LEFT JOIN council_districts ON mpc.project_component_id = council_districts.project_component_id
551+
LEFT JOIN subcomponents ON mpc.project_component_id = subcomponents.project_component_id
552+
LEFT JOIN work_types ON mpc.project_component_id = work_types.project_component_id
553+
LEFT JOIN component_tags ON mpc.project_component_id = component_tags.project_component_id
554+
LEFT JOIN project_list_view plv ON mpc.project_id = plv.project_id
555+
LEFT JOIN current_phase_view current_phase ON mpc.project_id = current_phase.project_id
556+
LEFT JOIN moped_phases mph ON mpc.phase_id = mph.phase_id
557+
LEFT JOIN moped_components mc ON mpc.component_id = mc.component_id
558+
LEFT JOIN related_projects rp ON mpc.project_id = rp.project_id
559+
LEFT JOIN latest_public_meeting_date lpmd ON mpc.project_id = lpmd.project_id
560+
LEFT JOIN earliest_active_or_construction_phase_date eaocpd ON mpc.project_id = eaocpd.project_id
561+
LEFT JOIN LATERAL (SELECT timezone('US/Central'::text, get_project_development_status_date(lpmd.latest::timestamp with time zone, eaocpd.earliest, coalesce(mpc.completion_date, plv.substantial_completion_date), plv.substantial_completion_date_estimated, coalesce(mph.phase_name_simple, current_phase.phase_name_simple))) AS result) project_development_status_date ON true
562+
WHERE mpc.is_deleted = false AND plv.is_deleted = false;
563+
564+
565+
-- Most recent migration: moped-database/migrations/default/1769119215514_combined_funding_toggle/up.sql
566+
567+
CREATE OR REPLACE VIEW exploded_component_arcgis_online_view AS SELECT
568+
component_arcgis_online_view.project_id,
569+
component_arcgis_online_view.project_component_id,
570+
st_geometrytype(dump.geom) AS geometry_type,
571+
dump.path[1] AS point_index,
572+
component_arcgis_online_view.geometry AS original_geometry,
573+
st_asgeojson(dump.geom) AS exploded_geometry,
574+
component_arcgis_online_view.project_updated_at
575+
FROM component_arcgis_online_view,
576+
LATERAL st_dump(st_geomfromgeojson(component_arcgis_online_view.geometry)) dump (path, geom)
577+
WHERE st_geometrytype(st_geomfromgeojson(component_arcgis_online_view.geometry)) = 'ST_MultiPoint'::text;

0 commit comments

Comments
 (0)