From 133f6aa4fa888acb9092846cccc1c8098db8705b Mon Sep 17 00:00:00 2001 From: Lim Ding Wen Date: Wed, 7 Aug 2024 04:35:32 +0800 Subject: [PATCH] Remove fill [blank] functions, can be done using SQL views instead --- .../functions/fill-debate-bill-ids/index.ts | 5 -- .../fill-debate-speaker-ids/index.ts | 5 -- .../shared-functions/fill-debate-bill-ids.ts | 45 ----------- .../fill-debate-speaker-ids.ts | 79 ------------------- supabase/functions/per-10-min/index.ts | 4 - 5 files changed, 138 deletions(-) delete mode 100644 supabase/functions/fill-debate-bill-ids/index.ts delete mode 100644 supabase/functions/fill-debate-speaker-ids/index.ts delete mode 100644 supabase/functions/lib/shared-functions/fill-debate-bill-ids.ts delete mode 100644 supabase/functions/lib/shared-functions/fill-debate-speaker-ids.ts diff --git a/supabase/functions/fill-debate-bill-ids/index.ts b/supabase/functions/fill-debate-bill-ids/index.ts deleted file mode 100644 index adcd137..0000000 --- a/supabase/functions/fill-debate-bill-ids/index.ts +++ /dev/null @@ -1,5 +0,0 @@ -import "https://esm.sh/@supabase/functions-js/src/edge-runtime.d.ts"; -import proxyToResponseWrapper from "../lib/utils/proxy-to-response-wrapper.ts"; -import fillDebateBillIds from "../lib/shared-functions/fill-debate-bill-ids.ts"; - -Deno.serve(proxyToResponseWrapper(fillDebateBillIds)); diff --git a/supabase/functions/fill-debate-speaker-ids/index.ts b/supabase/functions/fill-debate-speaker-ids/index.ts deleted file mode 100644 index 0f8c917..0000000 --- a/supabase/functions/fill-debate-speaker-ids/index.ts +++ /dev/null @@ -1,5 +0,0 @@ -import "https://esm.sh/@supabase/functions-js/src/edge-runtime.d.ts"; -import proxyToResponseWrapper from "../lib/utils/proxy-to-response-wrapper.ts"; -import fillDebateSpeakerIds from "../lib/shared-functions/fill-debate-speaker-ids.ts"; - -Deno.serve(proxyToResponseWrapper(fillDebateSpeakerIds)); diff --git a/supabase/functions/lib/shared-functions/fill-debate-bill-ids.ts b/supabase/functions/lib/shared-functions/fill-debate-bill-ids.ts deleted file mode 100644 index 00dee3b..0000000 --- a/supabase/functions/lib/shared-functions/fill-debate-bill-ids.ts +++ /dev/null @@ -1,45 +0,0 @@ -import buildResponseProxy from "../utils/build-response-proxy.ts"; -import { createSupabase } from "../utils/create-supabase.ts"; -import { isAdmin } from "../utils/check-admin.ts"; -import { SupabaseClient } from "https://esm.sh/v135/@supabase/supabase-js@2.24.0/dist/module/index.d.ts"; - -async function getBillDebateMatches(supabase: SupabaseClient) { - const { data, error } = await supabase - .from("debate_bill_match_view") - .select("debate_id, bill_id") - // Filter to make sure the debate's bill_id is null, so we don't run up on the limit selecting unneeded matches - .is("debate_bill_id", null); - if (error) throw error; - return data; -} - -async function updateDebateWithBillId( - supabase: SupabaseClient, - rowId: number, - billId: number, -) { - const { error } = await supabase - .from("debate") - .update({ bill_id: billId }) - .eq("id", rowId); - if (error) throw error; -} - -export default async function fillDebateBillIds(req: Request) { - const supabase = createSupabase(); - - if (!isAdmin(req)) { - return buildResponseProxy({ message: "Unauthorised." }, 401); - } - - let rowCount = 0; - const matches = await getBillDebateMatches(supabase); - for (const match of matches) { - await updateDebateWithBillId(supabase, match.debate_id, match.bill_id); - rowCount++; - } - - return buildResponseProxy({ - message: `Filled bill IDs for ${rowCount} debates.`, - }); -} diff --git a/supabase/functions/lib/shared-functions/fill-debate-speaker-ids.ts b/supabase/functions/lib/shared-functions/fill-debate-speaker-ids.ts deleted file mode 100644 index bb43168..0000000 --- a/supabase/functions/lib/shared-functions/fill-debate-speaker-ids.ts +++ /dev/null @@ -1,79 +0,0 @@ -import buildResponseProxy from "../utils/build-response-proxy.ts"; -import { createSupabase } from "../utils/create-supabase.ts"; -import { isAdmin } from "../utils/check-admin.ts"; -import { SupabaseClient } from "https://esm.sh/v135/@supabase/supabase-js@2.24.0/dist/module/index.d.ts"; - -async function getDebateSpeechRows(supabase: SupabaseClient) { - const { data, error } = await supabase - .from("debate_speech") - .select("id, speaker_name, fill_debate_speaker_ids_updated_at") - .is("speaker_id", null) - // Update the earliest-updated ones first - .order("fill_debate_speaker_ids_updated_at", { ascending: true }) - // Just make it explicit; Supabase already limits to 1000 by default - .limit(100); - if (error) throw error; - return data; -} - -async function fetchSpeakerIdWithSpeakerName( - supabase: SupabaseClient, - speakerName: string, -) { - const { data, error } = await supabase - .from("combined_mp_names_view") - .select("mp_id") - .or(`full_name.eq."${speakerName}", alias_name.eq."${speakerName}"`) - .limit(1) - .maybeSingle(); - if (error) throw error; - return data ? data.mp_id : null; -} - -async function updateDebateSpeechRowWithSpeakerId( - supabase: SupabaseClient, - rowId: number, - speakerId: number, -) { - const { error } = await supabase - .from("debate_speech") - .update({ - speaker_id: speakerId, - fill_debate_speaker_ids_updated_at: new Date(), - }) - .eq("id", rowId); - if (error) throw error; -} - -// Goes through all debate speeches and fills in the speaker_id field via the speaker_name field -// This is to enable dynamic aliasing without needing to re-scrape the entire database -export default async function fillDebateSpeakerIds(req: Request) { - const supabase = createSupabase(); - - if (!isAdmin(req)) { - return buildResponseProxy({ message: "Unauthorised." }, 401); - } - - let rowCount = 0; - let potentialRowCount = 0; - - const debateSpeechRows = await getDebateSpeechRows(supabase); - - for (const row of debateSpeechRows) { - const speakerId = await fetchSpeakerIdWithSpeakerName( - supabase, - row.speaker_name, - ); - - await updateDebateSpeechRowWithSpeakerId(supabase, row.id, speakerId); - if (speakerId) { - rowCount++; - } - - potentialRowCount++; - } - - return buildResponseProxy({ - message: `Filled speaker IDs for ${rowCount} out of ${potentialRowCount} debate speeches.`, - }); -} diff --git a/supabase/functions/per-10-min/index.ts b/supabase/functions/per-10-min/index.ts index 98727e0..9f72603 100644 --- a/supabase/functions/per-10-min/index.ts +++ b/supabase/functions/per-10-min/index.ts @@ -2,14 +2,10 @@ import "https://esm.sh/@supabase/functions-js/src/edge-runtime.d.ts"; import scrapeBillsPdf from "../lib/shared-functions/scrape-bills-pdf.ts"; import generateBillsSummary from "../lib/shared-functions/generate-bills-summary.ts"; import { buildResponse } from "../lib/utils/build-response.ts"; -// import fillDebateSpeakerIds from "../lib/shared-functions/fill-debate-speaker-ids.ts"; import generateDebateSummary from "../lib/shared-functions/generate-debate-summary.ts"; -import fillDebateBillIds from "../lib/shared-functions/fill-debate-bill-ids.ts"; Deno.serve(async (req) => { return buildResponse([ - // await fillDebateSpeakerIds(req), - await fillDebateBillIds(req), await scrapeBillsPdf(req), await generateBillsSummary(req), await generateDebateSummary(req),