Skip to content

Commit

Permalink
Fix related contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
IanPhilips committed Jul 9, 2024
1 parent 1e4c734 commit 86e1763
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 56 deletions.
79 changes: 48 additions & 31 deletions backend/supabase/functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -51,37 +51,54 @@ select ct.resolution_time is null
and ct.visibility = 'public'
and ((ct.close_time > now() + interval '10 minutes') or ct.close_time is null) $$ language sql;

create
or replace function search_contracts_by_group_slugs_1 (p_group_slugs text[], lim int, start int) returns jsonb[] stable parallel safe language sql as $$
select array_agg(data)
from (
select data
from contracts
where contracts.group_slugs && p_group_slugs
and is_valid_contract(contracts)
order by importance_score desc,
slug offset start
limit lim
) as search_contracts $$;

create
or replace function search_contracts_by_group_slugs_for_creator_1 (
creator_id text,
p_group_slugs text[],
lim int,
start int
) returns jsonb[] stable parallel safe language sql as $$
select array_agg(data)
from (
select data
from contracts
where contracts.group_slugs && p_group_slugs
and is_valid_contract(contracts)
and contracts.creator_id = $1
order by importance_score desc,
slug offset start
limit lim
) as search_contracts $$;
create or replace function get_related_contracts_by_group (p_contract_id text, lim int, start int)
returns table (data jsonb)
stable parallel safe language sql as $$
select distinct on (other_contracts.importance_score, other_contracts.slug) other_contracts.data
from contracts
join group_contracts on contracts.id = group_contracts.contract_id
join contracts as other_contracts on other_contracts.id in (
select gc.contract_id
from group_contracts gc
where gc.group_id in (
select group_id
from group_contracts
where contract_id = p_contract_id
)
)
where contracts.id = p_contract_id
and is_valid_contract(other_contracts)
and other_contracts.id != p_contract_id
order by other_contracts.importance_score desc, other_contracts.slug
offset start limit lim
$$;

create or replace function get_related_contracts_by_group_and_creator (
p_contract_id text,
lim int,
start int
)
returns table (data jsonb)
stable parallel safe language sql as $$
select distinct on (other_contracts.importance_score, other_contracts.slug) other_contracts.data
from contracts
join group_contracts on contracts.id = group_contracts.contract_id
join contracts as other_contracts on other_contracts.id in (
select gc.contract_id
from group_contracts gc
where gc.group_id in (
select group_id
from group_contracts
where contract_id = p_contract_id
)
)
where contracts.id = p_contract_id
and is_valid_contract(other_contracts)
and other_contracts.id != p_contract_id
and other_contracts.creator_id = contracts.creator_id
order by other_contracts.importance_score desc, other_contracts.slug
offset start limit lim
$$;

create
or replace function get_contract_metrics_with_contracts (uid text, count int, start int) returns table (contract_id text, metrics jsonb, contract jsonb) stable parallel safe language sql as $$
Expand Down
41 changes: 16 additions & 25 deletions web/hooks/use-related-contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { usePrivateUser } from './use-user'
import { isContractBlocked } from 'web/lib/firebase/users'
import { db } from 'web/lib/supabase/db'
import { useEvent } from './use-event'
import { convertContract } from 'common/supabase/contracts'

const GROUPS_PAGE_SIZE = 6
// const RELATED_PAGE_SIZE = 10
Expand Down Expand Up @@ -39,31 +40,21 @@ export const useRelatedMarkets = (
})
}

if (contract.groupSlugs?.length) {
const groupSlugsToUse = contract.groupSlugs.filter(
(slug) => !['spam', 'improperly-resolved'].includes(slug)
)
const [{ data: groupSlugData }, { data: creatorData }] =
await Promise.all([
db.rpc('search_contracts_by_group_slugs_1' as any, {
p_group_slugs: groupSlugsToUse,
lim: GROUPS_PAGE_SIZE,
start: groupsPage.current * GROUPS_PAGE_SIZE,
}),
db.rpc('search_contracts_by_group_slugs_for_creator_1' as any, {
creator_id: contract.creatorId,
p_group_slugs: groupSlugsToUse,
lim: GROUPS_PAGE_SIZE,
start: creatorPage.current * GROUPS_PAGE_SIZE,
}),
])

if (groupSlugData) setContracts(groupSlugData, groupsPage)
if (creatorData) setContracts(creatorData, creatorPage)
} else {
// const contracts = await getRelatedContracts(contract, RELATED_PAGE_SIZE)
// setContracts(contracts, relatedPage)
}
const [{ data: groupSlugData }, { data: creatorData }] = await Promise.all([
db.rpc('get_related_contracts_by_group' as any, {
p_contract_id: contract.id,
lim: GROUPS_PAGE_SIZE,
start: groupsPage.current * GROUPS_PAGE_SIZE,
}),
db.rpc('get_related_contracts_by_group_and_creator' as any, {
p_contract_id: contract.id,
lim: GROUPS_PAGE_SIZE,
start: creatorPage.current * GROUPS_PAGE_SIZE,
}),
])
if (groupSlugData)
setContracts(groupSlugData.map(convertContract), groupsPage)
if (creatorData) setContracts(creatorData.map(convertContract), creatorPage)
return hasLoadedMoreContracts
})

Expand Down

0 comments on commit 86e1763

Please sign in to comment.