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:
parent
f6ad0d3cb5
commit
f34aa809d9
|
|
@ -5,6 +5,21 @@ import type {
|
||||||
LabelCount,
|
LabelCount,
|
||||||
} from "../types/analytics.types"
|
} 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([
|
const INACTIVE_SUBSCRIPTION_STATUSES = new Set([
|
||||||
"INACTIVE",
|
"INACTIVE",
|
||||||
"CANCELLED",
|
"CANCELLED",
|
||||||
|
|
@ -20,6 +35,51 @@ export interface SubscriptionMetrics {
|
||||||
inactive: number
|
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. */
|
/** Derives inactive count from by_status when present, else total − active. */
|
||||||
export function getSubscriptionMetrics(
|
export function getSubscriptionMetrics(
|
||||||
subscriptions: DashboardSubscriptions,
|
subscriptions: DashboardSubscriptions,
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,7 @@ import {
|
||||||
getSeriesPeriodLabel,
|
getSeriesPeriodLabel,
|
||||||
getSubscriptionMetrics,
|
getSubscriptionMetrics,
|
||||||
getVideoLessonsSummary,
|
getVideoLessonsSummary,
|
||||||
|
buildSubscriptionStatusPie,
|
||||||
} from "../lib/analytics"
|
} from "../lib/analytics"
|
||||||
import type { DashboardData, DashboardFilters } from "../types/analytics.types"
|
import type { DashboardData, DashboardFilters } from "../types/analytics.types"
|
||||||
import { formatAverageStars } from "../lib/ratingsDisplay"
|
import { formatAverageStars } from "../lib/ratingsDisplay"
|
||||||
|
|
@ -156,13 +157,10 @@ export function DashboardPage() {
|
||||||
count: d.count,
|
count: d.count,
|
||||||
})) ?? []
|
})) ?? []
|
||||||
|
|
||||||
const subscriptionStatusData =
|
const subscriptionStatusData = buildSubscriptionStatusPie(
|
||||||
dashboard?.subscriptions.by_status.map((s, i) => ({
|
dashboard?.subscriptions.by_status,
|
||||||
name: s.label,
|
PIE_COLORS,
|
||||||
value: s.count,
|
)
|
||||||
color: PIE_COLORS[i % PIE_COLORS.length],
|
|
||||||
})) ?? []
|
|
||||||
|
|
||||||
const issueStatusData =
|
const issueStatusData =
|
||||||
dashboard?.issues.by_status.map((s, i) => ({
|
dashboard?.issues.by_status.map((s, i) => ({
|
||||||
name: s.label,
|
name: s.label,
|
||||||
|
|
|
||||||
|
|
@ -56,6 +56,7 @@ import {
|
||||||
getSubscriptionMetrics,
|
getSubscriptionMetrics,
|
||||||
getVideoLessonsSummary,
|
getVideoLessonsSummary,
|
||||||
formatPercentRate,
|
formatPercentRate,
|
||||||
|
buildSubscriptionStatusPie,
|
||||||
} from "../../lib/analytics"
|
} from "../../lib/analytics"
|
||||||
import type { DashboardData, DashboardFilters, LabelCount } from "../../types/analytics.types"
|
import type { DashboardData, DashboardFilters, LabelCount } from "../../types/analytics.types"
|
||||||
|
|
||||||
|
|
@ -400,12 +401,10 @@ export function AnalyticsPage() {
|
||||||
color: PIE_COLORS[i % PIE_COLORS.length],
|
color: PIE_COLORS[i % PIE_COLORS.length],
|
||||||
}))
|
}))
|
||||||
|
|
||||||
const subscriptionStatusPie = subscriptions.by_status.map((s, i) => ({
|
const subscriptionStatusPie = buildSubscriptionStatusPie(
|
||||||
name: s.label,
|
subscriptions.by_status,
|
||||||
value: s.count,
|
PIE_COLORS,
|
||||||
color: PIE_COLORS[i % PIE_COLORS.length],
|
)
|
||||||
}))
|
|
||||||
|
|
||||||
const notifByTypePie = notifications.by_type.slice(0, 8).map((s, i) => ({
|
const notifByTypePie = notifications.by_type.slice(0, 8).map((s, i) => ({
|
||||||
name: s.label,
|
name: s.label,
|
||||||
value: s.count,
|
value: s.count,
|
||||||
|
|
|
||||||
|
|
@ -59,10 +59,7 @@ import { DEFAULT_TABLE_PAGE_SIZE, TABLE_PAGE_SIZE_OPTIONS } from "../../lib/tabl
|
||||||
|
|
||||||
const STATUS_FILTERS: { value: PaymentStatus; label: string }[] = [
|
const STATUS_FILTERS: { value: PaymentStatus; label: string }[] = [
|
||||||
{ value: "PENDING", label: "Pending" },
|
{ value: "PENDING", label: "Pending" },
|
||||||
{ value: "PROCESSING", label: "Processing" },
|
|
||||||
{ value: "SUCCESS", label: "Success" },
|
{ value: "SUCCESS", label: "Success" },
|
||||||
{ value: "FAILED", label: "Failed" },
|
|
||||||
{ value: "CANCELLED", label: "Cancelled" },
|
|
||||||
{ value: "EXPIRED", label: "Expired" },
|
{ value: "EXPIRED", label: "Expired" },
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user