Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove expensive function in migration #891

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ jest.mock('app/utils/useChainAddresses', () => ({
useChainAddresses: jest.fn().mockReturnValue({ data: { address: '0x123' } }),
}))
jest.mock('wagmi')
jest.mock('@web3modal/wagmi/react', () => ({
useWeb3Modal: jest.fn().mockReturnValue({ open: jest.fn() }),
}))

jest.mock('@my/wagmi', () => ({
__esModule: true,
...jest.requireActual('@my/wagmi'),
Expand Down
15 changes: 5 additions & 10 deletions packages/app/features/affiliate/screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import { useAffiliateReferrals } from './utils/useAffiliateReferrals'
import { Fragment } from 'react'
import { useAffiliateStats } from './utils/useAffiliateStats'
import type { Functions } from '@my/supabase/database.types'

export const AffiliateScreen = () => {
return (
Expand Down Expand Up @@ -171,20 +172,14 @@ const ReferralsList = () => {
)
}

const ReferralsListRow = ({ referral }) => {
const ReferralsListRow = ({
referral,
}: { referral: Functions<'get_affiliate_referrals'>[number] }) => {
const date = new Date(referral?.created_at).toLocaleString(undefined, { dateStyle: 'medium' })

return (
<Card bc="$color0" ai="center">
<Link
href={`/profile/${referral.profile?.send_id}`}
f={1}
als="stretch"
px="$5"
py="$3"
w="100%"
h="100%"
>
<Link href={`/${referral.tag}`} f={1} als="stretch" px="$5" py="$3" w="100%" h="100%">
<XStack gap="$5" f={1} ai="center" jc={'space-between'}>
<XStack gap="$3.5" f={1} ai="center">
<Avatar size="$4.5" br="$4" gap="$2">
Expand Down
22 changes: 0 additions & 22 deletions packages/app/utils/OpenConnectModalWrapper.tsx

This file was deleted.

44 changes: 0 additions & 44 deletions supabase/database-generated.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1115,34 +1115,6 @@ export type Database = {
}
Relationships: []
}
affiliate_referrals: {
Row: {
referral:
| Database["public"]["CompositeTypes"]["affiliate_referral_type"]
| null
}
Relationships: []
}
affiliate_stats_summary: {
Row: {
affiliate_send_score: number | null
created_at: string | null
id: string | null
network_plus_minus: number | null
referral_count: number | null
send_plus_minus: number | null
user_id: string | null
}
Relationships: [
{
foreignKeyName: "affiliate_stats_user_id_fkey"
columns: ["user_id"]
isOneToOne: true
referencedRelation: "profiles"
referencedColumns: ["id"]
},
]
}
distribution_verifications_summary: {
Row: {
distribution_id: number | null
Expand Down Expand Up @@ -1381,22 +1353,6 @@ export type Database = {
send_id: number
tags: unknown
}
affiliate_referral_type: {
referred_id: string
send_plus_minus: number
avatar_url: string
tag: string
created_at: string
}
affiliate_stats_summary_type: {
id: number
created_at: string
user_id: string
send_plus_minus: number
referral_count: number
network_plus_minus: number
affiliate_send_score: number
}
multiplier_info: {
type: string
value: number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,61 +28,6 @@ CREATE POLICY "Users can see own and referrals affiliate stats" ON affiliate_sta
WHERE
referrer_id = auth.uid() AND referred_id = affiliate_stats.user_id));

CREATE OR REPLACE FUNCTION initialize_send_plus_minus()
RETURNS void
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path = public
AS $$
BEGIN
UPDATE
affiliate_stats a
SET
send_plus_minus =( WITH sent_amount AS(
SELECT
COALESCE(SUM(stt.v::numeric), 0) AS total
FROM
send_token_transfers stt
INNER JOIN send_accounts sa ON sa.address = concat('0x', encode(stt.f, 'hex'))::citext
WHERE
sa.user_id = a.user_id),
received_amount AS(
SELECT
COALESCE(SUM(stt.v::numeric), 0) AS total
FROM
send_token_transfers stt
INNER JOIN send_accounts sa ON sa.address = concat('0x', encode(stt.t, 'hex'))::citext
LEFT JOIN referrals r ON r.referrer_id = sa.user_id
WHERE
sa.user_id = a.user_id
AND concat('0x', encode(stt.f, 'hex'))::citext NOT IN(
SELECT
sa2.address
FROM
send_accounts sa2
INNER JOIN referrals r2 ON r2.referrer_id = sa2.user_id
WHERE
r2.referred_id = a.user_id))
SELECT
(
SELECT
total
FROM
sent_amount) -(
SELECT
total
FROM
received_amount));
END;
$$;

-- Execute the function
SELECT
initialize_send_plus_minus();

-- Drop the function after use
DROP FUNCTION initialize_send_plus_minus();

CREATE OR REPLACE FUNCTION public.update_affiliate_stats_on_transfer()
RETURNS TRIGGER
LANGUAGE plpgsql
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
DROP FUNCTION IF EXISTS get_affiliate_stats_summary();

CREATE OR REPLACE FUNCTION get_affiliate_stats_summary()
RETURNS TABLE(
id uuid,
created_at timestamptz,
user_id uuid,
referral_count bigint)
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path = public
AS $$
BEGIN
RETURN QUERY
SELECT
a.id,
a.created_at,
a.user_id,
COUNT(DISTINCT r.tag)::bigint AS referral_count
FROM
affiliate_stats a
LEFT JOIN referrals r ON r.referrer_id = a.user_id
WHERE
a.user_id = auth.uid()
GROUP BY
a.id,
a.created_at,
a.user_id;
END;
$$;

Loading
Loading