Skip to content

Commit

Permalink
fix: use new plan calculation function
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Mar 5, 2024
1 parent 7bb5f92 commit 02974b5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 30 deletions.
36 changes: 6 additions & 30 deletions supabase/functions/_backend/private/triggers/logsnag_insights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ interface GlobalStats {
updates: PromiseLike<number>
users: PromiseLike<number>
stars: Promise<number>
trial: PromiseLike<number>
onboarded: PromiseLike<number>
need_upgrade: PromiseLike<number>
paying: PromiseLike<number>
Expand Down Expand Up @@ -42,11 +41,6 @@ function getStats(c: Context): GlobalStats {
.select('*', { count: 'exact' })
.then(res => res.count || 0),
stars: getGithubStars(),
trial: supabase.rpc('count_all_trial', {}).single().then((res) => {
if (res.error || !res.data)
console.log('count_all_trial', res.error)
return res.data || 0
}),
paying: supabase.rpc('count_all_paying', {}).single().then((res) => {
if (res.error || !res.data)
console.log('count_all_paying', res.error)
Expand All @@ -62,25 +56,10 @@ function getStats(c: Context): GlobalStats {
console.log('count_all_need_upgrade', res.error)
return res.data || 0
}),
plans: supabase.from('plans').select('name, stripe_id').then(({ data: planNames, error }) => {
if (error || !planNames) {
console.log('get plans', error)
return {}
}
return supabase.rpc('count_all_plans', {}).then((res) => {
if (res.error || !res.data) {
console.log('count_all_plan', res.error)
return {}
}
// create object with name and count
const plans: any = {}
for (const plan of res.data) {
const name = planNames.find(p => p.stripe_id === plan.product_id)?.name
if (name)
plans[name] = plan.count
}
return plans
})
plans: supabase.rpc('count_all_plans_v2', {}).single().then((res) => {
if (res.error || !res.data)
console.log('count_all_plans_v2', res.error)
return res.data || {}
}),
}
}
Expand All @@ -96,7 +75,6 @@ app.post('/', middlewareAPISecret, async (c: Context) => {
updates,
users,
stars,
trial,
paying,
onboarded,
need_upgrade,
Expand All @@ -106,14 +84,13 @@ app.post('/', middlewareAPISecret, async (c: Context) => {
res.updates,
res.users,
res.stars,
res.trial,
res.paying,
res.onboarded,
res.need_upgrade,
res.plans,
])
const not_paying = users - paying
console.log('All Promises', apps, updates, users, stars, trial, paying, onboarded, need_upgrade, plans)
console.log('All Promises', apps, updates, users, stars, paying, onboarded, need_upgrade, plans)
// console.log('app', app.app_id, downloads, versions, shared, channels)
// create var date_id with yearn-month-day
const date_id = new Date().toISOString().slice(0, 10)
Expand All @@ -122,7 +99,6 @@ app.post('/', middlewareAPISecret, async (c: Context) => {
apps,
updates,
stars,
trial,
paying,
onboarded,
need_upgrade,
Expand Down Expand Up @@ -162,7 +138,7 @@ app.post('/', middlewareAPISecret, async (c: Context) => {
},
{
title: 'User trial',
value: trial,
value: plans.Trial,
icon: '👶',
},
{
Expand Down
47 changes: 47 additions & 0 deletions supabase/migrations/20240305054315_dashboard_counts.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
CREATE OR REPLACE FUNCTION "public"."count_all_plans_v2"()
RETURNS TABLE("plan_name" character varying, "count" bigint)
LANGUAGE "plpgsql"
AS $$
BEGIN
RETURN QUERY
WITH AllProducts AS (
SELECT p.name AS product_name
FROM stripe_info si
INNER JOIN plans p ON si.product_id = p.stripe_id
UNION
SELECT 'Free' AS product_name
UNION
SELECT 'Trial' AS product_name
),
StatusCounts AS (
SELECT
p.name AS product_name,
COUNT(*) AS count
FROM stripe_info si
INNER JOIN plans p ON si.product_id = p.stripe_id AND si.status = 'succeeded' AND si.product_id <> 'free'
GROUP BY p.name

UNION ALL

SELECT
'Free' AS product_name,
COUNT(*) AS count
FROM stripe_info si
WHERE si.product_id = 'free' AND (si.trial_at <= NOW() OR si.status = 'succeeded')

UNION ALL

SELECT
'Trial' AS product_name,
COUNT(*) AS count
FROM stripe_info si
WHERE si.product_id = 'free' AND si.trial_at > NOW()
)
SELECT
ap.product_name,
COALESCE(sc.count, 0) AS count
FROM AllProducts ap
LEFT JOIN StatusCounts sc ON ap.product_name = sc.product_name
ORDER BY ap.product_name;
END;
$$;

0 comments on commit 02974b5

Please sign in to comment.