|
| 1 | +defmodule DAU.OpenData.OtherSourcesOpenQuery do |
| 2 | + import Ecto.Query, warn: false |
| 3 | + alias DAU.OpenData.PartnerEscalation |
| 4 | + alias DAU.OpenData.AssessmentReport |
| 5 | + alias DAU.OpenData.AssessmentReportTag |
| 6 | + alias DAU.OpenData.PartnerEscalationTag |
| 7 | + alias DAU.OpenData.Tag |
| 8 | + |
| 9 | + alias DAU.Repo |
| 10 | + |
| 11 | + @page_size 25 |
| 12 | + |
| 13 | + def list_combined_data(search_params) do |
| 14 | + order = %{ |
| 15 | + "newest" => [desc: :date], |
| 16 | + "oldest" => [asc: :date] |
| 17 | + } |
| 18 | + |
| 19 | + tag_filter = present_filter(Keyword.get(search_params, :tag)) |
| 20 | + |
| 21 | + query = |
| 22 | + make_base_query(tag_filter) |
| 23 | + # Wrap the union so we can safely order/limit/offset on combined fields. |
| 24 | + |> subquery() |
| 25 | + |> maybe_filter_from_date(Keyword.get(search_params, :from)) |
| 26 | + |> maybe_filter_to_date(Keyword.get(search_params, :to)) |
| 27 | + |
| 28 | + count = Repo.aggregate(query, :count, :uuid) |
| 29 | + |
| 30 | + results = |
| 31 | + query |
| 32 | + |> order_by(^Map.get(order, Keyword.get(search_params, :sort), desc: :date)) |
| 33 | + |> limit(^@page_size) |
| 34 | + |> offset(^(@page_size * (Keyword.get(search_params, :page_num, 1) - 1))) |
| 35 | + |> Repo.all() |
| 36 | + |> bulk_add_s3_media_url() |
| 37 | + |
| 38 | + {count, results} |
| 39 | + end |
| 40 | + |
| 41 | + defp make_base_query(tag_filter) do |
| 42 | + # Build partner rows with tags aggregated into a JSON array per row. |
| 43 | + query_partners = |
| 44 | + PartnerEscalation |
| 45 | + # Left join keeps rows even when no tags exist. |
| 46 | + |> join(:left, [row], t in assoc(row, :tags)) |
| 47 | + |> where([row], row.media_urls != [] and not is_nil(row.media_urls)) |
| 48 | + |> where( |
| 49 | + [row], |
| 50 | + fragment( |
| 51 | + "EXISTS (SELECT 1 FROM unnest(?) AS u(url) WHERE u.url ~* ?)", |
| 52 | + row.media_urls, |
| 53 | + "^(https?://)" |
| 54 | + ) |
| 55 | + ) |
| 56 | + |> maybe_filter_tags_for_partner(tag_filter) |
| 57 | + # Group by row.id so the tag join doesn't multiply rows. |
| 58 | + |> group_by([row], row.id) |
| 59 | + |> select([row, t], %{ |
| 60 | + type: "partner", |
| 61 | + date: row.date, |
| 62 | + preview_url: nil, |
| 63 | + assessment_report_link: nil, |
| 64 | + count: row.count, |
| 65 | + tools_used: row.tools_used, |
| 66 | + observations: row.observations, |
| 67 | + uuid: row.uuid, |
| 68 | + # Aggregate tag slug+name into a JSONB array; empty array when no tags. |
| 69 | + tags: |
| 70 | + fragment( |
| 71 | + "coalesce(jsonb_agg(DISTINCT jsonb_build_object('slug', ?, 'name', ?)) FILTER (WHERE ? IS NOT NULL), '[]'::jsonb)", |
| 72 | + t.slug, |
| 73 | + t.name, |
| 74 | + t.slug |
| 75 | + ) |
| 76 | + }) |
| 77 | + |
| 78 | + # Build assessment report rows with tags aggregated into a JSON array per row. |
| 79 | + query_reports = |
| 80 | + AssessmentReport |
| 81 | + # Left join keeps rows even when no tags exist. |
| 82 | + |> join(:left, [row], t in assoc(row, :tags)) |
| 83 | + |> where([row], row.media_urls != [] and not is_nil(row.media_urls)) |
| 84 | + |> where( |
| 85 | + [row], |
| 86 | + fragment( |
| 87 | + "EXISTS (SELECT 1 FROM unnest(?) AS u(url) WHERE u.url ~* ?)", |
| 88 | + row.media_urls, |
| 89 | + "^(https?://)" |
| 90 | + ) |
| 91 | + ) |
| 92 | + |> maybe_filter_tags_for_report(tag_filter) |
| 93 | + # Group by row.id so the tag join doesn't multiply rows. |
| 94 | + |> group_by([row], row.id) |
| 95 | + |> select([row, t], %{ |
| 96 | + type: "assessment_report", |
| 97 | + date: row.date, |
| 98 | + preview_url: nil, |
| 99 | + assessment_report_link: row.assessment_report_link, |
| 100 | + count: nil, |
| 101 | + tools_used: row.tools_used, |
| 102 | + observations: row.observations, |
| 103 | + uuid: row.uuid, |
| 104 | + # Aggregate tag slug+name into a JSONB array; empty array when no tags. |
| 105 | + tags: |
| 106 | + fragment( |
| 107 | + "coalesce(jsonb_agg(DISTINCT jsonb_build_object('slug', ?, 'name', ?)) FILTER (WHERE ? IS NOT NULL), '[]'::jsonb)", |
| 108 | + t.slug, |
| 109 | + t.name, |
| 110 | + t.slug |
| 111 | + ) |
| 112 | + }) |
| 113 | + |
| 114 | + union_all(query_partners, ^query_reports) |
| 115 | + end |
| 116 | + |
| 117 | + def page_size, do: @page_size |
| 118 | + |
| 119 | + defp maybe_filter_from_date(query, nil), do: query |
| 120 | + |
| 121 | + defp maybe_filter_from_date(query, date_string) do |
| 122 | + case Date.from_iso8601(date_string) do |
| 123 | + {:ok, date} -> |
| 124 | + where(query, [c], c.date >= ^date) |
| 125 | + |
| 126 | + _ -> |
| 127 | + query |
| 128 | + end |
| 129 | + end |
| 130 | + |
| 131 | + defp maybe_filter_to_date(query, nil), do: query |
| 132 | + |
| 133 | + defp maybe_filter_to_date(query, date_string) do |
| 134 | + case Date.from_iso8601(date_string) do |
| 135 | + {:ok, date} -> |
| 136 | + where(query, [c], c.date <= ^date) |
| 137 | + |
| 138 | + _ -> |
| 139 | + query |
| 140 | + end |
| 141 | + end |
| 142 | + |
| 143 | + defp maybe_filter_tags_for_partner(query, nil), do: query |
| 144 | + |
| 145 | + defp maybe_filter_tags_for_partner(query, tag_slug) do |
| 146 | + # Filter rows by tag without shrinking the aggregated tag list. |
| 147 | + |
| 148 | + #explaination in below function |
| 149 | + where(query, [row, _t], |
| 150 | + fragment( |
| 151 | + "EXISTS (SELECT 1 FROM partner_escalation_tags pet JOIN tags t ON t.id = pet.tag_id WHERE pet.partner_escalation_id = ? AND t.slug = ?)", |
| 152 | + row.id, |
| 153 | + ^tag_slug |
| 154 | + ) |
| 155 | + ) |
| 156 | + end |
| 157 | + |
| 158 | + defp maybe_filter_tags_for_report(query, nil), do: query |
| 159 | + |
| 160 | + defp maybe_filter_tags_for_report(query, tag_slug) do |
| 161 | + # Filter rows by tag without shrinking the aggregated tag list. |
| 162 | + |
| 163 | +# The outer query has row (report) and t (joined tags for aggregation). |
| 164 | +# The WHERE EXISTS (...) is evaluated for each row. |
| 165 | +# Inside the EXISTS, we join the report‑tag join table to tags, and check: |
| 166 | +# assessment_report_id = row.id |
| 167 | +# tag.slug = desired slug |
| 168 | +# If a match exists → keep that row in the outer query. |
| 169 | +# If not → that row is filtered out. |
| 170 | + |
| 171 | +# The issue with the normal filtering is that this is happening before we are aggregating the tags |
| 172 | +# in the select. So, if a row had 3 tags t1, t2, t3. In normal filtering for t1, the |
| 173 | +# rows with t2, t3 will get discarded from the join. This would result in rows still being |
| 174 | +# properly filtered, but in the tags field of rows, we would have not gotten all the tags for the row ( |
| 175 | +# because they were deleted from the join table earlier). therefor this approach. |
| 176 | + |
| 177 | + where(query, [row, _t], |
| 178 | + fragment( |
| 179 | + "EXISTS (SELECT 1 FROM assessment_report_tags art JOIN tags t ON t.id = art.tag_id WHERE art.assessment_report_id = ? AND t.slug = ?)", |
| 180 | + row.id, |
| 181 | + ^tag_slug |
| 182 | + ) |
| 183 | + ) |
| 184 | + end |
| 185 | + |
| 186 | + defp present_filter(value) when value in [nil, "", "all"], do: nil |
| 187 | + defp present_filter(value), do: value |
| 188 | + |
| 189 | + defp bulk_add_s3_media_url(rows) do |
| 190 | + base_preview_url = Application.fetch_env!(:dau, :preview_other_sources_dataset_base_s3_url) |
| 191 | + |
| 192 | + Enum.map(rows, fn row -> |
| 193 | + Map.put(row, :preview_url, base_preview_url <> "thumbnail_" <> row.uuid <> ".png") |
| 194 | + end) |
| 195 | + end |
| 196 | +end |
0 commit comments