Show all subscription statuses in analytics pies, including zeros.

Also trim payment status filters to Pending, Success, and Expired.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Yared Yemane 2026-07-10 05:55:29 -07:00
parent f6ad0d3cb5
commit f34aa809d9
4 changed files with 70 additions and 16 deletions

View File

@ -5,6 +5,21 @@ import type {
LabelCount,
} from "../types/analytics.types"
/** Canonical subscription row statuses shown in analytics breakdowns. */
export const SUBSCRIPTION_STATUS_BREAKDOWN = [
"ACTIVE",
"PENDING",
"EXPIRED",
"CANCELLED",
] as const
const SUBSCRIPTION_STATUS_COLORS: Record<string, string> = {
ACTIVE: "#9E2891",
PENDING: "#FFD23F",
EXPIRED: "#F97316",
CANCELLED: "#EF4444",
}
const INACTIVE_SUBSCRIPTION_STATUSES = new Set([
"INACTIVE",
"CANCELLED",
@ -20,6 +35,51 @@ export interface SubscriptionMetrics {
inactive: number
}
export interface SubscriptionStatusPieSlice {
name: string
value: number
color: string
}
/**
* Ensures every canonical subscription status appears in the pie/legend,
* including statuses with a count of 0.
*/
export function buildSubscriptionStatusPie(
byStatus: LabelCount[] | null | undefined,
fallbackColors: string[] = ["#9E2891", "#FFD23F", "#1DE9B6", "#C26FC0"],
): SubscriptionStatusPieSlice[] {
const counts = new Map<string, number>()
for (const row of byStatus ?? []) {
const key = row.label.trim().toUpperCase()
if (!key) continue
counts.set(key, (counts.get(key) ?? 0) + row.count)
}
const known = new Set<string>(SUBSCRIPTION_STATUS_BREAKDOWN)
const slices: SubscriptionStatusPieSlice[] = SUBSCRIPTION_STATUS_BREAKDOWN.map(
(status, index) => ({
name: status,
value: counts.get(status) ?? 0,
color:
SUBSCRIPTION_STATUS_COLORS[status] ??
fallbackColors[index % fallbackColors.length],
}),
)
// Preserve any unexpected statuses from the API after the canonical set.
for (const [status, value] of counts) {
if (known.has(status)) continue
slices.push({
name: status,
value,
color: fallbackColors[slices.length % fallbackColors.length],
})
}
return slices
}
/** Derives inactive count from by_status when present, else total − active. */
export function getSubscriptionMetrics(
subscriptions: DashboardSubscriptions,

View File

@ -47,6 +47,7 @@ import {
getSeriesPeriodLabel,
getSubscriptionMetrics,
getVideoLessonsSummary,
buildSubscriptionStatusPie,
} from "../lib/analytics"
import type { DashboardData, DashboardFilters } from "../types/analytics.types"
import { formatAverageStars } from "../lib/ratingsDisplay"
@ -156,13 +157,10 @@ export function DashboardPage() {
count: d.count,
})) ?? []
const subscriptionStatusData =
dashboard?.subscriptions.by_status.map((s, i) => ({
name: s.label,
value: s.count,
color: PIE_COLORS[i % PIE_COLORS.length],
})) ?? []
const subscriptionStatusData = buildSubscriptionStatusPie(
dashboard?.subscriptions.by_status,
PIE_COLORS,
)
const issueStatusData =
dashboard?.issues.by_status.map((s, i) => ({
name: s.label,

View File

@ -56,6 +56,7 @@ import {
getSubscriptionMetrics,
getVideoLessonsSummary,
formatPercentRate,
buildSubscriptionStatusPie,
} from "../../lib/analytics"
import type { DashboardData, DashboardFilters, LabelCount } from "../../types/analytics.types"
@ -400,12 +401,10 @@ export function AnalyticsPage() {
color: PIE_COLORS[i % PIE_COLORS.length],
}))
const subscriptionStatusPie = subscriptions.by_status.map((s, i) => ({
name: s.label,
value: s.count,
color: PIE_COLORS[i % PIE_COLORS.length],
}))
const subscriptionStatusPie = buildSubscriptionStatusPie(
subscriptions.by_status,
PIE_COLORS,
)
const notifByTypePie = notifications.by_type.slice(0, 8).map((s, i) => ({
name: s.label,
value: s.count,

View File

@ -59,10 +59,7 @@ import { DEFAULT_TABLE_PAGE_SIZE, TABLE_PAGE_SIZE_OPTIONS } from "../../lib/tabl
const STATUS_FILTERS: { value: PaymentStatus; label: string }[] = [
{ value: "PENDING", label: "Pending" },
{ value: "PROCESSING", label: "Processing" },
{ value: "SUCCESS", label: "Success" },
{ value: "FAILED", label: "Failed" },
{ value: "CANCELLED", label: "Cancelled" },
{ value: "EXPIRED", label: "Expired" },
]