Use EAT for analytics windows and add payment_method filter.
Aligns revenue day/month buckets with Africa/Addis_Ababa and lets dashboards filter payment metrics by method. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
34307d7717
commit
651e9e7409
|
|
@ -1,12 +1,15 @@
|
||||||
-- =====================
|
-- =====================
|
||||||
-- Analytics (date-filtered)
|
-- Analytics (date-filtered)
|
||||||
-- =====================
|
-- =====================
|
||||||
|
-- Calendar days / months use Africa/Addis_Ababa (EAT, UTC+3).
|
||||||
-- Shared optional params (nullable = all-time):
|
-- Shared optional params (nullable = all-time):
|
||||||
-- range_start, range_end (exclusive upper bound)
|
-- range_start, range_end (exclusive upper bound, timestamptz)
|
||||||
-- Required chart params:
|
-- Required chart params:
|
||||||
-- series_start, series_end (inclusive dates)
|
-- series_start, series_end (inclusive EAT calendar dates)
|
||||||
-- Relative window anchor:
|
-- Relative window anchor:
|
||||||
-- ref_date (inclusive date used for new_today/week/month)
|
-- ref_date (inclusive EAT date used for new_today/week/month)
|
||||||
|
-- Payment/revenue queries also accept optional:
|
||||||
|
-- payment_method (exact match, or METHOD:% prefix variants)
|
||||||
|
|
||||||
-- =====================
|
-- =====================
|
||||||
-- User Analytics
|
-- User Analytics
|
||||||
|
|
@ -15,14 +18,14 @@
|
||||||
-- name: AnalyticsUsersSummary :one
|
-- name: AnalyticsUsersSummary :one
|
||||||
SELECT
|
SELECT
|
||||||
COUNT(*)::bigint AS total,
|
COUNT(*)::bigint AS total,
|
||||||
COUNT(*) FILTER (WHERE u.created_at::date = sqlc.arg('ref_date')::date)::bigint AS new_today,
|
COUNT(*) FILTER (WHERE (u.created_at AT TIME ZONE 'Africa/Addis_Ababa')::date = sqlc.arg('ref_date')::date)::bigint AS new_today,
|
||||||
COUNT(*) FILTER (
|
COUNT(*) FILTER (
|
||||||
WHERE u.created_at::date >= sqlc.arg('ref_date')::date - INTERVAL '6 days'
|
WHERE (u.created_at AT TIME ZONE 'Africa/Addis_Ababa')::date >= sqlc.arg('ref_date')::date - INTERVAL '6 days'
|
||||||
AND u.created_at::date <= sqlc.arg('ref_date')::date
|
AND (u.created_at AT TIME ZONE 'Africa/Addis_Ababa')::date <= sqlc.arg('ref_date')::date
|
||||||
)::bigint AS new_this_week,
|
)::bigint AS new_this_week,
|
||||||
COUNT(*) FILTER (
|
COUNT(*) FILTER (
|
||||||
WHERE u.created_at::date >= sqlc.arg('ref_date')::date - INTERVAL '29 days'
|
WHERE (u.created_at AT TIME ZONE 'Africa/Addis_Ababa')::date >= sqlc.arg('ref_date')::date - INTERVAL '29 days'
|
||||||
AND u.created_at::date <= sqlc.arg('ref_date')::date
|
AND (u.created_at AT TIME ZONE 'Africa/Addis_Ababa')::date <= sqlc.arg('ref_date')::date
|
||||||
)::bigint AS new_this_month
|
)::bigint AS new_this_month
|
||||||
FROM users u
|
FROM users u
|
||||||
WHERE (sqlc.narg('range_start')::timestamptz IS NULL OR u.created_at >= sqlc.narg('range_start')::timestamptz)
|
WHERE (sqlc.narg('range_start')::timestamptz IS NULL OR u.created_at >= sqlc.narg('range_start')::timestamptz)
|
||||||
|
|
@ -137,7 +140,7 @@ FROM generate_series(
|
||||||
sqlc.arg('series_end')::date,
|
sqlc.arg('series_end')::date,
|
||||||
INTERVAL '1 day'
|
INTERVAL '1 day'
|
||||||
) AS d(date)
|
) AS d(date)
|
||||||
LEFT JOIN users u ON u.created_at::date = d.date
|
LEFT JOIN users u ON (u.created_at AT TIME ZONE 'Africa/Addis_Ababa')::date = d.date
|
||||||
AND (sqlc.narg('range_start')::timestamptz IS NULL OR u.created_at >= sqlc.narg('range_start')::timestamptz)
|
AND (sqlc.narg('range_start')::timestamptz IS NULL OR u.created_at >= sqlc.narg('range_start')::timestamptz)
|
||||||
AND (sqlc.narg('range_end')::timestamptz IS NULL OR u.created_at < sqlc.narg('range_end')::timestamptz)
|
AND (sqlc.narg('range_end')::timestamptz IS NULL OR u.created_at < sqlc.narg('range_end')::timestamptz)
|
||||||
GROUP BY d.date
|
GROUP BY d.date
|
||||||
|
|
@ -153,17 +156,17 @@ SELECT
|
||||||
COUNT(*) FILTER (WHERE us.status = 'ACTIVE')::bigint AS active,
|
COUNT(*) FILTER (WHERE us.status = 'ACTIVE')::bigint AS active,
|
||||||
COUNT(*) FILTER (
|
COUNT(*) FILTER (
|
||||||
WHERE us.status = 'ACTIVE'
|
WHERE us.status = 'ACTIVE'
|
||||||
AND us.created_at::date = sqlc.arg('ref_date')::date
|
AND (us.created_at AT TIME ZONE 'Africa/Addis_Ababa')::date = sqlc.arg('ref_date')::date
|
||||||
)::bigint AS new_today,
|
)::bigint AS new_today,
|
||||||
COUNT(*) FILTER (
|
COUNT(*) FILTER (
|
||||||
WHERE us.status = 'ACTIVE'
|
WHERE us.status = 'ACTIVE'
|
||||||
AND us.created_at::date >= sqlc.arg('ref_date')::date - INTERVAL '6 days'
|
AND (us.created_at AT TIME ZONE 'Africa/Addis_Ababa')::date >= sqlc.arg('ref_date')::date - INTERVAL '6 days'
|
||||||
AND us.created_at::date <= sqlc.arg('ref_date')::date
|
AND (us.created_at AT TIME ZONE 'Africa/Addis_Ababa')::date <= sqlc.arg('ref_date')::date
|
||||||
)::bigint AS new_this_week,
|
)::bigint AS new_this_week,
|
||||||
COUNT(*) FILTER (
|
COUNT(*) FILTER (
|
||||||
WHERE us.status = 'ACTIVE'
|
WHERE us.status = 'ACTIVE'
|
||||||
AND us.created_at::date >= sqlc.arg('ref_date')::date - INTERVAL '29 days'
|
AND (us.created_at AT TIME ZONE 'Africa/Addis_Ababa')::date >= sqlc.arg('ref_date')::date - INTERVAL '29 days'
|
||||||
AND us.created_at::date <= sqlc.arg('ref_date')::date
|
AND (us.created_at AT TIME ZONE 'Africa/Addis_Ababa')::date <= sqlc.arg('ref_date')::date
|
||||||
)::bigint AS new_this_month
|
)::bigint AS new_this_month
|
||||||
FROM user_subscriptions us
|
FROM user_subscriptions us
|
||||||
WHERE (sqlc.narg('range_start')::timestamptz IS NULL OR us.created_at >= sqlc.narg('range_start')::timestamptz)
|
WHERE (sqlc.narg('range_start')::timestamptz IS NULL OR us.created_at >= sqlc.narg('range_start')::timestamptz)
|
||||||
|
|
@ -190,6 +193,11 @@ JOIN subscription_plans sp ON sp.id = p.plan_id
|
||||||
WHERE p.status = 'SUCCESS'
|
WHERE p.status = 'SUCCESS'
|
||||||
AND (sqlc.narg('range_start')::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) >= sqlc.narg('range_start')::timestamptz)
|
AND (sqlc.narg('range_start')::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) >= sqlc.narg('range_start')::timestamptz)
|
||||||
AND (sqlc.narg('range_end')::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) < sqlc.narg('range_end')::timestamptz)
|
AND (sqlc.narg('range_end')::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) < sqlc.narg('range_end')::timestamptz)
|
||||||
|
AND (
|
||||||
|
sqlc.narg('payment_method')::text IS NULL
|
||||||
|
OR UPPER(COALESCE(p.payment_method, '')) = UPPER(sqlc.narg('payment_method')::text)
|
||||||
|
OR UPPER(COALESCE(p.payment_method, '')) LIKE UPPER(sqlc.narg('payment_method')::text) || ':%'
|
||||||
|
)
|
||||||
GROUP BY sp.name, sp.currency
|
GROUP BY sp.name, sp.currency
|
||||||
ORDER BY total_revenue DESC;
|
ORDER BY total_revenue DESC;
|
||||||
|
|
||||||
|
|
@ -202,7 +210,7 @@ FROM generate_series(
|
||||||
sqlc.arg('series_end')::date,
|
sqlc.arg('series_end')::date,
|
||||||
INTERVAL '1 day'
|
INTERVAL '1 day'
|
||||||
) AS d(date)
|
) AS d(date)
|
||||||
LEFT JOIN user_subscriptions us ON us.created_at::date = d.date
|
LEFT JOIN user_subscriptions us ON (us.created_at AT TIME ZONE 'Africa/Addis_Ababa')::date = d.date
|
||||||
AND us.status = 'ACTIVE'
|
AND us.status = 'ACTIVE'
|
||||||
AND (sqlc.narg('range_start')::timestamptz IS NULL OR us.created_at >= sqlc.narg('range_start')::timestamptz)
|
AND (sqlc.narg('range_start')::timestamptz IS NULL OR us.created_at >= sqlc.narg('range_start')::timestamptz)
|
||||||
AND (sqlc.narg('range_end')::timestamptz IS NULL OR us.created_at < sqlc.narg('range_end')::timestamptz)
|
AND (sqlc.narg('range_end')::timestamptz IS NULL OR us.created_at < sqlc.narg('range_end')::timestamptz)
|
||||||
|
|
@ -313,6 +321,11 @@ JOIN subscription_plans sp ON sp.id = p.plan_id
|
||||||
WHERE p.status = 'SUCCESS'
|
WHERE p.status = 'SUCCESS'
|
||||||
AND (sqlc.narg('range_start')::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) >= sqlc.narg('range_start')::timestamptz)
|
AND (sqlc.narg('range_start')::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) >= sqlc.narg('range_start')::timestamptz)
|
||||||
AND (sqlc.narg('range_end')::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) < sqlc.narg('range_end')::timestamptz)
|
AND (sqlc.narg('range_end')::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) < sqlc.narg('range_end')::timestamptz)
|
||||||
|
AND (
|
||||||
|
sqlc.narg('payment_method')::text IS NULL
|
||||||
|
OR UPPER(COALESCE(p.payment_method, '')) = UPPER(sqlc.narg('payment_method')::text)
|
||||||
|
OR UPPER(COALESCE(p.payment_method, '')) LIKE UPPER(sqlc.narg('payment_method')::text) || ':%'
|
||||||
|
)
|
||||||
GROUP BY sp.category
|
GROUP BY sp.category
|
||||||
ORDER BY amount DESC;
|
ORDER BY amount DESC;
|
||||||
|
|
||||||
|
|
@ -325,7 +338,7 @@ FROM generate_series(
|
||||||
sqlc.arg('series_end')::date,
|
sqlc.arg('series_end')::date,
|
||||||
INTERVAL '1 day'
|
INTERVAL '1 day'
|
||||||
) AS d(date)
|
) AS d(date)
|
||||||
LEFT JOIN user_subscriptions us ON us.cancelled_at::date = d.date
|
LEFT JOIN user_subscriptions us ON (us.cancelled_at AT TIME ZONE 'Africa/Addis_Ababa')::date = d.date
|
||||||
AND (sqlc.narg('range_start')::timestamptz IS NULL OR us.cancelled_at >= sqlc.narg('range_start')::timestamptz)
|
AND (sqlc.narg('range_start')::timestamptz IS NULL OR us.cancelled_at >= sqlc.narg('range_start')::timestamptz)
|
||||||
AND (sqlc.narg('range_end')::timestamptz IS NULL OR us.cancelled_at < sqlc.narg('range_end')::timestamptz)
|
AND (sqlc.narg('range_end')::timestamptz IS NULL OR us.cancelled_at < sqlc.narg('range_end')::timestamptz)
|
||||||
GROUP BY d.date
|
GROUP BY d.date
|
||||||
|
|
@ -341,7 +354,7 @@ FROM generate_series(
|
||||||
sqlc.arg('series_end')::date,
|
sqlc.arg('series_end')::date,
|
||||||
INTERVAL '1 day'
|
INTERVAL '1 day'
|
||||||
) AS d(date)
|
) AS d(date)
|
||||||
LEFT JOIN user_subscriptions us ON us.expires_at::date = d.date
|
LEFT JOIN user_subscriptions us ON (us.expires_at AT TIME ZONE 'Africa/Addis_Ababa')::date = d.date
|
||||||
AND (sqlc.narg('range_start')::timestamptz IS NULL OR us.expires_at >= sqlc.narg('range_start')::timestamptz)
|
AND (sqlc.narg('range_start')::timestamptz IS NULL OR us.expires_at >= sqlc.narg('range_start')::timestamptz)
|
||||||
AND (sqlc.narg('range_end')::timestamptz IS NULL OR us.expires_at < sqlc.narg('range_end')::timestamptz)
|
AND (sqlc.narg('range_end')::timestamptz IS NULL OR us.expires_at < sqlc.narg('range_end')::timestamptz)
|
||||||
AND EXISTS (
|
AND EXISTS (
|
||||||
|
|
@ -449,7 +462,7 @@ FROM generate_series(
|
||||||
sqlc.arg('series_end')::date,
|
sqlc.arg('series_end')::date,
|
||||||
INTERVAL '1 day'
|
INTERVAL '1 day'
|
||||||
) AS d(date)
|
) AS d(date)
|
||||||
LEFT JOIN user_subscriptions us ON us.created_at::date = d.date
|
LEFT JOIN user_subscriptions us ON (us.created_at AT TIME ZONE 'Africa/Addis_Ababa')::date = d.date
|
||||||
AND (sqlc.narg('range_start')::timestamptz IS NULL OR us.created_at >= sqlc.narg('range_start')::timestamptz)
|
AND (sqlc.narg('range_start')::timestamptz IS NULL OR us.created_at >= sqlc.narg('range_start')::timestamptz)
|
||||||
AND (sqlc.narg('range_end')::timestamptz IS NULL OR us.created_at < sqlc.narg('range_end')::timestamptz)
|
AND (sqlc.narg('range_end')::timestamptz IS NULL OR us.created_at < sqlc.narg('range_end')::timestamptz)
|
||||||
AND EXISTS (
|
AND EXISTS (
|
||||||
|
|
@ -477,7 +490,12 @@ SELECT
|
||||||
COUNT(*) FILTER (WHERE p.status = 'SUCCESS')::bigint AS successful_payments
|
COUNT(*) FILTER (WHERE p.status = 'SUCCESS')::bigint AS successful_payments
|
||||||
FROM payments p
|
FROM payments p
|
||||||
WHERE (sqlc.narg('range_start')::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) >= sqlc.narg('range_start')::timestamptz)
|
WHERE (sqlc.narg('range_start')::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) >= sqlc.narg('range_start')::timestamptz)
|
||||||
AND (sqlc.narg('range_end')::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) < sqlc.narg('range_end')::timestamptz);
|
AND (sqlc.narg('range_end')::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) < sqlc.narg('range_end')::timestamptz)
|
||||||
|
AND (
|
||||||
|
sqlc.narg('payment_method')::text IS NULL
|
||||||
|
OR UPPER(COALESCE(p.payment_method, '')) = UPPER(sqlc.narg('payment_method')::text)
|
||||||
|
OR UPPER(COALESCE(p.payment_method, '')) LIKE UPPER(sqlc.narg('payment_method')::text) || ':%'
|
||||||
|
);
|
||||||
|
|
||||||
-- name: AnalyticsPaymentsByStatus :many
|
-- name: AnalyticsPaymentsByStatus :many
|
||||||
SELECT
|
SELECT
|
||||||
|
|
@ -487,6 +505,11 @@ SELECT
|
||||||
FROM payments p
|
FROM payments p
|
||||||
WHERE (sqlc.narg('range_start')::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) >= sqlc.narg('range_start')::timestamptz)
|
WHERE (sqlc.narg('range_start')::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) >= sqlc.narg('range_start')::timestamptz)
|
||||||
AND (sqlc.narg('range_end')::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) < sqlc.narg('range_end')::timestamptz)
|
AND (sqlc.narg('range_end')::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) < sqlc.narg('range_end')::timestamptz)
|
||||||
|
AND (
|
||||||
|
sqlc.narg('payment_method')::text IS NULL
|
||||||
|
OR UPPER(COALESCE(p.payment_method, '')) = UPPER(sqlc.narg('payment_method')::text)
|
||||||
|
OR UPPER(COALESCE(p.payment_method, '')) LIKE UPPER(sqlc.narg('payment_method')::text) || ':%'
|
||||||
|
)
|
||||||
GROUP BY p.status
|
GROUP BY p.status
|
||||||
ORDER BY count DESC;
|
ORDER BY count DESC;
|
||||||
|
|
||||||
|
|
@ -499,6 +522,11 @@ FROM payments p
|
||||||
WHERE p.status = 'SUCCESS'
|
WHERE p.status = 'SUCCESS'
|
||||||
AND (sqlc.narg('range_start')::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) >= sqlc.narg('range_start')::timestamptz)
|
AND (sqlc.narg('range_start')::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) >= sqlc.narg('range_start')::timestamptz)
|
||||||
AND (sqlc.narg('range_end')::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) < sqlc.narg('range_end')::timestamptz)
|
AND (sqlc.narg('range_end')::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) < sqlc.narg('range_end')::timestamptz)
|
||||||
|
AND (
|
||||||
|
sqlc.narg('payment_method')::text IS NULL
|
||||||
|
OR UPPER(COALESCE(p.payment_method, '')) = UPPER(sqlc.narg('payment_method')::text)
|
||||||
|
OR UPPER(COALESCE(p.payment_method, '')) LIKE UPPER(sqlc.narg('payment_method')::text) || ':%'
|
||||||
|
)
|
||||||
GROUP BY p.payment_method
|
GROUP BY p.payment_method
|
||||||
ORDER BY count DESC;
|
ORDER BY count DESC;
|
||||||
|
|
||||||
|
|
@ -511,40 +539,50 @@ FROM generate_series(
|
||||||
sqlc.arg('series_end')::date,
|
sqlc.arg('series_end')::date,
|
||||||
INTERVAL '1 day'
|
INTERVAL '1 day'
|
||||||
) AS d(date)
|
) AS d(date)
|
||||||
LEFT JOIN payments p ON COALESCE(p.paid_at, p.created_at)::date = d.date
|
LEFT JOIN payments p ON (COALESCE(p.paid_at, p.created_at) AT TIME ZONE 'Africa/Addis_Ababa')::date = d.date
|
||||||
AND p.status = 'SUCCESS'
|
AND p.status = 'SUCCESS'
|
||||||
AND (sqlc.narg('range_start')::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) >= sqlc.narg('range_start')::timestamptz)
|
AND (sqlc.narg('range_start')::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) >= sqlc.narg('range_start')::timestamptz)
|
||||||
AND (sqlc.narg('range_end')::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) < sqlc.narg('range_end')::timestamptz)
|
AND (sqlc.narg('range_end')::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) < sqlc.narg('range_end')::timestamptz)
|
||||||
|
AND (
|
||||||
|
sqlc.narg('payment_method')::text IS NULL
|
||||||
|
OR UPPER(COALESCE(p.payment_method, '')) = UPPER(sqlc.narg('payment_method')::text)
|
||||||
|
OR UPPER(COALESCE(p.payment_method, '')) LIKE UPPER(sqlc.narg('payment_method')::text) || ':%'
|
||||||
|
)
|
||||||
GROUP BY d.date
|
GROUP BY d.date
|
||||||
ORDER BY d.date;
|
ORDER BY d.date;
|
||||||
|
|
||||||
-- Monthly successful revenue for a calendar year (UTC buckets). Months with multiple currencies emit one row each; months with no revenue emit one row (currency ETB, revenue 0).
|
-- Monthly successful revenue for a calendar year (EAT buckets). Months with multiple currencies emit one row each; months with no revenue emit one row (currency ETB, revenue 0).
|
||||||
-- name: AnalyticsRevenueMonthlyByYear :many
|
-- name: AnalyticsRevenueMonthlyByYear :many
|
||||||
WITH months AS (
|
WITH months AS (
|
||||||
SELECT bucket
|
SELECT bucket
|
||||||
FROM generate_series(
|
FROM generate_series(
|
||||||
make_timestamptz(sqlc.arg('report_year')::int, 1, 1, 0, 0, 0, 'UTC'),
|
make_timestamptz(sqlc.arg('report_year')::int, 1, 1, 0, 0, 0, 'Africa/Addis_Ababa'),
|
||||||
make_timestamptz(sqlc.arg('report_year')::int, 12, 1, 0, 0, 0, 'UTC'),
|
make_timestamptz(sqlc.arg('report_year')::int, 12, 1, 0, 0, 0, 'Africa/Addis_Ababa'),
|
||||||
INTERVAL '1 month'
|
INTERVAL '1 month'
|
||||||
) AS gs(bucket)
|
) AS gs(bucket)
|
||||||
),
|
),
|
||||||
by_month_currency AS (
|
by_month_currency AS (
|
||||||
SELECT
|
SELECT
|
||||||
date_trunc('month', COALESCE(p.paid_at, p.created_at) AT TIME ZONE 'UTC') AS ym,
|
date_trunc('month', COALESCE(p.paid_at, p.created_at) AT TIME ZONE 'Africa/Addis_Ababa') AS ym,
|
||||||
p.currency,
|
p.currency,
|
||||||
SUM(p.amount)::float8 AS total_revenue
|
SUM(p.amount)::float8 AS total_revenue
|
||||||
FROM payments p
|
FROM payments p
|
||||||
WHERE p.status = 'SUCCESS'
|
WHERE p.status = 'SUCCESS'
|
||||||
AND EXTRACT(YEAR FROM COALESCE(p.paid_at, p.created_at) AT TIME ZONE 'UTC')::int = sqlc.arg('report_year')::int
|
AND EXTRACT(YEAR FROM COALESCE(p.paid_at, p.created_at) AT TIME ZONE 'Africa/Addis_Ababa')::int = sqlc.arg('report_year')::int
|
||||||
|
AND (
|
||||||
|
sqlc.narg('payment_method')::text IS NULL
|
||||||
|
OR UPPER(COALESCE(p.payment_method, '')) = UPPER(sqlc.narg('payment_method')::text)
|
||||||
|
OR UPPER(COALESCE(p.payment_method, '')) LIKE UPPER(sqlc.narg('payment_method')::text) || ':%'
|
||||||
|
)
|
||||||
GROUP BY 1, 2
|
GROUP BY 1, 2
|
||||||
)
|
)
|
||||||
SELECT
|
SELECT
|
||||||
(EXTRACT(MONTH FROM m.bucket AT TIME ZONE 'UTC'))::int AS month,
|
(EXTRACT(MONTH FROM m.bucket AT TIME ZONE 'Africa/Addis_Ababa'))::int AS month,
|
||||||
date_trunc('month', m.bucket AT TIME ZONE 'UTC')::date AS month_start,
|
date_trunc('month', m.bucket AT TIME ZONE 'Africa/Addis_Ababa')::date AS month_start,
|
||||||
COALESCE(b.currency, 'ETB'::varchar) AS currency,
|
COALESCE(b.currency, 'ETB'::varchar) AS currency,
|
||||||
COALESCE(b.total_revenue, 0)::float8 AS total_revenue
|
COALESCE(b.total_revenue, 0)::float8 AS total_revenue
|
||||||
FROM months m
|
FROM months m
|
||||||
LEFT JOIN by_month_currency b ON b.ym = date_trunc('month', m.bucket AT TIME ZONE 'UTC')
|
LEFT JOIN by_month_currency b ON b.ym = date_trunc('month', m.bucket AT TIME ZONE 'Africa/Addis_Ababa')
|
||||||
ORDER BY m.bucket, COALESCE(b.currency, ''::varchar);
|
ORDER BY m.bucket, COALESCE(b.currency, ''::varchar);
|
||||||
|
|
||||||
-- =====================
|
-- =====================
|
||||||
|
|
|
||||||
|
|
@ -330,7 +330,7 @@ FROM generate_series(
|
||||||
$2::date,
|
$2::date,
|
||||||
INTERVAL '1 day'
|
INTERVAL '1 day'
|
||||||
) AS d(date)
|
) AS d(date)
|
||||||
LEFT JOIN user_subscriptions us ON us.created_at::date = d.date
|
LEFT JOIN user_subscriptions us ON (us.created_at AT TIME ZONE 'Africa/Addis_Ababa')::date = d.date
|
||||||
AND us.status = 'ACTIVE'
|
AND us.status = 'ACTIVE'
|
||||||
AND ($3::timestamptz IS NULL OR us.created_at >= $3::timestamptz)
|
AND ($3::timestamptz IS NULL OR us.created_at >= $3::timestamptz)
|
||||||
AND ($4::timestamptz IS NULL OR us.created_at < $4::timestamptz)
|
AND ($4::timestamptz IS NULL OR us.created_at < $4::timestamptz)
|
||||||
|
|
@ -498,6 +498,11 @@ FROM payments p
|
||||||
WHERE p.status = 'SUCCESS'
|
WHERE p.status = 'SUCCESS'
|
||||||
AND ($1::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) >= $1::timestamptz)
|
AND ($1::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) >= $1::timestamptz)
|
||||||
AND ($2::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) < $2::timestamptz)
|
AND ($2::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) < $2::timestamptz)
|
||||||
|
AND (
|
||||||
|
$3::text IS NULL
|
||||||
|
OR UPPER(COALESCE(p.payment_method, '')) = UPPER($3::text)
|
||||||
|
OR UPPER(COALESCE(p.payment_method, '')) LIKE UPPER($3::text) || ':%'
|
||||||
|
)
|
||||||
GROUP BY p.payment_method
|
GROUP BY p.payment_method
|
||||||
ORDER BY count DESC
|
ORDER BY count DESC
|
||||||
`
|
`
|
||||||
|
|
@ -505,6 +510,7 @@ ORDER BY count DESC
|
||||||
type AnalyticsPaymentsByMethodParams struct {
|
type AnalyticsPaymentsByMethodParams struct {
|
||||||
RangeStart pgtype.Timestamptz `json:"range_start"`
|
RangeStart pgtype.Timestamptz `json:"range_start"`
|
||||||
RangeEnd pgtype.Timestamptz `json:"range_end"`
|
RangeEnd pgtype.Timestamptz `json:"range_end"`
|
||||||
|
PaymentMethod pgtype.Text `json:"payment_method"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type AnalyticsPaymentsByMethodRow struct {
|
type AnalyticsPaymentsByMethodRow struct {
|
||||||
|
|
@ -514,7 +520,7 @@ type AnalyticsPaymentsByMethodRow struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) AnalyticsPaymentsByMethod(ctx context.Context, arg AnalyticsPaymentsByMethodParams) ([]AnalyticsPaymentsByMethodRow, error) {
|
func (q *Queries) AnalyticsPaymentsByMethod(ctx context.Context, arg AnalyticsPaymentsByMethodParams) ([]AnalyticsPaymentsByMethodRow, error) {
|
||||||
rows, err := q.db.Query(ctx, AnalyticsPaymentsByMethod, arg.RangeStart, arg.RangeEnd)
|
rows, err := q.db.Query(ctx, AnalyticsPaymentsByMethod, arg.RangeStart, arg.RangeEnd, arg.PaymentMethod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -541,6 +547,11 @@ SELECT
|
||||||
FROM payments p
|
FROM payments p
|
||||||
WHERE ($1::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) >= $1::timestamptz)
|
WHERE ($1::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) >= $1::timestamptz)
|
||||||
AND ($2::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) < $2::timestamptz)
|
AND ($2::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) < $2::timestamptz)
|
||||||
|
AND (
|
||||||
|
$3::text IS NULL
|
||||||
|
OR UPPER(COALESCE(p.payment_method, '')) = UPPER($3::text)
|
||||||
|
OR UPPER(COALESCE(p.payment_method, '')) LIKE UPPER($3::text) || ':%'
|
||||||
|
)
|
||||||
GROUP BY p.status
|
GROUP BY p.status
|
||||||
ORDER BY count DESC
|
ORDER BY count DESC
|
||||||
`
|
`
|
||||||
|
|
@ -548,6 +559,7 @@ ORDER BY count DESC
|
||||||
type AnalyticsPaymentsByStatusParams struct {
|
type AnalyticsPaymentsByStatusParams struct {
|
||||||
RangeStart pgtype.Timestamptz `json:"range_start"`
|
RangeStart pgtype.Timestamptz `json:"range_start"`
|
||||||
RangeEnd pgtype.Timestamptz `json:"range_end"`
|
RangeEnd pgtype.Timestamptz `json:"range_end"`
|
||||||
|
PaymentMethod pgtype.Text `json:"payment_method"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type AnalyticsPaymentsByStatusRow struct {
|
type AnalyticsPaymentsByStatusRow struct {
|
||||||
|
|
@ -557,7 +569,7 @@ type AnalyticsPaymentsByStatusRow struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) AnalyticsPaymentsByStatus(ctx context.Context, arg AnalyticsPaymentsByStatusParams) ([]AnalyticsPaymentsByStatusRow, error) {
|
func (q *Queries) AnalyticsPaymentsByStatus(ctx context.Context, arg AnalyticsPaymentsByStatusParams) ([]AnalyticsPaymentsByStatusRow, error) {
|
||||||
rows, err := q.db.Query(ctx, AnalyticsPaymentsByStatus, arg.RangeStart, arg.RangeEnd)
|
rows, err := q.db.Query(ctx, AnalyticsPaymentsByStatus, arg.RangeStart, arg.RangeEnd, arg.PaymentMethod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -586,11 +598,17 @@ SELECT
|
||||||
FROM payments p
|
FROM payments p
|
||||||
WHERE ($1::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) >= $1::timestamptz)
|
WHERE ($1::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) >= $1::timestamptz)
|
||||||
AND ($2::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) < $2::timestamptz)
|
AND ($2::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) < $2::timestamptz)
|
||||||
|
AND (
|
||||||
|
$3::text IS NULL
|
||||||
|
OR UPPER(COALESCE(p.payment_method, '')) = UPPER($3::text)
|
||||||
|
OR UPPER(COALESCE(p.payment_method, '')) LIKE UPPER($3::text) || ':%'
|
||||||
|
)
|
||||||
`
|
`
|
||||||
|
|
||||||
type AnalyticsPaymentsSummaryParams struct {
|
type AnalyticsPaymentsSummaryParams struct {
|
||||||
RangeStart pgtype.Timestamptz `json:"range_start"`
|
RangeStart pgtype.Timestamptz `json:"range_start"`
|
||||||
RangeEnd pgtype.Timestamptz `json:"range_end"`
|
RangeEnd pgtype.Timestamptz `json:"range_end"`
|
||||||
|
PaymentMethod pgtype.Text `json:"payment_method"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type AnalyticsPaymentsSummaryRow struct {
|
type AnalyticsPaymentsSummaryRow struct {
|
||||||
|
|
@ -604,7 +622,7 @@ type AnalyticsPaymentsSummaryRow struct {
|
||||||
// Payment Analytics
|
// Payment Analytics
|
||||||
// =====================
|
// =====================
|
||||||
func (q *Queries) AnalyticsPaymentsSummary(ctx context.Context, arg AnalyticsPaymentsSummaryParams) (AnalyticsPaymentsSummaryRow, error) {
|
func (q *Queries) AnalyticsPaymentsSummary(ctx context.Context, arg AnalyticsPaymentsSummaryParams) (AnalyticsPaymentsSummaryRow, error) {
|
||||||
row := q.db.QueryRow(ctx, AnalyticsPaymentsSummary, arg.RangeStart, arg.RangeEnd)
|
row := q.db.QueryRow(ctx, AnalyticsPaymentsSummary, arg.RangeStart, arg.RangeEnd, arg.PaymentMethod)
|
||||||
var i AnalyticsPaymentsSummaryRow
|
var i AnalyticsPaymentsSummaryRow
|
||||||
err := row.Scan(
|
err := row.Scan(
|
||||||
&i.TotalRevenue,
|
&i.TotalRevenue,
|
||||||
|
|
@ -743,7 +761,7 @@ FROM generate_series(
|
||||||
$2::date,
|
$2::date,
|
||||||
INTERVAL '1 day'
|
INTERVAL '1 day'
|
||||||
) AS d(date)
|
) AS d(date)
|
||||||
LEFT JOIN user_subscriptions us ON us.created_at::date = d.date
|
LEFT JOIN user_subscriptions us ON (us.created_at AT TIME ZONE 'Africa/Addis_Ababa')::date = d.date
|
||||||
AND ($3::timestamptz IS NULL OR us.created_at >= $3::timestamptz)
|
AND ($3::timestamptz IS NULL OR us.created_at >= $3::timestamptz)
|
||||||
AND ($4::timestamptz IS NULL OR us.created_at < $4::timestamptz)
|
AND ($4::timestamptz IS NULL OR us.created_at < $4::timestamptz)
|
||||||
AND EXISTS (
|
AND EXISTS (
|
||||||
|
|
@ -809,6 +827,11 @@ JOIN subscription_plans sp ON sp.id = p.plan_id
|
||||||
WHERE p.status = 'SUCCESS'
|
WHERE p.status = 'SUCCESS'
|
||||||
AND ($1::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) >= $1::timestamptz)
|
AND ($1::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) >= $1::timestamptz)
|
||||||
AND ($2::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) < $2::timestamptz)
|
AND ($2::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) < $2::timestamptz)
|
||||||
|
AND (
|
||||||
|
$3::text IS NULL
|
||||||
|
OR UPPER(COALESCE(p.payment_method, '')) = UPPER($3::text)
|
||||||
|
OR UPPER(COALESCE(p.payment_method, '')) LIKE UPPER($3::text) || ':%'
|
||||||
|
)
|
||||||
GROUP BY sp.name, sp.currency
|
GROUP BY sp.name, sp.currency
|
||||||
ORDER BY total_revenue DESC
|
ORDER BY total_revenue DESC
|
||||||
`
|
`
|
||||||
|
|
@ -816,6 +839,7 @@ ORDER BY total_revenue DESC
|
||||||
type AnalyticsRevenueByPlanParams struct {
|
type AnalyticsRevenueByPlanParams struct {
|
||||||
RangeStart pgtype.Timestamptz `json:"range_start"`
|
RangeStart pgtype.Timestamptz `json:"range_start"`
|
||||||
RangeEnd pgtype.Timestamptz `json:"range_end"`
|
RangeEnd pgtype.Timestamptz `json:"range_end"`
|
||||||
|
PaymentMethod pgtype.Text `json:"payment_method"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type AnalyticsRevenueByPlanRow struct {
|
type AnalyticsRevenueByPlanRow struct {
|
||||||
|
|
@ -826,7 +850,7 @@ type AnalyticsRevenueByPlanRow struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) AnalyticsRevenueByPlan(ctx context.Context, arg AnalyticsRevenueByPlanParams) ([]AnalyticsRevenueByPlanRow, error) {
|
func (q *Queries) AnalyticsRevenueByPlan(ctx context.Context, arg AnalyticsRevenueByPlanParams) ([]AnalyticsRevenueByPlanRow, error) {
|
||||||
rows, err := q.db.Query(ctx, AnalyticsRevenueByPlan, arg.RangeStart, arg.RangeEnd)
|
rows, err := q.db.Query(ctx, AnalyticsRevenueByPlan, arg.RangeStart, arg.RangeEnd, arg.PaymentMethod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -860,6 +884,11 @@ JOIN subscription_plans sp ON sp.id = p.plan_id
|
||||||
WHERE p.status = 'SUCCESS'
|
WHERE p.status = 'SUCCESS'
|
||||||
AND ($1::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) >= $1::timestamptz)
|
AND ($1::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) >= $1::timestamptz)
|
||||||
AND ($2::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) < $2::timestamptz)
|
AND ($2::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) < $2::timestamptz)
|
||||||
|
AND (
|
||||||
|
$3::text IS NULL
|
||||||
|
OR UPPER(COALESCE(p.payment_method, '')) = UPPER($3::text)
|
||||||
|
OR UPPER(COALESCE(p.payment_method, '')) LIKE UPPER($3::text) || ':%'
|
||||||
|
)
|
||||||
GROUP BY sp.category
|
GROUP BY sp.category
|
||||||
ORDER BY amount DESC
|
ORDER BY amount DESC
|
||||||
`
|
`
|
||||||
|
|
@ -867,6 +896,7 @@ ORDER BY amount DESC
|
||||||
type AnalyticsRevenueByPlanCategoryParams struct {
|
type AnalyticsRevenueByPlanCategoryParams struct {
|
||||||
RangeStart pgtype.Timestamptz `json:"range_start"`
|
RangeStart pgtype.Timestamptz `json:"range_start"`
|
||||||
RangeEnd pgtype.Timestamptz `json:"range_end"`
|
RangeEnd pgtype.Timestamptz `json:"range_end"`
|
||||||
|
PaymentMethod pgtype.Text `json:"payment_method"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type AnalyticsRevenueByPlanCategoryRow struct {
|
type AnalyticsRevenueByPlanCategoryRow struct {
|
||||||
|
|
@ -876,7 +906,7 @@ type AnalyticsRevenueByPlanCategoryRow struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) AnalyticsRevenueByPlanCategory(ctx context.Context, arg AnalyticsRevenueByPlanCategoryParams) ([]AnalyticsRevenueByPlanCategoryRow, error) {
|
func (q *Queries) AnalyticsRevenueByPlanCategory(ctx context.Context, arg AnalyticsRevenueByPlanCategoryParams) ([]AnalyticsRevenueByPlanCategoryRow, error) {
|
||||||
rows, err := q.db.Query(ctx, AnalyticsRevenueByPlanCategory, arg.RangeStart, arg.RangeEnd)
|
rows, err := q.db.Query(ctx, AnalyticsRevenueByPlanCategory, arg.RangeStart, arg.RangeEnd, arg.PaymentMethod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -904,10 +934,15 @@ FROM generate_series(
|
||||||
$2::date,
|
$2::date,
|
||||||
INTERVAL '1 day'
|
INTERVAL '1 day'
|
||||||
) AS d(date)
|
) AS d(date)
|
||||||
LEFT JOIN payments p ON COALESCE(p.paid_at, p.created_at)::date = d.date
|
LEFT JOIN payments p ON (COALESCE(p.paid_at, p.created_at) AT TIME ZONE 'Africa/Addis_Ababa')::date = d.date
|
||||||
AND p.status = 'SUCCESS'
|
AND p.status = 'SUCCESS'
|
||||||
AND ($3::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) >= $3::timestamptz)
|
AND ($3::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) >= $3::timestamptz)
|
||||||
AND ($4::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) < $4::timestamptz)
|
AND ($4::timestamptz IS NULL OR COALESCE(p.paid_at, p.created_at) < $4::timestamptz)
|
||||||
|
AND (
|
||||||
|
$5::text IS NULL
|
||||||
|
OR UPPER(COALESCE(p.payment_method, '')) = UPPER($5::text)
|
||||||
|
OR UPPER(COALESCE(p.payment_method, '')) LIKE UPPER($5::text) || ':%'
|
||||||
|
)
|
||||||
GROUP BY d.date
|
GROUP BY d.date
|
||||||
ORDER BY d.date
|
ORDER BY d.date
|
||||||
`
|
`
|
||||||
|
|
@ -917,6 +952,7 @@ type AnalyticsRevenueLast30DaysParams struct {
|
||||||
SeriesEnd pgtype.Date `json:"series_end"`
|
SeriesEnd pgtype.Date `json:"series_end"`
|
||||||
RangeStart pgtype.Timestamptz `json:"range_start"`
|
RangeStart pgtype.Timestamptz `json:"range_start"`
|
||||||
RangeEnd pgtype.Timestamptz `json:"range_end"`
|
RangeEnd pgtype.Timestamptz `json:"range_end"`
|
||||||
|
PaymentMethod pgtype.Text `json:"payment_method"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type AnalyticsRevenueLast30DaysRow struct {
|
type AnalyticsRevenueLast30DaysRow struct {
|
||||||
|
|
@ -930,6 +966,7 @@ func (q *Queries) AnalyticsRevenueLast30Days(ctx context.Context, arg AnalyticsR
|
||||||
arg.SeriesEnd,
|
arg.SeriesEnd,
|
||||||
arg.RangeStart,
|
arg.RangeStart,
|
||||||
arg.RangeEnd,
|
arg.RangeEnd,
|
||||||
|
arg.PaymentMethod,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -953,31 +990,41 @@ const AnalyticsRevenueMonthlyByYear = `-- name: AnalyticsRevenueMonthlyByYear :m
|
||||||
WITH months AS (
|
WITH months AS (
|
||||||
SELECT bucket
|
SELECT bucket
|
||||||
FROM generate_series(
|
FROM generate_series(
|
||||||
make_timestamptz($1::int, 1, 1, 0, 0, 0, 'UTC'),
|
make_timestamptz($1::int, 1, 1, 0, 0, 0, 'Africa/Addis_Ababa'),
|
||||||
make_timestamptz($1::int, 12, 1, 0, 0, 0, 'UTC'),
|
make_timestamptz($1::int, 12, 1, 0, 0, 0, 'Africa/Addis_Ababa'),
|
||||||
INTERVAL '1 month'
|
INTERVAL '1 month'
|
||||||
) AS gs(bucket)
|
) AS gs(bucket)
|
||||||
),
|
),
|
||||||
by_month_currency AS (
|
by_month_currency AS (
|
||||||
SELECT
|
SELECT
|
||||||
date_trunc('month', COALESCE(p.paid_at, p.created_at) AT TIME ZONE 'UTC') AS ym,
|
date_trunc('month', COALESCE(p.paid_at, p.created_at) AT TIME ZONE 'Africa/Addis_Ababa') AS ym,
|
||||||
p.currency,
|
p.currency,
|
||||||
SUM(p.amount)::float8 AS total_revenue
|
SUM(p.amount)::float8 AS total_revenue
|
||||||
FROM payments p
|
FROM payments p
|
||||||
WHERE p.status = 'SUCCESS'
|
WHERE p.status = 'SUCCESS'
|
||||||
AND EXTRACT(YEAR FROM COALESCE(p.paid_at, p.created_at) AT TIME ZONE 'UTC')::int = $1::int
|
AND EXTRACT(YEAR FROM COALESCE(p.paid_at, p.created_at) AT TIME ZONE 'Africa/Addis_Ababa')::int = $1::int
|
||||||
|
AND (
|
||||||
|
$2::text IS NULL
|
||||||
|
OR UPPER(COALESCE(p.payment_method, '')) = UPPER($2::text)
|
||||||
|
OR UPPER(COALESCE(p.payment_method, '')) LIKE UPPER($2::text) || ':%'
|
||||||
|
)
|
||||||
GROUP BY 1, 2
|
GROUP BY 1, 2
|
||||||
)
|
)
|
||||||
SELECT
|
SELECT
|
||||||
(EXTRACT(MONTH FROM m.bucket AT TIME ZONE 'UTC'))::int AS month,
|
(EXTRACT(MONTH FROM m.bucket AT TIME ZONE 'Africa/Addis_Ababa'))::int AS month,
|
||||||
date_trunc('month', m.bucket AT TIME ZONE 'UTC')::date AS month_start,
|
date_trunc('month', m.bucket AT TIME ZONE 'Africa/Addis_Ababa')::date AS month_start,
|
||||||
COALESCE(b.currency, 'ETB'::varchar) AS currency,
|
COALESCE(b.currency, 'ETB'::varchar) AS currency,
|
||||||
COALESCE(b.total_revenue, 0)::float8 AS total_revenue
|
COALESCE(b.total_revenue, 0)::float8 AS total_revenue
|
||||||
FROM months m
|
FROM months m
|
||||||
LEFT JOIN by_month_currency b ON b.ym = date_trunc('month', m.bucket AT TIME ZONE 'UTC')
|
LEFT JOIN by_month_currency b ON b.ym = date_trunc('month', m.bucket AT TIME ZONE 'Africa/Addis_Ababa')
|
||||||
ORDER BY m.bucket, COALESCE(b.currency, ''::varchar)
|
ORDER BY m.bucket, COALESCE(b.currency, ''::varchar)
|
||||||
`
|
`
|
||||||
|
|
||||||
|
type AnalyticsRevenueMonthlyByYearParams struct {
|
||||||
|
ReportYear int32 `json:"report_year"`
|
||||||
|
PaymentMethod pgtype.Text `json:"payment_method"`
|
||||||
|
}
|
||||||
|
|
||||||
type AnalyticsRevenueMonthlyByYearRow struct {
|
type AnalyticsRevenueMonthlyByYearRow struct {
|
||||||
Month int32 `json:"month"`
|
Month int32 `json:"month"`
|
||||||
MonthStart pgtype.Date `json:"month_start"`
|
MonthStart pgtype.Date `json:"month_start"`
|
||||||
|
|
@ -985,9 +1032,9 @@ type AnalyticsRevenueMonthlyByYearRow struct {
|
||||||
TotalRevenue float64 `json:"total_revenue"`
|
TotalRevenue float64 `json:"total_revenue"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Monthly successful revenue for a calendar year (UTC buckets). Months with multiple currencies emit one row each; months with no revenue emit one row (currency ETB, revenue 0).
|
// Monthly successful revenue for a calendar year (EAT buckets). Months with multiple currencies emit one row each; months with no revenue emit one row (currency ETB, revenue 0).
|
||||||
func (q *Queries) AnalyticsRevenueMonthlyByYear(ctx context.Context, reportYear int32) ([]AnalyticsRevenueMonthlyByYearRow, error) {
|
func (q *Queries) AnalyticsRevenueMonthlyByYear(ctx context.Context, arg AnalyticsRevenueMonthlyByYearParams) ([]AnalyticsRevenueMonthlyByYearRow, error) {
|
||||||
rows, err := q.db.Query(ctx, AnalyticsRevenueMonthlyByYear, reportYear)
|
rows, err := q.db.Query(ctx, AnalyticsRevenueMonthlyByYear, arg.ReportYear, arg.PaymentMethod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -1067,7 +1114,7 @@ FROM generate_series(
|
||||||
$2::date,
|
$2::date,
|
||||||
INTERVAL '1 day'
|
INTERVAL '1 day'
|
||||||
) AS d(date)
|
) AS d(date)
|
||||||
LEFT JOIN user_subscriptions us ON us.cancelled_at::date = d.date
|
LEFT JOIN user_subscriptions us ON (us.cancelled_at AT TIME ZONE 'Africa/Addis_Ababa')::date = d.date
|
||||||
AND ($3::timestamptz IS NULL OR us.cancelled_at >= $3::timestamptz)
|
AND ($3::timestamptz IS NULL OR us.cancelled_at >= $3::timestamptz)
|
||||||
AND ($4::timestamptz IS NULL OR us.cancelled_at < $4::timestamptz)
|
AND ($4::timestamptz IS NULL OR us.cancelled_at < $4::timestamptz)
|
||||||
GROUP BY d.date
|
GROUP BY d.date
|
||||||
|
|
@ -1120,7 +1167,7 @@ FROM generate_series(
|
||||||
$2::date,
|
$2::date,
|
||||||
INTERVAL '1 day'
|
INTERVAL '1 day'
|
||||||
) AS d(date)
|
) AS d(date)
|
||||||
LEFT JOIN user_subscriptions us ON us.expires_at::date = d.date
|
LEFT JOIN user_subscriptions us ON (us.expires_at AT TIME ZONE 'Africa/Addis_Ababa')::date = d.date
|
||||||
AND ($3::timestamptz IS NULL OR us.expires_at >= $3::timestamptz)
|
AND ($3::timestamptz IS NULL OR us.expires_at >= $3::timestamptz)
|
||||||
AND ($4::timestamptz IS NULL OR us.expires_at < $4::timestamptz)
|
AND ($4::timestamptz IS NULL OR us.expires_at < $4::timestamptz)
|
||||||
AND EXISTS (
|
AND EXISTS (
|
||||||
|
|
@ -1413,17 +1460,17 @@ SELECT
|
||||||
COUNT(*) FILTER (WHERE us.status = 'ACTIVE')::bigint AS active,
|
COUNT(*) FILTER (WHERE us.status = 'ACTIVE')::bigint AS active,
|
||||||
COUNT(*) FILTER (
|
COUNT(*) FILTER (
|
||||||
WHERE us.status = 'ACTIVE'
|
WHERE us.status = 'ACTIVE'
|
||||||
AND us.created_at::date = $1::date
|
AND (us.created_at AT TIME ZONE 'Africa/Addis_Ababa')::date = $1::date
|
||||||
)::bigint AS new_today,
|
)::bigint AS new_today,
|
||||||
COUNT(*) FILTER (
|
COUNT(*) FILTER (
|
||||||
WHERE us.status = 'ACTIVE'
|
WHERE us.status = 'ACTIVE'
|
||||||
AND us.created_at::date >= $1::date - INTERVAL '6 days'
|
AND (us.created_at AT TIME ZONE 'Africa/Addis_Ababa')::date >= $1::date - INTERVAL '6 days'
|
||||||
AND us.created_at::date <= $1::date
|
AND (us.created_at AT TIME ZONE 'Africa/Addis_Ababa')::date <= $1::date
|
||||||
)::bigint AS new_this_week,
|
)::bigint AS new_this_week,
|
||||||
COUNT(*) FILTER (
|
COUNT(*) FILTER (
|
||||||
WHERE us.status = 'ACTIVE'
|
WHERE us.status = 'ACTIVE'
|
||||||
AND us.created_at::date >= $1::date - INTERVAL '29 days'
|
AND (us.created_at AT TIME ZONE 'Africa/Addis_Ababa')::date >= $1::date - INTERVAL '29 days'
|
||||||
AND us.created_at::date <= $1::date
|
AND (us.created_at AT TIME ZONE 'Africa/Addis_Ababa')::date <= $1::date
|
||||||
)::bigint AS new_this_month
|
)::bigint AS new_this_month
|
||||||
FROM user_subscriptions us
|
FROM user_subscriptions us
|
||||||
WHERE ($2::timestamptz IS NULL OR us.created_at >= $2::timestamptz)
|
WHERE ($2::timestamptz IS NULL OR us.created_at >= $2::timestamptz)
|
||||||
|
|
@ -1575,7 +1622,7 @@ FROM generate_series(
|
||||||
$2::date,
|
$2::date,
|
||||||
INTERVAL '1 day'
|
INTERVAL '1 day'
|
||||||
) AS d(date)
|
) AS d(date)
|
||||||
LEFT JOIN users u ON u.created_at::date = d.date
|
LEFT JOIN users u ON (u.created_at AT TIME ZONE 'Africa/Addis_Ababa')::date = d.date
|
||||||
AND ($3::timestamptz IS NULL OR u.created_at >= $3::timestamptz)
|
AND ($3::timestamptz IS NULL OR u.created_at >= $3::timestamptz)
|
||||||
AND ($4::timestamptz IS NULL OR u.created_at < $4::timestamptz)
|
AND ($4::timestamptz IS NULL OR u.created_at < $4::timestamptz)
|
||||||
GROUP BY d.date
|
GROUP BY d.date
|
||||||
|
|
@ -2034,14 +2081,14 @@ const AnalyticsUsersSummary = `-- name: AnalyticsUsersSummary :one
|
||||||
|
|
||||||
SELECT
|
SELECT
|
||||||
COUNT(*)::bigint AS total,
|
COUNT(*)::bigint AS total,
|
||||||
COUNT(*) FILTER (WHERE u.created_at::date = $1::date)::bigint AS new_today,
|
COUNT(*) FILTER (WHERE (u.created_at AT TIME ZONE 'Africa/Addis_Ababa')::date = $1::date)::bigint AS new_today,
|
||||||
COUNT(*) FILTER (
|
COUNT(*) FILTER (
|
||||||
WHERE u.created_at::date >= $1::date - INTERVAL '6 days'
|
WHERE (u.created_at AT TIME ZONE 'Africa/Addis_Ababa')::date >= $1::date - INTERVAL '6 days'
|
||||||
AND u.created_at::date <= $1::date
|
AND (u.created_at AT TIME ZONE 'Africa/Addis_Ababa')::date <= $1::date
|
||||||
)::bigint AS new_this_week,
|
)::bigint AS new_this_week,
|
||||||
COUNT(*) FILTER (
|
COUNT(*) FILTER (
|
||||||
WHERE u.created_at::date >= $1::date - INTERVAL '29 days'
|
WHERE (u.created_at AT TIME ZONE 'Africa/Addis_Ababa')::date >= $1::date - INTERVAL '29 days'
|
||||||
AND u.created_at::date <= $1::date
|
AND (u.created_at AT TIME ZONE 'Africa/Addis_Ababa')::date <= $1::date
|
||||||
)::bigint AS new_this_month
|
)::bigint AS new_this_month
|
||||||
FROM users u
|
FROM users u
|
||||||
WHERE ($2::timestamptz IS NULL OR u.created_at >= $2::timestamptz)
|
WHERE ($2::timestamptz IS NULL OR u.created_at >= $2::timestamptz)
|
||||||
|
|
@ -2064,17 +2111,22 @@ type AnalyticsUsersSummaryRow struct {
|
||||||
// =====================
|
// =====================
|
||||||
// Analytics (date-filtered)
|
// Analytics (date-filtered)
|
||||||
// =====================
|
// =====================
|
||||||
|
// Calendar days / months use Africa/Addis_Ababa (EAT, UTC+3).
|
||||||
// Shared optional params (nullable = all-time):
|
// Shared optional params (nullable = all-time):
|
||||||
//
|
//
|
||||||
// range_start, range_end (exclusive upper bound)
|
// range_start, range_end (exclusive upper bound, timestamptz)
|
||||||
//
|
//
|
||||||
// Required chart params:
|
// Required chart params:
|
||||||
//
|
//
|
||||||
// series_start, series_end (inclusive dates)
|
// series_start, series_end (inclusive EAT calendar dates)
|
||||||
//
|
//
|
||||||
// Relative window anchor:
|
// Relative window anchor:
|
||||||
//
|
//
|
||||||
// ref_date (inclusive date used for new_today/week/month)
|
// ref_date (inclusive EAT date used for new_today/week/month)
|
||||||
|
//
|
||||||
|
// Payment/revenue queries also accept optional:
|
||||||
|
//
|
||||||
|
// payment_method (exact match, or METHOD:% prefix variants)
|
||||||
//
|
//
|
||||||
// =====================
|
// =====================
|
||||||
// User Analytics
|
// User Analytics
|
||||||
|
|
|
||||||
|
|
@ -665,6 +665,14 @@ type UserAudioResponse struct {
|
||||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UserInitialAssessmentProgress struct {
|
||||||
|
UserID int64 `json:"user_id"`
|
||||||
|
NextLevel string `json:"next_level"`
|
||||||
|
HighestPassedLevel pgtype.Text `json:"highest_passed_level"`
|
||||||
|
Completed bool `json:"completed"`
|
||||||
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||||
|
}
|
||||||
|
|
||||||
type UserPracticeProgress struct {
|
type UserPracticeProgress struct {
|
||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
UserID int64 `json:"user_id"`
|
UserID int64 `json:"user_id"`
|
||||||
|
|
|
||||||
|
|
@ -137,6 +137,7 @@ SELECT id, title, description, set_type, owner_type, owner_id, banner_image, per
|
||||||
FROM question_sets
|
FROM question_sets
|
||||||
WHERE set_type = 'INITIAL_ASSESSMENT'
|
WHERE set_type = 'INITIAL_ASSESSMENT'
|
||||||
AND status = 'PUBLISHED'
|
AND status = 'PUBLISHED'
|
||||||
|
AND UPPER(TRIM(COALESCE(description, ''))) = 'A1'
|
||||||
ORDER BY created_at DESC
|
ORDER BY created_at DESC
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
`
|
`
|
||||||
|
|
@ -165,6 +166,45 @@ func (q *Queries) GetInitialAssessmentSet(ctx context.Context) (QuestionSet, err
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const GetInitialAssessmentSetByLevel = `-- name: GetInitialAssessmentSetByLevel :one
|
||||||
|
SELECT id, title, description, set_type, owner_type, owner_id, banner_image, persona, time_limit_minutes, passing_score, shuffle_questions, status, created_at, updated_at, display_order, intro_video_url
|
||||||
|
FROM question_sets
|
||||||
|
WHERE set_type = 'INITIAL_ASSESSMENT'
|
||||||
|
AND status = 'PUBLISHED'
|
||||||
|
AND UPPER(TRIM(COALESCE(description, ''))) = $1::text
|
||||||
|
ORDER BY
|
||||||
|
CASE
|
||||||
|
WHEN owner_type IS NULL OR UPPER(TRIM(owner_type)) = 'STANDALONE' THEN 0
|
||||||
|
ELSE 1
|
||||||
|
END,
|
||||||
|
created_at DESC
|
||||||
|
LIMIT 1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) GetInitialAssessmentSetByLevel(ctx context.Context, level string) (QuestionSet, error) {
|
||||||
|
row := q.db.QueryRow(ctx, GetInitialAssessmentSetByLevel, level)
|
||||||
|
var i QuestionSet
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.Title,
|
||||||
|
&i.Description,
|
||||||
|
&i.SetType,
|
||||||
|
&i.OwnerType,
|
||||||
|
&i.OwnerID,
|
||||||
|
&i.BannerImage,
|
||||||
|
&i.Persona,
|
||||||
|
&i.TimeLimitMinutes,
|
||||||
|
&i.PassingScore,
|
||||||
|
&i.ShuffleQuestions,
|
||||||
|
&i.Status,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
&i.DisplayOrder,
|
||||||
|
&i.IntroVideoUrl,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
const GetPublishedQuestionSetsByOwner = `-- name: GetPublishedQuestionSetsByOwner :many
|
const GetPublishedQuestionSetsByOwner = `-- name: GetPublishedQuestionSetsByOwner :many
|
||||||
SELECT id, title, description, set_type, owner_type, owner_id, banner_image, persona, time_limit_minutes, passing_score, shuffle_questions, status, created_at, updated_at, display_order, intro_video_url
|
SELECT id, title, description, set_type, owner_type, owner_id, banner_image, persona, time_limit_minutes, passing_score, shuffle_questions, status, created_at, updated_at, display_order, intro_video_url
|
||||||
FROM question_sets
|
FROM question_sets
|
||||||
|
|
@ -426,6 +466,35 @@ func (q *Queries) GetUserPersonasByQuestionSetID(ctx context.Context, questionSe
|
||||||
return items, nil
|
return items, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const ListPublishedInitialAssessmentLevels = `-- name: ListPublishedInitialAssessmentLevels :many
|
||||||
|
SELECT DISTINCT UPPER(TRIM(description))::text AS level
|
||||||
|
FROM question_sets
|
||||||
|
WHERE set_type = 'INITIAL_ASSESSMENT'
|
||||||
|
AND status = 'PUBLISHED'
|
||||||
|
AND UPPER(TRIM(COALESCE(description, ''))) IN ('A1', 'A2', 'B1', 'B2', 'C1', 'C2')
|
||||||
|
ORDER BY level
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) ListPublishedInitialAssessmentLevels(ctx context.Context) ([]string, error) {
|
||||||
|
rows, err := q.db.Query(ctx, ListPublishedInitialAssessmentLevels)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var items []string
|
||||||
|
for rows.Next() {
|
||||||
|
var level string
|
||||||
|
if err := rows.Scan(&level); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, level)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
const RemoveUserPersonaFromQuestionSet = `-- name: RemoveUserPersonaFromQuestionSet :exec
|
const RemoveUserPersonaFromQuestionSet = `-- name: RemoveUserPersonaFromQuestionSet :exec
|
||||||
DELETE FROM question_set_personas
|
DELETE FROM question_set_personas
|
||||||
WHERE question_set_id = $1
|
WHERE question_set_id = $1
|
||||||
|
|
|
||||||
|
|
@ -14,11 +14,23 @@ const (
|
||||||
AnalyticsFilterYear = "year"
|
AnalyticsFilterYear = "year"
|
||||||
AnalyticsFilterYearMonth = "year_month"
|
AnalyticsFilterYearMonth = "year_month"
|
||||||
AnalyticsFilterCustom = "custom"
|
AnalyticsFilterCustom = "custom"
|
||||||
|
|
||||||
|
AnalyticsTimezoneName = "Africa/Addis_Ababa"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// AnalyticsTimezone is East Africa Time (EAT, UTC+3), used for analytics calendar buckets.
|
||||||
|
func AnalyticsTimezone() *time.Location {
|
||||||
|
loc, err := time.LoadLocation(AnalyticsTimezoneName)
|
||||||
|
if err != nil {
|
||||||
|
return time.FixedZone("EAT", 3*60*60)
|
||||||
|
}
|
||||||
|
return loc
|
||||||
|
}
|
||||||
|
|
||||||
// AnalyticsDateFilter describes the effective reporting window for dashboard analytics.
|
// AnalyticsDateFilter describes the effective reporting window for dashboard analytics.
|
||||||
type AnalyticsDateFilter struct {
|
type AnalyticsDateFilter struct {
|
||||||
Mode string `json:"mode"`
|
Mode string `json:"mode"`
|
||||||
|
Timezone string `json:"timezone"`
|
||||||
Year *int `json:"year,omitempty"`
|
Year *int `json:"year,omitempty"`
|
||||||
Month *int `json:"month,omitempty"`
|
Month *int `json:"month,omitempty"`
|
||||||
From *time.Time `json:"from,omitempty"`
|
From *time.Time `json:"from,omitempty"`
|
||||||
|
|
@ -28,33 +40,46 @@ type AnalyticsDateFilter struct {
|
||||||
SeriesStart time.Time `json:"series_start"`
|
SeriesStart time.Time `json:"series_start"`
|
||||||
SeriesEnd time.Time `json:"series_end"`
|
SeriesEnd time.Time `json:"series_end"`
|
||||||
RefDate time.Time `json:"ref_date"`
|
RefDate time.Time `json:"ref_date"`
|
||||||
|
PaymentMethod *string `json:"payment_method,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseAnalyticsDateFilter resolves dashboard date filters from query params.
|
// ParseAnalyticsDateFilter resolves dashboard date filters from query params.
|
||||||
|
// Calendar year/month/day bounds and "today" use Africa/Addis_Ababa (EAT).
|
||||||
//
|
//
|
||||||
// Supported:
|
// Supported:
|
||||||
// - (none) => all-time totals; charts default to last 30 days
|
// - (none) => all-time totals; charts default to last 30 EAT days
|
||||||
// - year=2025 => calendar year
|
// - year=2025 => EAT calendar year
|
||||||
// - year=2025&month=3 => calendar month
|
// - year=2025&month=3 => EAT calendar month
|
||||||
// - from=YYYY-MM-DD&to=YYYY-MM-DD => inclusive custom range (to required with from)
|
// - from=YYYY-MM-DD&to=YYYY-MM-DD => inclusive custom EAT date range
|
||||||
|
// - payment_method=CHAPA => optional filter for payment/revenue metrics
|
||||||
func ParseAnalyticsDateFilter(c *fiber.Ctx) (AnalyticsDateFilter, error) {
|
func ParseAnalyticsDateFilter(c *fiber.Ctx) (AnalyticsDateFilter, error) {
|
||||||
now := time.Now().UTC()
|
loc := AnalyticsTimezone()
|
||||||
today := dateOnlyUTC(now)
|
now := time.Now().In(loc)
|
||||||
|
today := dateOnlyIn(loc, now)
|
||||||
|
|
||||||
fromRaw := strings.TrimSpace(c.Query("from"))
|
fromRaw := strings.TrimSpace(c.Query("from"))
|
||||||
toRaw := strings.TrimSpace(c.Query("to"))
|
toRaw := strings.TrimSpace(c.Query("to"))
|
||||||
yearRaw := strings.TrimSpace(c.Query("year"))
|
yearRaw := strings.TrimSpace(c.Query("year"))
|
||||||
monthRaw := strings.TrimSpace(c.Query("month"))
|
monthRaw := strings.TrimSpace(c.Query("month"))
|
||||||
|
paymentMethod, err := parseAnalyticsPaymentMethod(c.Query("payment_method"))
|
||||||
|
if err != nil {
|
||||||
|
return AnalyticsDateFilter{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
base := AnalyticsDateFilter{
|
||||||
|
Timezone: AnalyticsTimezoneName,
|
||||||
|
PaymentMethod: paymentMethod,
|
||||||
|
}
|
||||||
|
|
||||||
if fromRaw != "" || toRaw != "" {
|
if fromRaw != "" || toRaw != "" {
|
||||||
if fromRaw == "" || toRaw == "" {
|
if fromRaw == "" || toRaw == "" {
|
||||||
return AnalyticsDateFilter{}, fmt.Errorf("both from and to are required for a custom date range")
|
return AnalyticsDateFilter{}, fmt.Errorf("both from and to are required for a custom date range")
|
||||||
}
|
}
|
||||||
from, err := parseAnalyticsDate(fromRaw)
|
from, err := parseAnalyticsDateIn(fromRaw, loc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return AnalyticsDateFilter{}, fmt.Errorf("invalid from date: %w", err)
|
return AnalyticsDateFilter{}, fmt.Errorf("invalid from date: %w", err)
|
||||||
}
|
}
|
||||||
to, err := parseAnalyticsDate(toRaw)
|
to, err := parseAnalyticsDateIn(toRaw, loc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return AnalyticsDateFilter{}, fmt.Errorf("invalid to date: %w", err)
|
return AnalyticsDateFilter{}, fmt.Errorf("invalid to date: %w", err)
|
||||||
}
|
}
|
||||||
|
|
@ -67,16 +92,15 @@ func ParseAnalyticsDateFilter(c *fiber.Ctx) (AnalyticsDateFilter, error) {
|
||||||
if today.Before(to) {
|
if today.Before(to) {
|
||||||
ref = today
|
ref = today
|
||||||
}
|
}
|
||||||
return AnalyticsDateFilter{
|
base.Mode = AnalyticsFilterCustom
|
||||||
Mode: AnalyticsFilterCustom,
|
base.From = &from
|
||||||
From: &from,
|
base.To = &to
|
||||||
To: &to,
|
base.RangeStart = &rangeStart
|
||||||
RangeStart: &rangeStart,
|
base.RangeEnd = &rangeEnd
|
||||||
RangeEnd: &rangeEnd,
|
base.SeriesStart = from
|
||||||
SeriesStart: from,
|
base.SeriesEnd = to
|
||||||
SeriesEnd: to,
|
base.RefDate = ref
|
||||||
RefDate: ref,
|
return base, nil
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if yearRaw != "" {
|
if yearRaw != "" {
|
||||||
|
|
@ -86,45 +110,43 @@ func ParseAnalyticsDateFilter(c *fiber.Ctx) (AnalyticsDateFilter, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if monthRaw == "" {
|
if monthRaw == "" {
|
||||||
start := time.Date(year, time.January, 1, 0, 0, 0, 0, time.UTC)
|
start := time.Date(year, time.January, 1, 0, 0, 0, 0, loc)
|
||||||
endExclusive := start.AddDate(1, 0, 0)
|
endExclusive := start.AddDate(1, 0, 0)
|
||||||
lastDay := endExclusive.AddDate(0, 0, -1)
|
lastDay := endExclusive.AddDate(0, 0, -1)
|
||||||
ref := lastDay
|
ref := lastDay
|
||||||
if today.Year() == year && !today.After(lastDay) {
|
if today.Year() == year && !today.After(lastDay) {
|
||||||
ref = today
|
ref = today
|
||||||
}
|
}
|
||||||
return AnalyticsDateFilter{
|
base.Mode = AnalyticsFilterYear
|
||||||
Mode: AnalyticsFilterYear,
|
base.Year = &year
|
||||||
Year: &year,
|
base.RangeStart = &start
|
||||||
RangeStart: &start,
|
base.RangeEnd = &endExclusive
|
||||||
RangeEnd: &endExclusive,
|
base.SeriesStart = start
|
||||||
SeriesStart: start,
|
base.SeriesEnd = lastDay
|
||||||
SeriesEnd: lastDay,
|
base.RefDate = ref
|
||||||
RefDate: ref,
|
return base, nil
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
month, err := strconv.Atoi(monthRaw)
|
month, err := strconv.Atoi(monthRaw)
|
||||||
if err != nil || month < 1 || month > 12 {
|
if err != nil || month < 1 || month > 12 {
|
||||||
return AnalyticsDateFilter{}, fmt.Errorf("month must be between 1 and 12")
|
return AnalyticsDateFilter{}, fmt.Errorf("month must be between 1 and 12")
|
||||||
}
|
}
|
||||||
start := time.Date(year, time.Month(month), 1, 0, 0, 0, 0, time.UTC)
|
start := time.Date(year, time.Month(month), 1, 0, 0, 0, 0, loc)
|
||||||
endExclusive := start.AddDate(0, 1, 0)
|
endExclusive := start.AddDate(0, 1, 0)
|
||||||
lastDay := endExclusive.AddDate(0, 0, -1)
|
lastDay := endExclusive.AddDate(0, 0, -1)
|
||||||
ref := lastDay
|
ref := lastDay
|
||||||
if today.Year() == year && int(today.Month()) == month && !today.After(lastDay) {
|
if today.Year() == year && int(today.Month()) == month && !today.After(lastDay) {
|
||||||
ref = today
|
ref = today
|
||||||
}
|
}
|
||||||
return AnalyticsDateFilter{
|
base.Mode = AnalyticsFilterYearMonth
|
||||||
Mode: AnalyticsFilterYearMonth,
|
base.Year = &year
|
||||||
Year: &year,
|
base.Month = &month
|
||||||
Month: &month,
|
base.RangeStart = &start
|
||||||
RangeStart: &start,
|
base.RangeEnd = &endExclusive
|
||||||
RangeEnd: &endExclusive,
|
base.SeriesStart = start
|
||||||
SeriesStart: start,
|
base.SeriesEnd = lastDay
|
||||||
SeriesEnd: lastDay,
|
base.RefDate = ref
|
||||||
RefDate: ref,
|
return base, nil
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if monthRaw != "" {
|
if monthRaw != "" {
|
||||||
|
|
@ -133,26 +155,37 @@ func ParseAnalyticsDateFilter(c *fiber.Ctx) (AnalyticsDateFilter, error) {
|
||||||
|
|
||||||
seriesEnd := today
|
seriesEnd := today
|
||||||
seriesStart := seriesEnd.AddDate(0, 0, -29)
|
seriesStart := seriesEnd.AddDate(0, 0, -29)
|
||||||
return AnalyticsDateFilter{
|
base.Mode = AnalyticsFilterAllTime
|
||||||
Mode: AnalyticsFilterAllTime,
|
base.SeriesStart = seriesStart
|
||||||
SeriesStart: seriesStart,
|
base.SeriesEnd = seriesEnd
|
||||||
SeriesEnd: seriesEnd,
|
base.RefDate = today
|
||||||
RefDate: today,
|
return base, nil
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseAnalyticsDate(raw string) (time.Time, error) {
|
func parseAnalyticsPaymentMethod(raw string) (*string, error) {
|
||||||
if t, err := time.Parse(time.RFC3339, raw); err == nil {
|
trimmed := strings.TrimSpace(raw)
|
||||||
return dateOnlyUTC(t), nil
|
if trimmed == "" {
|
||||||
|
return nil, nil
|
||||||
}
|
}
|
||||||
t, err := time.Parse("2006-01-02", raw)
|
if len(trimmed) > 64 {
|
||||||
|
return nil, fmt.Errorf("payment_method is too long")
|
||||||
|
}
|
||||||
|
normalized := strings.ToUpper(trimmed)
|
||||||
|
return &normalized, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseAnalyticsDateIn(raw string, loc *time.Location) (time.Time, error) {
|
||||||
|
if t, err := time.Parse(time.RFC3339, raw); err == nil {
|
||||||
|
return dateOnlyIn(loc, t), nil
|
||||||
|
}
|
||||||
|
t, err := time.ParseInLocation("2006-01-02", raw, loc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return time.Time{}, err
|
return time.Time{}, err
|
||||||
}
|
}
|
||||||
return dateOnlyUTC(t), nil
|
return dateOnlyIn(loc, t), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func dateOnlyUTC(t time.Time) time.Time {
|
func dateOnlyIn(loc *time.Location, t time.Time) time.Time {
|
||||||
t = t.UTC()
|
t = t.In(loc)
|
||||||
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.UTC)
|
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, loc)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,12 +24,15 @@ func TestParseAnalyticsDateFilter_allTime(t *testing.T) {
|
||||||
if got.Mode != AnalyticsFilterAllTime {
|
if got.Mode != AnalyticsFilterAllTime {
|
||||||
t.Fatalf("mode=%q", got.Mode)
|
t.Fatalf("mode=%q", got.Mode)
|
||||||
}
|
}
|
||||||
|
if got.Timezone != AnalyticsTimezoneName {
|
||||||
|
t.Fatalf("timezone=%q", got.Timezone)
|
||||||
|
}
|
||||||
if got.RangeStart != nil || got.RangeEnd != nil {
|
if got.RangeStart != nil || got.RangeEnd != nil {
|
||||||
t.Fatal("expected no range bounds for all-time")
|
t.Fatal("expected no range bounds for all-time")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseAnalyticsDateFilter_yearMonth(t *testing.T) {
|
func TestParseAnalyticsDateFilter_yearMonthUsesEAT(t *testing.T) {
|
||||||
app := fiber.New()
|
app := fiber.New()
|
||||||
var got AnalyticsDateFilter
|
var got AnalyticsDateFilter
|
||||||
app.Get("/", func(c *fiber.Ctx) error {
|
app.Get("/", func(c *fiber.Ctx) error {
|
||||||
|
|
@ -48,10 +51,15 @@ func TestParseAnalyticsDateFilter_yearMonth(t *testing.T) {
|
||||||
if got.RangeStart == nil || got.RangeEnd == nil {
|
if got.RangeStart == nil || got.RangeEnd == nil {
|
||||||
t.Fatal("expected range bounds")
|
t.Fatal("expected range bounds")
|
||||||
}
|
}
|
||||||
wantStart := time.Date(2025, time.March, 1, 0, 0, 0, 0, time.UTC)
|
loc := AnalyticsTimezone()
|
||||||
wantEnd := time.Date(2025, time.April, 1, 0, 0, 0, 0, time.UTC)
|
wantStart := time.Date(2025, time.March, 1, 0, 0, 0, 0, loc)
|
||||||
|
wantEnd := time.Date(2025, time.April, 1, 0, 0, 0, 0, loc)
|
||||||
if !got.RangeStart.Equal(wantStart) || !got.RangeEnd.Equal(wantEnd) {
|
if !got.RangeStart.Equal(wantStart) || !got.RangeEnd.Equal(wantEnd) {
|
||||||
t.Fatalf("range=%v..%v", got.RangeStart, got.RangeEnd)
|
t.Fatalf("range=%v..%v want %v..%v", got.RangeStart, got.RangeEnd, wantStart, wantEnd)
|
||||||
|
}
|
||||||
|
// EAT midnight is 21:00 previous day in UTC.
|
||||||
|
if got.RangeStart.UTC().Hour() != 21 {
|
||||||
|
t.Fatalf("expected EAT midnight as 21:00 UTC, got %v", got.RangeStart.UTC())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -71,8 +79,27 @@ func TestParseAnalyticsDateFilter_custom(t *testing.T) {
|
||||||
if got.Mode != AnalyticsFilterCustom {
|
if got.Mode != AnalyticsFilterCustom {
|
||||||
t.Fatalf("mode=%q", got.Mode)
|
t.Fatalf("mode=%q", got.Mode)
|
||||||
}
|
}
|
||||||
wantEnd := time.Date(2025, time.January, 21, 0, 0, 0, 0, time.UTC)
|
loc := AnalyticsTimezone()
|
||||||
|
wantEnd := time.Date(2025, time.January, 21, 0, 0, 0, 0, loc)
|
||||||
if got.RangeEnd == nil || !got.RangeEnd.Equal(wantEnd) {
|
if got.RangeEnd == nil || !got.RangeEnd.Equal(wantEnd) {
|
||||||
t.Fatalf("range_end=%v", got.RangeEnd)
|
t.Fatalf("range_end=%v want %v", got.RangeEnd, wantEnd)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseAnalyticsDateFilter_paymentMethod(t *testing.T) {
|
||||||
|
app := fiber.New()
|
||||||
|
var got AnalyticsDateFilter
|
||||||
|
app.Get("/", func(c *fiber.Ctx) error {
|
||||||
|
var err error
|
||||||
|
got, err = ParseAnalyticsDateFilter(c)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
|
||||||
|
req := httptest.NewRequest("GET", "/?payment_method=chapa", nil)
|
||||||
|
if _, err := app.Test(req); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if got.PaymentMethod == nil || *got.PaymentMethod != "CHAPA" {
|
||||||
|
t.Fatalf("payment_method=%v", got.PaymentMethod)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ import (
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Short month labels for analytics monthly charts (aligned with UTC calendar months).
|
// Short month labels for analytics monthly charts (aligned with EAT calendar months).
|
||||||
var analyticsShortMonthLabels = []string{
|
var analyticsShortMonthLabels = []string{
|
||||||
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
|
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
|
||||||
}
|
}
|
||||||
|
|
@ -22,13 +22,14 @@ func toTime(v interface{}) time.Time {
|
||||||
|
|
||||||
// GetAnalyticsDashboard godoc
|
// GetAnalyticsDashboard godoc
|
||||||
// @Summary Analytics dashboard
|
// @Summary Analytics dashboard
|
||||||
// @Description Platform analytics with optional date filters: all-time (default), year, year+month, or custom from/to range. When year is set, payments.revenue_monthly returns Jan–Dec SUCCESS revenue totals (UTC) per currency for that calendar year — use for yearly revenue charts. Daily series remains in revenue_last_30_days (see date_filter.series_*). Courses section counts LMS + exam_prep inventory.
|
// @Description Platform analytics with optional date filters in Africa/Addis_Ababa (EAT): all-time (default), year, year+month, or custom from/to range. When year is set, payments.revenue_monthly returns Jan–Dec SUCCESS revenue totals (EAT) per currency for that calendar year. Optional payment_method filters payment/revenue metrics (exact or METHOD:% variants). Daily series remains in revenue_last_30_days (see date_filter.series_*). Courses section counts LMS + exam_prep inventory.
|
||||||
// @Tags analytics
|
// @Tags analytics
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Param year query int false "Calendar year (e.g. 2025)"
|
// @Param year query int false "EAT calendar year (e.g. 2025)"
|
||||||
// @Param month query int false "Calendar month 1-12 (requires year)"
|
// @Param month query int false "EAT calendar month 1-12 (requires year)"
|
||||||
// @Param from query string false "Custom range start (YYYY-MM-DD or RFC3339)"
|
// @Param from query string false "Custom EAT range start (YYYY-MM-DD or RFC3339)"
|
||||||
// @Param to query string false "Custom range end (YYYY-MM-DD or RFC3339, inclusive)"
|
// @Param to query string false "Custom EAT range end (YYYY-MM-DD or RFC3339, inclusive)"
|
||||||
|
// @Param payment_method query string false "Filter payment/revenue metrics (e.g. CHAPA, ARIFPAY, ADMIN_GRANT)"
|
||||||
// @Success 200 {object} domain.AnalyticsDashboard
|
// @Success 200 {object} domain.AnalyticsDashboard
|
||||||
// @Failure 400 {object} domain.ErrorResponse
|
// @Failure 400 {object} domain.ErrorResponse
|
||||||
// @Failure 500 {object} domain.ErrorResponse
|
// @Failure 500 {object} domain.ErrorResponse
|
||||||
|
|
@ -171,7 +172,9 @@ func (h *Handler) GetAnalyticsDashboard(c *fiber.Ctx) error {
|
||||||
var revenueMonthlyRows []dbgen.AnalyticsRevenueMonthlyByYearRow
|
var revenueMonthlyRows []dbgen.AnalyticsRevenueMonthlyByYearRow
|
||||||
var monthlyRevenueYear *int
|
var monthlyRevenueYear *int
|
||||||
if filter.Year != nil {
|
if filter.Year != nil {
|
||||||
rowsMonthly, err := h.analyticsDB.AnalyticsRevenueMonthlyByYear(ctx, int32(*filter.Year))
|
monthlyParams := p.RevenueMonthly
|
||||||
|
monthlyParams.ReportYear = int32(*filter.Year)
|
||||||
|
rowsMonthly, err := h.analyticsDB.AnalyticsRevenueMonthlyByYear(ctx, monthlyParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch monthly revenue series")
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch monthly revenue series")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,7 @@ type analyticsQueryParams struct {
|
||||||
PaymentsByStatus dbgen.AnalyticsPaymentsByStatusParams
|
PaymentsByStatus dbgen.AnalyticsPaymentsByStatusParams
|
||||||
PaymentsByMethod dbgen.AnalyticsPaymentsByMethodParams
|
PaymentsByMethod dbgen.AnalyticsPaymentsByMethodParams
|
||||||
RevenueSeries dbgen.AnalyticsRevenueLast30DaysParams
|
RevenueSeries dbgen.AnalyticsRevenueLast30DaysParams
|
||||||
|
RevenueMonthly dbgen.AnalyticsRevenueMonthlyByYearParams
|
||||||
|
|
||||||
QuestionsCounts dbgen.AnalyticsQuestionsCountsParams
|
QuestionsCounts dbgen.AnalyticsQuestionsCountsParams
|
||||||
QuestionsByType dbgen.AnalyticsQuestionsByTypeParams
|
QuestionsByType dbgen.AnalyticsQuestionsByTypeParams
|
||||||
|
|
@ -61,6 +62,7 @@ type analyticsQueryParams struct {
|
||||||
func newAnalyticsQueryParams(f domain.AnalyticsDateFilter) analyticsQueryParams {
|
func newAnalyticsQueryParams(f domain.AnalyticsDateFilter) analyticsQueryParams {
|
||||||
ref := pgAnalyticsDate(f.RefDate)
|
ref := pgAnalyticsDate(f.RefDate)
|
||||||
rs, re := pgAnalyticsTimestamptzPtr(f.RangeStart), pgAnalyticsTimestamptzPtr(f.RangeEnd)
|
rs, re := pgAnalyticsTimestamptzPtr(f.RangeStart), pgAnalyticsTimestamptzPtr(f.RangeEnd)
|
||||||
|
pm := pgAnalyticsTextPtr(f.PaymentMethod)
|
||||||
series := dbgen.AnalyticsUserRegistrationsLast30DaysParams{
|
series := dbgen.AnalyticsUserRegistrationsLast30DaysParams{
|
||||||
SeriesStart: pgAnalyticsDate(f.SeriesStart),
|
SeriesStart: pgAnalyticsDate(f.SeriesStart),
|
||||||
SeriesEnd: pgAnalyticsDate(f.SeriesEnd),
|
SeriesEnd: pgAnalyticsDate(f.SeriesEnd),
|
||||||
|
|
@ -84,7 +86,11 @@ func newAnalyticsQueryParams(f domain.AnalyticsDateFilter) analyticsQueryParams
|
||||||
|
|
||||||
SubscriptionsSummary: dbgen.AnalyticsSubscriptionsSummaryParams{RefDate: ref, RangeStart: rs, RangeEnd: re},
|
SubscriptionsSummary: dbgen.AnalyticsSubscriptionsSummaryParams{RefDate: ref, RangeStart: rs, RangeEnd: re},
|
||||||
SubscriptionsByStatus: dbgen.AnalyticsSubscriptionsByStatusParams{RangeStart: rs, RangeEnd: re},
|
SubscriptionsByStatus: dbgen.AnalyticsSubscriptionsByStatusParams{RangeStart: rs, RangeEnd: re},
|
||||||
RevenueByPlan: dbgen.AnalyticsRevenueByPlanParams{RangeStart: rs, RangeEnd: re},
|
RevenueByPlan: dbgen.AnalyticsRevenueByPlanParams{
|
||||||
|
RangeStart: rs,
|
||||||
|
RangeEnd: re,
|
||||||
|
PaymentMethod: pm,
|
||||||
|
},
|
||||||
NewSubscriptionsSeries: dbgen.AnalyticsNewSubscriptionsLast30DaysParams{
|
NewSubscriptionsSeries: dbgen.AnalyticsNewSubscriptionsLast30DaysParams{
|
||||||
SeriesStart: series.SeriesStart,
|
SeriesStart: series.SeriesStart,
|
||||||
SeriesEnd: series.SeriesEnd,
|
SeriesEnd: series.SeriesEnd,
|
||||||
|
|
@ -92,7 +98,11 @@ func newAnalyticsQueryParams(f domain.AnalyticsDateFilter) analyticsQueryParams
|
||||||
RangeEnd: re,
|
RangeEnd: re,
|
||||||
},
|
},
|
||||||
AcquisitionBySource: dbgen.AnalyticsSubscriptionAcquisitionBySourceParams{RangeStart: rs, RangeEnd: re},
|
AcquisitionBySource: dbgen.AnalyticsSubscriptionAcquisitionBySourceParams{RangeStart: rs, RangeEnd: re},
|
||||||
RevenueByCategory: dbgen.AnalyticsRevenueByPlanCategoryParams{RangeStart: rs, RangeEnd: re},
|
RevenueByCategory: dbgen.AnalyticsRevenueByPlanCategoryParams{
|
||||||
|
RangeStart: rs,
|
||||||
|
RangeEnd: re,
|
||||||
|
PaymentMethod: pm,
|
||||||
|
},
|
||||||
CancellationsSeries: dbgen.AnalyticsSubscriptionCancellationsLast30DaysParams{
|
CancellationsSeries: dbgen.AnalyticsSubscriptionCancellationsLast30DaysParams{
|
||||||
SeriesStart: series.SeriesStart,
|
SeriesStart: series.SeriesStart,
|
||||||
SeriesEnd: series.SeriesEnd,
|
SeriesEnd: series.SeriesEnd,
|
||||||
|
|
@ -113,14 +123,31 @@ func newAnalyticsQueryParams(f domain.AnalyticsDateFilter) analyticsQueryParams
|
||||||
RangeEnd: re,
|
RangeEnd: re,
|
||||||
},
|
},
|
||||||
|
|
||||||
PaymentsSummary: dbgen.AnalyticsPaymentsSummaryParams{RangeStart: rs, RangeEnd: re},
|
PaymentsSummary: dbgen.AnalyticsPaymentsSummaryParams{
|
||||||
PaymentsByStatus: dbgen.AnalyticsPaymentsByStatusParams{RangeStart: rs, RangeEnd: re},
|
RangeStart: rs,
|
||||||
PaymentsByMethod: dbgen.AnalyticsPaymentsByMethodParams{RangeStart: rs, RangeEnd: re},
|
RangeEnd: re,
|
||||||
|
PaymentMethod: pm,
|
||||||
|
},
|
||||||
|
PaymentsByStatus: dbgen.AnalyticsPaymentsByStatusParams{
|
||||||
|
RangeStart: rs,
|
||||||
|
RangeEnd: re,
|
||||||
|
PaymentMethod: pm,
|
||||||
|
},
|
||||||
|
PaymentsByMethod: dbgen.AnalyticsPaymentsByMethodParams{
|
||||||
|
RangeStart: rs,
|
||||||
|
RangeEnd: re,
|
||||||
|
PaymentMethod: pm,
|
||||||
|
},
|
||||||
RevenueSeries: dbgen.AnalyticsRevenueLast30DaysParams{
|
RevenueSeries: dbgen.AnalyticsRevenueLast30DaysParams{
|
||||||
SeriesStart: series.SeriesStart,
|
SeriesStart: series.SeriesStart,
|
||||||
SeriesEnd: series.SeriesEnd,
|
SeriesEnd: series.SeriesEnd,
|
||||||
RangeStart: rs,
|
RangeStart: rs,
|
||||||
RangeEnd: re,
|
RangeEnd: re,
|
||||||
|
PaymentMethod: pm,
|
||||||
|
},
|
||||||
|
RevenueMonthly: dbgen.AnalyticsRevenueMonthlyByYearParams{
|
||||||
|
ReportYear: 0, // set by caller when year filter is present
|
||||||
|
PaymentMethod: pm,
|
||||||
},
|
},
|
||||||
|
|
||||||
QuestionsCounts: dbgen.AnalyticsQuestionsCountsParams{RangeStart: rs, RangeEnd: re},
|
QuestionsCounts: dbgen.AnalyticsQuestionsCountsParams{RangeStart: rs, RangeEnd: re},
|
||||||
|
|
@ -144,8 +171,14 @@ func newAnalyticsQueryParams(f domain.AnalyticsDateFilter) analyticsQueryParams
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pgAnalyticsDate stores the EAT civil calendar date (Y-M-D) for Postgres date params.
|
||||||
func pgAnalyticsDate(t time.Time) pgtype.Date {
|
func pgAnalyticsDate(t time.Time) pgtype.Date {
|
||||||
return pgtype.Date{Time: t.UTC(), Valid: true}
|
loc := domain.AnalyticsTimezone()
|
||||||
|
local := t.In(loc)
|
||||||
|
return pgtype.Date{
|
||||||
|
Time: time.Date(local.Year(), local.Month(), local.Day(), 0, 0, 0, 0, time.UTC),
|
||||||
|
Valid: true,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func pgAnalyticsTimestamptzPtr(t *time.Time) pgtype.Timestamptz {
|
func pgAnalyticsTimestamptzPtr(t *time.Time) pgtype.Timestamptz {
|
||||||
|
|
@ -154,3 +187,10 @@ func pgAnalyticsTimestamptzPtr(t *time.Time) pgtype.Timestamptz {
|
||||||
}
|
}
|
||||||
return pgtype.Timestamptz{Time: t.UTC(), Valid: true}
|
return pgtype.Timestamptz{Time: t.UTC(), Valid: true}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func pgAnalyticsTextPtr(s *string) pgtype.Text {
|
||||||
|
if s == nil {
|
||||||
|
return pgtype.Text{Valid: false}
|
||||||
|
}
|
||||||
|
return pgtype.Text{String: *s, Valid: true}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user