Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9c5a08dc1e | ||
|
|
ada5f274d3 |
@@ -99,6 +99,18 @@ export function runTests() {
|
|||||||
assert(hook.includes("useLatestPatch"), "frontend patch hook must export useLatestPatch(city)");
|
assert(hook.includes("useLatestPatch"), "frontend patch hook must export useLatestPatch(city)");
|
||||||
assert(hook.includes("revision"), "frontend patch hook must track revisions and skip stale patches");
|
assert(hook.includes("revision"), "frontend patch hook must track revisions and skip stale patches");
|
||||||
assert(hook.includes("setTimeout"), "frontend patch hook must implement explicit reconnect backoff");
|
assert(hook.includes("setTimeout"), "frontend patch hook must implement explicit reconnect backoff");
|
||||||
|
assert(
|
||||||
|
hook.includes("SSE_SUBSCRIPTION_RECONNECT_DELAY_MS") &&
|
||||||
|
hook.includes("scheduleSubscriptionReconnect") &&
|
||||||
|
hook.includes("clearSubscriptionReconnectTimer"),
|
||||||
|
"frontend patch hook must debounce visible-city subscription changes before reopening SSE",
|
||||||
|
);
|
||||||
|
const subscriptionBlock = hook.match(/function registerCitySubscription[\s\S]*?\n}\r?\n\r?\nfunction normalizeLegacyPatch/)?.[0] || "";
|
||||||
|
assert(
|
||||||
|
subscriptionBlock.includes("scheduleSubscriptionReconnect()") &&
|
||||||
|
!subscriptionBlock.includes("ensureSsePatchConnection();"),
|
||||||
|
"city subscription mount/unmount should schedule one coalesced SSE reconnect instead of reconnecting per chart",
|
||||||
|
);
|
||||||
|
|
||||||
const bffEventsRoute = readFrontendFile("app", "api", "events", "route.ts");
|
const bffEventsRoute = readFrontendFile("app", "api", "events", "route.ts");
|
||||||
assert(bffEventsRoute.includes("searchParams"), "Next.js SSE proxy must forward query parameters to FastAPI");
|
assert(bffEventsRoute.includes("searchParams"), "Next.js SSE proxy must forward query parameters to FastAPI");
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { useEffect, useSyncExternalStore } from "react";
|
|||||||
import { resolveBackendApiUrl } from "@/lib/backend-api";
|
import { resolveBackendApiUrl } from "@/lib/backend-api";
|
||||||
|
|
||||||
const V1_EVENT_TYPE = "city_observation_patch.v1";
|
const V1_EVENT_TYPE = "city_observation_patch.v1";
|
||||||
|
const SSE_SUBSCRIPTION_RECONNECT_DELAY_MS = 150;
|
||||||
|
|
||||||
export type CityPatch = {
|
export type CityPatch = {
|
||||||
type?: string;
|
type?: string;
|
||||||
@@ -38,6 +39,7 @@ const subscribedCities = new Map<string, number>();
|
|||||||
|
|
||||||
let eventSource: EventSource | null = null;
|
let eventSource: EventSource | null = null;
|
||||||
let reconnectTimer: ReturnType<typeof setTimeout> | null = null;
|
let reconnectTimer: ReturnType<typeof setTimeout> | null = null;
|
||||||
|
let subscriptionReconnectTimer: ReturnType<typeof setTimeout> | null = null;
|
||||||
let reconnectAttempt = 0;
|
let reconnectAttempt = 0;
|
||||||
let patchVersion = 0;
|
let patchVersion = 0;
|
||||||
let resyncVersion = 0;
|
let resyncVersion = 0;
|
||||||
@@ -74,6 +76,12 @@ function clearReconnectTimer() {
|
|||||||
reconnectTimer = null;
|
reconnectTimer = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function clearSubscriptionReconnectTimer() {
|
||||||
|
if (!subscriptionReconnectTimer) return;
|
||||||
|
clearTimeout(subscriptionReconnectTimer);
|
||||||
|
subscriptionReconnectTimer = null;
|
||||||
|
}
|
||||||
|
|
||||||
function buildSseUrl(baseUrl: string) {
|
function buildSseUrl(baseUrl: string) {
|
||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams();
|
||||||
const cities = subscribedCityList();
|
const cities = subscribedCityList();
|
||||||
@@ -113,6 +121,7 @@ function closeEventSource() {
|
|||||||
function reconnectNow() {
|
function reconnectNow() {
|
||||||
if (typeof window === "undefined") return;
|
if (typeof window === "undefined") return;
|
||||||
clearReconnectTimer();
|
clearReconnectTimer();
|
||||||
|
clearSubscriptionReconnectTimer();
|
||||||
closeEventSource();
|
closeEventSource();
|
||||||
connectSsePatches();
|
connectSsePatches();
|
||||||
}
|
}
|
||||||
@@ -159,6 +168,7 @@ function connectSsePatches() {
|
|||||||
|
|
||||||
function ensureSsePatchConnection() {
|
function ensureSsePatchConnection() {
|
||||||
if (subscribedCities.size === 0) {
|
if (subscribedCities.size === 0) {
|
||||||
|
clearSubscriptionReconnectTimer();
|
||||||
closeEventSource();
|
closeEventSource();
|
||||||
clearReconnectTimer();
|
clearReconnectTimer();
|
||||||
return;
|
return;
|
||||||
@@ -167,6 +177,19 @@ function ensureSsePatchConnection() {
|
|||||||
reconnectNow();
|
reconnectNow();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function scheduleSubscriptionReconnect() {
|
||||||
|
if (typeof window === "undefined") return;
|
||||||
|
if (subscribedCities.size === 0) {
|
||||||
|
ensureSsePatchConnection();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
clearSubscriptionReconnectTimer();
|
||||||
|
subscriptionReconnectTimer = setTimeout(() => {
|
||||||
|
subscriptionReconnectTimer = null;
|
||||||
|
ensureSsePatchConnection();
|
||||||
|
}, SSE_SUBSCRIPTION_RECONNECT_DELAY_MS);
|
||||||
|
}
|
||||||
|
|
||||||
function registerCitySubscription(city: string) {
|
function registerCitySubscription(city: string) {
|
||||||
const cityKey = normalizeCityKey(city);
|
const cityKey = normalizeCityKey(city);
|
||||||
if (!cityKey) return () => {};
|
if (!cityKey) return () => {};
|
||||||
@@ -174,7 +197,7 @@ function registerCitySubscription(city: string) {
|
|||||||
const previousCount = subscribedCities.get(cityKey) ?? 0;
|
const previousCount = subscribedCities.get(cityKey) ?? 0;
|
||||||
subscribedCities.set(cityKey, previousCount + 1);
|
subscribedCities.set(cityKey, previousCount + 1);
|
||||||
if (previousCount === 0) {
|
if (previousCount === 0) {
|
||||||
ensureSsePatchConnection();
|
scheduleSubscriptionReconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
@@ -184,7 +207,7 @@ function registerCitySubscription(city: string) {
|
|||||||
} else {
|
} else {
|
||||||
subscribedCities.set(cityKey, nextCount);
|
subscribedCities.set(cityKey, nextCount);
|
||||||
}
|
}
|
||||||
ensureSsePatchConnection();
|
scheduleSubscriptionReconnect();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -315,6 +315,15 @@ def test_ops_billing_risk_surfaces_trial_payment_referral_and_points(monkeypatch
|
|||||||
DBManager,
|
DBManager,
|
||||||
"list_app_analytics_events",
|
"list_app_analytics_events",
|
||||||
lambda self, limit=20000, since_iso=None: [
|
lambda self, limit=20000, since_iso=None: [
|
||||||
|
{
|
||||||
|
"id": 10,
|
||||||
|
"event_type": "login_start",
|
||||||
|
"user_id": None,
|
||||||
|
"client_id": "c1",
|
||||||
|
"session_id": "session-gap",
|
||||||
|
"created_at": recent,
|
||||||
|
"payload": {"mode": "signup"},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"id": 11,
|
"id": 11,
|
||||||
"event_type": "signup_success",
|
"event_type": "signup_success",
|
||||||
@@ -419,6 +428,108 @@ def test_ops_billing_risk_does_not_flag_signup_when_backend_trial_exists(monkeyp
|
|||||||
assert not any(issue["category"] == "signup_trial" for issue in payload["issues"])
|
assert not any(issue["category"] == "signup_trial" for issue in payload["issues"])
|
||||||
|
|
||||||
|
|
||||||
|
def test_ops_billing_risk_ignores_account_visit_without_signup_intent(monkeypatch):
|
||||||
|
from src.database.db_manager import DBManager
|
||||||
|
|
||||||
|
recent = datetime.now(timezone.utc).isoformat()
|
||||||
|
|
||||||
|
monkeypatch.setattr(ops_api.legacy_routes, "_require_ops_admin", lambda request: {"email": "ops@example.com"})
|
||||||
|
monkeypatch.setattr(ops_api, "_supabase_rest_rows", lambda table, params, *, timeout=10: [])
|
||||||
|
monkeypatch.setattr(
|
||||||
|
DBManager,
|
||||||
|
"list_app_analytics_events",
|
||||||
|
lambda self, limit=20000, since_iso=None: [
|
||||||
|
{
|
||||||
|
"id": 61,
|
||||||
|
"event_type": "signup_success",
|
||||||
|
"user_id": "returning-no-trial-user",
|
||||||
|
"client_id": "client-return",
|
||||||
|
"session_id": "session-return",
|
||||||
|
"created_at": recent,
|
||||||
|
"payload": {
|
||||||
|
"entry": "account_center",
|
||||||
|
"user_id": "returning-no-trial-user",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
],
|
||||||
|
)
|
||||||
|
monkeypatch.setattr(
|
||||||
|
DBManager,
|
||||||
|
"list_payment_audit_events",
|
||||||
|
lambda self, limit=50, event_type=None: [],
|
||||||
|
)
|
||||||
|
|
||||||
|
payload = ops_api.get_ops_billing_risk(None, days=30, limit=20)
|
||||||
|
|
||||||
|
assert payload["summary"]["trial_gaps"] == 0
|
||||||
|
assert not any(issue["category"] == "signup_trial" for issue in payload["issues"])
|
||||||
|
|
||||||
|
|
||||||
|
def test_ops_billing_risk_treats_expired_signup_trial_subscription_as_backend_evidence(monkeypatch):
|
||||||
|
from src.database.db_manager import DBManager
|
||||||
|
|
||||||
|
now = datetime.now(timezone.utc)
|
||||||
|
recent = now.isoformat()
|
||||||
|
expired = (now - timedelta(days=2)).isoformat()
|
||||||
|
|
||||||
|
def fake_supabase_rows(table, params, *, timeout=10):
|
||||||
|
if table == "subscriptions" and params.get("or") == "(source.eq.signup_trial,plan_code.eq.signup_trial_3d)":
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
"id": 81,
|
||||||
|
"user_id": "expired-trial-user",
|
||||||
|
"plan_code": "signup_trial_3d",
|
||||||
|
"source": "signup_trial",
|
||||||
|
"status": "expired",
|
||||||
|
"starts_at": (now - timedelta(days=5)).isoformat(),
|
||||||
|
"expires_at": expired,
|
||||||
|
"created_at": (now - timedelta(days=5)).isoformat(),
|
||||||
|
"updated_at": expired,
|
||||||
|
}
|
||||||
|
]
|
||||||
|
return []
|
||||||
|
|
||||||
|
monkeypatch.setattr(ops_api.legacy_routes, "_require_ops_admin", lambda request: {"email": "ops@example.com"})
|
||||||
|
monkeypatch.setattr(ops_api, "_supabase_rest_rows", fake_supabase_rows)
|
||||||
|
monkeypatch.setattr(
|
||||||
|
DBManager,
|
||||||
|
"list_app_analytics_events",
|
||||||
|
lambda self, limit=20000, since_iso=None: [
|
||||||
|
{
|
||||||
|
"id": 80,
|
||||||
|
"event_type": "login_start",
|
||||||
|
"user_id": None,
|
||||||
|
"client_id": "client-expired",
|
||||||
|
"session_id": "session-expired",
|
||||||
|
"created_at": recent,
|
||||||
|
"payload": {"mode": "signup"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 82,
|
||||||
|
"event_type": "signup_success",
|
||||||
|
"user_id": "expired-trial-user",
|
||||||
|
"client_id": "client-expired",
|
||||||
|
"session_id": "session-expired",
|
||||||
|
"created_at": recent,
|
||||||
|
"payload": {
|
||||||
|
"entry": "account_center",
|
||||||
|
"user_id": "expired-trial-user",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
)
|
||||||
|
monkeypatch.setattr(
|
||||||
|
DBManager,
|
||||||
|
"list_payment_audit_events",
|
||||||
|
lambda self, limit=50, event_type=None: [],
|
||||||
|
)
|
||||||
|
|
||||||
|
payload = ops_api.get_ops_billing_risk(None, days=30, limit=20)
|
||||||
|
|
||||||
|
assert payload["summary"]["trial_gaps"] == 0
|
||||||
|
assert not any(issue["category"] == "signup_trial" for issue in payload["issues"])
|
||||||
|
|
||||||
|
|
||||||
def test_ops_payment_incidents_expose_top_level_reason_and_filters_resolved(monkeypatch):
|
def test_ops_payment_incidents_expose_top_level_reason_and_filters_resolved(monkeypatch):
|
||||||
from src.database.db_manager import DBManager
|
from src.database.db_manager import DBManager
|
||||||
|
|
||||||
|
|||||||
+62
-3
@@ -545,18 +545,41 @@ def get_ops_billing_risk(
|
|||||||
"limit": str(max(safe_limit * 10, 500)),
|
"limit": str(max(safe_limit * 10, 500)),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
subscription_rows = collect(
|
trial_subscription_rows = collect(
|
||||||
"subscriptions",
|
"subscriptions",
|
||||||
{
|
{
|
||||||
"select": (
|
"select": (
|
||||||
"id,user_id,plan_code,source,status,starts_at,expires_at,"
|
"id,user_id,plan_code,source,status,starts_at,expires_at,"
|
||||||
"created_at,updated_at"
|
"created_at,updated_at"
|
||||||
),
|
),
|
||||||
"or": "(source.eq.signup_trial,plan_code.eq.signup_trial_3d,status.eq.active)",
|
"or": "(source.eq.signup_trial,plan_code.eq.signup_trial_3d)",
|
||||||
"order": "created_at.desc",
|
"order": "created_at.desc",
|
||||||
"limit": str(max(safe_limit * 20, 1000)),
|
"limit": str(max(safe_limit * 20, 1000)),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
active_subscription_rows = collect(
|
||||||
|
"subscriptions",
|
||||||
|
{
|
||||||
|
"select": (
|
||||||
|
"id,user_id,plan_code,source,status,starts_at,expires_at,"
|
||||||
|
"created_at,updated_at"
|
||||||
|
),
|
||||||
|
"status": "eq.active",
|
||||||
|
"order": "created_at.desc",
|
||||||
|
"limit": str(max(safe_limit * 20, 1000)),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
subscription_rows: List[Dict[str, Any]] = []
|
||||||
|
seen_subscription_keys: set[str] = set()
|
||||||
|
for row in [*trial_subscription_rows, *active_subscription_rows]:
|
||||||
|
key = str(row.get("id") or "").strip() or (
|
||||||
|
f"{row.get('user_id')}:{row.get('plan_code')}:{row.get('source')}:"
|
||||||
|
f"{row.get('starts_at')}:{row.get('expires_at')}"
|
||||||
|
)
|
||||||
|
if key in seen_subscription_keys:
|
||||||
|
continue
|
||||||
|
seen_subscription_keys.add(key)
|
||||||
|
subscription_rows.append(row)
|
||||||
entitlement_trial_events = collect(
|
entitlement_trial_events = collect(
|
||||||
"entitlement_events",
|
"entitlement_events",
|
||||||
{
|
{
|
||||||
@@ -751,6 +774,40 @@ def get_ops_billing_risk(
|
|||||||
def normalize_user_key(value: Any) -> str:
|
def normalize_user_key(value: Any) -> str:
|
||||||
return str(value or "").strip().lower()
|
return str(value or "").strip().lower()
|
||||||
|
|
||||||
|
def analytics_payload(row: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
|
payload = row.get("payload")
|
||||||
|
return payload if isinstance(payload, dict) else {}
|
||||||
|
|
||||||
|
def analytics_correlation_keys(row: Dict[str, Any]) -> set[str]:
|
||||||
|
payload = analytics_payload(row)
|
||||||
|
keys: set[str] = set()
|
||||||
|
user_id = normalize_user_key(row.get("user_id") or payload.get("user_id"))
|
||||||
|
client_id = str(row.get("client_id") or "").strip()
|
||||||
|
session_id = str(row.get("session_id") or "").strip()
|
||||||
|
if user_id:
|
||||||
|
keys.add(f"user:{user_id}")
|
||||||
|
if client_id:
|
||||||
|
keys.add(f"client:{client_id}")
|
||||||
|
if session_id:
|
||||||
|
keys.add(f"session:{session_id}")
|
||||||
|
return keys
|
||||||
|
|
||||||
|
signup_intent_keys: set[str] = set()
|
||||||
|
for row in events:
|
||||||
|
if str(row.get("event_type") or "").strip().lower() != "login_start":
|
||||||
|
continue
|
||||||
|
payload = analytics_payload(row)
|
||||||
|
mode = str(payload.get("mode") or payload.get("auth_mode") or "").strip().lower()
|
||||||
|
if mode == "signup":
|
||||||
|
signup_intent_keys.update(analytics_correlation_keys(row))
|
||||||
|
|
||||||
|
def has_signup_intent(row: Dict[str, Any]) -> bool:
|
||||||
|
payload = analytics_payload(row)
|
||||||
|
mode = str(payload.get("mode") or payload.get("auth_mode") or "").strip().lower()
|
||||||
|
if mode == "signup" or payload.get("signup_intent") is True:
|
||||||
|
return True
|
||||||
|
return bool(analytics_correlation_keys(row).intersection(signup_intent_keys))
|
||||||
|
|
||||||
trial_actor_keys = {
|
trial_actor_keys = {
|
||||||
_app_analytics_actor_key(row)
|
_app_analytics_actor_key(row)
|
||||||
for row in events
|
for row in events
|
||||||
@@ -817,10 +874,12 @@ def get_ops_billing_risk(
|
|||||||
)
|
)
|
||||||
|
|
||||||
for row in signup_rows[:300]:
|
for row in signup_rows[:300]:
|
||||||
|
if not has_signup_intent(row):
|
||||||
|
continue
|
||||||
actor_key = _app_analytics_actor_key(row)
|
actor_key = _app_analytics_actor_key(row)
|
||||||
if actor_key in trial_actor_keys:
|
if actor_key in trial_actor_keys:
|
||||||
continue
|
continue
|
||||||
payload = row.get("payload") if isinstance(row.get("payload"), dict) else {}
|
payload = analytics_payload(row)
|
||||||
signup_user_id = normalize_user_key(row.get("user_id") or payload.get("user_id"))
|
signup_user_id = normalize_user_key(row.get("user_id") or payload.get("user_id"))
|
||||||
if not signup_user_id:
|
if not signup_user_id:
|
||||||
continue
|
continue
|
||||||
|
|||||||
Reference in New Issue
Block a user