OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
ralph(M10-005): Service factories, runtime path: event, profile, group, misc use their deps; overview + pages classes become factories; realtime
ralph52cebb582762
diff --git a/apps/api/src/main.ts b/apps/api/src/main.tsindex ac271b4be..700ba8e08 100644--- a/apps/api/src/main.ts+++ b/apps/api/src/main.ts@@ -251,6 +251,27 @@ function warnOnUnhandledSchedulers(schedulerIds: string[]): void {}}+/**+ * The boot scope as `ServiceDeps`. `AppDeps` carries a `QueueProducerHandle`;+ * a service wants an already-scoped `QueueProducers`, and outside a request+ * there is nothing to correlate with, so the boot scope stamps+ * `V1_COMPAT_REQUEST_ID` (M10-005 — the ingest consumer's event deps need the+ * same shape the v1-compat seam does).+ */+function bootServiceDeps(+ deps: AppDeps+): Parameters<typeof setV1CompatServices>[0] {+ return {+ db: deps.db,+ ch: deps.ch,+ redis: deps.redis,+ clients: deps.clients,+ buffers: deps.buffers,+ logger: deps.logger,+ queues: deps.producers.scope({ requestId: V1_COMPAT_REQUEST_ID }),+ };+}+/*** The Kafka events consumer. The kafkajs client, the topic, the consumer* group, the DLQ producer and the retry bounds all come from@@ -263,12 +284,16 @@ async function startIngestConsumer(assertKafkaConfigured();enableEventsHeartbeat();- const eventDeps = await loadIncomingEventDeps(logger, async (input) => {- await deps.producers.queues.sessions.session.add(- sessionEndJobPayload(input),- sessionEndEnqueueOptions(input.closedSession.id)- );- });+ const eventDeps = await loadIncomingEventDeps(+ bootServiceDeps(deps),+ logger,+ async (input) => {+ await deps.producers.queues.sessions.session.add(+ sessionEndJobPayload(input),+ sessionEndEnqueueOptions(input.closedSession.id)+ );+ }+ );return await startKafkaEventsConsumer({createConsumer: createKafkaEventsConsumer,@@ -471,15 +496,7 @@ async function main() {// exports with no `Ctx`, so the deps built above are registered once here// for them. Everything with a `Ctx` uses `ctx.services.*`. Deleted with// `packages/trpc` at P10.- setV1CompatServices({- db: deps.db,- ch: deps.ch,- redis: deps.redis,- clients: deps.clients,- buffers: deps.buffers,- logger: deps.logger,- queues: deps.producers.scope({ requestId: V1_COMPAT_REQUEST_ID }),- });+ setV1CompatServices(bootServiceDeps(deps));// HTTP and default metrics register everywhere (TARGET_ARCHITECTURE §18).registerDefaultMetrics();diff --git a/docs/TECH_DEBT.md b/docs/TECH_DEBT.mdindex 5bfeb2301..15327e2eb 100644--- a/docs/TECH_DEBT.md+++ b/docs/TECH_DEBT.md@@ -621,3 +621,192 @@ service factory).- `cd packages/core && bun test` (bare, no `--isolate`): **1457 pass, 12 skip,0 fail**, `Ran 1469 tests across 149 files. [34.00s]`.- `pnpm run check:deps` — the table above.++### 2026-09-06 — M10-005, the runtime path (event, profile, group, misc, overview+pages, realtime) onto `createXService(deps)`++Ran by: ralph (M10-005 implement task, docs/TECH_DEBT.md §4 steps 1/2/5 and+§5a, continuing M10-003/M10-004).++**Shape.** `createEventService`, `createProfileService`, `createGroupService`+and `createMiscService` take `deps` (no longer `_deps`) and bind functions that+use it. `OverviewService` and `PagesService` are no longer classes: their+methods are module-scope functions taking `ServiceDeps` first and otherwise the+same arguments, bound by `createOverviewService(deps)` /+`createPagesService(deps)` under their old method names.+`createRealtimeService(deps)` is new. All three are registered in `services.ts`+as `services.overview`, `services.pages` and `services.realtime`.++**Pages is REGISTERED, not folded in.** Both `OverviewService` and+`PagesService` expose a `getTopPages`, and they are different queries with+different inputs — one over `events` scoped by a search string, one over+`sessions` scoped by chart filters. Folding them into one service would have+had to rename one of them, which is a call-site contract this wave does not+change. `services.pages` is its own entry; `pages.service.ts` keeps its own+file.++**Every lazy `@openpanel/db` import inside those modules is gone.** So is the+`loadDbBuffers()` hop in all three of this wave's modules that used it: the+buffers are `deps.buffers.event` / `.bot` / `.profile` (M8-001), which is the+exact drift this wave was chartered to remove. `misc/src/data-health.ts` no+longer imports `@openpanel/db/src/prisma-client` either — it takes `deps` and+calls `project.service.ts`'s `getLastEventPerProject(deps)` directly.++**Two new files in `shared/`, both because six modules needed the same thing.**++- `shared/ch-query.ts` — `chQuery` / `chQueryWithMeta` over `deps.ch` and+ `deps.logger`. `deps.ch.query` IS `withRetry(client => client.query(...))`,+ the same transport `@openpanel/db`'s `chQuery` uses, and the Int-meta+ coercion is copied verbatim, so result sets are identical (proof below). The+ one field that cannot survive the move is `host`: the retry proxy does not+ report which replica served the query — `chart/src/run-query.ts` (M10-003)+ made the same trade. The gain is the `query info` line now carrying the+ request's id (ADR-018 R1). `overview/src/run-query.ts` is now a three-line+ wrapper over it (`timezone` is mandatory there, optional here).+- `shared/cacheable-per-deps.ts` — `cacheable` for a function that needs+ `ServiceDeps`. `cacheable` keys on EVERY argument through a recursive+ `stringify`, so passing `deps` would serialize the whole Prisma client into+ the Redis key. `deps` is not cache identity anyway, so the cacheable is built+ once per `deps` in a `WeakMap` and the key stays byte-identical —+ `cachable:getEventMetas:<projectId>`, `cachable:getProfiles:<args>`,+ `cachable:getProfilePropertyKeys:<projectId>`. **The explicit `name` is the+ load-bearing part**: `cacheable(fn, ttl)` derives the prefix from `fn.name`,+ and an inline arrow silently drops it.+ **Stated cost:** `createCtx` builds a fresh deps object per request, so the+ L1 LRU inside `cacheable` is now per scope rather than per process. The+ shared L2 Redis cache — the one that actually saves the query — is+ unchanged, so a repeat call costs one Redis GET where it used to cost none.+ Boot-scoped callers (the v1-compat seam, job handlers) keep a process-lived+ L1.++**What is deliberately still lazy in these modules, and why.**+`event.service.ts` keeps `loadCache()` (`@openpanel/redis`'s `getCache` — core+tests that partially mock that package with no `getCache` reach this module+through the barrel), `loadFilterCompiler()` and `loadSessionService()` (a real+core-internal cycle, documented in place; a second dynamic edge on that loop+panics rolldown). None of them reach `@openpanel/db`. `realtime` and `misc`+keep ONE hop each into `v1-compat.ts`'s `compatChHelpers()` for `TABLE_NAMES`+/ `clix` / `formatClickhouseDate` — the pure helpers that live beside the+client, not the client, which is `deps.ch` in both. That is the same seam+`project.service.ts` uses since M10-004 and it exists because ADR-013 converts+the analytics read path one query per P7 task: those statements are still raw+strings and clix builders, not `sql` fragments. `compatChHelpers()` gained+`formatClickhouseDate` and `toNullIfDefaultMinDate` for this.+`profile/src/dates.ts` and `group/src/dates.ts` gained their own+`toNullIfDefaultMinDate` instead — module-local, matching the five existing+`src/dates.ts` copies, because two modules is not "several".++**Blast radius outside the six modules, all forced by a signature change.**+`ingest.service.ts` (`IngestTransport` gains `deps`; `checkIngestBot` gains a+`ServiceDeps` first parameter, because `createBotEvent` needs the bot buffer),+`ingest/src/incoming-event-handler.ts` and `session/src/session-end.ts` (their+`load*Deps` builders take `deps` and bind `createEvent` to it),+`session.jobs.ts`, `apps/api/src/main.ts` (one `bootServiceDeps(deps)` helper,+extracted from the literal `setV1CompatServices` already built),+`chart.service.ts`, `export.routes.ts`, `overview.rpc.ts`, `event.rpc.ts`,+`group.rpc.ts`, `profile.rpc.ts`, `profile.routes.ts`, `realtime.rpc.ts`,+`realtime.routes.ts`, `misc.routes.ts`, `mcp`'s `page-performance.ts`. Three+callers that genuinely have no scope of their own reach the bare v1-compat+spellings, exactly as `loadWindDownDeps` already did for+`getLastEventPerProject`: `cohort.service.ts`'s `listCohortMemberProfiles`,+`session.service.ts`'s `getSessionList`, and+`organization/src/win-back-highlight.ts`. Converting those three modules is+their own task.++`v1-compat.ts` gained 88 bare wrappers plus `overviewService` / `pagesService`+as plain objects of wrappers (`packages/trpc`'s overview router calls+`overviewService.getMetrics.bind(overviewService)`; `bind` on a plain function+is a no-op, so the call site reads identically). `index.ts` now exports those+spellings instead of the raw deps-taking functions, same as M10-003/M10-004+did for their waves.++**`pnpm run check:deps`** (`.dependency-cruiser.cjs`, both rules unchanged at+their landed `warn` severity):++| Rule | Before (M10-004) | After (M10-005) | Delta |+|---|---:|---:|---:|+| `core-uses-ctx-not-db-internals` | 135 | **123** | **−12** |+| `core-no-self-barrel` | 59 | **56** | **−3** |++Exit `0` either way (`warn`, not `error`):+`x 179 dependency violations (0 errors, 179 warnings). 1993 modules, 8050+dependencies cruised.` (was 194.) Thirteen edges removed, one added:++```+REMOVED modules/event/event.service.ts -> db/src/clickhouse/client.ts+REMOVED modules/event/event.service.ts -> db/src/prisma-client.ts+REMOVED modules/group/group.service.ts -> db/src/clickhouse/client.ts+REMOVED modules/misc/misc.service.ts -> db/src/clickhouse/client.ts+REMOVED modules/misc/src/data-health.ts -> db/src/prisma-client.ts+REMOVED modules/overview/src/run-query.ts -> db/src/clickhouse/client.ts+REMOVED modules/overview/src/run-query.ts -> db/src/clickhouse/sql.ts+REMOVED modules/profile/profile.service.ts -> db/src/clickhouse/client.ts+REMOVED modules/realtime/realtime.service.ts -> db/src/clickhouse/client.ts+REMOVED modules/realtime/realtime.service.ts -> db/src/clickhouse/query-builder.ts+REMOVED modules/mcp/.../page-performance.test.ts -> db/src/clickhouse/client.ts+REMOVED modules/mcp/.../profiles.test.ts -> db/src/clickhouse/client.ts+REMOVED modules/realtime/realtime.service.test.ts -> db/src/clickhouse/client.ts+ADDED shared/ch-query.ts -> db/src/clickhouse/sql.ts+```++The one added edge is ADR-013's `sql` tag, which ADR-007 keeps in+`packages/db` by name: a compile-time template tag, no client, no request+scope — the same residual `chart.service.ts`'s header already records. The+`*.sql.ts` files in these modules keep theirs for the same reason.++**ClickHouse — old vs new transport, same data, same day.** No SQL TEXT+changed in this wave; what moved is which client executes it. Run by ralph on+2026-09-06 against the local single node (`http://localhost:8123/openpanel`,+the prod copy, read-only), calling `@openpanel/db`'s `chQuery` and core's new+`chQuery(deps, …)` back to back on each statement and comparing the serialized+result sets:++```+getStats/projects rows old=1864 new=1864 identical=true old=1332.6ms new=1309.1ms+getStats/last24h rows old=1 new=1 identical=true old=6.9ms new=5.6ms+runPingCron rows old=1 new=1 identical=true old=3.3ms new=3.1ms+event.topOrigins rows old=2 new=2 identical=true old=59.7ms new=41.9ms+profile.propertyKeys rows old=6 new=6 identical=true old=10.1ms new=6.3ms+group.types rows old=0 new=0 identical=true old=3.8ms new=3.6ms+```++`single-node` numbers, valid for the self-host topology (docs/ENVIRONMENT.md).+No `Distributed`-table construct was added, removed or reordered — no `IN`+became a `GLOBAL IN` or vice versa — so the cluster topology is unaffected by+this wave.++**Verification, all run by ralph on 2026-09-06:**++- `pnpm run typecheck` — all 25 workspaces, `Done`.+- `cd packages/core && bun test` (bare): **1457 pass, 12 skip, 0 fail**,+ `Ran 1469 tests across 149 files. [31.14s]`.+- `cd packages/core && bun test --isolate`: **1457 pass, 12 skip, 0 fail**,+ `Ran 1469 tests across 149 files. [102.80s]`.+- `pnpm run check:deps` — the table above.+- `verification/harness start` → `cd apps/api && pnpm run e2e:sessions`:+ **29/29 checks passed** (profile, group and event flushes are all on that+ path).+- `verification/golden/compare.sh`: **`passed: 131/137`, 0 diffs in the 131+ validated**; the other 6 are the calendar-stale clock-anchored cases the+ harness itself skips (`captured on 2026-09-05, replayed on 2026-09-06` —+ `export-events-no-dates`, `insights-overview-range-30d`,+ `insights-retention-cohort`, and the three+ `insights-pages-performance-*`). Re-capturing is an operator/verify task,+ not an implement one. **137/137 was not reachable on 2026-09-06** — see the+ task report.+- `verification/harness stop`, then `verification/full.sh`.++**Left for later, named not fixed.**++- `getEventMetasCached`, `getProfilesCached` and `getProfilePropertyKeysCached`+ keep V1's Redis key because `cacheablePerDeps` takes an explicit `name`.+ **M10-004's `getClientByIdCached` (client.service.ts) and+ `getProjectByIdCached` (project.service.ts) do NOT**: both were converted to+ `cacheable((id) => …, ttl)` with an inline arrow, whose `fn.name` is `''`, so+ their prefix is `cachable:` for both. That is a cache-key change (a cold+ window) AND a potential cross-function collision — `cachable::<id>` is the+ same key for a client id and a project id. Out of this task's scope; needs+ its own fix, which is one `cacheable(NAME, …)` argument each.+- `session/src/session-end.ts` still lazy-loads `chQuery` from+ `@openpanel/db/src/clickhouse/client`, and `ingest/src/incoming-event-handler.ts`+ still reaches `sessionBuffer` through `loadDbBuffers()`. Both are the+ `session`/`ingest` modules' own conversion, not this wave's.diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex 09cb9eb1b..1c4b843b5 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -486,31 +486,10 @@ export type {QueryEventsInput,} from './modules/event/event.service';export {- createBotEvent,- createEvent,EVENT_COLUMNS,- getBotEventsPage,- getConversionEventNames,- getConversionListPage,- getEventById,- getEventDetails,- getEventList,- getEventListPage,- getEventMetas,- getEventMetasCached,- getEventPropertyValuesCore,- getEvents,- getEventsCount,- getTopEventNames,- getTopOrigins,- getTopPages,- listEventNamesCore,- listEventPropertiesCore,- queryEventsCore,transformEvent,transformMinimalEvent,transformSessionToEvent,- updateEventMeta,} from './modules/event/event.service';export type { ExportService } from './modules/export/export.service';export { createExportService } from './modules/export/export.service';@@ -521,30 +500,6 @@ export type {IServiceGroupStats,IServiceUpsertGroup,} from './modules/group/group.service';-export {- createGroup,- deleteGroup,- findGroupsCore,- getGroupActivity,- getGroupById,- getGroupCore,- getGroupList,- getGroupListCount,- getGroupListPage,- getGroupMemberGrowth,- getGroupMemberProfiles,- getGroupMemberProfilesPage,- getGroupMetrics,- getGroupMostEvents,- getGroupPopularRoutes,- getGroupPropertyKeys,- getGroupStats,- getGroupsByIds,- getGroupTypes,- listGroupTypesCore,- updateGroup,- upsertGroup,-} from './modules/group/group.service';// Dissolved from @openpanel/db's src/gsc.ts + services/gsc.service.ts// (M5-002) — apps/worker's gsc job file, apps/api's gsc OAuth callback// controller and packages/trpc's gsc router call these directly, the same@@ -743,15 +698,11 @@ export type {IGetUserJourneyInput,ILiveData,ILiveMinuteCount,+ OverviewService,SegmentDailyPoint,TrafficColumn,} from './modules/overview/overview.service';export {- getAnalyticsOverviewCore,- getSegmentDailySeriesCore,- getTrafficBreakdownCore,- OverviewService,- overviewService,zGetMapDataInput,zGetMetricsInput,zGetTopEntryExitInput,@@ -767,14 +718,7 @@ export type {IPageConversionRow,IPageTimeseriesRow,ITopPage,-} from './modules/overview/pages.service';-export {- getEntryExitPagesCore,- getPageConversionsCore,- getPagePerformanceCore,- getTopPagesCore,PagesService,- pagesService,} from './modules/overview/pages.service';export type {AdjustProfilePropertyResult,@@ -787,31 +731,7 @@ export type {IServiceUpsertProfile,ProfileRequestContext,} from './modules/profile/profile.service';-export {- adjustProfileProperty,- findProfilesCore,- getPowerUsers,- getProfileActivity,- getProfileById,- getProfileList,- getProfileListCount,- getProfileListPage,- getProfileMetrics,- getProfileMetricsCore,- getProfileMostEvents,- getProfilePopularRoutes,- getProfilePropertyKeys,- getProfilePropertyKeysCached,- getProfilePropertyNames,- getProfileSessionsCore,- getProfiles,- getProfilesCached,- getProfileValues,- getProfileWithEvents,- identifyProfile,- transformProfile,- upsertProfile,-} from './modules/profile/profile.service';+export { transformProfile } from './modules/profile/profile.service';// Dissolved from @openpanel/db's services/project.service.ts (M6-002) —// packages/trpc's project router, apps/api's manage controller and several// core modules' `src/access.ts` call these directly, the same way V1@@ -836,14 +756,6 @@ export type {RealtimeBadgeDetailScope,RealtimeLocation,} from './modules/realtime/realtime.service';-export {- getRealtimeActiveSessions,- getRealtimeCoordinates,- getRealtimeGeo,- getRealtimeMapBadgeDetails,- getRealtimePaths,- getRealtimeReferrals,-} from './modules/realtime/realtime.service';// Moved from packages/db/src/services/reference.service.ts, plus the// query/mutation bodies packages/trpc/src/routers/reference.ts held inline// (M6-004) — packages/trpc's reference router calls these directly, the same@@ -1149,9 +1061,6 @@ export {getFavicon,getGeoReport,getOgImage,- getStats,- insertPingRecord,- runPingCron,} from './modules/misc/misc.service';// Moved from packages/db/src/services/salt.service.ts +// apps/worker/src/jobs/cron.salt.ts (M8-004) — `main.ts` calls@@ -1304,6 +1213,7 @@ export { assertSafeUrl, createPinnedLookup } from './shared/ssrf';// `ctx.services.*` instead. Deleted whole with `packages/trpc` at P10.export {AggregateChartEngine,+ adjustProfileProperty,applySaveDiscount,buildFunnelBase,ChartEngine,@@ -1313,8 +1223,11 @@ export {clearClientByIdCache,compatCh,compatDb,+ createBotEvent,createClientForOrganization,createDashboard,+ createEvent,+ createGroup,createProjectForOrganization,createReference,createReport,@@ -1324,6 +1237,7 @@ export {deleteClientForOrganization,deleteConversation,deleteDashboard,+ deleteGroup,deleteProjectForOrganization,deleteReference,deleteReport,@@ -1334,6 +1248,10 @@ export {executeAggregateChart,executeChart,extendSessionCookie,+ findGroupsCore,+ findProfilesCore,+ getAnalyticsOverviewCore,+ getBotEventsPage,getChartBucketProfiles,getChartPropertyValues,getChartReferences,@@ -1345,16 +1263,61 @@ export {getConversationById,getConversion,getConversionChart,+ getConversionEventNames,+ getConversionListPage,getDashboardById,getDashboardByIdOrThrow,getDashboardsByProjectId,getEngagementCore,+ getEntryExitPagesCore,+ getEventById,+ getEventDetails,+ getEventList,+ getEventListPage,+ getEventMetas,+ getEventPropertyValuesCore,+ getEvents,+ getEventsCount,getFunnel,getFunnelChart,getFunnelCore,getFunnelProfileIds,getFunnelStepProfiles,+ getGroupActivity,+ getGroupById,+ getGroupCore,+ getGroupList,+ getGroupListCount,+ getGroupListPage,+ getGroupMemberGrowth,+ getGroupMemberProfiles,+ getGroupMemberProfilesPage,+ getGroupMetrics,+ getGroupMostEvents,+ getGroupPopularRoutes,+ getGroupPropertyKeys,+ getGroupStats,+ getGroupsByIds,+ getGroupTypes,getLastEventPerProject,+ getPageConversionsCore,+ getPagePerformanceCore,+ getPowerUsers,+ getProfileActivity,+ getProfileById,+ getProfileList,+ getProfileListCount,+ getProfileListPage,+ getProfileMetrics,+ getProfileMetricsCore,+ getProfileMostEvents,+ getProfilePopularRoutes,+ getProfilePropertyKeys,+ getProfilePropertyNames,+ getProfileSessionsCore,+ getProfiles,+ getProfileValues,+ getProfileWithEvents,getProjectActivationStatus,getProjectById,getProjectByIdCached,@@ -1363,6 +1326,12 @@ export {getProjectForOrganization,getProjects,getProjectWithClients,+ getRealtimeActiveSessions,+ getRealtimeCoordinates,+ getRealtimeGeo,+ getRealtimeMapBadgeDetails,+ getRealtimePaths,+ getRealtimeReferrals,getReferenceById,getReferenceByIdOrThrow,getReportById,@@ -1380,6 +1349,7 @@ export {getSalts,getSankey,getSankeyChart,+ getSegmentDailySeriesCore,getShareByProjectId,getShareDashboard,getShareDashboardByDashboardId,@@ -1393,17 +1363,28 @@ export {getShareReportById,getShareReportByReportId,getShareReportSettings,+ getStats,+ getTopEventNames,+ getTopOrigins,+ getTopPages,+ getTopPagesCore,getTotpStatus,+ getTrafficBreakdownCore,getUsage,getUserAccount,getUserById,getUserFlowCore,getWeeklyRetentionSeriesCore,+ identifyProfile,+ insertPingRecord,listChartEvents,listChartProperties,listClientsForOrganization,listConversations,listDashboardsCore,+ listEventNamesCore,+ listEventPropertiesCore,+ listGroupTypesCore,listProducts,listProjectsCore,listProjectsForOrganization,@@ -1411,8 +1392,11 @@ export {listReportsCore,listUserDeletionBlockers,moveReport,+ overviewService,+ pagesService,pauseSubscription,portal,+ queryEventsCore,regenerateTotpRecoveryCodes,requestPasswordReset,resetPasswordWithToken,@@ -1420,6 +1404,7 @@ export {resetV1CompatServicesForTests,resolveClientProjectId,resumeSubscription,+ runPingCron,scheduleProjectDeletion,setupTotp,setV1CompatServices,@@ -1430,12 +1415,16 @@ export {signUpWithEmail,updateClientForOrganization,updateDashboard,+ updateEventMeta,+ updateGroup,updateProjectForOrganization,updateReference,updateReport,updateReportLayout,updateUserProfile,upsertConversationTitle,+ upsertGroup,+ upsertProfile,V1_COMPAT_REQUEST_ID,validateOverviewShareAccess,validateReportAccess,diff --git a/packages/core/src/modules/chart/chart.service.ts b/packages/core/src/modules/chart/chart.service.tsindex 63fdf7657..3bad699d5 100644--- a/packages/core/src/modules/chart/chart.service.ts+++ b/packages/core/src/modules/chart/chart.service.ts@@ -198,6 +198,7 @@ const FIXED_FILTER_PROPERTIES = [];async function getProfilesInBatches(+ deps: ServiceDeps,ids: string[],projectId: string,batchSize: number@@ -206,6 +207,7 @@ async function getProfilesInBatches(for (let index = 0; index < ids.length; index += batchSize) {profiles.push(...(await getProfilesCached(+ deps,ids.slice(index, index + batchSize),projectId))@@ -328,7 +330,7 @@ export async function listChartEvents(deps,eventNamesWithCountQuery(projectId)),- getEventMetasCached(projectId),+ getEventMetasCached(deps, projectId),]);return [@@ -359,7 +361,7 @@ export async function listChartProperties(): Promise<string[]> {const { projectId, event } = input;const [profileKeys, eventKeys] = await Promise.all([- getProfilePropertyKeysCached(projectId),+ getProfilePropertyKeysCached(deps, projectId),runQuery<{ property_key: string; created_at: string }>(deps,eventPropertyKeysQuery(projectId, event, EVENT_PROPERTY_KEY_LIMIT)@@ -670,7 +672,12 @@ export async function getChartBucketProfiles(if (ids.length === 0) {return [];}- return getProfilesInBatches(ids, input.projectId, BUCKET_PROFILES_BATCH_SIZE);+ return getProfilesInBatches(+ deps,+ ids,+ input.projectId,+ BUCKET_PROFILES_BATCH_SIZE+ );}export interface FunnelStepProfilesRequest {@@ -730,7 +737,7 @@ export async function getFunnelStepProfiles(if (ids.length === 0) {return [];}- return getProfilesInBatches(ids, projectId, FUNNEL_PROFILES_BATCH_SIZE);+ return getProfilesInBatches(deps, ids, projectId, FUNNEL_PROFILES_BATCH_SIZE);}// --- service -----------------------------------------------------------------diff --git a/packages/core/src/modules/cohort/cohort.jobs.test.ts b/packages/core/src/modules/cohort/cohort.jobs.test.tsindex 4357360a5..c8af1b733 100644--- a/packages/core/src/modules/cohort/cohort.jobs.test.ts+++ b/packages/core/src/modules/cohort/cohort.jobs.test.ts@@ -44,6 +44,9 @@ function stubJobCtx(cohort: Partial<CohortService>, jobName = 'test'): JobCtx {profile: {} as Services['profile'],group: {} as Services['group'],chart: {} as Services['chart'],+ overview: {} as Services['overview'],+ pages: {} as Services['pages'],+ realtime: {} as Services['realtime'],misc: {} as Services['misc'],report: {} as Services['report'],dashboard: {} as Services['dashboard'],diff --git a/packages/core/src/modules/cohort/cohort.service.ts b/packages/core/src/modules/cohort/cohort.service.tsindex e49f59bc2..c317a991c 100644--- a/packages/core/src/modules/cohort/cohort.service.ts+++ b/packages/core/src/modules/cohort/cohort.service.ts@@ -784,9 +784,11 @@ export async function listCohortMemberProfiles({}): Promise<{ data: IServiceProfile[]; count: number }> {const { chQuery } = await loadChClient();const { buildFilterWhere } = await import('../chart/src/table-filter-where');- const { getProfiles, profileSearchSql } = await import(- '../profile/profile.service'- );+ const { profileSearchSql } = await import('../profile/profile.service');+ // M10-005: `getProfiles` takes `ServiceDeps` now and this function has none+ // — packages/trpc's cohort router still calls it bare — so it reaches the+ // v1-compat spelling. Converting this module is its own task.+ const { getProfiles } = await import('../../v1-compat');const offset = Math.max(0, (cursor ?? 0) * take);const searchClause = profileSearchSql(search);diff --git a/packages/core/src/modules/event/event.rpc.ts b/packages/core/src/modules/event/event.rpc.tsindex c75c69f40..c1d3ce113 100644--- a/packages/core/src/modules/event/event.rpc.ts+++ b/packages/core/src/modules/event/event.rpc.ts@@ -10,9 +10,10 @@// `bots` was V1's only `publicProcedure` here (anonymous callers were let in// when a share-overview row existed); ADR-011 makes it protected.//-// `pages` / `pagesTimeseries` / `previousPages` / `pageTimeseries` read from-// `pagesService`, which the overview module owns (its own task) — the bodies-// are ported verbatim and reach it lazily through `@openpanel/db`.+// `pages` / `pagesTimeseries` / `previousPages` / `pageTimeseries` read the+// overview module's pages service; since M10-005 that is `ctx.services.pages`,+// so the three-way lazy `import('@openpanel/core')` hop this file used to make+// for it is gone.import {zChartEventFilter,@@ -22,6 +23,8 @@ import {import { z } from 'zod';import { createTRPCRouter, procedure, type TrpcContext } from '../../rpc/base';import { TRPCAccessError, TRPCNotFoundError } from '../../rpc/errors';+import { getChartStartEndDate } from '../../shared/date';+import { getSettingsForProject } from '../organization/organization.service';import {getBotEventsPage,getConversionEventNames,@@ -47,24 +50,6 @@ const zChartWindow = z.object({interval: zTimeInterval,});-function loadPagesRuntime() {- return Promise.all([- import('@openpanel/core'),- import('@openpanel/core'),- import('@openpanel/core'),- ]).then(- ([- { getSettingsForProject },- { getChartStartEndDate },- { pagesService },- ]) => ({- getSettingsForProject,- getChartStartEndDate,- pagesService,- })- );-}-function requireLogin(userId: string | null | undefined): string {if (!userId) {throw new TRPCAccessError('Not authenticated');@@ -102,13 +87,13 @@ export const eventRouter = createTRPCRouter({.mutation(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'write');- return updateEventMeta(input);+ return updateEventMeta(ctx, input);}),byId: procedure.input(zEventRef).query(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'read');- const event = await getEventById(input);+ const event = await getEventById(ctx, input);if (!event) {throw new TRPCNotFoundError('Event not found');}@@ -118,7 +103,7 @@ export const eventRouter = createTRPCRouter({details: procedure.input(zEventRef).query(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'read');- const details = await getEventDetails(input);+ const details = await getEventDetails(ctx, input);if (!details) {throw new TRPCNotFoundError('Event not found');}@@ -144,7 +129,7 @@ export const eventRouter = createTRPCRouter({.query(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'read');- return getEventListPage(input);+ return getEventListPage(ctx, input);}),conversionNames: procedure@@ -152,7 +137,7 @@ export const eventRouter = createTRPCRouter({.query(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'read');- return getConversionEventNames(input.projectId);+ return getConversionEventNames(ctx, input.projectId);}),conversions: procedure@@ -169,7 +154,7 @@ export const eventRouter = createTRPCRouter({.query(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'read');- return getConversionListPage(input);+ return getConversionListPage(ctx, input);}),bots: procedure@@ -183,7 +168,7 @@ export const eventRouter = createTRPCRouter({.query(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'read');- return getBotEventsPage(input);+ return getBotEventsPage(ctx, input);}),pages: procedure@@ -197,11 +182,9 @@ export const eventRouter = createTRPCRouter({.query(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'read');- const { getSettingsForProject, getChartStartEndDate, pagesService } =- await loadPagesRuntime();const { timezone } = await getSettingsForProject(input.projectId);const { startDate, endDate } = getChartStartEndDate(input, timezone);- return pagesService.getTopPages({+ return ctx.services.pages.getTopPages({projectId: input.projectId,startDate,endDate,@@ -216,11 +199,9 @@ export const eventRouter = createTRPCRouter({.query(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'read');- const { getSettingsForProject, getChartStartEndDate, pagesService } =- await loadPagesRuntime();const { timezone } = await getSettingsForProject(input.projectId);const { startDate, endDate } = getChartStartEndDate(input, timezone);- return pagesService.getPageTimeseries({+ return ctx.services.pages.getPageTimeseries({projectId: input.projectId,startDate,endDate,@@ -232,8 +213,6 @@ export const eventRouter = createTRPCRouter({previousPages: procedure.input(zChartWindow).query(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'read');- const { getSettingsForProject, getChartStartEndDate, pagesService } =- await loadPagesRuntime();const { timezone } = await getSettingsForProject(input.projectId);const { startDate, endDate } = getChartStartEndDate(input, timezone);@@ -243,7 +222,7 @@ export const eventRouter = createTRPCRouter({const previousEnd = new Date(startMs - 1);const previousStart = new Date(previousEnd.getTime() - duration);- return pagesService.getTopPages({+ return ctx.services.pages.getTopPages({projectId: input.projectId,startDate: formatClickhouseDateTime(previousStart),endDate: formatClickhouseDateTime(previousEnd),@@ -256,11 +235,9 @@ export const eventRouter = createTRPCRouter({.query(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'read');- const { getSettingsForProject, getChartStartEndDate, pagesService } =- await loadPagesRuntime();const { timezone } = await getSettingsForProject(input.projectId);const { startDate, endDate } = getChartStartEndDate(input, timezone);- return pagesService.getPageTimeseries({+ return ctx.services.pages.getPageTimeseries({projectId: input.projectId,startDate,endDate,@@ -276,6 +253,6 @@ export const eventRouter = createTRPCRouter({.query(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'read');- return getTopOrigins(input.projectId);+ return getTopOrigins(ctx, input.projectId);}),});diff --git a/packages/core/src/modules/event/event.service.ts b/packages/core/src/modules/event/event.service.tsindex 226c23e40..85c04abc1 100644--- a/packages/core/src/modules/event/event.service.ts+++ b/packages/core/src/modules/event/event.service.ts@@ -17,18 +17,25 @@// custom(sb)` — its only caller (event.conversions) is the typed// `conversionNames` option instead, since there is no builder to hand out.//-// db/ch/buffer access is LAZY (`load*` below) for the reason-// insight.service.ts's header gives: constructing @openpanel/db's clients at-// import time spawns a pino-pretty worker per `bun test --isolate` file.+// M10-005: every function that touches a database takes `ServiceDeps` and+// reaches Postgres as `deps.db`, ClickHouse as `deps.ch` (through+// shared/ch-query.ts) and the event/bot buffers as `deps.buffers.*`. The+// `loadDb` / `loadChClient` / `loadDbBuffers` lazy loaders are gone, and so+// are the two `import('@openpanel/core')` self-barrel hops this file made for+// `resolveMaxLookbackDays` / `resolveDateRange` — both are imported straight+// from `shared/` (docs/TECH_DEBT.md §2, §4). What stays lazy is named and+// argued at each remaining `load*` below; none of them reach @openpanel/db.import { DateTime, toDots } from '@openpanel/common';import type { SqlFragment } from '@openpanel/db/src/clickhouse/sql';import type { EventMeta, Prisma } from '@openpanel/db/src/prisma-client';-import { cacheable } from '@openpanel/redis';import type { IChartEventFilter } from '@openpanel/validation';import { clone, mergeDeepRight, uniq } from 'ramda';-import { loadDbBuffers } from '../../buffers/lazy-db-buffers';import type { ServiceDeps } from '../../services';+import { cacheablePerDeps } from '../../shared/cacheable-per-deps';+import { chQuery } from '../../shared/ch-query';+import { resolveDateRange } from '../../shared/date';+import { resolveMaxLookbackDays } from '../../shared/lookback';import { getEventFiltersWhereClause } from '../chart/src/filter-where';import {getProfileById,@@ -66,6 +73,9 @@ export {} from './src/event.sql';const EVENT_METAS_CACHE_SECONDS = 60 * 5;+// V1's `cacheable(getEventMetas, ...)` derived this from the function's own+// name; naming it keeps the Redis key `cachable:getEventMetas:<projectId>`.+const EVENT_METAS_CACHE_NAME = 'getEventMetas';const TOP_EVENT_NAMES_CACHE_SECONDS = 60 * 10;const EVENT_LIST_DEFAULT_LOOKBACK_DAYS = 0.5;const EVENT_LIST_MAX_LOOKBACK_DAYS_DEFAULT = 365 * 5;@@ -91,17 +101,9 @@ const QUERY_EVENTS_FILTER_TARGET = {groupsExpr: 'groups',} as const;-function loadChClient() {- return import('@openpanel/db/src/clickhouse/client');-}--function loadDb() {- return import('@openpanel/db/src/prisma-client').then((m) => m.db);-}-// Lazy for the same reason subscription.service.ts's is: core tests that// partially mock `@openpanel/redis` (no `getCache`) reach this module through-// @openpanel/db's re-export shims and the core barrel.+// the core barrel.function loadCache() {return import('@openpanel/redis').then((m) => m.getCache);}@@ -110,10 +112,6 @@ function loadFilterCompiler() {return import('../chart/src/table-filter-where');}-function loadLookback() {- return import('@openpanel/core');-}-// Lazy: session.service → session-end → this module is a static chain, and a// static edge back would be a plain import cycle evaluated in the wrong order.// Keep this the ONLY dynamic edge on that loop — a second one (e.g. session-end@@ -123,10 +121,6 @@ function loadSessionService() {return import('../session/session.service');}-function loadDateService() {- return import('@openpanel/core');-}-export type IImportedEvent = Omit<IClickhouseEvent,'properties' | 'profile' | 'meta' | 'imported_at'@@ -413,16 +407,16 @@ export function transformMinimalEvent(};}-export async function getEventMetas(projectId: string) {- const db = await loadDb();- return db.eventMeta.findMany({+export async function getEventMetas(deps: ServiceDeps, projectId: string) {+ return deps.db.eventMeta.findMany({where: {projectId,},});}-export const getEventMetasCached = cacheable(+export const getEventMetasCached = cacheablePerDeps(+ EVENT_METAS_CACHE_NAME,getEventMetas,EVENT_METAS_CACHE_SECONDS);@@ -443,11 +437,15 @@ function emptyProfile(profileId: string, projectId: string): IServiceProfile {};}-async function attachProfiles(events: IClickhouseEvent[], projectId: string) {+async function attachProfiles(+ deps: ServiceDeps,+ events: IClickhouseEvent[],+ projectId: string+) {const ids = events.filter((e) => e.device_id !== e.profile_id).map((e) => e.profile_id);- const profiles = await getProfilesCached(ids, projectId);+ const profiles = await getProfilesCached(deps, ids, projectId);const map = new Map<string, IServiceProfile>();for (const profile of profiles) {@@ -460,8 +458,12 @@ async function attachProfiles(events: IClickhouseEvent[], projectId: string) {}}-async function attachMetas(events: IClickhouseEvent[], projectId: string) {- const metas = await getEventMetasCached(projectId);+async function attachMetas(+ deps: ServiceDeps,+ events: IClickhouseEvent[],+ projectId: string+) {+ const metas = await getEventMetasCached(deps, projectId);const map = new Map<string, EventMeta>();for (const meta of metas) {map.set(meta.name, meta);@@ -472,17 +474,17 @@ async function attachMetas(events: IClickhouseEvent[], projectId: string) {}export async function getEvents(+ deps: ServiceDeps,query: SqlFragment,options: GetEventsOptions = {}): Promise<IServiceEvent[]> {- const { chQuery } = await loadChClient();- const events = await chQuery<IClickhouseEvent>(query);+ const events = await chQuery<IClickhouseEvent>(deps, query);const projectId = events[0]?.project_id;if (options.profile && projectId) {- await attachProfiles(events, projectId);+ await attachProfiles(deps, events, projectId);}if (options.meta && projectId) {- await attachMetas(events, projectId);+ await attachMetas(deps, events, projectId);}return events.map(transformEvent);}@@ -497,7 +499,10 @@ export async function getEvents(* else (session_start, session_end) the session-row update is correctly a* no-op anyway.*/-export async function createEvent(payload: IServiceCreateEventPayloadWithId) {+export async function createEvent(+ deps: ServiceDeps,+ payload: IServiceCreateEventPayloadWithId+) {if (!payload.profileId && payload.deviceId) {payload.profileId = payload.deviceId;}@@ -542,8 +547,7 @@ export async function createEvent(payload: IServiceCreateEventPayloadWithId) {groups: payload.groups ?? [],};- const { eventBuffer } = await loadDbBuffers();- eventBuffer.add(event);+ deps.buffers.event.add(event);const promises: Promise<unknown>[] = [];@@ -589,7 +593,7 @@ export async function createEvent(payload: IServiceCreateEventPayloadWithId) {// (c) recency queries should derive from event timestamps, not from// profile.last_seen_at.if (payload.name === 'session_start' || payload.name === 'session_end') {- promises.push(upsertProfile(profile, true));+ promises.push(upsertProfile(deps, profile, true));}}@@ -713,6 +717,7 @@ function hasEventListLookback(options: GetEventListOptions): boolean {}export async function getEventList(+ deps: ServiceDeps,options: GetEventListOptions): Promise<IServiceEvent[]> {const {@@ -732,7 +737,6 @@ export async function getEventList(dateIntervalInDays = EVENT_LIST_DEFAULT_LOOKBACK_DAYS,} = options;- const { resolveMaxLookbackDays } = await loadLookback();// Deployment-tunable ceiling for the empty-result lookback (see lookback.ts).const maxLookbackDays = resolveMaxLookbackDays(EVENT_LIST_MAX_LOOKBACK_DAYS_ENV,@@ -770,14 +774,14 @@ export async function getEventList(conversionNames,};- const data = await getEvents(eventListQuery(query), {+ const data = await getEvents(deps, eventListQuery(query), {profile: select.profile ?? true,meta: select.meta ?? true,});// If we dont get any events, try without the cursor windowif (data.length === 0 && hasLookback && lookbackDays < maxLookbackDays) {- return getEventList({+ return getEventList(deps, {...options,dateIntervalInDays: dateIntervalInDays * 2,});@@ -786,16 +790,19 @@ export async function getEventList(return data;}-export async function getEventsCount({- projectId,- profileId,- groupId,- cohortId,- events,- filters,- startDate,- endDate,-}: Omit<GetEventListOptions, 'cursor' | 'take'>): Promise<number> {+export async function getEventsCount(+ deps: ServiceDeps,+ {+ projectId,+ profileId,+ groupId,+ cohortId,+ events,+ filters,+ startDate,+ endDate,+ }: Omit<GetEventListOptions, 'cursor' | 'take'>+): Promise<number> {const { filterClauses, joins } = await compileEventFilters(filters,projectId@@ -811,20 +818,15 @@ export async function getEventsCount({filterClauses,joins,};- const { chQuery } = await loadChClient();- const res = await chQuery<{ count: number }>(eventsCountQuery(query));+ const res = await chQuery<{ count: number }>(deps, eventsCountQuery(query));return res[0]?.count ?? 0;}-export async function createBotEvent({- name,- type,- projectId,- createdAt,- path,-}: IServiceCreateBotEventPayload) {- const { botBuffer } = await loadDbBuffers();- return botBuffer.add({+export async function createBotEvent(+ deps: ServiceDeps,+ { name, type, projectId, createdAt, path }: IServiceCreateBotEventPayload+) {+ return deps.buffers.bot.add({id: crypto.randomUUID(),name,type,@@ -834,9 +836,11 @@ export async function createBotEvent({});}-export async function getConversionEventNames(projectId: string) {- const db = await loadDb();- return db.eventMeta.findMany({+export async function getConversionEventNames(+ deps: ServiceDeps,+ projectId: string+) {+ return deps.db.eventMeta.findMany({where: {projectId,conversion: true,@@ -844,19 +848,22 @@ export async function getConversionEventNames(projectId: string) {});}-export async function getTopPages({- projectId,- cursor,- take,- search,-}: {- projectId: string;- cursor?: number;- take: number;- search?: string;-}): Promise<IServicePage[]> {- const { chQuery } = await loadChClient();+export async function getTopPages(+ deps: ServiceDeps,+ {+ projectId,+ cursor,+ take,+ search,+ }: {+ projectId: string;+ cursor?: number;+ take: number;+ search?: string;+ }+): Promise<IServicePage[]> {return chQuery<IServicePage>(+ deps,topPagesQuery({projectId,take,@@ -867,22 +874,25 @@ export async function getTopPages({}/** V1's `eventService.getById`. */-export async function getEventById({- projectId,- id,- createdAt,-}: {- projectId: string;- id: string;- createdAt?: Date;-}): Promise<IServiceEvent | null> {- const { chQuery } = await loadChClient();+export async function getEventById(+ deps: ServiceDeps,+ {+ projectId,+ id,+ createdAt,+ }: {+ projectId: string;+ id: string;+ createdAt?: Date;+ }+): Promise<IServiceEvent | null> {const [rows, metas] = await Promise.all([chQuery<IClickhouseEvent>(+ deps,eventByIdQuery({ projectId, id, createdAt }),CLIX_SESSION_TIMEZONE),- getEventMetasCached(projectId),+ getEventMetasCached(deps, projectId),]);const row = rows[0];if (!row) {@@ -891,7 +901,7 @@ export async function getEventById({const event = transformEvent(row);if (event.profileId) {- const profile = await getProfileById(event.profileId, projectId);+ const profile = await getProfileById(deps, event.profileId, projectId);if (profile) {event.profile = profile;}@@ -902,14 +912,17 @@ export async function getEventById({return event;}-export async function getTopEventNames(projectId: string): Promise<string[]> {+export async function getTopEventNames(+ deps: ServiceDeps,+ projectId: string+): Promise<string[]> {const getCache = await loadCache();return getCache(`mcp:event-names:${projectId}`,TOP_EVENT_NAMES_CACHE_SECONDS,async () => {- const { chQuery } = await loadChClient();const rows = await chQuery<{ name: string; count: number }>(+ deps,topEventNamesQuery(projectId),CLIX_SESSION_TIMEZONE);@@ -918,8 +931,10 @@ export async function getTopEventNames(projectId: string): Promise<string[]> {);}-export const listEventNamesCore = (projectId: string): Promise<string[]> =>- getTopEventNames(projectId);+export const listEventNamesCore = (+ deps: ServiceDeps,+ projectId: string+): Promise<string[]> => getTopEventNames(deps, projectId);/*** Top-level filterable columns on the `events` table. These apply to@@ -957,28 +972,34 @@ export const EVENT_COLUMNS = [export type IEventColumn = (typeof EVENT_COLUMNS)[number];-export async function listEventPropertiesCore(input: {- projectId: string;- eventName?: string;-}): Promise<{+export async function listEventPropertiesCore(+ deps: ServiceDeps,+ input: {+ projectId: string;+ eventName?: string;+ }+): Promise<{columns: readonly string[];properties: Array<{ property_key: string; event_name: string }>;}> {- const { chQuery } = await loadChClient();const rows = await chQuery<{ property_key: string; event_name: string }>(+ deps,eventPropertiesQuery(input),CLIX_SESSION_TIMEZONE);return { columns: EVENT_COLUMNS, properties: rows };}-export async function getEventPropertyValuesCore(input: {- projectId: string;- eventName: string;- propertyKey: string;-}): Promise<{ event: string; property: string; values: string[] }> {- const { chQuery } = await loadChClient();+export async function getEventPropertyValuesCore(+ deps: ServiceDeps,+ input: {+ projectId: string;+ eventName: string;+ propertyKey: string;+ }+): Promise<{ event: string; property: string; values: string[] }> {const rows = await chQuery<{ value: string }>(+ deps,eventPropertyValuesQuery(input),CLIX_SESSION_TIMEZONE);@@ -1044,14 +1065,13 @@ function queryEventsEquals(// is unique and narrow enough to query directly. Without this, an older// session's events would be silently excluded. A caller that still passes// dates alongside a sessionId is honoured.-async function queryEventsDateRange(+function queryEventsDateRange(input: QueryEventsInput-): Promise<{ start: string; end: string } | undefined> {+): { start: string; end: string } | undefined {const wantsRange = !input.sessionId || input.startDate || input.endDate;if (!wantsRange) {return undefined;}- const { resolveDateRange } = await loadDateService();const { startDate, endDate } = resolveDateRange(input.startDate,input.endDate@@ -1063,6 +1083,7 @@ async function queryEventsDateRange(}export async function queryEventsCore(+ deps: ServiceDeps,input: QueryEventsInput): Promise<IClickhouseEvent[]> {let filterClauses: CompiledFilterClauses = {};@@ -1074,8 +1095,8 @@ export async function queryEventsCore(QUERY_EVENTS_FILTER_TARGET);}- const { chQuery } = await loadChClient();return chQuery<IClickhouseEvent>(+ deps,queryEventsQuery({projectId: input.projectId,sessionId: input.sessionId,@@ -1084,7 +1105,7 @@ export async function queryEventsCore(eventNames: input.eventNames,equals: queryEventsEquals(input),properties: input.properties,- dateRange: await queryEventsDateRange(input),+ dateRange: queryEventsDateRange(input),filterClauses,limit: input.limit ?? QUERY_EVENTS_DEFAULT_LIMIT,}),@@ -1102,16 +1123,12 @@ export interface UpdateEventMetaInput {conversion?: boolean;}-export async function updateEventMeta({- projectId,- name,- icon,- color,- conversion,-}: UpdateEventMetaInput) {- await getEventMetasCached.clear(projectId);- const db = await loadDb();- return db.eventMeta.upsert({+export async function updateEventMeta(+ deps: ServiceDeps,+ { projectId, name, icon, color, conversion }: UpdateEventMetaInput+) {+ await getEventMetasCached.clear(deps, projectId);+ return deps.db.eventMeta.upsert({where: {name_projectId: {name,@@ -1123,12 +1140,15 @@ export async function updateEventMeta({});}-export async function getEventDetails(input: {- projectId: string;- id: string;- createdAt?: Date;-}) {- const event = await getEventById(input);+export async function getEventDetails(+ deps: ServiceDeps,+ input: {+ projectId: string;+ id: string;+ createdAt?: Date;+ }+) {+ const event = await getEventById(deps, input);if (!event) {return null;}@@ -1203,11 +1223,11 @@ function toEventListPage(items: IServiceEvent[]) {};}-export async function getEventListPage({- columnVisibility,- ...input-}: EventListPageInput) {- const items = await getEventList({+export async function getEventListPage(+ deps: ServiceDeps,+ { columnVisibility, ...input }: EventListPageInput+) {+ const items = await getEventList(deps, {projectId: input.projectId,filters: input.filters,profileId: input.profileId ?? undefined,@@ -1234,11 +1254,11 @@ export type ConversionListPageInput = Pick<| 'columnVisibility'>;-export async function getConversionListPage({- columnVisibility,- ...input-}: ConversionListPageInput) {- const conversions = await getConversionEventNames(input.projectId);+export async function getConversionListPage(+ deps: ServiceDeps,+ { columnVisibility, ...input }: ConversionListPageInput+) {+ const conversions = await getConversionEventNames(deps, input.projectId);const filteredConversions = conversions.filter((event) => {if (input.events && input.events.length > 0) {return input.events.includes(event.name);@@ -1250,7 +1270,7 @@ export async function getConversionListPage({return { data: [], meta: { next: null } };}- const items = await getEventList({+ const items = await getEventList(deps, {projectId: input.projectId,startDate: input.startDate ?? undefined,endDate: input.endDate ?? undefined,@@ -1263,21 +1283,24 @@ export async function getConversionListPage({return toEventListPage(items);}-export async function getBotEventsPage({- projectId,- cursor,- limit,-}: {- projectId: string;- cursor?: number;- limit: number;-}) {- const { chQuery } = await loadChClient();+export async function getBotEventsPage(+ deps: ServiceDeps,+ {+ projectId,+ cursor,+ limit,+ }: {+ projectId: string;+ cursor?: number;+ limit: number;+ }+) {const [events, counts] = await Promise.all([chQuery<IClickhouseBotEvent>(+ deps,botEventsQuery({ projectId, limit, offset: (cursor ?? 0) * limit })),- chQuery<{ count: number }>(botEventsCountQuery(projectId)),+ chQuery<{ count: number }>(deps, botEventsCountQuery(projectId)),]);return {@@ -1289,9 +1312,9 @@ export async function getBotEventsPage({};}-export async function getTopOrigins(projectId: string) {- const { chQuery } = await loadChClient();+export async function getTopOrigins(deps: ServiceDeps, projectId: string) {const res = await chQuery<{ origin: string; count: number }>(+ deps,topOriginsQuery(projectId));return res.filter(@@ -1310,6 +1333,9 @@ export interface EventService {): Promise<{ document: IClickhouseEvent }>;}-export function createEventService(_deps: ServiceDeps): EventService {- return { getById: getEventById, create: createEvent };+export function createEventService(deps: ServiceDeps): EventService {+ return {+ getById: (input) => getEventById(deps, input),+ create: (payload) => createEvent(deps, payload),+ };}diff --git a/packages/core/src/modules/event/src/create-event-id.test.ts b/packages/core/src/modules/event/src/create-event-id.test.tsindex 07aebc392..755c1a135 100644--- a/packages/core/src/modules/event/src/create-event-id.test.ts+++ b/packages/core/src/modules/event/src/create-event-id.test.ts@@ -1,23 +1,13 @@// Ported from packages/db/src/services/create-event-id.test.ts (M7-002).-// `createEvent` reaches the buffers lazily (`loadBuffers`), so the buffer-// mock is installed before the subject is imported; spread-actual + snapshot-// restore, same as realtime.service.test.ts.+// M10-005: `createEvent` takes `ServiceDeps` and writes through+// `deps.buffers.event`, so the buffer is handed in rather than mocked onto a+// module specifier — no `mock.module` and no restore to get wrong.-import { afterAll, beforeAll, describe, expect, mock, test } from 'bun:test';+import { beforeAll, describe, expect, mock, test } from 'bun:test';+import type { ServiceDeps } from '../../../services';const add = mock((_event: { id: string }) => undefined);--const actualBuffers = await import('@openpanel/queue/src/buffers');-const realBuffers = { ...actualBuffers };-mock.module('@openpanel/queue/src/buffers', () => ({- ...realBuffers,- eventBuffer: { add },- botBuffer: { add: mock(() => undefined) },-}));--afterAll(() => {- mock.module('@openpanel/queue/src/buffers', () => realBuffers);-});+const deps = { buffers: { event: { add } } } as unknown as ServiceDeps;let createEvent: typeof import('../event.service').createEvent;beforeAll(async () => {@@ -63,9 +53,15 @@ describe('createEvent id', () => {test('uses the producer-minted id for the ClickHouse row', async () => {add.mockClear();- const first = await createEvent({ ...payload, id: PRODUCED_EVENT_ID });+ const first = await createEvent(deps, {+ ...payload,+ id: PRODUCED_EVENT_ID,+ });// The same message redelivered by Kafka.- const second = await createEvent({ ...payload, id: PRODUCED_EVENT_ID });+ const second = await createEvent(deps, {+ ...payload,+ id: PRODUCED_EVENT_ID,+ });expect(add).toHaveBeenCalledTimes(2);expect(add.mock.calls[0]?.[0].id).toBe(PRODUCED_EVENT_ID);@@ -76,8 +72,8 @@ describe('createEvent id', () => {test('falls back to a generated uuid when the payload carries no id', async () => {add.mockClear();- await createEvent({ ...payload });- await createEvent({ ...payload });+ await createEvent(deps, { ...payload });+ await createEvent(deps, { ...payload });expect(add).toHaveBeenCalledTimes(2);const [firstId, secondId] = add.mock.calls.map(([event]) => event.id);diff --git a/packages/core/src/modules/export/export.routes.ts b/packages/core/src/modules/export/export.routes.tsindex f0b6f8287..7f7d22ef1 100644--- a/packages/core/src/modules/export/export.routes.ts+++ b/packages/core/src/modules/export/export.routes.ts@@ -35,6 +35,7 @@ import {zReport,} from '@openpanel/validation';import { z } from 'zod';+import type { Ctx } from '../../context';import type { ClientType } from '../../http/client-auth';import { defineRoutes } from '../../http/define';import { parseQueryStringTransform } from '../../http/query';@@ -71,7 +72,6 @@ import type {import {getAnalyticsOverviewCore,getTrafficBreakdownCore,- overviewService,} from '../overview/overview.service';import {getEntryExitPagesCore,@@ -245,8 +245,8 @@ export const exportRoutes = defineRoutes((app) =>};const [data, totalCount] = await Promise.all([- getEventList(options),- getEventsCount(options),+ getEventList(ctx, options),+ getEventsCount(ctx, options),]);return {@@ -355,6 +355,7 @@ type InsightsClient = Parameters<typeof resolveInsightsProjectId>[0];/** Shared body of the 18 `overviewColumns` routes below (V1's `getOverviewGeneric(column)`). */async function getOverviewGeneric(+ ctx: Ctx,column: IGetTopGenericInput['column'],params: InsightsProjectParam,query: OverviewGenericQuery,@@ -363,7 +364,7 @@ async function getOverviewGeneric(const projectId = await resolveInsightsProjectId(client, params);const { timezone } = await getSettingsForProject(projectId);const { startDate, endDate } = getChartStartEndDate(query, timezone);- return overviewService.getTopGeneric({+ return ctx.services.overview.getTopGeneric({column,projectId,filters: query.filters,@@ -538,13 +539,13 @@ export const insightsRoutes = defineRoutes((app) =>.onTransform(parseQueryStringTransform).get('/insights/:projectId/overview',- async ({ params, query, client }) => {+ async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);const { startDate, endDate } = await resolveInsightsDateRange(projectId,query);- return getAnalyticsOverviewCore({+ return getAnalyticsOverviewCore(ctx, {projectId,startDate,endDate,@@ -613,13 +614,13 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/pages/top',- async ({ params, query, client }) => {+ async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);const { startDate, endDate } = await resolveInsightsDateRange(projectId,query);- return getTopPagesCore({ projectId, startDate, endDate });+ return getTopPagesCore(ctx, { projectId, startDate, endDate });},{clientAuth: CLIENT_ALLOW,@@ -634,13 +635,13 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/pages/entry_exit',- async ({ params, query, client }) => {+ async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);const { startDate, endDate } = await resolveInsightsDateRange(projectId,query);- return getEntryExitPagesCore({+ return getEntryExitPagesCore(ctx, {projectId,startDate,endDate,@@ -659,13 +660,13 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/pages/performance',- async ({ params, query, client }) => {+ async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);const { startDate, endDate } = await resolveInsightsDateRange(projectId,query);- return getPagePerformanceCore({+ return getPagePerformanceCore(ctx, {projectId,startDate,endDate,@@ -685,11 +686,11 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/metrics',- async ({ params, query, client }) => {+ async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);const { timezone } = await getSettingsForProject(projectId);const { startDate, endDate } = getChartStartEndDate(query, timezone);- return overviewService.getMetrics({+ return ctx.services.overview.getMetrics({projectId,filters: query.filters,startDate,@@ -728,11 +729,11 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/pages',- async ({ params, query, client }) => {+ async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);const { timezone } = await getSettingsForProject(projectId);const { startDate, endDate } = getChartStartEndDate(query, timezone);- return overviewService.getTopPages({+ return ctx.services.overview.getTopPages({projectId,filters: query.filters,startDate,@@ -753,8 +754,8 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/referrer',- ({ params, query, client }) =>- getOverviewGeneric('referrer', params, query, client),+ ({ params, query, client, ctx }) =>+ getOverviewGeneric(ctx, 'referrer', params, query, client),{clientAuth: CLIENT_ALLOW,params: projectIdParam,@@ -767,8 +768,8 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/referrer_name',- ({ params, query, client }) =>- getOverviewGeneric('referrer_name', params, query, client),+ ({ params, query, client, ctx }) =>+ getOverviewGeneric(ctx, 'referrer_name', params, query, client),{clientAuth: CLIENT_ALLOW,params: projectIdParam,@@ -781,8 +782,8 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/referrer_type',- ({ params, query, client }) =>- getOverviewGeneric('referrer_type', params, query, client),+ ({ params, query, client, ctx }) =>+ getOverviewGeneric(ctx, 'referrer_type', params, query, client),{clientAuth: CLIENT_ALLOW,params: projectIdParam,@@ -795,8 +796,8 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/utm_source',- ({ params, query, client }) =>- getOverviewGeneric('utm_source', params, query, client),+ ({ params, query, client, ctx }) =>+ getOverviewGeneric(ctx, 'utm_source', params, query, client),{clientAuth: CLIENT_ALLOW,params: projectIdParam,@@ -809,8 +810,8 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/utm_medium',- ({ params, query, client }) =>- getOverviewGeneric('utm_medium', params, query, client),+ ({ params, query, client, ctx }) =>+ getOverviewGeneric(ctx, 'utm_medium', params, query, client),{clientAuth: CLIENT_ALLOW,params: projectIdParam,@@ -823,8 +824,8 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/utm_campaign',- ({ params, query, client }) =>- getOverviewGeneric('utm_campaign', params, query, client),+ ({ params, query, client, ctx }) =>+ getOverviewGeneric(ctx, 'utm_campaign', params, query, client),{clientAuth: CLIENT_ALLOW,params: projectIdParam,@@ -837,8 +838,8 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/utm_term',- ({ params, query, client }) =>- getOverviewGeneric('utm_term', params, query, client),+ ({ params, query, client, ctx }) =>+ getOverviewGeneric(ctx, 'utm_term', params, query, client),{clientAuth: CLIENT_ALLOW,params: projectIdParam,@@ -851,8 +852,8 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/utm_content',- ({ params, query, client }) =>- getOverviewGeneric('utm_content', params, query, client),+ ({ params, query, client, ctx }) =>+ getOverviewGeneric(ctx, 'utm_content', params, query, client),{clientAuth: CLIENT_ALLOW,params: projectIdParam,@@ -865,8 +866,8 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/region',- ({ params, query, client }) =>- getOverviewGeneric('region', params, query, client),+ ({ params, query, client, ctx }) =>+ getOverviewGeneric(ctx, 'region', params, query, client),{clientAuth: CLIENT_ALLOW,params: projectIdParam,@@ -879,8 +880,8 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/country',- ({ params, query, client }) =>- getOverviewGeneric('country', params, query, client),+ ({ params, query, client, ctx }) =>+ getOverviewGeneric(ctx, 'country', params, query, client),{clientAuth: CLIENT_ALLOW,params: projectIdParam,@@ -893,8 +894,8 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/city',- ({ params, query, client }) =>- getOverviewGeneric('city', params, query, client),+ ({ params, query, client, ctx }) =>+ getOverviewGeneric(ctx, 'city', params, query, client),{clientAuth: CLIENT_ALLOW,params: projectIdParam,@@ -907,8 +908,8 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/device',- ({ params, query, client }) =>- getOverviewGeneric('device', params, query, client),+ ({ params, query, client, ctx }) =>+ getOverviewGeneric(ctx, 'device', params, query, client),{clientAuth: CLIENT_ALLOW,params: projectIdParam,@@ -921,8 +922,8 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/brand',- ({ params, query, client }) =>- getOverviewGeneric('brand', params, query, client),+ ({ params, query, client, ctx }) =>+ getOverviewGeneric(ctx, 'brand', params, query, client),{clientAuth: CLIENT_ALLOW,params: projectIdParam,@@ -935,8 +936,8 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/model',- ({ params, query, client }) =>- getOverviewGeneric('model', params, query, client),+ ({ params, query, client, ctx }) =>+ getOverviewGeneric(ctx, 'model', params, query, client),{clientAuth: CLIENT_ALLOW,params: projectIdParam,@@ -949,8 +950,8 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/browser',- ({ params, query, client }) =>- getOverviewGeneric('browser', params, query, client),+ ({ params, query, client, ctx }) =>+ getOverviewGeneric(ctx, 'browser', params, query, client),{clientAuth: CLIENT_ALLOW,params: projectIdParam,@@ -963,8 +964,8 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/browser_version',- ({ params, query, client }) =>- getOverviewGeneric('browser_version', params, query, client),+ ({ params, query, client, ctx }) =>+ getOverviewGeneric(ctx, 'browser_version', params, query, client),{clientAuth: CLIENT_ALLOW,params: projectIdParam,@@ -977,8 +978,8 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/os',- ({ params, query, client }) =>- getOverviewGeneric('os', params, query, client),+ ({ params, query, client, ctx }) =>+ getOverviewGeneric(ctx, 'os', params, query, client),{clientAuth: CLIENT_ALLOW,params: projectIdParam,@@ -991,8 +992,8 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/os_version',- ({ params, query, client }) =>- getOverviewGeneric('os_version', params, query, client),+ ({ params, query, client, ctx }) =>+ getOverviewGeneric(ctx, 'os_version', params, query, client),{clientAuth: CLIENT_ALLOW,params: projectIdParam,@@ -1032,13 +1033,13 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/traffic/referrers',- async ({ params, query, client }) => {+ async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);const { startDate, endDate } = await resolveInsightsDateRange(projectId,query);- return getTrafficBreakdownCore({+ return getTrafficBreakdownCore(ctx, {projectId,startDate,endDate,@@ -1057,13 +1058,13 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/traffic/geo',- async ({ params, query, client }) => {+ async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);const { startDate, endDate } = await resolveInsightsDateRange(projectId,query);- return getTrafficBreakdownCore({+ return getTrafficBreakdownCore(ctx, {projectId,startDate,endDate,@@ -1083,13 +1084,13 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/traffic/devices',- async ({ params, query, client }) => {+ async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);const { startDate, endDate } = await resolveInsightsDateRange(projectId,query);- return getTrafficBreakdownCore({+ return getTrafficBreakdownCore(ctx, {projectId,startDate,endDate,@@ -1149,9 +1150,9 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/events',- async ({ params, query, client }) => {+ async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);- return queryEventsCore({ projectId, ...query });+ return queryEventsCore(ctx, { projectId, ...query });},{clientAuth: CLIENT_ALLOW,@@ -1166,9 +1167,9 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/events/names',- async ({ params, client }) => {+ async ({ params, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);- return listEventNamesCore(projectId);+ return listEventNamesCore(ctx, projectId);},{clientAuth: CLIENT_ALLOW,@@ -1181,9 +1182,9 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/events/properties',- async ({ params, query, client }) => {+ async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);- return listEventPropertiesCore({+ return listEventPropertiesCore(ctx, {projectId,eventName: query.eventName,});@@ -1200,9 +1201,9 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/events/property_values',- async ({ params, query, client }) => {+ async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);- return getEventPropertyValuesCore({ projectId, ...query });+ return getEventPropertyValuesCore(ctx, { projectId, ...query });},{clientAuth: CLIENT_ALLOW,@@ -1216,9 +1217,9 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/profiles',- async ({ params, query, client }) => {+ async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);- return findProfilesCore({ projectId, ...query });+ return findProfilesCore(ctx, { projectId, ...query });},{clientAuth: CLIENT_ALLOW,@@ -1232,9 +1233,10 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/profiles/:profileId',- async ({ params, query, client, status }) => {+ async ({ params, query, client, status, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);const result = await getProfileWithEvents(+ ctx,projectId,params.profileId,query.eventLimit@@ -1259,9 +1261,14 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/profiles/:profileId/sessions',- async ({ params, query, client }) => {+ async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);- return getProfileSessionsCore(projectId, params.profileId, query.limit);+ return getProfileSessionsCore(+ ctx,+ projectId,+ params.profileId,+ query.limit+ );},{clientAuth: CLIENT_ALLOW,@@ -1275,9 +1282,9 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/profiles/:profileId/metrics',- async ({ params, client }) => {+ async ({ params, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);- return getProfileMetricsCore({+ return getProfileMetricsCore(ctx, {projectId,profileId: params.profileId,});@@ -1309,9 +1316,9 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/groups/types',- async ({ params, client }) => {+ async ({ params, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);- return listGroupTypesCore(projectId);+ return listGroupTypesCore(ctx, projectId);},{clientAuth: CLIENT_ALLOW,@@ -1324,9 +1331,9 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/groups',- async ({ params, query, client }) => {+ async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);- return findGroupsCore({ projectId, ...query });+ return findGroupsCore(ctx, { projectId, ...query });},{clientAuth: CLIENT_ALLOW,@@ -1340,9 +1347,9 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/groups/:groupId',- async ({ params, query, client }) => {+ async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);- return getGroupCore({+ return getGroupCore(ctx, {projectId,groupId: params.groupId,memberLimit: query.memberLimit,diff --git a/packages/core/src/modules/group/group.rpc.ts b/packages/core/src/modules/group/group.rpc.tsindex 28c3287b6..27259eff9 100644--- a/packages/core/src/modules/group/group.rpc.ts+++ b/packages/core/src/modules/group/group.rpc.ts@@ -66,32 +66,32 @@ export const groupRouter = createTRPCRouter({.query(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'read');- return getGroupListPage(input);+ return getGroupListPage(ctx, input);}),byId: procedure.input(zGroupRef).query(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'read');- return getGroupById(input.id, input.projectId);+ return getGroupById(ctx, input.id, input.projectId);}),create: procedure.input(zCreateGroup).mutation(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'write');- return createGroup(input);+ return createGroup(ctx, input);}),update: procedure.input(zUpdateGroup).mutation(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'write');const { id, projectId, ...data } = input;- return updateGroup(id, projectId, data);+ return updateGroup(ctx, id, projectId, data);}),delete: procedure.input(zGroupRef).mutation(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'write');- return deleteGroup(input.id, input.projectId);+ return deleteGroup(ctx, input.id, input.projectId);}),types: procedure@@ -99,25 +99,25 @@ export const groupRouter = createTRPCRouter({.query(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'read');- return getGroupTypes(input.projectId);+ return getGroupTypes(ctx, input.projectId);}),metrics: procedure.input(zGroupRef).query(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'read');- return getGroupMetrics(input.id, input.projectId);+ return getGroupMetrics(ctx, input.id, input.projectId);}),activity: procedure.input(zGroupRef).query(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'read');- return getGroupActivity(input.id, input.projectId);+ return getGroupActivity(ctx, input.id, input.projectId);}),memberGrowth: procedure.input(zGroupRef).query(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'read');- return getGroupMemberGrowth(input.id, input.projectId);+ return getGroupMemberGrowth(ctx, input.id, input.projectId);}),listProfiles: procedure@@ -133,19 +133,19 @@ export const groupRouter = createTRPCRouter({.query(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'read');- return getGroupMemberProfilesPage(input);+ return getGroupMemberProfilesPage(ctx, input);}),mostEvents: procedure.input(zGroupRef).query(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'read');- return getGroupMostEvents(input.id, input.projectId);+ return getGroupMostEvents(ctx, input.id, input.projectId);}),popularRoutes: procedure.input(zGroupRef).query(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'read');- return getGroupPopularRoutes(input.id, input.projectId);+ return getGroupPopularRoutes(ctx, input.id, input.projectId);}),properties: procedure@@ -153,7 +153,7 @@ export const groupRouter = createTRPCRouter({.query(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'read');- return getGroupPropertyKeys(input.projectId);+ return getGroupPropertyKeys(ctx, input.projectId);}),listByIds: procedure@@ -161,6 +161,6 @@ export const groupRouter = createTRPCRouter({.query(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'read');- return getGroupsByIds(input.projectId, input.ids);+ return getGroupsByIds(ctx, input.projectId, input.ids);}),});diff --git a/packages/core/src/modules/group/group.service.ts b/packages/core/src/modules/group/group.service.tsindex c2917e78e..ce53d5d07 100644--- a/packages/core/src/modules/group/group.service.ts+++ b/packages/core/src/modules/group/group.service.ts@@ -7,14 +7,16 @@// Every query is a `sql` fragment (src/group.sql.ts), converted one at a time// with a result-set proof each, per ADR-013.//-// db/ch access is LAZY (`loadChClient`) for the reason insight.service.ts's-// header gives: constructing @openpanel/db's clients at import time spawns a-// pino-pretty worker per `bun test --isolate` file.+// M10-005: every function takes `ServiceDeps` and reaches ClickHouse as+// `deps.ch` — reads through shared/ch-query.ts, the one write (`writeGroupToCh`)+// through `deps.ch.insert`. The `loadChClient` lazy loader is gone+// (docs/TECH_DEBT.md §2, §4).import { toDots } from '@openpanel/common';import type { ServiceDeps } from '../../services';+import { chQuery } from '../../shared/ch-query';import { getProfiles, type IServiceProfile } from '../profile/profile.service';-import { formatClickhouseDate } from './src/dates';+import { formatClickhouseDate, toNullIfDefaultMinDate } from './src/dates';import {groupActivityQuery,groupByIdQuery,@@ -36,10 +38,6 @@ const GROUPS_TABLE = 'groups';const FIND_GROUPS_DEFAULT_LIMIT = 20;const GROUP_MEMBERS_DEFAULT_LIMIT = 10;-function loadChClient() {- return import('@openpanel/db/src/clickhouse/client');-}-export interface IServiceGroup {id: string;projectId: string;@@ -85,6 +83,7 @@ function pageOffset(cursor: number | undefined, take: number): number {}async function writeGroupToCh(+ deps: ServiceDeps,group: {id: string;projectId: string;@@ -95,8 +94,7 @@ async function writeGroupToCh(},deleted = 0) {- const { ch } = await loadChClient();- await ch.insert({+ await deps.ch.insert({format: 'JSONEachRow',table: GROUPS_TABLE,values: [@@ -114,9 +112,12 @@ async function writeGroupToCh(});}-export async function upsertGroup(input: IServiceUpsertGroup) {- const existing = await getGroupById(input.id, input.projectId);- await writeGroupToCh({+export async function upsertGroup(+ deps: ServiceDeps,+ input: IServiceUpsertGroup+) {+ const existing = await getGroupById(deps, input.id, input.projectId);+ await writeGroupToCh(deps, {id: input.id,projectId: input.projectId,type: input.type,@@ -130,11 +131,12 @@ export async function upsertGroup(input: IServiceUpsertGroup) {}export async function getGroupById(+ deps: ServiceDeps,id: string,projectId: string): Promise<IServiceGroup | null> {- const { chQuery } = await loadChClient();const rows = await chQuery<IClickhouseGroup>(+ deps,groupByIdQuery({ id, projectId }));return rows[0] ? transformGroup(rows[0]) : null;@@ -148,15 +150,12 @@ export interface GetGroupListOptions {type?: string;}-export async function getGroupList({- projectId,- cursor,- take,- search,- type,-}: GetGroupListOptions): Promise<IServiceGroup[]> {- const { chQuery } = await loadChClient();+export async function getGroupList(+ deps: ServiceDeps,+ { projectId, cursor, take, search, type }: GetGroupListOptions+): Promise<IServiceGroup[]> {const rows = await chQuery<IClickhouseGroup>(+ deps,groupListQuery({projectId,take,@@ -168,39 +167,51 @@ export async function getGroupList({return rows.map(transformGroup);}-export async function getGroupListCount({- projectId,- type,- search,-}: {- projectId: string;- type?: string;- search?: string;-}): Promise<number> {- const { chQuery } = await loadChClient();+export async function getGroupListCount(+ deps: ServiceDeps,+ {+ projectId,+ type,+ search,+ }: {+ projectId: string;+ type?: string;+ search?: string;+ }+): Promise<number> {const rows = await chQuery<{ count: number }>(+ deps,groupListCountQuery({ projectId, type, search }));return rows[0]?.count ?? 0;}-export async function getGroupTypes(projectId: string): Promise<string[]> {- const { chQuery } = await loadChClient();- const rows = await chQuery<{ type: string }>(groupTypesQuery(projectId));+export async function getGroupTypes(+ deps: ServiceDeps,+ projectId: string+): Promise<string[]> {+ const rows = await chQuery<{ type: string }>(+ deps,+ groupTypesQuery(projectId)+ );return rows.map((r) => r.type);}-export async function createGroup(input: IServiceUpsertGroup) {- await upsertGroup(input);- return getGroupById(input.id, input.projectId);+export async function createGroup(+ deps: ServiceDeps,+ input: IServiceUpsertGroup+) {+ await upsertGroup(deps, input);+ return getGroupById(deps, input.id, input.projectId);}export async function updateGroup(+ deps: ServiceDeps,id: string,projectId: string,data: { type?: string; name?: string; properties?: Record<string, unknown> }) {- const existing = await getGroupById(id, projectId);+ const existing = await getGroupById(deps, id, projectId);if (!existing) {throw new Error(`Group ${id} not found`);}@@ -219,16 +230,21 @@ export async function updateGroup(properties: normalizedProperties,createdAt: existing.createdAt,};- await writeGroupToCh(updated);+ await writeGroupToCh(deps, updated);return { ...existing, ...updated };}-export async function deleteGroup(id: string, projectId: string) {- const existing = await getGroupById(id, projectId);+export async function deleteGroup(+ deps: ServiceDeps,+ id: string,+ projectId: string+) {+ const existing = await getGroupById(deps, id, projectId);if (!existing) {throw new Error(`Group ${id} not found`);}await writeGroupToCh(+ deps,{id,projectId,@@ -243,10 +259,11 @@ export async function deleteGroup(id: string, projectId: string) {}export async function getGroupPropertyKeys(+ deps: ServiceDeps,projectId: string): Promise<string[]> {- const { chQuery } = await loadChClient();const rows = await chQuery<{ key: string }>(+ deps,groupPropertyKeysQuery(projectId));return rows.map((r) => r.key).sort();@@ -259,6 +276,7 @@ export interface IServiceGroupStats {}export async function getGroupStats(+ deps: ServiceDeps,projectId: string,groupIds: string[]): Promise<Map<string, IServiceGroupStats>> {@@ -266,12 +284,11 @@ export async function getGroupStats(return new Map();}- const { chQuery } = await loadChClient();const rows = await chQuery<{group_id: string;member_count: number;last_active_at: string;- }>(groupStatsQuery({ projectId, groupIds }));+ }>(deps, groupStatsQuery({ projectId, groupIds }));return new Map(rows.map((r) => [@@ -286,6 +303,7 @@ export async function getGroupStats(}export async function getGroupsByIds(+ deps: ServiceDeps,projectId: string,ids: string[]): Promise<IServiceGroup[]> {@@ -293,8 +311,8 @@ export async function getGroupsByIds(return [];}- const { chQuery } = await loadChClient();const rows = await chQuery<IClickhouseGroup>(+ deps,groupsByIdsQuery({ projectId, ids }));return rows.map(transformGroup);@@ -308,18 +326,15 @@ export interface GetGroupMemberProfilesOptions {search?: string;}-export async function getGroupMemberProfiles({- projectId,- groupId,- cursor,- take,- search,-}: GetGroupMemberProfilesOptions): Promise<{+export async function getGroupMemberProfiles(+ deps: ServiceDeps,+ { projectId, groupId, cursor, take, search }: GetGroupMemberProfilesOptions+): Promise<{data: IServiceProfile[];count: number;}> {- const { chQuery } = await loadChClient();const rows = await chQuery<{ id: string; total_count: number }>(+ deps,groupMemberProfilesQuery({projectId,groupId,@@ -336,7 +351,7 @@ export async function getGroupMemberProfiles({return { data: [], count };}- const profiles = await getProfiles(profileIds, projectId);+ const profiles = await getProfiles(deps, profileIds, projectId);const byId = new Map(profiles.map((p) => [p.id, p]));const data = profileIds.map((id) => byId.get(id))@@ -347,12 +362,16 @@ export async function getGroupMemberProfiles({// ---- the trpc group router's bodies/** `list`: the page plus each group's member count / last activity. */-export async function getGroupListPage(input: GetGroupListOptions) {+export async function getGroupListPage(+ deps: ServiceDeps,+ input: GetGroupListOptions+) {const [data, count] = await Promise.all([- getGroupList(input),- getGroupListCount(input),+ getGroupList(deps, input),+ getGroupListCount(deps, input),]);const stats = await getGroupStats(+ deps,input.projectId,data.map((g) => g.id));@@ -366,13 +385,18 @@ export async function getGroupListPage(input: GetGroupListOptions) {};}-export async function getGroupMetrics(id: string, projectId: string) {- const { chQuery, toNullIfDefaultMinDate } = await loadChClient();+export async function getGroupMetrics(+ deps: ServiceDeps,+ id: string,+ projectId: string+) {const [eventData, profileData] = await Promise.all([chQuery<{ totalEvents: number; firstSeen: string; lastSeen: string }>(+ deps,groupEventMetricsQuery({ projectId, groupId: id })),chQuery<{ uniqueProfiles: number }>(+ deps,groupUniqueProfilesQuery({ projectId, groupId: id })),]);@@ -385,38 +409,55 @@ export async function getGroupMetrics(id: string, projectId: string) {};}-export async function getGroupActivity(id: string, projectId: string) {- const { chQuery } = await loadChClient();+export async function getGroupActivity(+ deps: ServiceDeps,+ id: string,+ projectId: string+) {return chQuery<{ count: number; date: string }>(+ deps,groupActivityQuery({ projectId, groupId: id }));}-export async function getGroupMemberGrowth(id: string, projectId: string) {- const { chQuery } = await loadChClient();+export async function getGroupMemberGrowth(+ deps: ServiceDeps,+ id: string,+ projectId: string+) {return chQuery<{ date: string; count: number }>(+ deps,groupMemberGrowthQuery({ projectId, groupId: id }));}-export async function getGroupMostEvents(id: string, projectId: string) {- const { chQuery } = await loadChClient();+export async function getGroupMostEvents(+ deps: ServiceDeps,+ id: string,+ projectId: string+) {return chQuery<{ count: number; name: string }>(+ deps,groupMostEventsQuery({ projectId, groupId: id }));}-export async function getGroupPopularRoutes(id: string, projectId: string) {- const { chQuery } = await loadChClient();+export async function getGroupPopularRoutes(+ deps: ServiceDeps,+ id: string,+ projectId: string+) {return chQuery<{ count: number; path: string }>(+ deps,groupPopularRoutesQuery({ projectId, groupId: id }));}export async function getGroupMemberProfilesPage(+ deps: ServiceDeps,input: GetGroupMemberProfilesOptions) {- const { data, count } = await getGroupMemberProfiles(input);+ const { data, count } = await getGroupMemberProfiles(deps, input);return {data,meta: { count, pageCount: input.take },@@ -425,18 +466,21 @@ export async function getGroupMemberProfilesPage(// ---- the assistant/mcp tool entry points-export async function listGroupTypesCore(projectId: string) {- const types = await getGroupTypes(projectId);+export async function listGroupTypesCore(deps: ServiceDeps, projectId: string) {+ const types = await getGroupTypes(deps, projectId);return { types };}-export async function findGroupsCore(input: {- projectId: string;- type?: string;- search?: string;- limit?: number;-}) {- return getGroupList({+export async function findGroupsCore(+ deps: ServiceDeps,+ input: {+ projectId: string;+ type?: string;+ search?: string;+ limit?: number;+ }+) {+ return getGroupList(deps, {projectId: input.projectId,type: input.type,search: input.search,@@ -444,14 +488,17 @@ export async function findGroupsCore(input: {});}-export async function getGroupCore(input: {- projectId: string;- groupId: string;- memberLimit?: number;-}) {+export async function getGroupCore(+ deps: ServiceDeps,+ input: {+ projectId: string;+ groupId: string;+ memberLimit?: number;+ }+) {const [group, members] = await Promise.all([- getGroupById(input.groupId, input.projectId),- getGroupMemberProfiles({+ getGroupById(deps, input.groupId, input.projectId),+ getGroupMemberProfiles(deps, {projectId: input.projectId,groupId: input.groupId,take: input.memberLimit ?? GROUP_MEMBERS_DEFAULT_LIMIT,@@ -474,9 +521,9 @@ export interface GroupService {upsert(input: IServiceUpsertGroup): Promise<void>;}-export function createGroupService(_deps: ServiceDeps): GroupService {+export function createGroupService(deps: ServiceDeps): GroupService {return {- byId: getGroupById,- upsert: upsertGroup,+ byId: (id, projectId) => getGroupById(deps, id, projectId),+ upsert: (input) => upsertGroup(deps, input),};}diff --git a/packages/core/src/modules/group/src/dates.ts b/packages/core/src/modules/group/src/dates.tsindex 81c54b966..4b4cd2526 100644--- a/packages/core/src/modules/group/src/dates.ts+++ b/packages/core/src/modules/group/src/dates.ts@@ -11,3 +11,21 @@ export function formatClickhouseDate(date: Date | string): string {.replace('T', ' ').replace(CLICKHOUSE_DATE_SUFFIX, '');}++export function convertClickhouseDateToJs(date: string): Date {+ return new Date(`${date.replace(' ', 'T')}Z`);+}++const ROLLUP_DATE_PREFIX = '1970-01-01';+const ROLLUP_DATE_PREFIX_ALT = '1969-12-31';++/** ClickHouse's zero date for a metric no row ever populated. */+export function toNullIfDefaultMinDate(date?: string | null): Date | null {+ if (!date) {+ return null;+ }+ return date.startsWith(ROLLUP_DATE_PREFIX) ||+ date.startsWith(ROLLUP_DATE_PREFIX_ALT)+ ? null+ : convertClickhouseDateToJs(date);+}diff --git a/packages/core/src/modules/gsc/gsc.jobs.test.ts b/packages/core/src/modules/gsc/gsc.jobs.test.tsindex 81d9eed95..a865306c1 100644--- a/packages/core/src/modules/gsc/gsc.jobs.test.ts+++ b/packages/core/src/modules/gsc/gsc.jobs.test.ts@@ -40,6 +40,9 @@ function stubJobCtx(gsc: Partial<GscService>, jobName = 'test'): JobCtx {profile: {} as Services['profile'],group: {} as Services['group'],chart: {} as Services['chart'],+ overview: {} as Services['overview'],+ pages: {} as Services['pages'],+ realtime: {} as Services['realtime'],misc: {} as Services['misc'],report: {} as Services['report'],dashboard: {} as Services['dashboard'],@@ -160,6 +163,9 @@ test('gscSync fans out one gscProjectSync enqueue per connected project', asyncprofile: {} as Services['profile'],group: {} as Services['group'],chart: {} as Services['chart'],+ overview: {} as Services['overview'],+ pages: {} as Services['pages'],+ realtime: {} as Services['realtime'],misc: {} as Services['misc'],report: {} as Services['report'],dashboard: {} as Services['dashboard'],diff --git a/packages/core/src/modules/import/import.jobs.test.ts b/packages/core/src/modules/import/import.jobs.test.tsindex 5c842a6dc..927a4cb40 100644--- a/packages/core/src/modules/import/import.jobs.test.ts+++ b/packages/core/src/modules/import/import.jobs.test.ts@@ -40,6 +40,9 @@ function stubJobCtx(importService: Partial<ImportService>): JobCtx {profile: {} as Services['profile'],group: {} as Services['group'],chart: {} as Services['chart'],+ overview: {} as Services['overview'],+ pages: {} as Services['pages'],+ realtime: {} as Services['realtime'],misc: {} as Services['misc'],report: {} as Services['report'],dashboard: {} as Services['dashboard'],diff --git a/packages/core/src/modules/ingest/ingest.routes.ts b/packages/core/src/modules/ingest/ingest.routes.tsindex cc55a9efc..5f98ad6b1 100644--- a/packages/core/src/modules/ingest/ingest.routes.ts+++ b/packages/core/src/modules/ingest/ingest.routes.ts@@ -104,7 +104,7 @@ async function botGuard({ctx: HttpCtx;status: StatusFn;}) {- const bot = await checkIngestBot({+ const bot = await checkIngestBot(ctx, {headers: toIngestHeaders(ctx.headers),clientSecretAuth: client.secretPresented,projectId: client.projectId,@@ -175,6 +175,7 @@ export const ingestRoutes = defineRoutes((app, deps: AppDeps) =>{buffers: ctx.buffers,produceIncomingEvent: deps.produceIncomingEvent,+ deps: ctx,});@@ -244,6 +245,7 @@ export const ingestRoutes = defineRoutes((app, deps: AppDeps) =>{buffers: ctx.buffers,produceIncomingEvent: deps.produceIncomingEvent,+ deps: ctx,});diff --git a/packages/core/src/modules/ingest/ingest.service.test.ts b/packages/core/src/modules/ingest/ingest.service.test.tsindex 57413ce2b..12b2f6ecd 100644--- a/packages/core/src/modules/ingest/ingest.service.test.ts+++ b/packages/core/src/modules/ingest/ingest.service.test.ts@@ -191,6 +191,7 @@ const transport = (group: { add: mock(async () => undefined) },},produceIncomingEvent,+ deps: { buffers: {} },}) as unknown as Parameters<typeof ingestTrack>[1];const request = (body: ITrackHandlerPayload) => ({diff --git a/packages/core/src/modules/ingest/ingest.service.ts b/packages/core/src/modules/ingest/ingest.service.tsindex b853566ce..ca1180a2d 100644--- a/packages/core/src/modules/ingest/ingest.service.ts+++ b/packages/core/src/modules/ingest/ingest.service.ts@@ -101,6 +101,9 @@ export type IngestBuffers = Pick<Buffers, 'session' | 'replay' | 'group'>;export interface IngestTransport {buffers: IngestBuffers;produceIncomingEvent: IncomingEventProducer;+ /** M10-005: the profile writes below reach ClickHouse and the profile+ * buffer through the scope, not a module singleton. */+ deps: ServiceDeps;}const QUEUE_PAYLOAD_HEADERS = [@@ -323,7 +326,7 @@ async function handleTrack(// If we have more than one property in the identity object, we should identify the user// Otherwise its only a profileId and we should not identify the userif (context.identity && Object.keys(context.identity).length > 1) {- promises.push(handleIdentify(context.identity, context));+ promises.push(handleIdentify(transport.deps, context.identity, context));}const queueData: IncomingEventPayload = {@@ -352,25 +355,27 @@ async function handleTrack(}async function handleIdentify(+ deps: ServiceDeps,payload: IIdentifyPayload,context: TrackContext): Promise<void> {// Profiles must not carry forged bot verdicts either.stripBotProperties(payload.properties);const userAgent = parseUserAgent(context.ua, payload.properties);- await identifyProfile(context.projectId, payload, {+ await identifyProfile(deps, context.projectId, payload, {geo: context.geo,userAgent,});}async function adjustProfileProperty(+ deps: ServiceDeps,payload: IIncrementPayload | IDecrementPayload,projectId: string,direction: 1 | -1): Promise<TrackOutcome | null> {const { profileId, property, value } = payload;- const profile = await getProfileById(String(profileId), projectId);+ const profile = await getProfileById(deps, String(profileId), projectId);if (!profile) {return { status: 'profile-not-found' };}@@ -384,7 +389,7 @@ async function adjustProfileProperty(return { status: 'profile-property-not-a-number' };}- await upsertProfile({+ await upsertProfile(deps, {id: profile.id,projectId,properties: assocPath(@@ -442,6 +447,7 @@ async function handleGroup(}async function handleAssignGroup(+ deps: ServiceDeps,payload: IAssignGroupPayload,context: TrackContext): Promise<void> {@@ -449,7 +455,7 @@ async function handleAssignGroup(if (!profileId) {return;}- await upsertProfile({+ await upsertProfile(deps, {id: String(profileId),projectId: context.projectId,isExternal: !!payload.profileId,@@ -480,10 +486,11 @@ export async function ingestTrack(await handleTrack(body.payload, context, transport);break;case 'identify':- await handleIdentify(body.payload, context);+ await handleIdentify(transport.deps, body.payload, context);break;case 'increment': {const failure = await adjustProfileProperty(+ transport.deps,body.payload,context.projectId,1@@ -495,6 +502,7 @@ export async function ingestTrack(}case 'decrement': {const failure = await adjustProfileProperty(+ transport.deps,body.payload,context.projectId,-1@@ -523,7 +531,7 @@ export async function ingestTrack(await handleGroup(body.payload, context, transport.buffers.group);break;case 'assign_group':- await handleAssignGroup(body.payload, context);+ await handleAssignGroup(transport.deps, body.payload, context);break;default:return { status: 'invalid-type' };@@ -837,12 +845,15 @@ export type BotVerdict = { name: string; type: string } | null;* never treat them as bots — bot detection is for public/frontend* (origin-authenticated) traffic.*/-export async function checkIngestBot(request: {- headers: IngestHeaders;- clientSecretAuth: boolean;- projectId: string | null | undefined;- body: unknown;-}): Promise<BotVerdict> {+export async function checkIngestBot(+ deps: ServiceDeps,+ request: {+ headers: IngestHeaders;+ clientSecretAuth: boolean;+ projectId: string | null | undefined;+ body: unknown;+ }+): Promise<BotVerdict> {if (request.clientSecretAuth) {return null;}@@ -856,7 +867,7 @@ export async function checkIngestBot(request: {const path = getBotEventPath(request.body);if (path) {- await createBotEvent({+ await createBotEvent(deps, {...bot,projectId: request.projectId,path,@@ -898,15 +909,19 @@ export interface IngestService {request: TrackRequest,produceIncomingEvent: IncomingEventProducer): Promise<TrackOutcome>;- checkBot: typeof checkIngestBot;+ checkBot(request: Parameters<typeof checkIngestBot>[1]): Promise<BotVerdict>;isDuplicate: typeof isDuplicateIngestRequest;}export function createIngestService(deps: ServiceDeps): IngestService {return {track: (request, produceIncomingEvent) =>- ingestTrack(request, { buffers: deps.buffers, produceIncomingEvent }),- checkBot: checkIngestBot,+ ingestTrack(request, {+ buffers: deps.buffers,+ produceIncomingEvent,+ deps,+ }),+ checkBot: (request) => checkIngestBot(deps, request),isDuplicate: isDuplicateIngestRequest,};}diff --git a/packages/core/src/modules/ingest/legacy-event.test.ts b/packages/core/src/modules/ingest/legacy-event.test.tsindex b0c95a46b..96b30aa66 100644--- a/packages/core/src/modules/ingest/legacy-event.test.ts+++ b/packages/core/src/modules/ingest/legacy-event.test.ts@@ -24,6 +24,7 @@ import {} from 'bun:test';import type { Buffers } from '../../buffers/create-buffers';import type { GeoLocation } from '../../clients/geo';+import type { ServiceDeps } from '../../services';import type { DeprecatedPostEventPayload } from './ingest.constants';import type { IncomingEventPayload } from './src/incoming-event';import {@@ -92,6 +93,10 @@ const buffers = {group: {},} as unknown as Buffers;+// The legacy route produces to Kafka and never touches Postgres/ClickHouse;+// `deps` is on `IngestTransport` for the profile writes /track makes (M10-005).+const transportDeps = { buffers } as unknown as ServiceDeps;+const legacyBody = (overrides: Partial<DeprecatedPostEventPayload> = {}): DeprecatedPostEventPayload => ({@@ -128,6 +133,7 @@ describe('ingestLegacyEvent', () => {const outcome = await ingestLegacyEvent(makeRequest(), {buffers,produceIncomingEvent,+ deps: transportDeps,});expect(outcome.status).toBe('ok');@@ -142,13 +148,21 @@ describe('ingestLegacyEvent', () => {});it('mints the event id at the producer, like /track', async () => {- await ingestLegacyEvent(makeRequest(), { buffers, produceIncomingEvent });+ await ingestLegacyEvent(makeRequest(), {+ buffers,+ produceIncomingEvent,+ deps: transportDeps,+ });expect(produced[0]?.payload.id).toMatch(UUID_PATTERN);});it('replaces the client-supplied timestamp with the server timestamp', async () => {- await ingestLegacyEvent(makeRequest(), { buffers, produceIncomingEvent });+ await ingestLegacyEvent(makeRequest(), {+ buffers,+ produceIncomingEvent,+ deps: transportDeps,+ });const { payload } = produced[0]!;expect(payload.event.timestamp).toBe(requestTimestamp);@@ -159,6 +173,7 @@ describe('ingestLegacyEvent', () => {const outcome = await ingestLegacyEvent(makeRequest(null), {buffers,produceIncomingEvent,+ deps: transportDeps,});expect(outcome.status).toBe('missing-project-id');diff --git a/packages/core/src/modules/ingest/src/bot-check.test.ts b/packages/core/src/modules/ingest/src/bot-check.test.tsindex 52f0e0fd1..f6120dc40 100644--- a/packages/core/src/modules/ingest/src/bot-check.test.ts+++ b/packages/core/src/modules/ingest/src/bot-check.test.ts@@ -11,10 +11,16 @@ import { beforeAll, beforeEach, describe, expect, it, mock } from 'bun:test';import type { BotMatch } from './bots/detect';const isBot = mock(async (_ua: string): Promise<BotMatch | null> => null);-const createBotEvent = mock(async (_payload: unknown) => undefined);+const createBotEvent = mock(+ async (_deps: unknown, _payload: unknown) => undefined+);let checkIngestBot: typeof import('../ingest.service').checkIngestBot;+// M10-005: `checkIngestBot` takes `ServiceDeps` first and hands it to+// `createBotEvent`, which is mocked below — nothing here reads a client.+const deps = {} as unknown as import('../../../services').ServiceDeps;+// Spread the real modules and override by name: ingest.service re-exports// `detectBot` and reaches more of event.service than `createBotEvent`, and a// factory returning only the overrides makes those exports vanish under@@ -44,7 +50,7 @@ beforeEach(() => {describe('checkIngestBot', () => {it('skips bot detection entirely for client-secret (server-side) traffic', async () => {- const bot = await checkIngestBot({+ const bot = await checkIngestBot(deps, {...baseRequest,clientSecretAuth: true,});@@ -57,11 +63,11 @@ describe('checkIngestBot', () => {it('records a bot event and reports the bot for public bot traffic', async () => {isBot.mockResolvedValue({ name: 'Googlebot', type: 'Search bot' });- const bot = await checkIngestBot(baseRequest);+ const bot = await checkIngestBot(deps, baseRequest);expect(isBot).toHaveBeenCalledWith('Googlebot/2.1');expect(createBotEvent).toHaveBeenCalledTimes(1);- expect(createBotEvent.mock.calls[0]?.[0]).toMatchObject({+ expect(createBotEvent.mock.calls[0]?.[1]).toMatchObject({name: 'Googlebot',type: 'Search bot',projectId: 'proj-1',@@ -73,7 +79,7 @@ describe('checkIngestBot', () => {it('passes legitimate public traffic through untouched', async () => {isBot.mockResolvedValue(null);- const bot = await checkIngestBot({+ const bot = await checkIngestBot(deps, {...baseRequest,headers: { 'user-agent': 'node' },});diff --git a/packages/core/src/modules/ingest/src/incoming-event-handler.ts b/packages/core/src/modules/ingest/src/incoming-event-handler.tsindex 87dc74c47..3c12208e4 100644--- a/packages/core/src/modules/ingest/src/incoming-event-handler.ts+++ b/packages/core/src/modules/ingest/src/incoming-event-handler.ts@@ -19,6 +19,7 @@ import { anyPass, isEmpty, isNil, mergeDeepRight, omit, reject } from 'ramda';import { loadDbBuffers } from '../../../buffers/lazy-db-buffers';import type { SessionIngestResult } from '../../../buffers/session-buffer';import type { Logger } from '../../../logger';+import type { ServiceDeps } from '../../../services';import {getReferrerWithQuery,parseReferrer,@@ -115,6 +116,7 @@ export interface IncomingEventDeps {* lazy — see event.service.ts's header.*/export async function loadIncomingEventDeps(+ deps: ServiceDeps,logger: Logger,enqueueSessionEnd: (input: EnqueueSessionEndInput) => Promise<unknown>,overrides: Partial<IncomingEventDeps> = {}@@ -129,7 +131,7 @@ export async function loadIncomingEventDeps(return {logger,sessions: sessionBuffer,- createEvent,+ createEvent: (payload) => createEvent(deps, payload),checkNotificationRulesForEvent:notifications.checkNotificationRulesForEvent,projects: {diff --git a/packages/core/src/modules/insight/insight.jobs.test.ts b/packages/core/src/modules/insight/insight.jobs.test.tsindex 4f87128ad..843e0b321 100644--- a/packages/core/src/modules/insight/insight.jobs.test.ts+++ b/packages/core/src/modules/insight/insight.jobs.test.ts@@ -51,6 +51,9 @@ function stubJobCtx(profile: {} as Services['profile'],group: {} as Services['group'],chart: {} as Services['chart'],+ overview: {} as Services['overview'],+ pages: {} as Services['pages'],+ realtime: {} as Services['realtime'],misc: {} as Services['misc'],report: {} as Services['report'],dashboard: {} as Services['dashboard'],@@ -154,6 +157,9 @@ test('insightsDaily fans out one insightsProject enqueue per candidate, jobId deprofile: {} as Services['profile'],group: {} as Services['group'],chart: {} as Services['chart'],+ overview: {} as Services['overview'],+ pages: {} as Services['pages'],+ realtime: {} as Services['realtime'],misc: {} as Services['misc'],report: {} as Services['report'],dashboard: {} as Services['dashboard'],diff --git a/packages/core/src/modules/mcp/src/tools/analytics/page-performance.test.ts b/packages/core/src/modules/mcp/src/tools/analytics/page-performance.test.tsindex 76ce524ef..1ba88cc41 100644--- a/packages/core/src/modules/mcp/src/tools/analytics/page-performance.test.ts+++ b/packages/core/src/modules/mcp/src/tools/analytics/page-performance.test.ts@@ -17,15 +17,12 @@ const mockGetSettingsForProject = mock(() =>// '@openpanel/db' barrel — a whole-barrel replacement would drop every other// export the barrel carries for any other file sharing this process// (bun:test only isolates modules per file under `--isolate`; see AGENTS.md).-// The pages/organization/project loaders all resolve through this same-// specifier now (M9-CLEANUP-001) — one mock.module call, not three, or each-// later call replaces the whole module and drops the earlier overrides.+// The organization/project loaders resolve through this same specifier+// (M9-CLEANUP-001) — one mock.module call, not two, or each later call+// replaces the whole module and drops the earlier overrides.const actualCore = await import('@openpanel/core');mock.module('@openpanel/core', () => ({...actualCore,- PagesService: mock().mockImplementation(() => ({- getTopPages: mockGetTopPages,- })),getSettingsForProject: mockGetSettingsForProject,resolveClientProjectId: mock(({ clientProjectId }: { clientProjectId: string }) =>@@ -33,37 +30,27 @@ mock.module('@openpanel/core', () => ({),}));-const actualClickhouseClient = await import(- '@openpanel/db/src/clickhouse/client'-);-mock.module('@openpanel/db/src/clickhouse/client', () => ({- ...actualClickhouseClient,- ch: {},+// M10-005: `PagesService` is no longer a class the tool constructs — the tool+// resolves the pages service through `../shared`'s one compat hop, so that is+// what this file stubs. Spread-actual: every other helper in `../shared` is+// live for whatever else shares this process.+const actualShared = await import('../shared');+mock.module('../shared', () => ({+ ...actualShared,+ loadCompatPagesService: async () => ({ getTopPages: mockGetTopPages }),}));+afterAll(() => {+ mock.module('../shared', () => actualShared);+ mock.module('@openpanel/core', () => actualCore);+});+let registerPagePerformanceTools: typeof import('./page-performance').registerPagePerformanceTools;beforeAll(async () => {- // `loadCompatCh` (../shared) reaches ClickHouse through v1-compat.ts's- // memoized fallback singleton — a process-lifetime cache once resolved,- // so an earlier test file's `bun test` (no `--isolate`) run can have- // already resolved it against a DIFFERENT `@openpanel/db/src/clickhouse/client`- // mock (or none at all). Reset it so this file's own mock above is what- // gets picked up.- const { resetV1CompatServicesForTests } = await import(- '../../../../../v1-compat'- );- resetV1CompatServicesForTests();({ registerPagePerformanceTools } = await import('./page-performance'));});-afterAll(async () => {- const { resetV1CompatServicesForTests } = await import(- '../../../../../v1-compat'- );- resetV1CompatServicesForTests();-});-function makeServer() {let handler: ((input: unknown) => Promise<unknown>) | null = null;return {diff --git a/packages/core/src/modules/mcp/src/tools/analytics/page-performance.ts b/packages/core/src/modules/mcp/src/tools/analytics/page-performance.tsindex 340be9f9f..72f20cdd4 100644--- a/packages/core/src/modules/mcp/src/tools/analytics/page-performance.ts+++ b/packages/core/src/modules/mcp/src/tools/analytics/page-performance.ts@@ -1,9 +1,9 @@import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';-import { getSettingsForProject, PagesService } from '@openpanel/core';+import { getSettingsForProject } from '@openpanel/core';import { z } from 'zod';import type { McpAuthContext } from '../../auth';import {- loadCompatCh,+ loadCompatPagesService,projectIdSchema,resolveDateRange,resolveProjectId,@@ -67,14 +67,12 @@ export function registerPagePerformanceTools(const projectId = await resolveProjectId(context, inputProjectId);const { startDate, endDate } = resolveDateRange(sd, ed);const { timezone } = await getSettingsForProject(projectId);- // Constructed per call, not once at module scope: a module-scope- // singleton binds `ch`/`PagesService` to whatever was live the first- // time this file was imported by *any* test in the process (bun:test- // shares one module registry without `--isolate` — see AGENTS.md),- // permanently defeating this file's own mocks whenever the- // integration suite imports it first.- const ch = await loadCompatCh();- const pagesService = new PagesService(ch);+ // Resolved per call, not once at module scope: a module-scope binding+ // would freeze whatever was live the first time this file was imported+ // by *any* test in the process (bun:test shares one module registry+ // without `--isolate` — see AGENTS.md), permanently defeating this+ // file's own mocks whenever the integration suite imports it first.+ const pagesService = await loadCompatPagesService();const pages = await pagesService.getTopPages({projectId,diff --git a/packages/core/src/modules/mcp/src/tools/analytics/profiles.test.ts b/packages/core/src/modules/mcp/src/tools/analytics/profiles.test.tsindex 507cdd469..617f259c1 100644--- a/packages/core/src/modules/mcp/src/tools/analytics/profiles.test.ts+++ b/packages/core/src/modules/mcp/src/tools/analytics/profiles.test.ts@@ -1,59 +1,47 @@-import {- afterAll,- beforeAll,- beforeEach,- describe,- expect,- it,- mock,-} from 'bun:test';-import type { SqlFragment } from '@openpanel/db/src/clickhouse/sql';--const mockChQuery = mock((..._args: unknown[]) =>- Promise.resolve([] as unknown[])+import { beforeAll, beforeEach, describe, expect, it, mock } from 'bun:test';+import type { ServiceDeps } from '../../../../../services';++// M10-005: `findProfilesCore` takes `ServiceDeps` and reaches ClickHouse as+// `deps.ch` (through shared/ch-query.ts), so the client is HANDED IN rather+// than mocked onto `@openpanel/db/src/clickhouse/client` process-wide. Same+// assertions, one less shared-registry hazard.+const chQuery = mock(+ async (_params: {+ query: string;+ query_params?: Record<string, unknown>;+ }) => ({+ json: async () => ({ data: [] as unknown[], meta: [], rows: 0 }),+ }));-// Mock the ClickHouse client at the specifier profile.service.ts imports —-// mock.module applies at call time (unlike vitest's hoisted vi.mock), so the-// mock must be registered before profile.service's first (dynamic) import.-//-// Spread the real module rather than hand-listing every export: `mock.module`-// replaces this specifier process-wide (bun runs every test file in one-// shared module registry without `--isolate` — see AGENTS.md). A hand-rolled-// factory here previously left `ch`/`originalCh` empty objects and `TABLE_NAMES`-// a hand-copied subset — invisible to `findProfilesCore` (it only reaches-// `chQuery`/`TABLE_NAMES.events`, both covered), but not to every *other*-// consumer of this same live-bound specifier sharing this process (e.g. the-// mcp integration suite's real, unmocked service calls) — restored in-// afterAll below so the leak doesn't outlive this file.-const actualClickhouseClient = await import(- '@openpanel/db/src/clickhouse/client'-);-mock.module('@openpanel/db/src/clickhouse/client', () => ({- ...actualClickhouseClient,- chQuery: mockChQuery,-}));--afterAll(() => {- mock.module('@openpanel/db/src/clickhouse/client', () => ({- ...actualClickhouseClient,- }));-});--let findProfilesCore: typeof import('@openpanel/core').findProfilesCore;+const noop = () => undefined;+const deps = {+ ch: { query: chQuery },+ logger: {+ fatal: noop,+ error: noop,+ warn: noop,+ info: noop,+ debug: noop,+ trace: noop,+ child: () => deps.logger,+ },+} as unknown as ServiceDeps;++let findProfilesCore: typeof import('../../../../profile/profile.service').findProfilesCore;beforeAll(async () => {- ({ findProfilesCore } = await import(- '@openpanel/core'- ));+ ({ findProfilesCore } = await import('../../../../profile/profile.service'));});-// Since M7-002 the service hands `chQuery` a `sql` fragment (ADR-013): the+// Since M7-002 the service hands ClickHouse a `sql` fragment (ADR-013): the// text carries `{pN:Type}` placeholders and every value travels in params.function captured(): { query: string; params: Record<string, unknown> } {- const fragment = mockChQuery.mock.calls[0]?.[0] as SqlFragment;- const { query, query_params } = fragment.toStatement();- return { query: query.replace(/\s+/g, ' '), params: query_params };+ const call = chQuery.mock.calls[0]?.[0];+ return {+ query: (call?.query ?? '').replace(/\s+/g, ' '),+ params: call?.query_params ?? {},+ };}function boundValues(): unknown[] {@@ -61,25 +49,24 @@ function boundValues(): unknown[] {}beforeEach(() => {- mockChQuery.mockClear();- mockChQuery.mockResolvedValue([]);+ chQuery.mockClear();});describe('findProfilesCore — SQL conditions', () => {it('always includes project_id condition', async () => {- await findProfilesCore({ projectId: 'proj-1' });+ await findProfilesCore(deps, { projectId: 'proj-1' });expect(captured().query).toContain('project_id = {p1:String}');expect(captured().params.p1).toBe('proj-1');});it('adds email ILIKE condition when email is provided', async () => {- await findProfilesCore({ projectId: 'proj-1', email: 'carl@' });+ await findProfilesCore(deps, { projectId: 'proj-1', email: 'carl@' });expect(captured().query).toContain('email ILIKE {p2:String}');expect(captured().params.p2).toBe('%carl@%');});it('searches across first/last/full name for name filter', async () => {- await findProfilesCore({ projectId: 'proj-1', name: 'Carl' });+ await findProfilesCore(deps, { projectId: 'proj-1', name: 'Carl' });const { query } = captured();expect(query).toContain('first_name ILIKE');expect(query).toContain('last_name ILIKE');@@ -88,7 +75,7 @@ describe('findProfilesCore — SQL conditions', () => {});it('matches multi-token name queries by ANDing each token', async () => {- await findProfilesCore({ projectId: 'proj-1', name: 'John Smith' });+ await findProfilesCore(deps, { projectId: 'proj-1', name: 'John Smith' });const { query } = captured();expect(boundValues()).toContain('%John%');expect(boundValues()).toContain('%Smith%');@@ -97,13 +84,13 @@ describe('findProfilesCore — SQL conditions', () => {});it('adds country property condition', async () => {- await findProfilesCore({ projectId: 'proj-1', country: 'SE' });+ await findProfilesCore(deps, { projectId: 'proj-1', country: 'SE' });expect(captured().query).toContain('properties[{p2:String}] = {p3:String}');expect(captured().params).toMatchObject({ p2: 'country', p3: 'SE' });});it('adds inactiveDays NOT IN subquery', async () => {- await findProfilesCore({ projectId: 'proj-1', inactiveDays: 14 });+ await findProfilesCore(deps, { projectId: 'proj-1', inactiveDays: 14 });const { query, params } = captured();expect(query).toContain('NOT IN');expect(query).toContain('INTERVAL {p3:UInt64} DAY');@@ -111,42 +98,45 @@ describe('findProfilesCore — SQL conditions', () => {});it('floors inactiveDays to integer (prevents SQL injection via floats)', async () => {- await findProfilesCore({ projectId: 'proj-1', inactiveDays: 14.9 });+ await findProfilesCore(deps, { projectId: 'proj-1', inactiveDays: 14.9 });expect(captured().params.p3).toBe(14);expect(boundValues()).not.toContain(14.9);});it('adds minSessions HAVING subquery', async () => {- await findProfilesCore({ projectId: 'proj-1', minSessions: 5 });+ await findProfilesCore(deps, { projectId: 'proj-1', minSessions: 5 });const { query, params } = captured();expect(query).toContain('HAVING count() >= {p3:UInt64}');expect(params.p3).toBe(5);});it('adds performedEvent IN subquery', async () => {- await findProfilesCore({ projectId: 'proj-1', performedEvent: 'purchase' });+ await findProfilesCore(deps, {+ projectId: 'proj-1',+ performedEvent: 'purchase',+ });expect(captured().query).toContain('AND name = {p3:String}');expect(captured().params.p3).toBe('purchase');});it('defaults to ORDER BY created_at DESC', async () => {- await findProfilesCore({ projectId: 'proj-1' });+ await findProfilesCore(deps, { projectId: 'proj-1' });expect(captured().query).toContain('ORDER BY created_at DESC');});it('respects sortOrder: asc', async () => {- await findProfilesCore({ projectId: 'proj-1', sortOrder: 'asc' });+ await findProfilesCore(deps, { projectId: 'proj-1', sortOrder: 'asc' });expect(captured().query).toContain('ORDER BY created_at ASC');});it('defaults limit to 20', async () => {- await findProfilesCore({ projectId: 'proj-1' });+ await findProfilesCore(deps, { projectId: 'proj-1' });expect(captured().query).toContain('LIMIT {p2:UInt64}');expect(captured().params.p2).toBe(20);});it('caps limit at 100 regardless of input', async () => {- await findProfilesCore({ projectId: 'proj-1', limit: 9999 });+ await findProfilesCore(deps, { projectId: 'proj-1', limit: 9999 });expect(captured().params.p2).toBe(100);expect(boundValues()).not.toContain(9999);});@@ -155,39 +145,43 @@ describe('findProfilesCore — SQL conditions', () => {describe('findProfilesCore — SQL injection protection', () => {it('binds the projectId instead of interpolating it', async () => {const hostile = "proj'; DROP TABLE profiles;--";- await findProfilesCore({ projectId: hostile });+ await findProfilesCore(deps, { projectId: hostile });expect(captured().query).not.toContain(hostile);expect(captured().params.p1).toBe(hostile);});it('binds an apostrophe in the name search', async () => {- await findProfilesCore({ projectId: 'proj-1', name: "O'Brien" });+ await findProfilesCore(deps, { projectId: 'proj-1', name: "O'Brien" });const { query } = captured();expect(query).not.toContain("O'Brien");expect(boundValues()).toContain("%O'Brien%");});it('binds a backslash in the email', async () => {- await findProfilesCore({ projectId: 'proj-1', email: 'test\\@x.com' });+ await findProfilesCore(deps, {+ projectId: 'proj-1',+ email: 'test\\@x.com',+ });expect(captured().query).not.toContain('test\\@x.com');expect(captured().params.p2).toBe('%test\\@x.com%');});});describe('findProfilesCore — return value', () => {- it('returns whatever chQuery resolves with', async () => {+ it('returns whatever the ClickHouse response carries', async () => {const fakeProfiles = [{ id: 'p1', first_name: 'Alice' }];- mockChQuery.mockResolvedValueOnce(fakeProfiles);- const result = await findProfilesCore({ projectId: 'proj-1' });- // findProfilesCore is an opaque passthrough of whatever chQuery resolves- // with — the fake row is deliberately partial, so it needs a cast against- // the real (fuller) IClickhouseProfile return type.+ chQuery.mockResolvedValueOnce({+ json: async () => ({ data: fakeProfiles, meta: [], rows: 1 }),+ });+ const result = await findProfilesCore(deps, { projectId: 'proj-1' });+ // findProfilesCore is an opaque passthrough of the returned rows — the+ // fake row is deliberately partial, so it needs a cast against the real+ // (fuller) IClickhouseProfile return type.expect(result).toEqual(fakeProfiles as unknown as typeof result);});it('returns empty array when no profiles found', async () => {- mockChQuery.mockResolvedValueOnce([]);- const result = await findProfilesCore({ projectId: 'proj-1' });+ const result = await findProfilesCore(deps, { projectId: 'proj-1' });expect(result).toEqual([]);});});diff --git a/packages/core/src/modules/mcp/src/tools/shared.ts b/packages/core/src/modules/mcp/src/tools/shared.tsindex ae7cc0ac6..390c10e58 100644--- a/packages/core/src/modules/mcp/src/tools/shared.ts+++ b/packages/core/src/modules/mcp/src/tools/shared.ts@@ -38,6 +38,14 @@ export async function loadCompatChHelpers() {return (await import('../../../../v1-compat')).compatChHelpers();}+/** `analytics/page-performance.ts` used to construct its own `PagesService`+ * per call to dodge a module-singleton mocking hazard; since M10-005 there is+ * no singleton and no class, so it reaches the bare, v1-compat-wrapped pages+ * service instead. */+export async function loadCompatPagesService() {+ return (await import('../../../../v1-compat')).pagesService;+}+/*** Resolve the effective projectId from context + optional tool input.* Thin adapter so tool files don't repeat the full argument object every call.diff --git a/packages/core/src/modules/misc/misc.jobs.ts b/packages/core/src/modules/misc/misc.jobs.tsindex 248288042..1241991ca 100644--- a/packages/core/src/modules/misc/misc.jobs.ts+++ b/packages/core/src/modules/misc/misc.jobs.ts@@ -29,7 +29,7 @@ export const miscCronJobs = {payload: z.null(),handler: async ({ ctx }) => {const logger = ctx.logger.child({ job: 'data-health' });- await runDataHealthCron(await loadDataHealthDeps(logger));+ await runDataHealthCron(await loadDataHealthDeps(ctx, logger));},}),};diff --git a/packages/core/src/modules/misc/misc.routes.ts b/packages/core/src/modules/misc/misc.routes.tsindex 5a7f52299..770729835 100644--- a/packages/core/src/modules/misc/misc.routes.ts+++ b/packages/core/src/modules/misc/misc.routes.ts@@ -35,7 +35,7 @@ export const miscRoutes = defineRoutes((app) =>'/misc/ping',async ({ body, ctx, status }) => {try {- await insertPingRecord(body);+ await insertPingRecord(ctx, body);return { message: 'Success', count: body.count, domain: body.domain };} catch (error) {ctx.logger.error({ err: error }, 'Failed to insert ping');@@ -44,7 +44,7 @@ export const miscRoutes = defineRoutes((app) =>},{ body: zPingBody, detail: { tags: TAGS } })- .get('/misc/stats', () => getStats(), { detail: { tags: TAGS } })+ .get('/misc/stats', ({ ctx }) => getStats(ctx), { detail: { tags: TAGS } }).get('/misc/favicon',async ({ query, ctx, set }) => {diff --git a/packages/core/src/modules/misc/misc.service.ts b/packages/core/src/modules/misc/misc.service.tsindex 0896d927e..cd16edf01 100644--- a/packages/core/src/modules/misc/misc.service.ts+++ b/packages/core/src/modules/misc/misc.service.ts@@ -7,15 +7,13 @@// entry #6 grades them RULED + DEAD (`docs/ANSWERS.md` §1.4 confirms no proxy// depends on them), so this wave is where V1 drops them too.//-// ClickHouse access is lazy (`loadCh()` below) and targets the narrow-// `@openpanel/db/src/clickhouse/client` submodule, not the package's full-// barrel — same reason organization.service.ts's/onboarding.service.ts's-// headers give: this module is pulled into the eager `@openpanel/core`-// barrel via `services.ts`, and a dynamic edge into the FULL `@openpanel/db`-// barrel from a second core module (alongside export.service.ts's own) is-// exactly the "two dynamic-import edges close one cycle" shape that panics-// apps/worker's rolldown build (`module_finalizers "no entry found for-// key"`) — see the controller's rolldown-dynamic-import-cycle-panic note.+// M10-005: the ClickHouse CLIENT is `deps.ch` — reads through+// shared/ch-query.ts, the one write through `deps.ch.insert` — so the+// `loadCh()` lazy import of `@openpanel/db` is gone. What is left is+// `loadChHelpers()`, the same v1-compat hop project.service.ts uses: these+// three statements are raw strings, not `sql` fragments, so they still need+// `TABLE_NAMES` / `formatClickhouseDate`, which are pure helpers living beside+// the client. Converting the statements is ADR-013's P7 work, not this wave's.//// `getCache` (not `getRedisCache`) is lazy for the same reason// subscription.service.ts's header gives: a static `import { getCache } from@@ -28,6 +26,7 @@ import { getRedisCache } from '@openpanel/redis';import { type GeoLocation, getGeoLocation } from '../../clients/geo';import type { Logger } from '../../logger';import type { ServiceDeps } from '../../services';+import { chQuery } from '../../shared/ch-query';import {DEFAULT_IP_HEADER_ORDER,getClientIpFromHeaders,@@ -45,8 +44,8 @@ import {} from './src/image-proxy';import { parseUrlMeta } from './src/parse-url-meta';-function loadCh() {- return import('@openpanel/db/src/clickhouse/client');+function loadChHelpers() {+ return import('../../v1-compat').then((m) => m.compatChHelpers());}function loadCache() {@@ -364,17 +363,19 @@ export interface StatsResult {eventsLast24hCount: number;}-export async function getStats(): Promise<StatsResult> {- const { chQuery, TABLE_NAMES } = await loadCh();+export async function getStats(deps: ServiceDeps): Promise<StatsResult> {+ const { TABLE_NAMES } = await loadChHelpers();const getCache = await loadCache();const res = await getCache(STATS_CACHE_KEY,STATS_CACHE_TTL_SECONDS,async () => {const projects = await chQuery<{ project_id: string; count: number }>(+ deps,`SELECT project_id, count(*) as count from ${TABLE_NAMES.events} GROUP by project_id order by count()`);const last24h = await chQuery<{ count: number }>(+ deps,`SELECT count(*) as count from ${TABLE_NAMES.events} WHERE created_at > now() - interval '24 hours'`);return { projects, last24hCount: last24h[0]?.count || 0 };@@ -394,9 +395,12 @@ export interface PingRecord {}/** `POST /misc/ping` — records a self-hosted instance's telemetry ping. */-export async function insertPingRecord(record: PingRecord): Promise<void> {- const { ch, TABLE_NAMES, formatClickhouseDate } = await loadCh();- await ch.insert({+export async function insertPingRecord(+ deps: ServiceDeps,+ record: PingRecord+): Promise<void> {+ const { TABLE_NAMES, formatClickhouseDate } = await loadChHelpers();+ await deps.ch.insert({table: TABLE_NAMES.self_hosting,values: [{@@ -457,13 +461,14 @@ export async function getGeoReport(* route above, on OUR side). Ported verbatim from* apps/worker/src/jobs/cron.ping.ts.*/-export async function runPingCron(): Promise<unknown> {+export async function runPingCron(deps: ServiceDeps): Promise<unknown> {if (process.env.DISABLE_PING) {return;}- const { chQuery, TABLE_NAMES } = await loadCh();+ const { TABLE_NAMES } = await loadChHelpers();const [res] = await chQuery<{ count: number }>(+ deps,`SELECT COUNT(*) as count FROM ${TABLE_NAMES.events}`);@@ -492,6 +497,6 @@ export interface MiscService {runPingCron(): Promise<unknown>;}-export function createMiscService(_deps: ServiceDeps): MiscService {- return { runPingCron };+export function createMiscService(deps: ServiceDeps): MiscService {+ return { runPingCron: () => runPingCron(deps) };}diff --git a/packages/core/src/modules/misc/src/data-health.ts b/packages/core/src/modules/misc/src/data-health.tsindex 359082437..b24bbb834 100644--- a/packages/core/src/modules/misc/src/data-health.ts+++ b/packages/core/src/modules/misc/src/data-health.ts@@ -5,6 +5,7 @@// `mock.module` (same idiom as modules/session/src/runtime.ts).import type { Logger } from '../../../logger';+import type { ServiceDeps } from '../../../services';const DAY_MS = 24 * 60 * 60 * 1000;// A brand-new project gets 48h to send its first event before we reach out.@@ -60,23 +61,23 @@ export interface DataHealthDeps {}export async function loadDataHealthDeps(+ deps: ServiceDeps,logger: Logger): Promise<DataHealthDeps> {- // project.service.ts's `getLastEventPerProject` takes `ServiceDeps` now- // (M10-004); this file has no `deps` of its own (just the bare `db` above),- // so it reaches the bare, v1-compat-wrapped, zero-arg spelling instead.- const [{ db }, { getLastEventPerProject }, { sendEmail }] = await Promise.all(- [- import('@openpanel/db/src/prisma-client'),- import('../../../v1-compat'),- import('../../../clients/email'),- ]- );+ // M10-005: Prisma is `deps.db` and `getLastEventPerProject` is the sibling+ // module's `ServiceDeps` spelling, so this job no longer reaches+ // @openpanel/db or the v1-compat seam. Both imports stay lazy: this file is+ // reached from jobs.registry.ts, and project.service.ts -> v1-compat.ts ->+ // services.ts is a cycle a static edge here would join.+ const [{ getLastEventPerProject }, { sendEmail }] = await Promise.all([+ import('../../project/project.service'),+ import('../../../clients/email'),+ ]);return {- db: db as unknown as DataHealthDb,+ db: deps.db as unknown as DataHealthDb,logger,- getLastEventPerProject,+ getLastEventPerProject: () => getLastEventPerProject(deps),sendEmail: (template, options) =>sendEmail(template, options as { to: string; data: never }),};diff --git a/packages/core/src/modules/notification/notification.jobs.test.ts b/packages/core/src/modules/notification/notification.jobs.test.tsindex 6ddcd572e..7fef3bfef 100644--- a/packages/core/src/modules/notification/notification.jobs.test.ts+++ b/packages/core/src/modules/notification/notification.jobs.test.ts@@ -40,6 +40,9 @@ function stubJobCtx(notification: Partial<NotificationService>): JobCtx {profile: {} as Services['profile'],group: {} as Services['group'],chart: {} as Services['chart'],+ overview: {} as Services['overview'],+ pages: {} as Services['pages'],+ realtime: {} as Services['realtime'],misc: {} as Services['misc'],report: {} as Services['report'],dashboard: {} as Services['dashboard'],diff --git a/packages/core/src/modules/onboarding/onboarding.jobs.test.ts b/packages/core/src/modules/onboarding/onboarding.jobs.test.tsindex d5b42a927..c3afd14d8 100644--- a/packages/core/src/modules/onboarding/onboarding.jobs.test.ts+++ b/packages/core/src/modules/onboarding/onboarding.jobs.test.ts@@ -43,6 +43,9 @@ function stubJobCtx(profile: {} as Services['profile'],group: {} as Services['group'],chart: {} as Services['chart'],+ overview: {} as Services['overview'],+ pages: {} as Services['pages'],+ realtime: {} as Services['realtime'],misc: {} as Services['misc'],report: {} as Services['report'],dashboard: {} as Services['dashboard'],diff --git a/packages/core/src/modules/organization/organization.jobs.test.ts b/packages/core/src/modules/organization/organization.jobs.test.tsindex aa65550ae..ba0cced5e 100644--- a/packages/core/src/modules/organization/organization.jobs.test.ts+++ b/packages/core/src/modules/organization/organization.jobs.test.ts@@ -46,6 +46,9 @@ function stubJobCtx(profile: {} as Services['profile'],group: {} as Services['group'],chart: {} as Services['chart'],+ overview: {} as Services['overview'],+ pages: {} as Services['pages'],+ realtime: {} as Services['realtime'],misc: {} as Services['misc'],report: {} as Services['report'],dashboard: {} as Services['dashboard'],diff --git a/packages/core/src/modules/organization/src/win-back-highlight.ts b/packages/core/src/modules/organization/src/win-back-highlight.tsindex d62486887..26f41403d 100644--- a/packages/core/src/modules/organization/src/win-back-highlight.ts+++ b/packages/core/src/modules/organization/src/win-back-highlight.ts@@ -93,13 +93,15 @@ export interface WinBackHighlightDeps {export async function loadWinBackHighlightDeps(logger: Logger): Promise<WinBackHighlightDeps> {+ // M10-005: both overview entry points take `ServiceDeps` now; the wind-down+ // cron that reaches this has none of its own, so — same as+ // `loadWindDownDeps`'s `getLastEventPerProject` — it uses the bare,+ // v1-compat-wrapped spellings.const [- { getAnalyticsOverviewCore },- { getTopPagesCore },+ { getAnalyticsOverviewCore, getTopPagesCore },{ generateWinBackPitch },] = await Promise.all([- import('../../overview/overview.service'),- import('../../overview/pages.service'),+ import('../../../v1-compat'),import('../../../clients/ai/win-back'),]);diff --git a/packages/core/src/modules/overview/overview.rpc.ts b/packages/core/src/modules/overview/overview.rpc.tsindex 4ce7082fb..daf702fd4 100644--- a/packages/core/src/modules/overview/overview.rpc.ts+++ b/packages/core/src/modules/overview/overview.rpc.ts@@ -8,7 +8,7 @@// response cache.//// `liveData`'s ClickHouse queries used to live inline in packages/trpc's-// router; they moved to `overviewService.getLiveData` (src/overview.sql.ts)+// router; they moved to `services.overview.getLiveData` (src/overview.sql.ts)// so every query this module runs goes through the same `sql` tag (M7-005).import {@@ -30,9 +30,7 @@ import {getOrganizationSubscriptionChartEndDate,getSettingsForProject,} from '../organization/organization.service';-import { getActiveVisitorCount } from '../realtime/realtime.service';import {- overviewService,zGetMapDataInput,zGetMetricsInput,zGetTopEventsInput,@@ -160,14 +158,14 @@ function getCurrentAndPrevious<export const overviewRouter = createTRPCRouter({liveVisitors: overviewProcedure.input(z.object({ projectId: z.string(), shareId: z.string().optional() }))- .query(async ({ input }) => {- return getActiveVisitorCount(input.projectId);+ .query(async ({ input, ctx }) => {+ return ctx.services.realtime.getActiveVisitorCount(input.projectId);}),liveData: overviewProcedure.input(z.object({ projectId: z.string(), shareId: z.string().optional() }))- .query(async ({ input }) => {- return overviewService.getLiveData(input.projectId);+ .query(async ({ input, ctx }) => {+ return ctx.services.overview.getLiveData(input.projectId);}),stats: overviewProcedure@@ -179,13 +177,13 @@ export const overviewRouter = createTRPCRouter({shareId: z.string().optional(),}))- .query(async ({ input }) => {+ .query(async ({ input, ctx }) => {const { timezone } = await getSettingsForProject(input.projectId);const { current, previous } = await getCurrentAndPrevious({ ...input, timezone },true,timezone- )(overviewService.getMetrics.bind(overviewService));+ )(ctx.services.overview.getMetrics);return {metrics: {...current.metrics,@@ -258,7 +256,7 @@ export const overviewRouter = createTRPCRouter({shareId: z.string().optional(),}))- .query(async ({ input }) => {+ .query(async ({ input, ctx }) => {const { timezone } = await getSettingsForProject(input.projectId);const { current } = await getCurrentAndPrevious({ ...input },@@ -266,14 +264,14 @@ export const overviewRouter = createTRPCRouter({timezone)(async (input) => {if (input.mode === 'page') {- return overviewService.getTopPages({ ...input, timezone });+ return ctx.services.overview.getTopPages({ ...input, timezone });}if (input.mode === 'bot') {return Promise.resolve([]);}- return overviewService.getTopEntryExit({+ return ctx.services.overview.getTopEntryExit({...input,mode: input.mode,timezone,@@ -292,13 +290,13 @@ export const overviewRouter = createTRPCRouter({shareId: z.string().optional(),}))- .query(async ({ input }) => {+ .query(async ({ input, ctx }) => {const { timezone } = await getSettingsForProject(input.projectId);const { current } = await getCurrentAndPrevious({ ...input, timezone },false,timezone- )(overviewService.getTopGeneric.bind(overviewService));+ )(ctx.services.overview.getTopGeneric);return current;}),@@ -314,13 +312,13 @@ export const overviewRouter = createTRPCRouter({shareId: z.string().optional(),}))- .query(async ({ input }) => {+ .query(async ({ input, ctx }) => {const { timezone } = await getSettingsForProject(input.projectId);const { current } = await getCurrentAndPrevious({ ...input, timezone },false,timezone- )(overviewService.getTopGenericSeries.bind(overviewService));+ )(ctx.services.overview.getTopGenericSeries);return current;}),@@ -335,14 +333,14 @@ export const overviewRouter = createTRPCRouter({shareId: z.string().optional(),}))- .query(async ({ input }) => {+ .query(async ({ input, ctx }) => {const { timezone } = await getSettingsForProject(input.projectId);const { current } = await getCurrentAndPrevious({ ...input, timezone },false,timezone)(async (input) => {- return overviewService.getUserJourney({+ return ctx.services.overview.getUserJourney({...input,steps: input.steps ?? 5,timezone,@@ -361,13 +359,13 @@ export const overviewRouter = createTRPCRouter({shareId: z.string().optional(),}))- .query(async ({ input }) => {+ .query(async ({ input, ctx }) => {const { timezone } = await getSettingsForProject(input.projectId);const { current } = await getCurrentAndPrevious({ ...input, timezone },false,timezone- )(overviewService.getTopEvents.bind(overviewService));+ )(ctx.services.overview.getTopEvents);return current;}),@@ -379,8 +377,8 @@ export const overviewRouter = createTRPCRouter({shareId: z.string().optional(),}))- .query(async ({ input }) => {- return getConversionEventNames(input.projectId);+ .query(async ({ input, ctx }) => {+ return getConversionEventNames(ctx, input.projectId);}),topLinkOut: overviewProcedure@@ -392,13 +390,13 @@ export const overviewRouter = createTRPCRouter({shareId: z.string().optional(),}))- .query(async ({ input }) => {+ .query(async ({ input, ctx }) => {const { timezone } = await getSettingsForProject(input.projectId);const { current } = await getCurrentAndPrevious({ ...input, timezone },false,timezone- )(overviewService.getTopLinkOut.bind(overviewService));+ )(ctx.services.overview.getTopLinkOut);return current;}),@@ -438,13 +436,13 @@ export const overviewRouter = createTRPCRouter({shareId: z.string().optional(),}))- .query(async ({ input }) => {+ .query(async ({ input, ctx }) => {const { timezone } = await getSettingsForProject(input.projectId);const { current } = await getCurrentAndPrevious({ ...input, timezone },false,timezone- )(overviewService.getMapData.bind(overviewService));+ )(ctx.services.overview.getMapData);return current;}),diff --git a/packages/core/src/modules/overview/overview.service.ts b/packages/core/src/modules/overview/overview.service.tsindex 2d372d0a7..792b2412c 100644--- a/packages/core/src/modules/overview/overview.service.ts+++ b/packages/core/src/modules/overview/overview.service.ts@@ -1,17 +1,22 @@// Ported from packages/db/src/services/overview.service.ts. The ClickHouse-// queries moved to src/overview.sql.ts (ADR-013, M7-005); `OverviewService`-// keeps V1's `constructor(client: typeof ch)` shape and public method names —-// `apps/api`'s insights controller and the mcp analytics tools still call-// `overviewService.getX(...)` as a bound method, and V1's own db package is-// now a re-export shim onto this module (DELEGATE PATTERN).+// queries moved to src/overview.sql.ts (ADR-013, M7-005).+//+// M10-005: `OverviewService` is no longer a class and there is no+// `overviewService` module singleton. Every method is a module-scope function+// taking `ServiceDeps` first and otherwise the same arguments, and+// `createOverviewService(deps)` binds them under their old method names, so+// `services.overview.getMetrics(input)` reads exactly as+// `overviewService.getMetrics(input)` did. The caller-supplied+// `constructor(client)` slot is gone with it: the client is `deps.ch`, which+// is what puts the request's id on the query's log line (ADR-018 R1).-import type { ClickHouseClient } from '@clickhouse/client';import { average, sum } from '@openpanel/common';import { chartColors } from '@openpanel/constants';import type { SqlFragment } from '@openpanel/db/src/clickhouse/sql';import type { IChartEventFilter, IInterval } from '@openpanel/validation';import { zTimeInterval } from '@openpanel/validation';import { z } from 'zod';+import type { ServiceDeps } from '../../services';import { convertClickhouseDateToJs } from '../chart/src/dates';import { getEventFiltersWhereClause } from '../chart/src/filter-where';import { getSettingsForProject } from '../organization/organization.service';@@ -244,11 +249,9 @@ export interface ILiveData {referrers: Array<{ referrer: string; count: number }>;}-export class OverviewService {- /** No client means the singleton path — `run-query.ts` resolves `ch` lazily. */- constructor(private client?: ClickHouseClient) {}-- private async createRevenueQuery({+async function createRevenueQuery(+ deps: ServiceDeps,+ {projectId,startDate,endDate,@@ -262,402 +265,527 @@ export class OverviewService {interval: IInterval;timezone: string;filters: IChartEventFilter[];- }): Promise<{ date: string; total_revenue: number }[]> {- const rows = await runQuery<{ date: string; total_revenue: number }>(- this.client,- revenueQuery({- projectId,- startDate,- endDate,- interval,- rawFilterWhere: this.getRawWhereClause('events', filters),- }),- timezone- );- return rows.map((row) => ({- ...row,- date: convertClickhouseDateToJs(row.date).toISOString(),- }));}+): Promise<{ date: string; total_revenue: number }[]> {+ const rows = await runQuery<{ date: string; total_revenue: number }>(+ deps,+ revenueQuery({+ projectId,+ startDate,+ endDate,+ interval,+ rawFilterWhere: getRawWhereClause('events', filters),+ }),+ timezone+ );+ return rows.map((row) => ({+ ...row,+ date: convertClickhouseDateToJs(row.date).toISOString(),+ }));+}- private mergeRevenueIntoSeries<T extends { date: string }>(- series: T[],- revenueData: { date: string; total_revenue: number }[]- ): (T & { total_revenue: number })[] {- const revenueByDate = new Map(- revenueData- .filter((r) => !isClickhouseDefaultMinDate(r.date))- .map((r) => [r.date, r.total_revenue])- );- return series.map((row) => ({- ...row,- total_revenue: revenueByDate.get(row.date) ?? 0,- }));- }+function mergeRevenueIntoSeries<T extends { date: string }>(+ series: T[],+ revenueData: { date: string; total_revenue: number }[]+): (T & { total_revenue: number })[] {+ const revenueByDate = new Map(+ revenueData+ .filter((r) => !isClickhouseDefaultMinDate(r.date))+ .map((r) => [r.date, r.total_revenue])+ );+ return series.map((row) => ({+ ...row,+ total_revenue: revenueByDate.get(row.date) ?? 0,+ }));+}- private getOverallRevenue(- revenueData: { date: string; total_revenue: number }[]- ): number {- return (- revenueData.find((r) => isClickhouseDefaultMinDate(r.date))- ?.total_revenue ?? 0- );- }+function getOverallRevenue(+ revenueData: { date: string; total_revenue: number }[]+): number {+ return (+ revenueData.find((r) => isClickhouseDefaultMinDate(r.date))+ ?.total_revenue ?? 0+ );+}- /**- * V1 `withDistinctSessionsIfNeeded`: a page (`path`) filter has no column on- * `sessions`, so it's resolved against `events` first and the session ids- * that match are intersected in. Returns the mutually-exclusive pair every- * `sessions`-scoped query builder takes.- */- private sessionsFilterMode(params: {- filters: IChartEventFilter[];- projectId: string;- startDate: string;- endDate: string;- }): { rawFilterWhere: string; distinctSessionsCte: SqlFragment | null } {- if (!this.isPageFilter(params.filters)) {- return {- rawFilterWhere: this.getRawWhereClause('sessions', params.filters),- distinctSessionsCte: null,- };- }+/**+ * V1 `withDistinctSessionsIfNeeded`: a page (`path`) filter has no column on+ * `sessions`, so it's resolved against `events` first and the session ids+ * that match are intersected in. Returns the mutually-exclusive pair every+ * `sessions`-scoped query builder takes.+ */+function sessionsFilterMode(params: {+ filters: IChartEventFilter[];+ projectId: string;+ startDate: string;+ endDate: string;+}): { rawFilterWhere: string; distinctSessionsCte: SqlFragment | null } {+ if (!isPageFilter(params.filters)) {return {- rawFilterWhere: '',- distinctSessionsCte: distinctSessionsQuery({- projectId: params.projectId,- startDate: params.startDate,- endDate: params.endDate,- rawFilterWhere: this.getRawWhereClause('events', params.filters),- }),+ rawFilterWhere: getRawWhereClause('sessions', params.filters),+ distinctSessionsCte: null,};}+ return {+ rawFilterWhere: '',+ distinctSessionsCte: distinctSessionsQuery({+ projectId: params.projectId,+ startDate: params.startDate,+ endDate: params.endDate,+ rawFilterWhere: getRawWhereClause('events', params.filters),+ }),+ };+}- isPageFilter(filters: IChartEventFilter[]) {- return filters.some((filter) => filter.name === 'path' && filter.value);- }+export function isPageFilter(filters: IChartEventFilter[]) {+ return filters.some((filter) => filter.name === 'path' && filter.value);+}- async getMetrics({+export async function getMetrics(+ deps: ServiceDeps,+ {projectId,filters,startDate,endDate,interval,timezone,- }: IGetMetricsInput): Promise<{- metrics: {- bounce_rate: number;- unique_visitors: number;- total_sessions: number;- avg_session_duration: number;- total_screen_views: number;- views_per_session: number;- total_revenue: number;- };- series: {- date: string;- bounce_rate: number;- unique_visitors: number;- total_sessions: number;- avg_session_duration: number;- total_screen_views: number;- views_per_session: number;- total_revenue: number;- }[];- }> {- return this.isPageFilter(filters)- ? this.getMetricsWithPageFilter({- projectId,- filters,- startDate,- endDate,- interval,- timezone,- })- : this.getMetricsFromSessions({- projectId,- filters,- startDate,- endDate,- interval,- timezone,- });- }+ }: IGetMetricsInput+): Promise<{+ metrics: {+ bounce_rate: number;+ unique_visitors: number;+ total_sessions: number;+ avg_session_duration: number;+ total_screen_views: number;+ views_per_session: number;+ total_revenue: number;+ };+ series: {+ date: string;+ bounce_rate: number;+ unique_visitors: number;+ total_sessions: number;+ avg_session_duration: number;+ total_screen_views: number;+ views_per_session: number;+ total_revenue: number;+ }[];+}> {+ return isPageFilter(filters)+ ? getMetricsWithPageFilter(deps, {+ projectId,+ filters,+ startDate,+ endDate,+ interval,+ timezone,+ })+ : getMetricsFromSessions(deps, {+ projectId,+ filters,+ startDate,+ endDate,+ interval,+ timezone,+ });+}- private async getMetricsFromSessions({+async function getMetricsFromSessions(+ deps: ServiceDeps,+ {projectId,filters,startDate,endDate,interval,timezone,- }: IGetMetricsInput): Promise<{- metrics: MetricsRow & { total_revenue: number };- series: MetricsSeriesRow[];- }> {- const [sessionRes, revenueRes] = await Promise.all([- runQuery<{- date: string;- bounce_rate: number;- unique_visitors: number;- total_sessions: number;- avg_session_duration: number;- total_screen_views: number;- views_per_session: number;- }>(- this.client,- sessionMetricsQuery({- projectId,- startDate,- endDate,- interval,- rawFilterWhere: this.getRawWhereClause('sessions', filters),- }),- timezone- ).then((rows) =>- rows.map((row) => ({ ...row, date: new Date(row.date).toISOString() }))- ),- this.createRevenueQuery({ projectId, startDate, endDate, interval, timezone, filters }),- ]);+ }: IGetMetricsInput+): Promise<{+ metrics: MetricsRow & { total_revenue: number };+ series: MetricsSeriesRow[];+}> {+ const [sessionRes, revenueRes] = await Promise.all([+ runQuery<{+ date: string;+ bounce_rate: number;+ unique_visitors: number;+ total_sessions: number;+ avg_session_duration: number;+ total_screen_views: number;+ views_per_session: number;+ }>(+ deps,+ sessionMetricsQuery({+ projectId,+ startDate,+ endDate,+ interval,+ rawFilterWhere: getRawWhereClause('sessions', filters),+ }),+ timezone+ ).then((rows) =>+ rows.map((row) => ({ ...row, date: new Date(row.date).toISOString() }))+ ),+ createRevenueQuery(deps, {+ projectId,+ startDate,+ endDate,+ interval,+ timezone,+ filters,+ }),+ ]);- const overallRevenue = this.getOverallRevenue(revenueRes);- const series = this.mergeRevenueIntoSeries(sessionRes.slice(1), revenueRes);+ const overallRevenue = getOverallRevenue(revenueRes);+ const series = mergeRevenueIntoSeries(sessionRes.slice(1), revenueRes);- return {- metrics: {- bounce_rate: sessionRes[0]?.bounce_rate ?? 0,- unique_visitors: sessionRes[0]?.unique_visitors ?? 0,- total_sessions: sessionRes[0]?.total_sessions ?? 0,- avg_session_duration: sessionRes[0]?.avg_session_duration ?? 0,- total_screen_views: sessionRes[0]?.total_screen_views ?? 0,- views_per_session: sessionRes[0]?.views_per_session ?? 0,- total_revenue: overallRevenue,- },- series,- };- }+ return {+ metrics: {+ bounce_rate: sessionRes[0]?.bounce_rate ?? 0,+ unique_visitors: sessionRes[0]?.unique_visitors ?? 0,+ total_sessions: sessionRes[0]?.total_sessions ?? 0,+ avg_session_duration: sessionRes[0]?.avg_session_duration ?? 0,+ total_screen_views: sessionRes[0]?.total_screen_views ?? 0,+ views_per_session: sessionRes[0]?.views_per_session ?? 0,+ total_revenue: overallRevenue,+ },+ series,+ };+}- private async getMetricsWithPageFilter({+async function getMetricsWithPageFilter(+ deps: ServiceDeps,+ {projectId,filters,startDate,endDate,interval,timezone,- }: IGetMetricsInput): Promise<{- metrics: MetricsRow & { total_revenue: number };- series: MetricsSeriesRow[];- }> {- const rawEventFilterWhere = this.getRawWhereClause('events', filters);- const rawSessionFilterWhere = this.getRawWhereClause('sessions', filters);-- const [mainRes, revenueRes] = await Promise.all([- runQuery<{- date: string;- bounce_rate: number;- unique_visitors: number;- total_sessions: number;- avg_session_duration: number;- total_screen_views: number;- views_per_session: number;- overall_unique_visitors: number;- overall_total_sessions: number;- overall_bounce_rate: number;- }>(- this.client,- metricsWithPageFilterQuery({- projectId,- startDate,- endDate,- interval,- rawSessionFilterWhere,- rawEventFilterWhere,- }),- timezone- ).then((rows) =>- rows.map((row) => ({ ...row, date: new Date(row.date).toISOString() }))- ),- this.createRevenueQuery({ projectId, startDate, endDate, interval, timezone, filters }),- ]);+ }: IGetMetricsInput+): Promise<{+ metrics: MetricsRow & { total_revenue: number };+ series: MetricsSeriesRow[];+}> {+ const rawEventFilterWhere = getRawWhereClause('events', filters);+ const rawSessionFilterWhere = getRawWhereClause('sessions', filters);++ const [mainRes, revenueRes] = await Promise.all([+ runQuery<{+ date: string;+ bounce_rate: number;+ unique_visitors: number;+ total_sessions: number;+ avg_session_duration: number;+ total_screen_views: number;+ views_per_session: number;+ overall_unique_visitors: number;+ overall_total_sessions: number;+ overall_bounce_rate: number;+ }>(+ deps,+ metricsWithPageFilterQuery({+ projectId,+ startDate,+ endDate,+ interval,+ rawSessionFilterWhere,+ rawEventFilterWhere,+ }),+ timezone+ ).then((rows) =>+ rows.map((row) => ({ ...row, date: new Date(row.date).toISOString() }))+ ),+ createRevenueQuery(deps, {+ projectId,+ startDate,+ endDate,+ interval,+ timezone,+ filters,+ }),+ ]);- const overallRevenue = this.getOverallRevenue(revenueRes);- const series = this.mergeRevenueIntoSeries(mainRes, revenueRes);+ const overallRevenue = getOverallRevenue(revenueRes);+ const series = mergeRevenueIntoSeries(mainRes, revenueRes);- const anyRowWithData = mainRes.find(- (item) =>- item.overall_bounce_rate !== null ||- item.overall_total_sessions !== null ||- item.overall_unique_visitors !== null- );+ const anyRowWithData = mainRes.find(+ (item) =>+ item.overall_bounce_rate !== null ||+ item.overall_total_sessions !== null ||+ item.overall_unique_visitors !== null+ );- return {- metrics: {- bounce_rate: anyRowWithData?.overall_bounce_rate ?? 0,- unique_visitors: anyRowWithData?.overall_unique_visitors ?? 0,- total_sessions: anyRowWithData?.overall_total_sessions ?? 0,- avg_session_duration: average(- mainRes.map((item) => item.avg_session_duration)- ),- total_screen_views: sum(mainRes.map((item) => item.total_screen_views)),- views_per_session: average(- mainRes.map((item) => item.views_per_session)- ),- total_revenue: overallRevenue,- },- series,- };- }+ return {+ metrics: {+ bounce_rate: anyRowWithData?.overall_bounce_rate ?? 0,+ unique_visitors: anyRowWithData?.overall_unique_visitors ?? 0,+ total_sessions: anyRowWithData?.overall_total_sessions ?? 0,+ avg_session_duration: average(+ mainRes.map((item) => item.avg_session_duration)+ ),+ total_screen_views: sum(mainRes.map((item) => item.total_screen_views)),+ views_per_session: average(mainRes.map((item) => item.views_per_session)),+ total_revenue: overallRevenue,+ },+ series,+ };+}- getRawWhereClause(type: 'events' | 'sessions', filters: IChartEventFilter[]) {- const where = getEventFiltersWhereClause(- filters.flatMap((item) => {- if (!WHITELISTED_FILTERS.includes(item.name)) {- return [];+export function getRawWhereClause(+ type: 'events' | 'sessions',+ filters: IChartEventFilter[]+) {+ const where = getEventFiltersWhereClause(+ filters.flatMap((item) => {+ if (!WHITELISTED_FILTERS.includes(item.name)) {+ return [];+ }+ if (type === 'sessions') {+ if (item.name === 'path') {+ return [{ ...item, name: 'entry_path' }];+ }+ if (item.name === 'origin') {+ return [{ ...item, name: 'entry_origin' }];}- if (type === 'sessions') {- if (item.name === 'path') {- return [{ ...item, name: 'entry_path' }];- }- if (item.name === 'origin') {- return [{ ...item, name: 'entry_origin' }];- }- if (item.name.startsWith('properties.__query.utm_')) {- return [- {- ...item,- name: item.name.replace('properties.__query.utm_', 'utm_'),- },- ];- }- // sessions table has no `properties` map for arbitrary keys —- // drop them instead of generating an invalid WHERE clause.- if (item.name.startsWith('properties.')) {- return [];- }- return [item];+ if (item.name.startsWith('properties.__query.utm_')) {+ return [+ {+ ...item,+ name: item.name.replace('properties.__query.utm_', 'utm_'),+ },+ ];}- // events table has no top-level utm_* columns — those live in the- // properties map under the __query.utm_* keys. Route them through- // getEventFiltersWhereClause's properties.* path so we emit- // `properties['__query.utm_source']` instead of the bare column.- if (UTM_COLUMNS.includes(item.name)) {- return [{ ...item, name: `properties.__query.${item.name}` }];+ // sessions table has no `properties` map for arbitrary keys —+ // drop them instead of generating an invalid WHERE clause.+ if (item.name.startsWith('properties.')) {+ return [];}return [item];- }),- undefined,- undefined,- type- );+ }+ // events table has no top-level utm_* columns — those live in the+ // properties map under the __query.utm_* keys. Route them through+ // getEventFiltersWhereClause's properties.* path so we emit+ // `properties['__query.utm_source']` instead of the bare column.+ if (UTM_COLUMNS.includes(item.name)) {+ return [{ ...item, name: `properties.__query.${item.name}` }];+ }+ return [item];+ }),+ undefined,+ undefined,+ type+ );++ return Object.values(where).join(' AND ');+}- return Object.values(where).join(' AND ');- }+export async function getTopPages(+ deps: ServiceDeps,+ { projectId, filters, startDate, endDate, timezone, limit }: IGetTopPagesInput+) {+ return runQuery<{+ origin: string;+ path: string;+ sessions: number;+ pageviews: number;+ revenue?: number;+ }>(+ deps,+ topPagesQuery({+ projectId,+ startDate,+ endDate,+ rawFilterWhere: getRawWhereClause('events', filters),+ limit: Math.min(limit ?? MAX_RECORDS_LIMIT, MAX_RECORDS_LIMIT),+ }),+ timezone+ );+}- async getTopPages({+export async function getTopEntryExit(+ deps: ServiceDeps,+ {projectId,filters,startDate,endDate,+ mode,timezone,limit,- }: IGetTopPagesInput) {- return runQuery<{- origin: string;- path: string;- sessions: number;- pageviews: number;- revenue?: number;- }>(- this.client,- topPagesQuery({- projectId,- startDate,- endDate,- rawFilterWhere: this.getRawWhereClause('events', filters),- limit: Math.min(limit ?? MAX_RECORDS_LIMIT, MAX_RECORDS_LIMIT),- }),- timezone- );- }+ }: IGetTopEntryExitInput+) {+ const sessionsFilter = sessionsFilterMode({+ filters,+ projectId,+ startDate,+ endDate,+ });+ return runQuery<{+ origin: string;+ path: string;+ sessions: number;+ pageviews: number;+ revenue?: number;+ }>(+ deps,+ topEntryExitQuery({+ projectId,+ startDate,+ endDate,+ mode,+ limit: Math.min(limit ?? MAX_RECORDS_LIMIT, MAX_RECORDS_LIMIT),+ rawFilterWhere: sessionsFilter.rawFilterWhere,+ distinctSessionsCte: sessionsFilter.distinctSessionsCte,+ }),+ timezone+ );+}- async getTopEntryExit({+export async function getTopGeneric(+ deps: ServiceDeps,+ {projectId,filters,startDate,endDate,- mode,+ column,timezone,- limit,- }: IGetTopEntryExitInput) {- const sessionsFilter = this.sessionsFilterMode({ filters, projectId, startDate, endDate });- return runQuery<{- origin: string;- path: string;- sessions: number;- pageviews: number;- revenue?: number;- }>(- this.client,- topEntryExitQuery({- projectId,- startDate,- endDate,- mode,- limit: Math.min(limit ?? MAX_RECORDS_LIMIT, MAX_RECORDS_LIMIT),- rawFilterWhere: sessionsFilter.rawFilterWhere,- distinctSessionsCte: sessionsFilter.distinctSessionsCte,- }),- timezone- );+ }: IGetTopGenericInput+) {+ if (!WHITELISTED_FILTERS.includes(column)) {+ return [];}- async getTopGeneric({+ const prefixColumn = COLUMN_PREFIX_MAP[column] ?? null;+ const sessionsFilter = sessionsFilterMode({+ filters,+ projectId,+ startDate,+ endDate,+ });++ return runQuery<{+ prefix?: string;+ name: string;+ sessions: number;+ pageviews: number;+ revenue?: number;+ }>(+ deps,+ topGenericQuery({+ projectId,+ startDate,+ endDate,+ column,+ prefixColumn,+ limit: MAX_RECORDS_LIMIT,+ rawFilterWhere: sessionsFilter.rawFilterWhere,+ distinctSessionsCte: sessionsFilter.distinctSessionsCte,+ }),+ timezone+ );+}++export async function getTopGenericSeries(+ deps: ServiceDeps,+ {projectId,filters,startDate,endDate,column,+ interval,timezone,- }: IGetTopGenericInput) {- if (!WHITELISTED_FILTERS.includes(column)) {- return [];- }-- const prefixColumn = COLUMN_PREFIX_MAP[column] ?? null;- const sessionsFilter = this.sessionsFilterMode({ filters, projectId, startDate, endDate });-- return runQuery<{- prefix?: string;- name: string;+ }: IGetTopGenericSeriesInput+): Promise<{+ items: Array<{+ name: string;+ prefix?: string;+ data: Array<{+ date: string;sessions: number;pageviews: number;revenue?: number;- }>(- this.client,- topGenericQuery({- projectId,- startDate,- endDate,- column,- prefixColumn,- limit: MAX_RECORDS_LIMIT,- rawFilterWhere: sessionsFilter.rawFilterWhere,- distinctSessionsCte: sessionsFilter.distinctSessionsCte,- }),- timezone- );- }+ }>;+ total: { sessions: number; pageviews: number; revenue?: number };+ }>;+}> {+ const prefixColumn = COLUMN_PREFIX_MAP[column] ?? null;+ const TOP_LIMIT = 500;- async getTopGenericSeries({- projectId,+ const topItemsSessionsFilter = sessionsFilterMode({filters,+ projectId,startDate,endDate,- column,- interval,- timezone,- }: IGetTopGenericSeriesInput): Promise<{- items: Array<{+ });++ const topItems = await runQuery<{+ prefix?: string;+ name: string;+ sessions: number;+ pageviews: number;+ revenue?: number;+ }>(+ deps,+ topGenericSeriesTopItemsQuery({+ projectId,+ startDate,+ endDate,+ column,+ prefixColumn,+ limit: TOP_LIMIT,+ rawFilterWhere: topItemsSessionsFilter.rawFilterWhere,+ distinctSessionsCte: topItemsSessionsFilter.distinctSessionsCte,+ }),+ timezone+ );++ if (topItems.length === 0) {+ return { items: [] };+ }++ // V1 always applies the sessions rawWhere here (unlike the top-items+ // query above), additionally wrapping in distinct_sessions on a page+ // filter — see overview.sql.ts's topGenericSeriesTimeSeriesQuery header.+ const timeSeriesSessionsFilter = isPageFilter(filters)+ ? distinctSessionsQuery({+ projectId,+ startDate,+ endDate,+ rawFilterWhere: getRawWhereClause('events', filters),+ })+ : null;++ const timeSeriesData = await runQuery<{+ date: string;+ prefix?: string;+ name: string;+ sessions: number;+ pageviews: number;+ revenue?: number;+ }>(+ deps,+ topGenericSeriesTimeSeriesQuery({+ projectId,+ startDate,+ endDate,+ interval,+ column,+ prefixColumn,+ rawFilterWhere: getRawWhereClause('sessions', filters),+ distinctSessionsCte: timeSeriesSessionsFilter,+ }),+ timezone+ ).then((rows) =>+ rows.map((row) => ({ ...row, date: new Date(row.date).toISOString() }))+ );++ const itemsMap = new Map<+ string,+ {name: string;prefix?: string;data: Array<{@@ -667,347 +795,267 @@ export class OverviewService {revenue?: number;}>;total: { sessions: number; pageviews: number; revenue?: number };- }>;- }> {- const prefixColumn = COLUMN_PREFIX_MAP[column] ?? null;- const TOP_LIMIT = 500;-- const topItemsSessionsFilter = this.sessionsFilterMode({- filters,- projectId,- startDate,- endDate,- });-- const topItems = await runQuery<{- prefix?: string;- name: string;- sessions: number;- pageviews: number;- revenue?: number;- }>(- this.client,- topGenericSeriesTopItemsQuery({- projectId,- startDate,- endDate,- column,- prefixColumn,- limit: TOP_LIMIT,- rawFilterWhere: topItemsSessionsFilter.rawFilterWhere,- distinctSessionsCte: topItemsSessionsFilter.distinctSessionsCte,- }),- timezone- );-- if (topItems.length === 0) {- return { items: [] };}+ >();++ for (const item of topItems) {+ const key = `${item.prefix || ''}:${item.name}`;+ itemsMap.set(key, {+ name: item.name,+ prefix: item.prefix,+ data: [],+ total: {+ sessions: item.sessions,+ pageviews: item.pageviews,+ revenue: item.revenue ?? 0,+ },+ });+ }- // V1 always applies the sessions rawWhere here (unlike the top-items- // query above), additionally wrapping in distinct_sessions on a page- // filter — see overview.sql.ts's topGenericSeriesTimeSeriesQuery header.- const timeSeriesSessionsFilter = this.isPageFilter(filters)- ? distinctSessionsQuery({- projectId,- startDate,- endDate,- rawFilterWhere: this.getRawWhereClause('events', filters),- })- : null;-- const timeSeriesData = await runQuery<{- date: string;- prefix?: string;- name: string;- sessions: number;- pageviews: number;- revenue?: number;- }>(- this.client,- topGenericSeriesTimeSeriesQuery({- projectId,- startDate,- endDate,- interval,- column,- prefixColumn,- rawFilterWhere: this.getRawWhereClause('sessions', filters),- distinctSessionsCte: timeSeriesSessionsFilter,- }),- timezone- ).then((rows) =>- rows.map((row) => ({ ...row, date: new Date(row.date).toISOString() }))- );-- const itemsMap = new Map<- string,- {- name: string;- prefix?: string;- data: Array<{- date: string;- sessions: number;- pageviews: number;- revenue?: number;- }>;- total: { sessions: number; pageviews: number; revenue?: number };- }- >();-- for (const item of topItems) {- const key = `${item.prefix || ''}:${item.name}`;- itemsMap.set(key, {- name: item.name,- prefix: item.prefix,- data: [],- total: {- sessions: item.sessions,- pageviews: item.pageviews,- revenue: item.revenue ?? 0,- },+ for (const row of timeSeriesData) {+ const key = `${row.prefix || ''}:${row.name}`;+ const item = itemsMap.get(key);+ if (item) {+ item.data.push({+ date: row.date,+ sessions: row.sessions,+ pageviews: row.pageviews,+ revenue: row.revenue,});}-- for (const row of timeSeriesData) {- const key = `${row.prefix || ''}:${row.name}`;- const item = itemsMap.get(key);- if (item) {- item.data.push({- date: row.date,- sessions: row.sessions,- pageviews: row.pageviews,- revenue: row.revenue,- });- }- }-- return {- items: Array.from(itemsMap.values()),- };}- async getUserJourney({+ return {+ items: Array.from(itemsMap.values()),+ };+}++export async function getUserJourney(+ deps: ServiceDeps,+ {projectId,filters,startDate,endDate,steps = 5,timezone,- }: IGetUserJourneyInput): Promise<{- nodes: Array<{- id: string;- label: string;- nodeColor: string;- percentage?: number;- value?: number;- step?: number;- }>;- links: Array<{ source: string; target: string; value: number }>;- }> {- // Config- const TOP_ENTRIES = 3; // Only show top 3 entry pages- const TOP_DESTINATIONS_PER_NODE = 3; // Top 3 destinations from each node-- // Color palette - each entry page gets a consistent color- const COLORS = chartColors.map((color) => color.main);-- const rawFilterWhere = this.getRawWhereClause('events', filters);- const orderedEventsInput = { projectId, startDate, endDate, rawFilterWhere };-- const topEntries = await runQuery<{ entry_page: string; count: number }>(- this.client,- topEntriesQuery({ ...orderedEventsInput, steps, topEntries: TOP_ENTRIES }),- timezone- );-- if (topEntries.length === 0) {- return { nodes: [], links: [] };- }-- const topEntryPages = topEntries.map((e) => e.entry_page);- const totalSessions = topEntries.reduce((total, e) => total + e.count, 0);-- const transitions = await runQuery<{- source: string;- target: string;- step: number;- value: number;- }>(- this.client,- transitionsQuery({ ...orderedEventsInput, steps, topEntryPages }),- timezone- );+ }: IGetUserJourneyInput+): Promise<{+ nodes: Array<{+ id: string;+ label: string;+ nodeColor: string;+ percentage?: number;+ value?: number;+ step?: number;+ }>;+ links: Array<{ source: string; target: string; value: number }>;+}> {+ // Config+ const TOP_ENTRIES = 3; // Only show top 3 entry pages+ const TOP_DESTINATIONS_PER_NODE = 3; // Top 3 destinations from each node++ // Color palette - each entry page gets a consistent color+ const COLORS = chartColors.map((color) => color.main);++ const rawFilterWhere = getRawWhereClause('events', filters);+ const orderedEventsInput = { projectId, startDate, endDate, rawFilterWhere };++ const topEntries = await runQuery<{ entry_page: string; count: number }>(+ deps,+ topEntriesQuery({ ...orderedEventsInput, steps, topEntries: TOP_ENTRIES }),+ timezone+ );++ if (topEntries.length === 0) {+ return { nodes: [], links: [] };+ }- if (transitions.length === 0) {- return { nodes: [], links: [] };- }+ const topEntryPages = topEntries.map((e) => e.entry_page);+ const totalSessions = topEntries.reduce((total, e) => total + e.count, 0);++ const transitions = await runQuery<{+ source: string;+ target: string;+ step: number;+ value: number;+ }>(+ deps,+ transitionsQuery({ ...orderedEventsInput, steps, topEntryPages }),+ timezone+ );++ if (transitions.length === 0) {+ return { nodes: [], links: [] };+ }- // Build the sankey progressively step by step. Start with entry nodes,- // then follow top destinations at each step. Node IDs combine path with- // step to prevent circular references.- const nodes = new Map<- string,- { path: string; value: number; step: number; color: string }- >();- const links: Array<{ source: string; target: string; value: number }> = [];-- const getNodeId = (path: string, step: number) => `${path}::step${step}`;-- const transitionsByStep = new Map<number, typeof transitions>();- for (const t of transitions) {- if (!transitionsByStep.has(t.step)) {- transitionsByStep.set(t.step, []);- }- transitionsByStep.get(t.step)!.push(t);+ // Build the sankey progressively step by step. Start with entry nodes,+ // then follow top destinations at each step. Node IDs combine path with+ // step to prevent circular references.+ const nodes = new Map<+ string,+ { path: string; value: number; step: number; color: string }+ >();+ const links: Array<{ source: string; target: string; value: number }> = [];++ const getNodeId = (path: string, step: number) => `${path}::step${step}`;++ const transitionsByStep = new Map<number, typeof transitions>();+ for (const t of transitions) {+ if (!transitionsByStep.has(t.step)) {+ transitionsByStep.set(t.step, []);}+ transitionsByStep.get(t.step)!.push(t);+ }- const activeNodes = new Map<string, string>(); // path -> nodeId- topEntries.forEach((entry, idx) => {- const nodeId = getNodeId(entry.entry_page, 1);- nodes.set(nodeId, {- path: entry.entry_page,- value: entry.count,- step: 1,- color: COLORS[idx % COLORS.length]!,- });- activeNodes.set(entry.entry_page, nodeId);+ const activeNodes = new Map<string, string>(); // path -> nodeId+ topEntries.forEach((entry, idx) => {+ const nodeId = getNodeId(entry.entry_page, 1);+ nodes.set(nodeId, {+ path: entry.entry_page,+ value: entry.count,+ step: 1,+ color: COLORS[idx % COLORS.length]!,});+ activeNodes.set(entry.entry_page, nodeId);+ });++ for (let step = 1; step < steps; step++) {+ const stepTransitions = transitionsByStep.get(step) || [];+ const nextActiveNodes = new Map<string, string>();- for (let step = 1; step < steps; step++) {- const stepTransitions = transitionsByStep.get(step) || [];- const nextActiveNodes = new Map<string, string>();+ for (const [sourcePath, sourceNodeId] of activeNodes) {+ const fromSource = stepTransitions+ .filter((t) => t.source === sourcePath)+ .sort((a, b) => b.value - a.value)+ .slice(0, TOP_DESTINATIONS_PER_NODE);- for (const [sourcePath, sourceNodeId] of activeNodes) {- const fromSource = stepTransitions- .filter((t) => t.source === sourcePath)- .sort((a, b) => b.value - a.value)- .slice(0, TOP_DESTINATIONS_PER_NODE);+ for (const t of fromSource) {+ if (t.source === t.target) {+ continue;+ }- for (const t of fromSource) {- if (t.source === t.target) {- continue;- }+ const targetNodeId = getNodeId(t.target, step + 1);- const targetNodeId = getNodeId(t.target, step + 1);+ links.push({+ source: sourceNodeId,+ target: targetNodeId,+ value: t.value,+ });- links.push({- source: sourceNodeId,- target: targetNodeId,+ const existing = nodes.get(targetNodeId);+ if (existing) {+ existing.value += t.value;+ } else {+ const sourceData = nodes.get(sourceNodeId);+ nodes.set(targetNodeId, {+ path: t.target,value: t.value,+ step: step + 1,+ color: sourceData?.color || COLORS[nodes.size % COLORS.length]!,});-- const existing = nodes.get(targetNodeId);- if (existing) {- existing.value += t.value;- } else {- const sourceData = nodes.get(sourceNodeId);- nodes.set(targetNodeId, {- path: t.target,- value: t.value,- step: step + 1,- color: sourceData?.color || COLORS[nodes.size % COLORS.length]!,- });- }-- nextActiveNodes.set(t.target, targetNodeId);}- }- activeNodes.clear();- for (const [path, nodeId] of nextActiveNodes) {- activeNodes.set(path, nodeId);+ nextActiveNodes.set(t.target, targetNodeId);}+ }- if (activeNodes.size === 0) {- break;- }+ activeNodes.clear();+ for (const [path, nodeId] of nextActiveNodes) {+ activeNodes.set(path, nodeId);}- // Filter links by threshold (0.25% of total sessions)- const MIN_LINK_PERCENT = 0.25;- const minLinkValue = Math.ceil((totalSessions * MIN_LINK_PERCENT) / 100);- const filteredLinks = links.filter((link) => link.value >= minLinkValue);+ if (activeNodes.size === 0) {+ break;+ }+ }- const referencedNodeIds = new Set<string>();- filteredLinks.forEach((link) => {- referencedNodeIds.add(link.source);- referencedNodeIds.add(link.target);- });+ // Filter links by threshold (0.25% of total sessions)+ const MIN_LINK_PERCENT = 0.25;+ const minLinkValue = Math.ceil((totalSessions * MIN_LINK_PERCENT) / 100);+ const filteredLinks = links.filter((link) => link.value >= minLinkValue);- const nodeValuesFromLinks = new Map<string, number>();- filteredLinks.forEach((link) => {- const current = nodeValuesFromLinks.get(link.target) || 0;- nodeValuesFromLinks.set(link.target, current + link.value);- });+ const referencedNodeIds = new Set<string>();+ filteredLinks.forEach((link) => {+ referencedNodeIds.add(link.source);+ referencedNodeIds.add(link.target);+ });- // For entry nodes (step 1), only keep them if they have outgoing links after filtering- nodes.forEach((nodeData, nodeId) => {- if (nodeData.step === 1) {- const hasOutgoing = filteredLinks.some((l) => l.source === nodeId);- if (!hasOutgoing) {- referencedNodeIds.delete(nodeId);- }- }- });+ const nodeValuesFromLinks = new Map<string, number>();+ filteredLinks.forEach((link) => {+ const current = nodeValuesFromLinks.get(link.target) || 0;+ nodeValuesFromLinks.set(link.target, current + link.value);+ });- const finalNodes = Array.from(nodes.entries())- .filter(([id]) => referencedNodeIds.has(id))- .map(([id, data]) => {- const value =- data.step === 1- ? data.value- : nodeValuesFromLinks.get(id) || data.value;- return {- id,- label: data.path,- nodeColor: data.color,- percentage: (value / totalSessions) * 100,- value,- step: data.step,- };- })- .sort((a, b) => {- if (a.step !== b.step) {- return a.step - b.step;- }- return b.value - a.value;- });+ // For entry nodes (step 1), only keep them if they have outgoing links after filtering+ nodes.forEach((nodeData, nodeId) => {+ if (nodeData.step === 1) {+ const hasOutgoing = filteredLinks.some((l) => l.source === nodeId);+ if (!hasOutgoing) {+ referencedNodeIds.delete(nodeId);+ }+ }+ });- const nodeIds = new Set(finalNodes.map((n) => n.id));- const invalidLinks = filteredLinks.filter(- (link) => !(nodeIds.has(link.source) && nodeIds.has(link.target))- );- if (invalidLinks.length > 0) {- console.warn(- `UserJourney: Found ${invalidLinks.length} links with missing nodes`- );- const validLinks = filteredLinks.filter(- (link) => nodeIds.has(link.source) && nodeIds.has(link.target)- );+ const finalNodes = Array.from(nodes.entries())+ .filter(([id]) => referencedNodeIds.has(id))+ .map(([id, data]) => {+ const value =+ data.step === 1+ ? data.value+ : nodeValuesFromLinks.get(id) || data.value;return {- nodes: finalNodes,- links: validLinks,+ id,+ label: data.path,+ nodeColor: data.color,+ percentage: (value / totalSessions) * 100,+ value,+ step: data.step,};- }-- const stepsValid = finalNodes.every((node, idx, arr) => {- if (idx === 0) {- return true;+ })+ .sort((a, b) => {+ if (a.step !== b.step) {+ return a.step - b.step;}- return node.step! >= arr[idx - 1]!.step!;+ return b.value - a.value;});- if (!stepsValid) {- console.warn('UserJourney: Steps are not monotonic');- }+ const nodeIds = new Set(finalNodes.map((n) => n.id));+ const invalidLinks = filteredLinks.filter(+ (link) => !(nodeIds.has(link.source) && nodeIds.has(link.target))+ );+ if (invalidLinks.length > 0) {+ console.warn(+ `UserJourney: Found ${invalidLinks.length} links with missing nodes`+ );+ const validLinks = filteredLinks.filter(+ (link) => nodeIds.has(link.source) && nodeIds.has(link.target)+ );return {nodes: finalNodes,- links: filteredLinks,+ links: validLinks,};}- async getTopEvents({+ const stepsValid = finalNodes.every((node, idx, arr) => {+ if (idx === 0) {+ return true;+ }+ return node.step! >= arr[idx - 1]!.step!;+ });+ if (!stepsValid) {+ console.warn('UserJourney: Steps are not monotonic');+ }++ return {+ nodes: finalNodes,+ links: filteredLinks,+ };+}++export async function getTopEvents(+ deps: ServiceDeps,+ {projectId,filters,startDate,@@ -1021,21 +1069,24 @@ export class OverviewService {endDate: string;timezone: string;excludeEvents?: string[];- }): Promise<Array<{ name: string; count: number }>> {- return runQuery<{ name: string; count: number }>(- this.client,- topEventsQuery({- projectId,- startDate,- endDate,- rawFilterWhere: this.getRawWhereClause('events', filters),- excludeEvents,- }),- timezone- );}+): Promise<Array<{ name: string; count: number }>> {+ return runQuery<{ name: string; count: number }>(+ deps,+ topEventsQuery({+ projectId,+ startDate,+ endDate,+ rawFilterWhere: getRawWhereClause('events', filters),+ excludeEvents,+ }),+ timezone+ );+}- async getTopLinkOut({+export async function getTopLinkOut(+ deps: ServiceDeps,+ {projectId,filters,startDate,@@ -1047,20 +1098,23 @@ export class OverviewService {startDate: string;endDate: string;timezone: string;- }): Promise<Array<{ href: string; count: number }>> {- return runQuery<{ href: string; count: number }>(- this.client,- topLinkOutQuery({- projectId,- startDate,- endDate,- rawFilterWhere: this.getRawWhereClause('events', filters),- }),- timezone- );}+): Promise<Array<{ href: string; count: number }>> {+ return runQuery<{ href: string; count: number }>(+ deps,+ topLinkOutQuery({+ projectId,+ startDate,+ endDate,+ rawFilterWhere: getRawWhereClause('events', filters),+ }),+ timezone+ );+}- async getMapData({+export async function getMapData(+ deps: ServiceDeps,+ {projectId,filters,startDate,@@ -1072,105 +1126,106 @@ export class OverviewService {startDate: string;endDate: string;timezone: string;- }): Promise<- Array<{- country: string;- region?: string;- city?: string;- lat: number;- lng: number;- count: number;- }>- > {- const results = await runQuery<{- country: string;- region: string | null;- city: string | null;- count: number;- }>(- this.client,- mapDataQuery({- projectId,- startDate,- endDate,- rawFilterWhere: this.getRawWhereClause('events', filters),- }),- timezone- );-- // Placeholder lat/lng — geocoding is unresolved, same as V1.- return results.map((row) => ({- country: row.country,- region: row.region ?? undefined,- city: row.city ?? undefined,- lat: 0,- lng: 0,- count: row.count,- }));}+): Promise<+ Array<{+ country: string;+ region?: string;+ city?: string;+ lat: number;+ lng: number;+ count: number;+ }>+> {+ const results = await runQuery<{+ country: string;+ region: string | null;+ city: string | null;+ count: number;+ }>(+ deps,+ mapDataQuery({+ projectId,+ startDate,+ endDate,+ rawFilterWhere: getRawWhereClause('events', filters),+ }),+ timezone+ );++ // Placeholder lat/lng — geocoding is unresolved, same as V1.+ return results.map((row) => ({+ country: row.country,+ region: row.region ?? undefined,+ city: row.city ?? undefined,+ lat: 0,+ lng: 0,+ count: row.count,+ }));+}- /** The dashboard's live/30-minute-window widget — moved from packages/trpc's overview router (M7-005). */- async getLiveData(projectId: string): Promise<ILiveData> {- const [totalSessions, minuteCounts, minuteReferrers, referrers] =- await Promise.all([- runQuery<{ total_sessions: number }>(- this.client,- liveTotalSessionsQuery({ projectId }),- 'UTC'- ),- runQuery<{ minute: string; session_count: number; visitor_count: number }>(- this.client,- liveMinuteCountsQuery({ projectId }),- 'UTC'- ),- runQuery<{ minute: string; referrer_name: string; count: number }>(- this.client,- liveMinuteReferrersQuery({ projectId }),- 'UTC'- ),- runQuery<{ referrer: string; count: number }>(- this.client,- liveReferrersQuery({ projectId }),- 'UTC'- ),- ]);-- const referrersByMinute = new Map<- string,- Array<{ referrer: string; count: number }>- >();- for (const item of minuteReferrers) {- if (!referrersByMinute.has(item.minute)) {- referrersByMinute.set(item.minute, []);- }- referrersByMinute.get(item.minute)!.push({- referrer: item.referrer_name,- count: item.count,- });- }+/** The dashboard's live/30-minute-window widget — moved from packages/trpc's overview router (M7-005). */+export async function getLiveData(+ deps: ServiceDeps,+ projectId: string+): Promise<ILiveData> {+ const [totalSessions, minuteCounts, minuteReferrers, referrers] =+ await Promise.all([+ runQuery<{ total_sessions: number }>(+ deps,+ liveTotalSessionsQuery({ projectId }),+ 'UTC'+ ),+ runQuery<{+ minute: string;+ session_count: number;+ visitor_count: number;+ }>(deps, liveMinuteCountsQuery({ projectId }), 'UTC'),+ runQuery<{ minute: string; referrer_name: string; count: number }>(+ deps,+ liveMinuteReferrersQuery({ projectId }),+ 'UTC'+ ),+ runQuery<{ referrer: string; count: number }>(+ deps,+ liveReferrersQuery({ projectId }),+ 'UTC'+ ),+ ]);- return {- totalSessions: totalSessions[0]?.total_sessions || 0,- minuteCounts: minuteCounts.map((item) => ({- minute: item.minute,- sessionCount: item.session_count,- visitorCount: item.visitor_count,- timestamp: new Date(item.minute).getTime(),- time: new Date(item.minute).toLocaleTimeString([], {- hour: '2-digit',- minute: '2-digit',- }),- referrers: referrersByMinute.get(item.minute) || [],- })),- referrers: referrers.map((item) => ({- referrer: item.referrer,- count: item.count,- })),- };+ const referrersByMinute = new Map<+ string,+ Array<{ referrer: string; count: number }>+ >();+ for (const item of minuteReferrers) {+ if (!referrersByMinute.has(item.minute)) {+ referrersByMinute.set(item.minute, []);+ }+ referrersByMinute.get(item.minute)!.push({+ referrer: item.referrer_name,+ count: item.count,+ });}-}-export const overviewService = new OverviewService();+ return {+ totalSessions: totalSessions[0]?.total_sessions || 0,+ minuteCounts: minuteCounts.map((item) => ({+ minute: item.minute,+ sessionCount: item.session_count,+ visitorCount: item.visitor_count,+ timestamp: new Date(item.minute).getTime(),+ time: new Date(item.minute).toLocaleTimeString([], {+ hour: '2-digit',+ minute: '2-digit',+ }),+ referrers: referrersByMinute.get(item.minute) || [],+ })),+ referrers: referrers.map((item) => ({+ referrer: item.referrer,+ count: item.count,+ })),+ };+}export type TrafficColumn =| 'referrer'@@ -1186,15 +1241,18 @@ export type TrafficColumn =| 'browser'| 'os';-export async function getTrafficBreakdownCore(input: {- projectId: string;- startDate: string;- endDate: string;- column: TrafficColumn;- filters?: IChartEventFilter[];-}) {+export async function getTrafficBreakdownCore(+ deps: ServiceDeps,+ input: {+ projectId: string;+ startDate: string;+ endDate: string;+ column: TrafficColumn;+ filters?: IChartEventFilter[];+ }+) {const { timezone } = await getSettingsForProject(input.projectId);- return overviewService.getTopGeneric({+ return getTopGeneric(deps, {projectId: input.projectId,filters: input.filters ?? [],startDate: input.startDate,@@ -1233,19 +1291,22 @@ export interface SegmentDailyPoint {// growth) instead of only current-vs-baseline totals. Returns one point per day// with zero-filled gaps; empty when the column isn't session-derived or the// value never appears in the window.-export async function getSegmentDailySeriesCore(input: {- projectId: string;- column: string;- value: string;- startDate: string;- endDate: string;-}): Promise<SegmentDailyPoint[]> {+export async function getSegmentDailySeriesCore(+ deps: ServiceDeps,+ input: {+ projectId: string;+ column: string;+ value: string;+ startDate: string;+ endDate: string;+ }+): Promise<SegmentDailyPoint[]> {if (!SEGMENT_SERIES_COLUMNS.has(input.column)) {return [];}const { timezone } = await getSettingsForProject(input.projectId);- const { items } = await overviewService.getTopGenericSeries({+ const { items } = await getTopGenericSeries(deps, {projectId: input.projectId,filters: [],startDate: input.startDate,@@ -1278,11 +1339,14 @@ export interface GetAnalyticsOverviewInput {filters?: IChartEventFilter[];}-export async function getAnalyticsOverviewCore(input: GetAnalyticsOverviewInput) {+export async function getAnalyticsOverviewCore(+ deps: ServiceDeps,+ input: GetAnalyticsOverviewInput+) {const { timezone } = await getSettingsForProject(input.projectId);const interval = input.interval ?? 'day';- const result = await overviewService.getMetrics({+ const result = await getMetrics(deps, {projectId: input.projectId,filters: input.filters ?? [],startDate: input.startDate,@@ -1300,3 +1364,62 @@ export async function getAnalyticsOverviewCore(input: GetAnalyticsOverviewInput)};}+export interface OverviewService {+ isPageFilter(filters: IChartEventFilter[]): boolean;+ getRawWhereClause(+ type: 'events' | 'sessions',+ filters: IChartEventFilter[]+ ): string;+ getMetrics(input: IGetMetricsInput): ReturnType<typeof getMetrics>;+ getTopPages(input: IGetTopPagesInput): ReturnType<typeof getTopPages>;+ getTopEntryExit(+ input: IGetTopEntryExitInput+ ): ReturnType<typeof getTopEntryExit>;+ getTopGeneric(input: IGetTopGenericInput): ReturnType<typeof getTopGeneric>;+ getTopGenericSeries(+ input: IGetTopGenericSeriesInput+ ): ReturnType<typeof getTopGenericSeries>;+ getUserJourney(+ input: IGetUserJourneyInput+ ): ReturnType<typeof getUserJourney>;+ getTopEvents(+ input: Parameters<typeof getTopEvents>[1]+ ): ReturnType<typeof getTopEvents>;+ getTopLinkOut(+ input: Parameters<typeof getTopLinkOut>[1]+ ): ReturnType<typeof getTopLinkOut>;+ getMapData(+ input: Parameters<typeof getMapData>[1]+ ): ReturnType<typeof getMapData>;+ getLiveData(projectId: string): Promise<ILiveData>;+ getTrafficBreakdownCore(+ input: Parameters<typeof getTrafficBreakdownCore>[1]+ ): ReturnType<typeof getTrafficBreakdownCore>;+ getSegmentDailySeriesCore(+ input: Parameters<typeof getSegmentDailySeriesCore>[1]+ ): Promise<SegmentDailyPoint[]>;+ getAnalyticsOverviewCore(+ input: GetAnalyticsOverviewInput+ ): ReturnType<typeof getAnalyticsOverviewCore>;+}++export function createOverviewService(deps: ServiceDeps): OverviewService {+ return {+ isPageFilter,+ getRawWhereClause,+ getMetrics: (input) => getMetrics(deps, input),+ getTopPages: (input) => getTopPages(deps, input),+ getTopEntryExit: (input) => getTopEntryExit(deps, input),+ getTopGeneric: (input) => getTopGeneric(deps, input),+ getTopGenericSeries: (input) => getTopGenericSeries(deps, input),+ getUserJourney: (input) => getUserJourney(deps, input),+ getTopEvents: (input) => getTopEvents(deps, input),+ getTopLinkOut: (input) => getTopLinkOut(deps, input),+ getMapData: (input) => getMapData(deps, input),+ getLiveData: (projectId) => getLiveData(deps, projectId),+ getTrafficBreakdownCore: (input) => getTrafficBreakdownCore(deps, input),+ getSegmentDailySeriesCore: (input) =>+ getSegmentDailySeriesCore(deps, input),+ getAnalyticsOverviewCore: (input) => getAnalyticsOverviewCore(deps, input),+ };+}diff --git a/packages/core/src/modules/overview/pages.service.ts b/packages/core/src/modules/overview/pages.service.tsindex 5b1a391f9..d0ead3d65 100644--- a/packages/core/src/modules/overview/pages.service.ts+++ b/packages/core/src/modules/overview/pages.service.ts@@ -1,14 +1,24 @@// Ported from packages/db/src/services/pages.service.ts. The ClickHouse-// queries moved to src/pages.sql.ts (ADR-013, M7-005); `PagesService` keeps-// V1's `constructor(client: typeof ch)` shape — the mcp-// `get_page_performance` tool constructs a fresh instance per call to dodge a-// module-singleton mocking hazard (see page-performance.ts), so the-// constructor's client really is caller-supplied.+// queries moved to src/pages.sql.ts (ADR-013, M7-005).+//+// M10-005: `PagesService` is no longer a class and there is no `pagesService`+// module singleton. It is registered as its OWN service (`services.pages`)+// rather than folded into `overview`, because both expose a `getTopPages` and+// they are different queries with different inputs — one over `events` scoped+// by search, one over `sessions` scoped by filters. Folding them would have+// had to rename one, which is a call-site contract this wave does not change.+// The caller-supplied `constructor(client)` slot is gone: the mcp+// `get_page_performance` tool constructed a fresh instance per call only to+// dodge a module-singleton mocking hazard, and there is no singleton left to+// dodge.-import type { ClickHouseClient } from '@clickhouse/client';import type { IChartEventFilter, IInterval } from '@openpanel/validation';+import type { ServiceDeps } from '../../services';import { getSettingsForProject } from '../organization/organization.service';-import { OverviewService } from './overview.service';+import {+ getTopPages as getOverviewTopPages,+ getTopEntryExit,+} from './overview.service';import {pageConversionsQuery,pageTimeseriesQuery,@@ -43,26 +53,20 @@ export interface ITopPage {bounce_rate: number;}-export class PagesService {- /** No client means the singleton path — `run-query.ts` resolves `ch` lazily. */- constructor(private client?: ClickHouseClient) {}-- async getTopPages({- projectId,- startDate,- endDate,- timezone,- search,- limit,- }: IGetPagesInput): Promise<ITopPage[]> {- return runQuery<ITopPage>(- this.client,- topPagesQuery({ projectId, startDate, endDate, search, limit }),- timezone- );- }+export function getTopPages(+ deps: ServiceDeps,+ { projectId, startDate, endDate, timezone, search, limit }: IGetPagesInput+): Promise<ITopPage[]> {+ return runQuery<ITopPage>(+ deps,+ topPagesQuery({ projectId, startDate, endDate, search, limit }),+ timezone+ );+}- async getPageTimeseries({+export function getPageTimeseries(+ deps: ServiceDeps,+ {projectId,startDate,endDate,@@ -74,34 +78,34 @@ export class PagesService {interval: IInterval;filterOrigin?: string;filterPath?: string;- }): Promise<IPageTimeseriesRow[]> {- return runQuery<IPageTimeseriesRow>(- this.client,- pageTimeseriesQuery({- projectId,- startDate,- endDate,- interval,- filterOrigin,- filterPath,- }),- timezone- );}+): Promise<IPageTimeseriesRow[]> {+ return runQuery<IPageTimeseriesRow>(+ deps,+ pageTimeseriesQuery({+ projectId,+ startDate,+ endDate,+ interval,+ filterOrigin,+ filterPath,+ }),+ timezone+ );}-export const pagesService = new PagesService();-const overviewServiceForPages = new OverviewService();--export async function getTopPagesCore(input: {- projectId: string;- startDate: string;- endDate: string;- limit?: number;- filters?: IChartEventFilter[];-}) {+export async function getTopPagesCore(+ deps: ServiceDeps,+ input: {+ projectId: string;+ startDate: string;+ endDate: string;+ limit?: number;+ filters?: IChartEventFilter[];+ }+) {const { timezone } = await getSettingsForProject(input.projectId);- return overviewServiceForPages.getTopPages({+ return getOverviewTopPages(deps, {projectId: input.projectId,filters: input.filters ?? [],startDate: input.startDate,@@ -111,16 +115,19 @@ export async function getTopPagesCore(input: {});}-export async function getEntryExitPagesCore(input: {- projectId: string;- startDate: string;- endDate: string;- mode: 'entry' | 'exit';- limit?: number;- filters?: IChartEventFilter[];-}) {+export async function getEntryExitPagesCore(+ deps: ServiceDeps,+ input: {+ projectId: string;+ startDate: string;+ endDate: string;+ mode: 'entry' | 'exit';+ limit?: number;+ filters?: IChartEventFilter[];+ }+) {const { timezone } = await getSettingsForProject(input.projectId);- return overviewServiceForPages.getTopEntryExit({+ return getTopEntryExit(deps, {projectId: input.projectId,filters: input.filters ?? [],startDate: input.startDate,@@ -131,17 +138,20 @@ export async function getEntryExitPagesCore(input: {});}-export async function getPagePerformanceCore(input: {- projectId: string;- startDate: string;- endDate: string;- search?: string;- sortBy?: 'sessions' | 'pageviews' | 'bounce_rate' | 'avg_duration';- sortOrder?: 'asc' | 'desc';- limit?: number;-}) {+export async function getPagePerformanceCore(+ deps: ServiceDeps,+ input: {+ projectId: string;+ startDate: string;+ endDate: string;+ search?: string;+ sortBy?: 'sessions' | 'pageviews' | 'bounce_rate' | 'avg_duration';+ sortOrder?: 'asc' | 'desc';+ limit?: number;+ }+) {const { timezone } = await getSettingsForProject(input.projectId);- const results = await pagesService.getTopPages({+ const results = await getTopPages(deps, {projectId: input.projectId,startDate: input.startDate,endDate: input.endDate,@@ -184,16 +194,19 @@ export interface IPageConversionRow {const DEFAULT_CONVERSION_WINDOW_HOURS = 24;const DEFAULT_CONVERSION_LIMIT = 100;-export async function getPageConversionsCore(input: {- projectId: string;- startDate: string;- endDate: string;- conversionEvent: string;- windowHours?: number;- limit?: number;-}): Promise<IPageConversionRow[]> {+export async function getPageConversionsCore(+ deps: ServiceDeps,+ input: {+ projectId: string;+ startDate: string;+ endDate: string;+ conversionEvent: string;+ windowHours?: number;+ limit?: number;+ }+): Promise<IPageConversionRow[]> {return runQuery<IPageConversionRow>(- undefined,+ deps,pageConversionsQuery({projectId: input.projectId,startDate: input.startDate,@@ -205,3 +218,37 @@ export async function getPageConversionsCore(input: {'UTC');}++export interface PagesService {+ getTopPages(input: IGetPagesInput): Promise<ITopPage[]>;+ getPageTimeseries(+ input: IGetPagesInput & {+ interval: IInterval;+ filterOrigin?: string;+ filterPath?: string;+ }+ ): Promise<IPageTimeseriesRow[]>;+ getTopPagesCore(+ input: Parameters<typeof getTopPagesCore>[1]+ ): ReturnType<typeof getTopPagesCore>;+ getEntryExitPagesCore(+ input: Parameters<typeof getEntryExitPagesCore>[1]+ ): ReturnType<typeof getEntryExitPagesCore>;+ getPagePerformanceCore(+ input: Parameters<typeof getPagePerformanceCore>[1]+ ): ReturnType<typeof getPagePerformanceCore>;+ getPageConversionsCore(+ input: Parameters<typeof getPageConversionsCore>[1]+ ): Promise<IPageConversionRow[]>;+}++export function createPagesService(deps: ServiceDeps): PagesService {+ return {+ getTopPages: (input) => getTopPages(deps, input),+ getPageTimeseries: (input) => getPageTimeseries(deps, input),+ getTopPagesCore: (input) => getTopPagesCore(deps, input),+ getEntryExitPagesCore: (input) => getEntryExitPagesCore(deps, input),+ getPagePerformanceCore: (input) => getPagePerformanceCore(deps, input),+ getPageConversionsCore: (input) => getPageConversionsCore(deps, input),+ };+}diff --git a/packages/core/src/modules/overview/src/overview-raw-where.sql.test.ts b/packages/core/src/modules/overview/src/overview-raw-where.sql.test.tsindex 55fc92492..8d68c9ebf 100644--- a/packages/core/src/modules/overview/src/overview-raw-where.sql.test.ts+++ b/packages/core/src/modules/overview/src/overview-raw-where.sql.test.ts@@ -1,11 +1,13 @@/**- * SQL-syntax tests for `OverviewService.getRawWhereClause` (the UTM+ * SQL-syntax tests for the overview module's `getRawWhereClause` (the UTM* remapping). Moved from packages/db/src/services/overview-sql.test.ts* (M8-005) alongside its subject, which moved to core in M7-005 — same* pattern as the chart cases moving to chart.sql.test.ts with theirs* (M7-003). `getRawWhereClause` itself is still clix/sqlstring-based- * (pre-ADR-013); the rest of OverviewService's queries already run through- * overview.sql.ts, covered by overview.sql.test.ts.+ * (pre-ADR-013); the rest of the overview module's queries already run+ * through overview.sql.ts, covered by overview.sql.test.ts. M10-005 turned it+ * from a class method into a module function — it never needed a client,+ * being pure string building.** Strategy: build the SQL string, then run `EXPLAIN <sql>` against the local* ClickHouse instance. EXPLAIN parses the query, resolves columns, and builds@@ -20,7 +22,7 @@import { afterAll, beforeAll, describe, expect, it, spyOn } from 'bun:test';import { ch } from '@openpanel/db/src/clickhouse/client';-import { OverviewService } from '../overview.service';+import { getRawWhereClause } from '../overview.service';const PROJECT_ID = 'test-sql-validation';@@ -61,10 +63,8 @@ const itCH = (name: string, fn: () => Promise<void>) =>});describe('overview.service / getRawWhereClause (UTM remapping)', () => {- const svc = new OverviewService(ch);-it('rewrites utm_* to properties[__query.utm_*] for the events table', () => {- const where = svc.getRawWhereClause('events', [+ const where = getRawWhereClause('events', [{ name: 'utm_source', operator: 'is', value: ['awn'] },]);expect(where).toContain("properties['__query.utm_source']");@@ -72,7 +72,7 @@ describe('overview.service / getRawWhereClause (UTM remapping)', () => {});it('keeps utm_* as a top-level column for the sessions table', () => {- const where = svc.getRawWhereClause('sessions', [+ const where = getRawWhereClause('sessions', [{ name: 'utm_source', operator: 'is', value: ['awn'] },]);expect(where).toMatch(/(?<![._\w])utm_source\s*=/);@@ -80,7 +80,7 @@ describe('overview.service / getRawWhereClause (UTM remapping)', () => {});it('drops non-whitelisted filters', () => {- const where = svc.getRawWhereClause('events', [+ const where = getRawWhereClause('events', [{ name: 'malicious_column', operator: 'is', value: ['x'] },]);expect(where).toBe('');@@ -89,7 +89,7 @@ describe('overview.service / getRawWhereClause (UTM remapping)', () => {itCH('events utm_source filter parses against real events table',async () => {- const where = svc.getRawWhereClause('events', [+ const where = getRawWhereClause('events', [{ name: 'utm_source', operator: 'is', value: ['awn'] },]);expect(where).toBeTruthy();@@ -102,7 +102,7 @@ describe('overview.service / getRawWhereClause (UTM remapping)', () => {itCH('sessions utm_source filter parses against real sessions table',async () => {- const where = svc.getRawWhereClause('sessions', [+ const where = getRawWhereClause('sessions', [{ name: 'utm_source', operator: 'is', value: ['awn'] },]);expect(where).toBeTruthy();diff --git a/packages/core/src/modules/overview/src/run-query.ts b/packages/core/src/modules/overview/src/run-query.tsindex 021d495fa..4408d159c 100644--- a/packages/core/src/modules/overview/src/run-query.ts+++ b/packages/core/src/modules/overview/src/run-query.ts@@ -1,52 +1,24 @@-// The overview module's one bridge to a ClickHouse client.+// The overview module's one ClickHouse entry point.//-// `OverviewService`/`PagesService` keep V1's `constructor(client: typeof ch)`-// shape — `page-performance.ts` (mcp) constructs a fresh `PagesService` per-// call specifically to dodge a module-singleton mocking hazard, so a caller-// may still inject its own client. The module-scope `overviewService`/-// `pagesService` singletons construct with none, so the round-robin `ch`-// singleton is imported lazily here instead — importing-// `@openpanel/db/src/clickhouse/client` eagerly constructs a real ClickHouse-// client and a pino logger, and this module reaches nearly every core test-// file through the barrel (same reasoning as chart's `run-query.ts`).+// M10-005: the client is `deps.ch` — the same round-robin/retry proxy+// `main.ts` builds and hands to every scope — instead of a lazy+// `import('@openpanel/db/src/clickhouse/client')`. `OverviewService` /+// `PagesService` were classes carrying a caller-supplied client precisely+// because there was no scope to reach one through; there is now, so both are+// factories over `ServiceDeps` and this file just forwards.//-// `chQuery` always goes through that singleton's `withRetry`, so it can't-// stand in for a caller-supplied client; this mirrors its exact behaviour-// (params, `session_timezone`, the same Int-meta coercion) either way.+// `timezone` is mandatory here (every overview query sends one, several as a+// literal 'UTC') where the shared helper leaves it optional, which is why this+// wrapper is worth keeping over calling `chQuery` directly.-import type { ClickHouseClient } from '@clickhouse/client';-import { type SqlFragment, toStatement } from '@openpanel/db/src/clickhouse/sql';+import type { SqlFragment } from '@openpanel/db/src/clickhouse/sql';+import type { ServiceDeps } from '../../../services';+import { chQuery } from '../../../shared/ch-query';-function loadChClient() {- return import('@openpanel/db/src/clickhouse/client');-}--export async function runQuery<T extends object>(- client: ClickHouseClient | undefined,+export function runQuery<T extends object>(+ deps: ServiceDeps,statement: SqlFragment,timezone: string): Promise<T[]> {- const resolvedClient = client ?? (await loadChClient()).ch;- const { query, query_params } = toStatement(statement);- const result = await resolvedClient.query({- query,- query_params: Object.keys(query_params).length ? query_params : undefined,- clickhouse_settings: { session_timezone: timezone },- });- const json = await result.json<T>();- const keys = Object.keys(json.data[0] ?? {});- // Same coercion `chQuery` applies: ClickHouse's JSON format returns every- // Int*/UInt* column as a string, so the caller's shape (`count: number`)- // only holds if we parse it back out here, same as V1's clix did.- return json.data.map((item) =>- keys.reduce((acc, key) => {- const meta = json.meta?.find((m) => m.name === key);- const value = item[key as keyof T];- acc[key as keyof T] =- value && meta?.type.includes('Int')- ? (Number.parseFloat(value as string) as T[keyof T])- : value;- return acc;- }, {} as T)- );+ return chQuery<T>(deps, statement, { session_timezone: timezone });}diff --git a/packages/core/src/modules/profile/profile.routes.test.ts b/packages/core/src/modules/profile/profile.routes.test.tsindex 33516282b..3f6d4f6fc 100644--- a/packages/core/src/modules/profile/profile.routes.test.ts+++ b/packages/core/src/modules/profile/profile.routes.test.ts@@ -37,8 +37,10 @@ const authenticateClient = mock((_deps: unknown, _headers: Headers, _options: ClientAuthOptions) =>Promise.resolve(clientResult));+// M10-005: both take `ServiceDeps` first; the route hands them `ctx`.const identifyProfile = mock((+ _deps: unknown,_projectId: string,_payload: IdentifyProfileInput,_request: ProfileRequestContext@@ -46,6 +48,7 @@ const identifyProfile = mock();const adjustProfileProperty = mock((+ _deps: unknown,_projectId: string,_input: { profileId: string; property: string; delta: number }) => Promise.resolve(adjustResult)@@ -103,7 +106,7 @@ test('POST /profile identifies with geo + user agent and answers 202 + profileIdexpect(response.status).toBe(202);expect(await response.text()).toBe('prof-1');expect(identifyProfile).toHaveBeenCalledTimes(1);- const [projectId, received, context] = identifyProfile.mock.calls[0]!;+ const [, projectId, received, context] = identifyProfile.mock.calls[0]!;expect(projectId).toBe('proj-1');expect(received).toEqual(payload);expect(context.geo).toEqual(GEO);@@ -120,7 +123,7 @@ test('POST /profile/increment passes value through; /decrement negates it', asynexpect(incremented.status).toBe(202);expect(await incremented.text()).toBe('prof-1');expect(decremented.status).toBe(202);- expect(adjustProfileProperty.mock.calls).toEqual([+ expect(adjustProfileProperty.mock.calls.map(([, ...rest]) => rest)).toEqual([['proj-1', { profileId: 'prof-1', property: 'visits', delta: 3 }],['proj-1', { profileId: 'prof-1', property: 'visits', delta: -3 }],]);diff --git a/packages/core/src/modules/profile/profile.routes.ts b/packages/core/src/modules/profile/profile.routes.tsindex fd9c0a0c9..f25145a7f 100644--- a/packages/core/src/modules/profile/profile.routes.ts+++ b/packages/core/src/modules/profile/profile.routes.ts@@ -67,7 +67,10 @@ export const profileRoutes = defineRoutes((app) =>payload.properties);const geo = await getGeoLocation(ctx.ip);- await identifyProfile(client.projectId, payload, { geo, userAgent });+ await identifyProfile(ctx, client.projectId, payload, {+ geo,+ userAgent,+ });set.status = 202;return payload.profileId;},@@ -81,12 +84,12 @@ export const profileRoutes = defineRoutes((app) =>).post('/profile/increment',- async ({ body, client, status, set }) => {+ async ({ body, client, ctx, status, set }) => {if (!client.projectId) {return status(400, 'No projectId');}const { profileId, property, value } = body as AdjustPropertyBody;- const result = await adjustProfileProperty(client.projectId, {+ const result = await adjustProfileProperty(ctx, client.projectId, {profileId,property,delta: value,@@ -103,12 +106,12 @@ export const profileRoutes = defineRoutes((app) =>).post('/profile/decrement',- async ({ body, client, status, set }) => {+ async ({ body, client, ctx, status, set }) => {if (!client.projectId) {return status(400, 'No projectId');}const { profileId, property, value } = body as AdjustPropertyBody;- const result = await adjustProfileProperty(client.projectId, {+ const result = await adjustProfileProperty(ctx, client.projectId, {profileId,property,delta: -value,diff --git a/packages/core/src/modules/profile/profile.rpc.ts b/packages/core/src/modules/profile/profile.rpc.tsindex 02a31561a..941fe632a 100644--- a/packages/core/src/modules/profile/profile.rpc.ts+++ b/packages/core/src/modules/profile/profile.rpc.ts@@ -47,31 +47,31 @@ export const profileRouter = createTRPCRouter({byId: procedure.input(zProfileRef).query(async ({ input, ctx }) => {await requireReadAccess(ctx, input.projectId);- return getProfileById(input.profileId, input.projectId);+ return getProfileById(ctx, input.profileId, input.projectId);}),metrics: procedure.input(zProfileRef).query(async ({ input, ctx }) => {await requireReadAccess(ctx, input.projectId);- return getProfileMetrics(input.profileId, input.projectId);+ return getProfileMetrics(ctx, input.profileId, input.projectId);}),activity: procedure.input(zProfileRef).query(async ({ input, ctx }) => {await requireReadAccess(ctx, input.projectId);- return getProfileActivity(input.profileId, input.projectId);+ return getProfileActivity(ctx, input.profileId, input.projectId);}),mostEvents: procedure.input(zProfileRef).query(async ({ input, ctx }) => {await requireReadAccess(ctx, input.projectId);- return getProfileMostEvents(input.profileId, input.projectId);+ return getProfileMostEvents(ctx, input.profileId, input.projectId);}),popularRoutes: procedure.input(zProfileRef).query(async ({ input, ctx }) => {await requireReadAccess(ctx, input.projectId);- return getProfilePopularRoutes(input.profileId, input.projectId);+ return getProfilePopularRoutes(ctx, input.profileId, input.projectId);}),properties: procedure@@ -79,7 +79,7 @@ export const profileRouter = createTRPCRouter({.query(async ({ input, ctx }) => {await requireReadAccess(ctx, input.projectId);- return getProfilePropertyNames(input.projectId);+ return getProfilePropertyNames(ctx, input.projectId);}),list: procedure@@ -96,7 +96,7 @@ export const profileRouter = createTRPCRouter({.query(async ({ input, ctx }) => {await requireReadAccess(ctx, input.projectId);- return getProfileListPage(input);+ return getProfileListPage(ctx, input);}),powerUsers: procedure@@ -110,7 +110,7 @@ export const profileRouter = createTRPCRouter({.query(async ({ input, ctx }) => {await requireReadAccess(ctx, input.projectId);- return getPowerUsers(input);+ return getPowerUsers(ctx, input);}),values: procedure@@ -118,6 +118,6 @@ export const profileRouter = createTRPCRouter({.query(async ({ input, ctx }) => {await requireReadAccess(ctx, input.projectId);- return getProfileValues(input);+ return getProfileValues(ctx, input);}),});diff --git a/packages/core/src/modules/profile/profile.service.ts b/packages/core/src/modules/profile/profile.service.tsindex c3ffdcab2..af641b9f5 100644--- a/packages/core/src/modules/profile/profile.service.ts+++ b/packages/core/src/modules/profile/profile.service.ts@@ -10,19 +10,25 @@// converted here: it is the shared filter compiler, out of this task's scope;// src/filter-clauses.ts is the one bridge.//-// db/ch/buffer access is LAZY (`load*` below) for the reason-// insight.service.ts's header gives: constructing @openpanel/db's clients at-// import time spawns a pino-pretty worker per `bun test --isolate` file.+// M10-005: every function that touches ClickHouse or the profile buffer takes+// `ServiceDeps` and reaches them as `deps.ch` (through shared/ch-query.ts) and+// `deps.buffers.profile`. The `loadChClient` / `loadProfileBuffer` lazy+// loaders are gone (docs/TECH_DEBT.md §2, §4); `loadFilterCompiler` stays and+// reaches a core sibling, not @openpanel/db.import { strip, toObject } from '@openpanel/common';-import { cacheable } from '@openpanel/redis';import type { IChartEventFilter } from '@openpanel/validation';import { assocPath, flatten, map, pathOr, pipe, prop, sort, uniq } from 'ramda';-import { loadDbBuffers } from '../../buffers/lazy-db-buffers';import type { ServiceDeps } from '../../services';+import { cacheablePerDeps } from '../../shared/cacheable-per-deps';+import { chQuery } from '../../shared/ch-query';import type { IClickhouseEvent } from '../event/event.service';import type { IClickhouseSession } from '../session/session.service';-import { convertClickhouseDateToJs, formatClickhouseDate } from './src/dates';+import {+ convertClickhouseDateToJs,+ formatClickhouseDate,+ toNullIfDefaultMinDate,+} from './src/dates';import type { CompiledFilterClauses } from './src/filter-clauses';import {findProfilesQuery,@@ -47,6 +53,11 @@ export { profileSearchCondition, profileSearchSql } from './src/profile.sql';const PROFILES_CACHE_SECONDS = 60 * 5;const PROPERTY_KEYS_CACHE_SECONDS = 60;+// V1's `cacheable(fn, ...)` derived these from the functions' own names;+// naming them keeps the Redis keys `cachable:getProfiles:<args>` and+// `cachable:getProfilePropertyKeys:<projectId>`.+const PROFILES_CACHE_NAME = 'getProfiles';+const PROPERTY_KEYS_CACHE_NAME = 'getProfilePropertyKeys';const RECENT_EVENTS_DEFAULT_LIMIT = 10;const SESSIONS_DEFAULT_LIMIT = 20;const FIND_PROFILES_DEFAULT_LIMIT = 20;@@ -65,14 +76,6 @@ const PROFILE_FILTER_TARGET = {const ARRAY_INDEX_PATH_SEGMENT = /\.([0-9]+)\./g;const ARRAY_INDEX_PATH_TAIL = /\.([0-9]+)/g;-function loadChClient() {- return import('@openpanel/db/src/clickhouse/client');-}--function loadProfileBuffer() {- return loadDbBuffers().then((m) => m.profileBuffer);-}-function loadFilterCompiler() {return import('../chart/src/table-filter-where');}@@ -188,16 +191,16 @@ async function compileProfileFilters(}export async function getProfileMetrics(+ deps: ServiceDeps,profileId: string,projectId: string): Promise<IProfileMetrics> {- const { chQuery, toNullIfDefaultMinDate } = await loadChClient();const [data] = await chQuery<Omit<IProfileMetrics, 'lastSeen' | 'firstSeen'> & {lastSeen: string;firstSeen: string;}- >(profileMetricsQuery({ profileId, projectId }));+ >(deps, profileMetricsQuery({ profileId, projectId }));const metrics = data!;return {...metrics,@@ -207,6 +210,7 @@ export async function getProfileMetrics(}export async function getProfileById(+ deps: ServiceDeps,id: string,projectId: string): Promise<IServiceProfile | null> {@@ -214,14 +218,16 @@ export async function getProfileById(return null;}- const profileBuffer = await loadProfileBuffer();- const cachedProfile = await profileBuffer.fetchFromCache(id, projectId);+ const cachedProfile = await deps.buffers.profile.fetchFromCache(+ id,+ projectId+ );if (cachedProfile) {return transformProfile(cachedProfile);}- const { chQuery } = await loadChClient();const [profile] = await chQuery<IClickhouseProfile>(+ deps,profileByIdQuery({ id: String(id), projectId }));@@ -229,6 +235,7 @@ export async function getProfileById(}export async function getProfiles(+ deps: ServiceDeps,ids: string[],projectId: string): Promise<IServiceProfile[]> {@@ -237,14 +244,18 @@ export async function getProfiles(return [];}- const { chQuery } = await loadChClient();const data = await chQuery<IClickhouseProfile>(+ deps,profilesByIdsQuery({ projectId, ids: filteredIds }));return data.map(transformProfile);}-export const getProfilesCached = cacheable(getProfiles, PROFILES_CACHE_SECONDS);+export const getProfilesCached = cacheablePerDeps(+ PROFILES_CACHE_NAME,+ getProfiles,+ PROFILES_CACHE_SECONDS+);export interface GetProfileListOptions {projectId: string;@@ -255,16 +266,19 @@ export interface GetProfileListOptions {isExternal?: boolean;}-export async function getProfileList({- take,- cursor,- projectId,- filters,- search,- isExternal,-}: GetProfileListOptions): Promise<IServiceProfile[]> {- const { chQuery } = await loadChClient();+export async function getProfileList(+ deps: ServiceDeps,+ {+ take,+ cursor,+ projectId,+ filters,+ search,+ isExternal,+ }: GetProfileListOptions+): Promise<IServiceProfile[]> {const data = await chQuery<IClickhouseProfile>(+ deps,profileListQuery({projectId,take,@@ -277,14 +291,17 @@ export async function getProfileList({return data.map(transformProfile);}-export async function getProfileListCount({- projectId,- filters,- isExternal,- search,-}: Omit<GetProfileListOptions, 'cursor' | 'take'>): Promise<number> {- const { chQuery } = await loadChClient();+export async function getProfileListCount(+ deps: ServiceDeps,+ {+ projectId,+ filters,+ isExternal,+ search,+ }: Omit<GetProfileListOptions, 'cursor' | 'take'>+): Promise<number> {const data = await chQuery<{ count: number }>(+ deps,profileListCountQuery({projectId,search,@@ -296,6 +313,7 @@ export async function getProfileListCount({}export async function upsertProfile(+ deps: ServiceDeps,{id,firstName,@@ -327,8 +345,7 @@ export async function upsertProfile(groups: groups ?? [],};- const profileBuffer = await loadProfileBuffer();- await profileBuffer.add(profile, isFromEvent);+ await deps.buffers.profile.add(profile, isFromEvent);}export interface FindProfilesInput {@@ -349,10 +366,11 @@ export interface FindProfilesInput {}export async function findProfilesCore(+ deps: ServiceDeps,input: FindProfilesInput): Promise<IClickhouseProfile[]> {- const { chQuery } = await loadChClient();return chQuery<IClickhouseProfile>(+ deps,findProfilesQuery({...input,inactiveDays:@@ -377,6 +395,7 @@ export async function findProfilesCore(}export async function getProfileWithEvents(+ deps: ServiceDeps,projectId: string,profileId: string,eventLimit = RECENT_EVENTS_DEFAULT_LIMIT@@ -384,10 +403,13 @@ export async function getProfileWithEvents(profile: IClickhouseProfile | null;recent_events: IClickhouseEvent[];}> {- const { chQuery } = await loadChClient();const [profiles, recent_events] = await Promise.all([- chQuery<IClickhouseProfile>(profileRowQuery({ projectId, profileId })),+ chQuery<IClickhouseProfile>(+ deps,+ profileRowQuery({ projectId, profileId })+ ),chQuery<IClickhouseEvent>(+ deps,profileRecentEventsQuery({ projectId, profileId, limit: eventLimit }),CLIX_SESSION_TIMEZONE),@@ -397,22 +419,26 @@ export async function getProfileWithEvents(}export async function getProfileSessionsCore(+ deps: ServiceDeps,projectId: string,profileId: string,limit = SESSIONS_DEFAULT_LIMIT): Promise<IClickhouseSession[]> {- const { chQuery } = await loadChClient();return chQuery<IClickhouseSession>(+ deps,profileSessionsQuery({ projectId, profileId, limit }),CLIX_SESSION_TIMEZONE);}-export async function getProfileMetricsCore(input: {- projectId: string;- profileId: string;-}) {- const raw = await getProfileMetrics(input.profileId, input.projectId);+export async function getProfileMetricsCore(+ deps: ServiceDeps,+ input: {+ projectId: string;+ profileId: string;+ }+) {+ const raw = await getProfileMetrics(deps, input.profileId, input.projectId);if (!raw) {throw new Error(`Profile not found or has no events: ${input.profileId}`);}@@ -436,10 +462,11 @@ export async function getProfileMetricsCore(input: {/** Every distinct key in any external profile's `properties` map. */export async function getProfilePropertyKeys(+ deps: ServiceDeps,projectId: string): Promise<string[]> {- const { chQuery } = await loadChClient();const rows = await chQuery<{ key: string }>(+ deps,profilePropertyKeysQuery(projectId),CLIX_SESSION_TIMEZONE);@@ -449,46 +476,54 @@ export async function getProfilePropertyKeys(// Cached by projectId only: the picker's tRPC-level cache keys on the whole// input, which includes `event`, so without this the full profile scan would// repeat once per event within the same window.-export const getProfilePropertyKeysCached = cacheable(+export const getProfilePropertyKeysCached = cacheablePerDeps(+ PROPERTY_KEYS_CACHE_NAME,getProfilePropertyKeys,PROPERTY_KEYS_CACHE_SECONDS);// ---- the trpc profile router's bodies-export async function getProfileActivity(profileId: string, projectId: string) {- const { chQuery } = await loadChClient();+export async function getProfileActivity(+ deps: ServiceDeps,+ profileId: string,+ projectId: string+) {return chQuery<{ count: number; date: string }>(+ deps,profileActivityQuery({ projectId, profileId }));}export async function getProfileMostEvents(+ deps: ServiceDeps,profileId: string,projectId: string) {- const { chQuery } = await loadChClient();return chQuery<{ count: number; name: string }>(+ deps,profileMostEventsQuery({ projectId, profileId }));}export async function getProfilePopularRoutes(+ deps: ServiceDeps,profileId: string,projectId: string) {- const { chQuery } = await loadChClient();return chQuery<{ count: number; path: string }>(+ deps,profilePopularRoutesQuery({ projectId, profileId }));}/** Property paths for the profile filter picker, array indexes wildcarded. */export async function getProfilePropertyNames(+ deps: ServiceDeps,projectId: string): Promise<string[]> {- const { chQuery } = await loadChClient();const rows = await chQuery<{ keys: string[] }>(+ deps,profilePropertyNamesQuery(projectId));@@ -506,10 +541,13 @@ export async function getProfilePropertyNames()(properties);}-export async function getProfileListPage(input: GetProfileListOptions) {+export async function getProfileListPage(+ deps: ServiceDeps,+ input: GetProfileListOptions+) {const [data, count] = await Promise.all([- getProfileList(input),- getProfileListCount(input),+ getProfileList(deps, input),+ getProfileListCount(deps, input),]);return {data,@@ -517,13 +555,16 @@ export async function getProfileListPage(input: GetProfileListOptions) {};}-export async function getPowerUsers(input: {- projectId: string;- cursor?: number;- take: number;-}) {- const { chQuery } = await loadChClient();+export async function getPowerUsers(+ deps: ServiceDeps,+ input: {+ projectId: string;+ cursor?: number;+ take: number;+ }+) {const rows = await chQuery<{ profile_id: string; count: number }>(+ deps,powerUsersQuery({projectId: input.projectId,take: input.take,@@ -531,6 +572,7 @@ export async function getPowerUsers(input: {}));const profiles = await getProfiles(+ deps,rows.map((r) => r.profile_id),input.projectId);@@ -550,12 +592,17 @@ export async function getPowerUsers(input: {};}-export async function getProfileValues(input: {- projectId: string;- property: string;-}): Promise<{ values: string[] }> {- const { chQuery } = await loadChClient();- const rows = await chQuery<{ values: string[] }>(profileValuesQuery(input));+export async function getProfileValues(+ deps: ServiceDeps,+ input: {+ projectId: string;+ property: string;+ }+): Promise<{ values: string[] }> {+ const rows = await chQuery<{ values: string[] }>(+ deps,+ profileValuesQuery(input)+ );const values = pipe((data: typeof rows) => map(prop('values'), data),@@ -601,11 +648,12 @@ export interface ProfileRequestContext {/** `POST /profile`: geo + UA fields override whatever the payload carried. */export async function identifyProfile(+ deps: ServiceDeps,projectId: string,payload: IdentifyProfileInput,request: ProfileRequestContext): Promise<void> {- await upsertProfile({+ await upsertProfile(deps, {...payload,id: payload.profileId,isExternal: true,@@ -635,10 +683,11 @@ export type AdjustProfilePropertyResult =/** `POST /profile/increment|decrement`: `delta` is signed by the caller. */export async function adjustProfileProperty(+ deps: ServiceDeps,projectId: string,input: { profileId: string; property: string; delta: number }): Promise<AdjustProfilePropertyResult> {- const profile = await getProfileById(input.profileId, projectId);+ const profile = await getProfileById(deps, input.profileId, projectId);if (!profile) {return { status: 'not-found' };}@@ -652,7 +701,7 @@ export async function adjustProfileProperty(return { status: 'not-a-number' };}- await upsertProfile({+ await upsertProfile(deps, {id: profile.id,projectId,properties: assocPath(path, current + input.delta, profile.properties),@@ -667,9 +716,9 @@ export interface ProfileService {upsert(input: IServiceUpsertProfile, isFromEvent?: boolean): Promise<void>;}-export function createProfileService(_deps: ServiceDeps): ProfileService {+export function createProfileService(deps: ServiceDeps): ProfileService {return {- byId: getProfileById,- upsert: upsertProfile,+ byId: (id, projectId) => getProfileById(deps, id, projectId),+ upsert: (input, isFromEvent) => upsertProfile(deps, input, isFromEvent),};}diff --git a/packages/core/src/modules/profile/src/dates.ts b/packages/core/src/modules/profile/src/dates.tsindex bcfcf8cb5..a53929669 100644--- a/packages/core/src/modules/profile/src/dates.ts+++ b/packages/core/src/modules/profile/src/dates.ts@@ -16,3 +16,17 @@ export function formatClickhouseDate(date: Date | string): string {export function convertClickhouseDateToJs(date: string): Date {return new Date(`${date.replace(' ', 'T')}Z`);}++const ROLLUP_DATE_PREFIX = '1970-01-01';+const ROLLUP_DATE_PREFIX_ALT = '1969-12-31';++/** ClickHouse's zero date for a metric no row ever populated. */+export function toNullIfDefaultMinDate(date?: string | null): Date | null {+ if (!date) {+ return null;+ }+ return date.startsWith(ROLLUP_DATE_PREFIX) ||+ date.startsWith(ROLLUP_DATE_PREFIX_ALT)+ ? null+ : convertClickhouseDateToJs(date);+}diff --git a/packages/core/src/modules/realtime/realtime.routes.ts b/packages/core/src/modules/realtime/realtime.routes.tsindex e441a7e1a..c7adb3d98 100644--- a/packages/core/src/modules/realtime/realtime.routes.ts+++ b/packages/core/src/modules/realtime/realtime.routes.ts@@ -80,10 +80,11 @@ export const realtimeRoutes = defineRoutes((app) =>params: projectParams,async open(ws) {const { projectId } = ws.data.params;+ const { ctx } = ws.data;rememberUnsubscribe(ws,await subscribeToVisitorActivity(projectId, () => {- getActiveVisitorCount(projectId).then(+ getActiveVisitorCount(ctx, projectId).then((count) => ws.send(String(count)),() => ws.send('0'));diff --git a/packages/core/src/modules/realtime/realtime.rpc.ts b/packages/core/src/modules/realtime/realtime.rpc.tsindex 2794974d4..add72d507 100644--- a/packages/core/src/modules/realtime/realtime.rpc.ts+++ b/packages/core/src/modules/realtime/realtime.rpc.ts@@ -66,7 +66,7 @@ export const realtimeRouter = createTRPCRouter({.query(async ({ input, ctx }) => {await requireReadAccess(ctx, input.projectId);- return getRealtimeCoordinates(input.projectId);+ return getRealtimeCoordinates(ctx, input.projectId);}),mapBadgeDetails: procedure.input(@@ -82,34 +82,34 @@ export const realtimeRouter = createTRPCRouter({.query(async ({ input, ctx }) => {await requireReadAccess(ctx, input.projectId);- return getRealtimeMapBadgeDetails(input);+ return getRealtimeMapBadgeDetails(ctx, input);}),activeSessions: procedure.input(z.object({ projectId: z.string() })).query(async ({ input, ctx }) => {await requireReadAccess(ctx, input.projectId);- return getRealtimeActiveSessions(input.projectId);+ return getRealtimeActiveSessions(ctx, input.projectId);}),paths: procedure.input(z.object({ projectId: z.string() })).query(async ({ input, ctx }) => {await requireReadAccess(ctx, input.projectId);- return getRealtimePaths(input.projectId);+ return getRealtimePaths(ctx, input.projectId);}),referrals: procedure.input(z.object({ projectId: z.string() })).query(async ({ input, ctx }) => {await requireReadAccess(ctx, input.projectId);- return getRealtimeReferrals(input.projectId);+ return getRealtimeReferrals(ctx, input.projectId);}),geo: procedure.input(z.object({ projectId: z.string() })).query(async ({ input, ctx }) => {await requireReadAccess(ctx, input.projectId);- return getRealtimeGeo(input.projectId);+ return getRealtimeGeo(ctx, input.projectId);}),});diff --git a/packages/core/src/modules/realtime/realtime.service.test.ts b/packages/core/src/modules/realtime/realtime.service.test.tsindex 5da102ab6..cb84d0a02 100644--- a/packages/core/src/modules/realtime/realtime.service.test.ts+++ b/packages/core/src/modules/realtime/realtime.service.test.ts@@ -1,12 +1,16 @@-// realtime.service.ts's db/ch/redis access is lazy (`await import(...)`-// inside each function — see the file's header), which is exactly what makes-// `mock.module` work here with no import-time side effects to race: every-// mock below is registered before the subject's first call, not before its-// (side-effect-free) import.+// M10-005: realtime.service.ts takes `ServiceDeps` now, so the event buffer+// and the ClickHouse client are HANDED IN (`deps.buffers.event`, `deps.ch`)+// rather than mocked onto a module specifier. `@openpanel/redis` is still+// reached lazily (see the subject's `loadRedis` header), so that one stays a+// `mock.module` registered before the subject's first call.+//+// Every assertion below is the one it was before the deps switch: the same+// project scoping, the same 30-minute window, the same filter/limit wiring,+// the same subscribe/unsubscribe behaviour.//// Scope: the "/live websocket glue" (new logic this wave adds) plus one// representative ClickHouse query (`getRealtimeActiveSessions`) proving the-// lazy-load + filter/limit wiring. The other five queries are a verbatim+// deps + filter/limit wiring. The other five queries are a verbatim// port of packages/trpc/src/routers/realtime.ts's SQL — mechanical, not new// behaviour — and are exercised end-to-end by realtime.rpc.test.ts's// unauthenticated-boundary tests plus this repo's local ClickHouse run (see@@ -20,23 +24,6 @@ const eventBuffer = {async (projectId: string) => activeVisitorCountByProject.get(projectId) ?? 0),};-// Spread the real module — `event.service.ts` (reached via-// `getRealtimeActiveSessions`'s `transformEvent` load) imports `botBuffer`-// alongside `eventBuffer` from this same specifier; a partial factory here-// would break that unrelated import, same hazard as the clickhouse/client-// mock below. Snapshotted into a plain object BEFORE mocking: the live-// import binding would reflect the mock too once `mock.module` below swaps-// the specifier, making a same-binding "restore" a no-op.-const actualBuffers = await import('@openpanel/queue/src/buffers');-const realBuffers = { ...actualBuffers };-mock.module('@openpanel/queue/src/buffers', () => ({- ...realBuffers,- eventBuffer,-}));--afterAll(() => {- mock.module('@openpanel/queue/src/buffers', () => realBuffers);-});const subscribeToPublishedEvent = mock((_channel: string, _type: string, _cb: (event: unknown) => void) => {@@ -61,22 +48,27 @@ afterAll(() => {mock.module('@openpanel/redis', () => realRedis);});-const chQuery = mock(async (_query: string) => [] as unknown[]);-const actualClickhouseClient = await import(- '@openpanel/db/src/clickhouse/client'-);-const realClickhouseClient = { ...actualClickhouseClient };-mock.module('@openpanel/db/src/clickhouse/client', () => ({- ...realClickhouseClient,- chQuery,+// `deps.ch.query` is what shared/ch-query.ts calls; it returns the raw+// ClickHouse response envelope, so the stub speaks that shape.+let nextRows: unknown[] = [];+const chQuery = mock(async ({ query: _query }: { query: string }) => ({+ json: async () => ({ data: nextRows, meta: [], rows: nextRows.length }),}));-afterAll(() => {- mock.module(- '@openpanel/db/src/clickhouse/client',- () => realClickhouseClient- );-});+const noop = () => undefined;+const deps = {+ ch: { query: chQuery },+ buffers: { event: eventBuffer },+ logger: {+ fatal: noop,+ error: noop,+ warn: noop,+ info: noop,+ debug: noop,+ trace: noop,+ child: () => deps.logger,+ },+} as unknown as import('../../services').ServiceDeps;let subject: typeof import('./realtime.service');@@ -88,13 +80,14 @@ beforeEach(() => {activeVisitorCountByProject.clear();subscribeToPublishedEvent.mockClear();chQuery.mockClear();+ nextRows = [];unsubscribeCalls = 0;});test('getActiveVisitorCount reads through the event buffer, keyed by project', async () => {activeVisitorCountByProject.set('proj_1', 7);- await expect(subject.getActiveVisitorCount('proj_1')).resolves.toBe(7);+ await expect(subject.getActiveVisitorCount(deps, 'proj_1')).resolves.toBe(7);expect(eventBuffer.getActiveVisitorCount).toHaveBeenCalledWith('proj_1');});@@ -163,7 +156,7 @@ test('subscribeToOrganizationSubscriptionUpdates subscribes on organization:subs});test('getRealtimeActiveSessions scopes the query to the project and the 30-minute window', async () => {- chQuery.mockImplementationOnce(async () => [+ nextRows = [{name: 'screen_view',session_id: 'sess_1',@@ -181,12 +174,12 @@ test('getRealtimeActiveSessions scopes the query to the project and the 30-minutbrowser_version: '',device: 'desktop',},- ]);+ ];- const result = await subject.getRealtimeActiveSessions('proj_1');+ const result = await subject.getRealtimeActiveSessions(deps, 'proj_1');expect(chQuery).toHaveBeenCalledTimes(1);- const [query] = chQuery.mock.calls[0]!;+ const { query } = chQuery.mock.calls[0]![0];expect(query).toContain("project_id = 'proj_1'");expect(query).toContain('created_at >=');expect(query).toContain('LIMIT 50');diff --git a/packages/core/src/modules/realtime/realtime.service.ts b/packages/core/src/modules/realtime/realtime.service.tsindex a2e50c045..6e47493e1 100644--- a/packages/core/src/modules/realtime/realtime.service.ts+++ b/packages/core/src/modules/realtime/realtime.service.ts@@ -20,24 +20,26 @@// (`@openpanel/redis`, plain callback), so both stacks call the exact// same Redis subscription underneath.//-// db/ch access is LAZY (`load*` below), not a static top-level import — see-// insight.service.ts's header for the full reasoning (jobs.registry.ts and-// rpc.router.ts pull this module into the eager barrel chain nearly every-// core test file reaches, and constructing @openpanel/db's clients at import-// time would spawn a pino-pretty transport worker thread per test file).-// `clix` (query-builder.ts) has no such side effect and stays a static-// import, same as cohort.service.ts / project.service.ts.+// M10-005: every query function takes `ServiceDeps`. The ClickHouse CLIENT is+// `deps.ch` — reads through shared/ch-query.ts, `clix(deps.ch)` for the four+// builder queries — and the event buffer is `deps.buffers.event`, so the+// `loadChClient` / `loadEventBuffer` lazy imports of @openpanel/db are gone+// and the requestId reaches the query (ADR-018 R1, docs/TECH_DEBT.md §2, §4).//-// ClickHouse queries here still go through clix/chQuery/sqlstring, not the-// `sql` tag: ADR-013 converts the analytics read path one query per P7 task,-// and this module's queries haven't been converted yet.+// ClickHouse queries here still go through clix/sqlstring, not the `sql` tag:+// ADR-013 converts the analytics read path one query per P7 task, and this+// module's queries haven't been converted yet. `clix` and `TABLE_NAMES` are+// therefore still needed and come from the v1-compat seam's pure-helper hop+// (project.service.ts's `loadChHelpers` does the same) rather than a direct+// @openpanel/db import.-import { clix } from '@openpanel/db/src/clickhouse/query-builder';import type { IPublishChannels } from '@openpanel/redis';import { subMinutes } from 'date-fns';import sqlstring from 'sqlstring';-import { loadDbBuffers } from '../../buffers/lazy-db-buffers';-import type { IClickhouseEvent } from '../event/event.service';+import type { ServiceDeps } from '../../services';+import { chQuery } from '../../shared/ch-query';+import { type IClickhouseEvent, transformEvent } from '../event/event.service';+import { getProfiles } from '../profile/profile.service';const REALTIME_WINDOW_MINUTES = 30;const ACTIVE_SESSIONS_LIMIT = 50;@@ -50,20 +52,8 @@ const MAP_BADGE_TOP_LIMIT = 3;const MAP_BADGE_RECENT_SESSIONS_LIMIT = 8;const CLUSTER_RADII_DEGREES = [0.5, 1, 3, 10];-function loadChClient() {- return import('@openpanel/db/src/clickhouse/client');-}--function loadEventService() {- return import('../event/event.service');-}--function loadProfileService() {- return import('../profile/profile.service');-}--function loadEventBuffer() {- return loadDbBuffers().then((m) => m.eventBuffer);+function loadChHelpers() {+ return import('../../v1-compat').then((m) => m.compatChHelpers());}function since(): Date {@@ -219,10 +209,14 @@ function buildRealtimeBadgeDetailsFilter(input: {return buildRealtimeLocationFilter(input.locations);}-export async function getRealtimeCoordinates(projectId: string) {- const { chQuery, TABLE_NAMES } = await loadChClient();+export async function getRealtimeCoordinates(+ deps: ServiceDeps,+ projectId: string+) {+ const { TABLE_NAMES } = await loadChHelpers();const res = await chQuery<CoordinatePoint>(+ deps,`SELECTcountry,city,@@ -242,24 +236,21 @@ export async function getRealtimeCoordinates(projectId: string) {return adaptiveCluster(res, COORDINATES_CLUSTER_TARGET);}-export async function getRealtimeMapBadgeDetails(input: {- projectId: string;- detailScope: RealtimeBadgeDetailScope;- locations: RealtimeLocation[];-}) {- const {- ch,- chQuery,- TABLE_NAMES,- formatClickhouseDate,- convertClickhouseDateToJs,- } = await loadChClient();- const { getProfiles } = await loadProfileService();+export async function getRealtimeMapBadgeDetails(+ deps: ServiceDeps,+ input: {+ projectId: string;+ detailScope: RealtimeBadgeDetailScope;+ locations: RealtimeLocation[];+ }+) {+ const { clix, TABLE_NAMES, formatClickhouseDate, convertClickhouseDateToJs } =+ await loadChHelpers();const sinceDate = formatClickhouseDate(since());const locationFilter = buildRealtimeBadgeDetailsFilter(input);- const summaryQuery = clix(ch)+ const summaryQuery = clix(deps.ch).select<{total_sessions: number;total_profiles: number;@@ -272,7 +263,7 @@ export async function getRealtimeMapBadgeDetails(input: {.where('created_at', '>=', sinceDate).rawWhere(locationFilter);- const topReferrersQuery = clix(ch)+ const topReferrersQuery = clix(deps.ch).select<{referrer_name: string;count: number;@@ -286,7 +277,7 @@ export async function getRealtimeMapBadgeDetails(input: {.orderBy('count', 'DESC').limit(MAP_BADGE_TOP_LIMIT);- const topPathsQuery = clix(ch)+ const topPathsQuery = clix(deps.ch).select<{origin: string;path: string;@@ -301,7 +292,7 @@ export async function getRealtimeMapBadgeDetails(input: {.orderBy('count', 'DESC').limit(MAP_BADGE_TOP_LIMIT);- const topEventsQuery = clix(ch)+ const topEventsQuery = clix(deps.ch).select<{name: string;count: number;@@ -330,6 +321,7 @@ export async function getRealtimeMapBadgeDetails(input: {country: string;city: string;}>(+ deps,`SELECTsession_id,profile_id,@@ -362,6 +354,7 @@ export async function getRealtimeMapBadgeDetails(input: {]);const profiles = await getProfiles(+ deps,recentSessions.map((item) => item.profile_id).filter(Boolean),input.projectId);@@ -407,11 +400,14 @@ export async function getRealtimeMapBadgeDetails(input: {};}-export async function getRealtimeActiveSessions(projectId: string) {- const { chQuery, TABLE_NAMES, formatClickhouseDate } = await loadChClient();- const { transformEvent } = await loadEventService();+export async function getRealtimeActiveSessions(+ deps: ServiceDeps,+ projectId: string+) {+ const { TABLE_NAMES, formatClickhouseDate } = await loadChHelpers();const rows = await chQuery<IClickhouseEvent>(+ deps,`SELECTname, session_id, created_at, path, origin, referrer, referrer_name,country, city, region, os, os_version, browser, browser_version,@@ -425,10 +421,10 @@ export async function getRealtimeActiveSessions(projectId: string) {return rows.map(transformEvent);}-export async function getRealtimePaths(projectId: string) {- const { ch, TABLE_NAMES, formatClickhouseDate } = await loadChClient();+export async function getRealtimePaths(deps: ServiceDeps, projectId: string) {+ const { clix, TABLE_NAMES, formatClickhouseDate } = await loadChHelpers();- return await clix(ch)+ return await clix(deps.ch).select<{origin: string;path: string;@@ -452,10 +448,13 @@ export async function getRealtimePaths(projectId: string) {.execute();}-export async function getRealtimeReferrals(projectId: string) {- const { ch, TABLE_NAMES, formatClickhouseDate } = await loadChClient();+export async function getRealtimeReferrals(+ deps: ServiceDeps,+ projectId: string+) {+ const { clix, TABLE_NAMES, formatClickhouseDate } = await loadChHelpers();- return await clix(ch)+ return await clix(deps.ch).select<{referrer_name: string;count: number;@@ -477,10 +476,10 @@ export async function getRealtimeReferrals(projectId: string) {.execute();}-export async function getRealtimeGeo(projectId: string) {- const { ch, TABLE_NAMES, formatClickhouseDate } = await loadChClient();+export async function getRealtimeGeo(deps: ServiceDeps, projectId: string) {+ const { clix, TABLE_NAMES, formatClickhouseDate } = await loadChHelpers();- return await clix(ch)+ return await clix(deps.ch).select<{country: string;city: string;@@ -509,8 +508,8 @@ export async function getRealtimeGeo(projectId: string) {// `subscribeToPublishedEvent`'s own shape. realtime.routes.ts (Elysia/Bun)// awaits these directly; nothing here is HTTP- or ws-library-specific.//-// `@openpanel/redis` is LAZY here too, same reasoning as `loadChClient` above-// but for a different failure mode: it has no import-time side effect (its+// `@openpanel/redis` is LAZY here for its own reason: it has no+// import-time side effect (its// clients connect lazily on first use — insight.service.ts already imports// `getRedisCache` from it statically), but several modules' tests replace the// whole package with `mock.module('@openpanel/redis', () => ({ subset }))`.@@ -521,11 +520,11 @@ function loadRedis() {return import('@openpanel/redis');}-export async function getActiveVisitorCount(+export function getActiveVisitorCount(+ deps: ServiceDeps,projectId: string): Promise<number> {- const eventBuffer = await loadEventBuffer();- return await eventBuffer.getActiveVisitorCount(projectId);+ return deps.buffers.event.getActiveVisitorCount(projectId);}/** V1: `wsVisitors` — re-count on every batch touching this project. */@@ -586,3 +585,31 @@ export async function subscribeToOrganizationSubscriptionUpdates(onUpdate);}++export interface RealtimeService {+ getCoordinates(projectId: string): ReturnType<typeof getRealtimeCoordinates>;+ getMapBadgeDetails(+ input: Parameters<typeof getRealtimeMapBadgeDetails>[1]+ ): ReturnType<typeof getRealtimeMapBadgeDetails>;+ getActiveSessions(+ projectId: string+ ): ReturnType<typeof getRealtimeActiveSessions>;+ getPaths(projectId: string): ReturnType<typeof getRealtimePaths>;+ getReferrals(projectId: string): ReturnType<typeof getRealtimeReferrals>;+ getGeo(projectId: string): ReturnType<typeof getRealtimeGeo>;+ getActiveVisitorCount(projectId: string): Promise<number>;+}++export function createRealtimeService(deps: ServiceDeps): RealtimeService {+ return {+ getCoordinates: (projectId) => getRealtimeCoordinates(deps, projectId),+ getMapBadgeDetails: (input) => getRealtimeMapBadgeDetails(deps, input),+ getActiveSessions: (projectId) =>+ getRealtimeActiveSessions(deps, projectId),+ getPaths: (projectId) => getRealtimePaths(deps, projectId),+ getReferrals: (projectId) => getRealtimeReferrals(deps, projectId),+ getGeo: (projectId) => getRealtimeGeo(deps, projectId),+ getActiveVisitorCount: (projectId) =>+ getActiveVisitorCount(deps, projectId),+ };+}diff --git a/packages/core/src/modules/session/session.jobs.ts b/packages/core/src/modules/session/session.jobs.tsindex ca4b4dde9..9fbe58762 100644--- a/packages/core/src/modules/session/session.jobs.ts+++ b/packages/core/src/modules/session/session.jobs.ts@@ -59,7 +59,11 @@ export const sessionQueueJobs = {payload: sessionEndPayload,handler: async ({ payload: data, ctx }) => {const logger = ctx.logger.child({ payload: data.event });- const deps = await loadSessionEndDeps(await loadSessionRuntime(), logger);+ const deps = await loadSessionEndDeps(+ ctx,+ await loadSessionRuntime(),+ logger+ );await createSessionEnd(data, deps);diff --git a/packages/core/src/modules/session/session.service.ts b/packages/core/src/modules/session/session.service.tsindex 9a2488bbe..931b01f6b 100644--- a/packages/core/src/modules/session/session.service.ts+++ b/packages/core/src/modules/session/session.service.ts@@ -45,14 +45,6 @@ function loadChClient() {return import('@openpanel/db/src/clickhouse/client');}-// Core's own profile service, not @openpanel/db's shim: the shim re-exports-// the core barrel, which would route this lazy edge back through-// session-end → event.service → here — a dynamic-import cycle rolldown-// cannot finalize when apps/worker bundles the workspace.-function loadProfileService() {- return import('../profile/profile.service');-}-function loadFilterCompiler() {return import('../chart/src/table-filter-where');}@@ -307,7 +299,10 @@ export async function getSessionList(options: GetSessionListOptions) {});}- const { getProfilesCached } = await loadProfileService();+ // M10-005: `getProfilesCached` takes `ServiceDeps` now and `getSessionList`+ // has none — packages/trpc's session router still calls it bare — so it+ // reaches the v1-compat spelling. Converting this module is its own task.+ const { getProfilesCached } = await import('../../v1-compat');const profileIds = data.filter((e) => e.device_id !== e.profile_id).map((e) => e.profile_id);diff --git a/packages/core/src/modules/session/src/session-end.ts b/packages/core/src/modules/session/src/session-end.tsindex 9a592b56d..6f9301c09 100644--- a/packages/core/src/modules/session/src/session-end.ts+++ b/packages/core/src/modules/session/src/session-end.ts@@ -5,6 +5,7 @@import { loadDbBuffers } from '../../../buffers/lazy-db-buffers';import type { EnqueueOptions } from '../../../jobs/define';import type { Logger } from '../../../logger';+import type { ServiceDeps } from '../../../services';// Static, not lazy like the db imports below: core's event.service touches no// client at import time, and a dynamic edge here closes a dynamic-import cycle// (event.service ⇢ session.service → this file) that panics rolldown when@@ -116,6 +117,7 @@ export interface SessionEndDeps extends SessionRuntime {}export async function loadSessionEndDeps(+ deps: ServiceDeps,runtime: SessionRuntime,logger: Logger): Promise<SessionEndDeps> {@@ -133,7 +135,7 @@ export async function loadSessionEndDeps(return {...runtime,logger,- createEvent,+ createEvent: (payload) => createEvent(deps, payload),transformEvent,transformSessionToEvent,getEvents: async (query) =>diff --git a/packages/core/src/services.ts b/packages/core/src/services.tsindex 5123bacf0..92716ff40 100644--- a/packages/core/src/services.ts+++ b/packages/core/src/services.ts@@ -73,6 +73,14 @@ import {createOrganizationService,type OrganizationService,} from './modules/organization/organization.service';+import {+ createOverviewService,+ type OverviewService,+} from './modules/overview/overview.service';+import {+ createPagesService,+ type PagesService,+} from './modules/overview/pages.service';import {createProfileService,type ProfileService,@@ -81,6 +89,10 @@ import {createProjectService,type ProjectService,} from './modules/project/project.service';+import {+ createRealtimeService,+ type RealtimeService,+} from './modules/realtime/realtime.service';import {createReferenceService,type ReferenceService,@@ -140,6 +152,9 @@ export interface Services {profile: ProfileService;group: GroupService;chart: ChartService;+ overview: OverviewService;+ pages: PagesService;+ realtime: RealtimeService;misc: MiscService;report: ReportService;dashboard: DashboardService;@@ -175,6 +190,9 @@ export function createServices(deps: ServiceDeps): Services {profile: createProfileService(deps),group: createGroupService(deps),chart: createChartService(deps),+ overview: createOverviewService(deps),+ pages: createPagesService(deps),+ realtime: createRealtimeService(deps),misc: createMiscService(deps),report: createReportService(deps),dashboard: createDashboardService(deps),diff --git a/packages/core/src/shared/cacheable-per-deps.ts b/packages/core/src/shared/cacheable-per-deps.tsnew file mode 100644index 000000000..1b67566ba--- /dev/null+++ b/packages/core/src/shared/cacheable-per-deps.ts@@ -0,0 +1,70 @@+// `cacheable` for a function that needs the caller's `ServiceDeps`.+//+// M10-005: the three cross-module caches on the runtime path+// (`getEventMetasCached`, `getProfilesCached`, `getProfilePropertyKeysCached`)+// wrap a function that now reaches Postgres/ClickHouse through `deps`. Passing+// `deps` to `cacheable` directly is not an option: it keys on EVERY argument+// through `stringify`, which walks an object's entries recursively — it would+// serialize the whole Prisma client into the Redis key.+//+// `deps` is not cache identity anyway. It is the scope a call runs in (the+// process's clients plus this request's logger); two calls with the same+// arguments are the same call whichever scope makes them. So the cacheable is+// built once per `deps` and remembered in a WeakMap, and the Redis key stays+// byte-identical to what the module-scope `cacheable(fn, ttl)` this replaces+// produced — hence the EXPLICIT `name`, which is what `fn.name` used to+// supply and what an inline arrow would silently drop.+//+// Consequence, stated: `createCtx` builds a fresh deps object per request+// (context.ts's `installLazyServices`), so the L1 LRU inside `cacheable` is+// per scope rather than per process. The L2 Redis cache — the shared one, the+// one that actually saves the query — is unchanged, so a repeat call costs one+// Redis GET where it used to cost none. Boot-scoped callers (the v1-compat+// seam, job handlers) keep a process-lived L1.++import { cacheable } from '@openpanel/redis';+import type { ServiceDeps } from '../services';++type Cacheable<A extends unknown[], R> = ((...args: A) => Promise<R>) & {+ getKey: (...args: A) => string;+ clear: (...args: A) => Promise<number>;+};++export type CacheablePerDeps<A extends unknown[], R> = ((+ deps: ServiceDeps,+ ...args: A+) => Promise<R>) & {+ getKey: (deps: ServiceDeps, ...args: A) => string;+ clear: (deps: ServiceDeps, ...args: A) => Promise<number>;+};++export function cacheablePerDeps<A extends unknown[], R>(+ name: string,+ fn: (deps: ServiceDeps, ...args: A) => Promise<R>,+ expireInSec: number+): CacheablePerDeps<A, R> {+ const byDeps = new WeakMap<ServiceDeps, Cacheable<A, R>>();++ const forDeps = (deps: ServiceDeps): Cacheable<A, R> => {+ const existing = byDeps.get(deps);+ if (existing) {+ return existing;+ }+ const built = cacheable(+ name,+ (...args: A) => fn(deps, ...args),+ expireInSec+ ) as unknown as Cacheable<A, R>;+ byDeps.set(deps, built);+ return built;+ };++ const run = (deps: ServiceDeps, ...args: A): Promise<R> =>+ forDeps(deps)(...args);+ run.getKey = (deps: ServiceDeps, ...args: A): string =>+ forDeps(deps).getKey(...args);+ run.clear = (deps: ServiceDeps, ...args: A): Promise<number> =>+ forDeps(deps).clear(...args);++ return run;+}diff --git a/packages/core/src/shared/ch-query.ts b/packages/core/src/shared/ch-query.tsnew file mode 100644index 000000000..c39b184a6--- /dev/null+++ b/packages/core/src/shared/ch-query.ts@@ -0,0 +1,95 @@+// core's ClickHouse read path, bound to the scope's client.+//+// M10-005: the six modules on the runtime path used to reach ClickHouse+// through `await import('@openpanel/db/src/clickhouse/client')` and call that+// package's `chQuery`. That client has no request scope, so the requestId+// minted at the edge stopped there (ADR-018 R1, docs/TECH_DEBT.md §2). These+// two functions are `chQuery` / `chQueryWithMeta` over `deps.ch` — the same+// round-robin/retry proxy `main.ts` builds and hands to every scope — and+// `deps.logger`, so the `query info` line carries the request's id.+//+// Behaviour is @openpanel/db's, unchanged: `deps.ch.query` IS `withRetry+// (client => client.query(...))`, the same transport, and the Int-meta+// coercion below is the same one — ClickHouse's JSON format returns every+// Int*/UInt* column as a string, so a caller's `count: number` only holds if+// it is parsed back out here. The one field that cannot survive the move is+// `host`: the retry proxy does not report which replica served the query.+// chart/src/run-query.ts (M10-003) made the same trade.+//+// `@openpanel/db/src/clickhouse/sql` is a value import and stays one: ADR-013+// puts the `sql` tag in packages/db by name, and it is a compile-time+// template tag with no client and no request scope.++import type { ClickHouseSettings, ResponseJSON } from '@clickhouse/client';+import {+ type SqlFragment,+ toStatement,+} from '@openpanel/db/src/clickhouse/sql';+import type { ServiceDeps } from '../services';++/** A raw statement or an ADR-013 fragment carrying its own bound params. */+export type ChQueryInput = string | SqlFragment;++const NEWLINES = /\n/g;+const WHITESPACE_RUNS = /\s+/g;++function cleanQuery(query: string): string {+ return query.replace(NEWLINES, '').replace(WHITESPACE_RUNS, ' ').trim();+}++export async function chQueryWithMeta<T extends object>(+ deps: ServiceDeps,+ query: ChQueryInput,+ clickhouseSettings?: ClickHouseSettings+): Promise<ResponseJSON<T>> {+ const start = Date.now();+ const statement = toStatement(query);+ const paramNames = Object.keys(statement.query_params);+ const hasParams = paramNames.length > 0;++ const res = await deps.ch.query({+ query: statement.query,+ query_params: hasParams ? statement.query_params : undefined,+ clickhouse_settings: clickhouseSettings,+ });+ const json = await res.json<T>();++ const keys = Object.keys(json.data[0] ?? {});+ const response = {+ ...json,+ data: json.data.map((item) =>+ keys.reduce((acc, key) => {+ const meta = json.meta?.find((m) => m.name === key);+ const value = item[key as keyof T];+ acc[key as keyof T] =+ value && meta?.type.includes('Int')+ ? (Number.parseFloat(value as string) as T[keyof T])+ : value;+ return acc;+ }, {} as T)+ ),+ };++ deps.logger.info(+ {+ query: cleanQuery(statement.query),+ // Names only: bound values are user data and must not reach the logs.+ queryParams: hasParams ? paramNames : undefined,+ rows: json.rows,+ stats: response.statistics,+ elapsed: Date.now() - start,+ clickhouseSettings,+ },+ 'query info'+ );++ return response;+}++export async function chQuery<T extends object>(+ deps: ServiceDeps,+ query: ChQueryInput,+ clickhouseSettings?: ClickHouseSettings+): Promise<T[]> {+ return (await chQueryWithMeta<T>(deps, query, clickhouseSettings)).data;+}diff --git a/packages/core/src/v1-compat.ts b/packages/core/src/v1-compat.tsindex 3317ed6e9..da4d183bf 100644--- a/packages/core/src/v1-compat.ts+++ b/packages/core/src/v1-compat.ts@@ -42,6 +42,101 @@ import {type SignInShareInput,signInToShare as signInToShareWithDeps,} from './modules/auth/auth.service';+import {+ createBotEvent as event_createBotEvent,+ createEvent as event_createEvent,+ getBotEventsPage as event_getBotEventsPage,+ getConversionEventNames as event_getConversionEventNames,+ getConversionListPage as event_getConversionListPage,+ getEventById as event_getEventById,+ getEventDetails as event_getEventDetails,+ getEventList as event_getEventList,+ getEventListPage as event_getEventListPage,+ getEventMetas as event_getEventMetas,+ getEventPropertyValuesCore as event_getEventPropertyValuesCore,+ getEvents as event_getEvents,+ getEventsCount as event_getEventsCount,+ getTopEventNames as event_getTopEventNames,+ getTopOrigins as event_getTopOrigins,+ getTopPages as event_getTopPages,+ listEventNamesCore as event_listEventNamesCore,+ listEventPropertiesCore as event_listEventPropertiesCore,+ queryEventsCore as event_queryEventsCore,+ updateEventMeta as event_updateEventMeta,+} from './modules/event/event.service';+import {+ createGroup as group_createGroup,+ deleteGroup as group_deleteGroup,+ findGroupsCore as group_findGroupsCore,+ getGroupActivity as group_getGroupActivity,+ getGroupById as group_getGroupById,+ getGroupCore as group_getGroupCore,+ getGroupList as group_getGroupList,+ getGroupListCount as group_getGroupListCount,+ getGroupListPage as group_getGroupListPage,+ getGroupMemberGrowth as group_getGroupMemberGrowth,+ getGroupMemberProfiles as group_getGroupMemberProfiles,+ getGroupMemberProfilesPage as group_getGroupMemberProfilesPage,+ getGroupMetrics as group_getGroupMetrics,+ getGroupMostEvents as group_getGroupMostEvents,+ getGroupPopularRoutes as group_getGroupPopularRoutes,+ getGroupPropertyKeys as group_getGroupPropertyKeys,+ getGroupStats as group_getGroupStats,+ getGroupsByIds as group_getGroupsByIds,+ getGroupTypes as group_getGroupTypes,+ listGroupTypesCore as group_listGroupTypesCore,+ updateGroup as group_updateGroup,+ upsertGroup as group_upsertGroup,+} from './modules/group/group.service';+import {+ getStats as misc_getStats,+ insertPingRecord as misc_insertPingRecord,+ runPingCron as misc_runPingCron,+} from './modules/misc/misc.service';+import {+ getRawWhereClause,+ isPageFilter,+ getAnalyticsOverviewCore as overview_getAnalyticsOverviewCore,+ getSegmentDailySeriesCore as overview_getSegmentDailySeriesCore,+ getTrafficBreakdownCore as overview_getTrafficBreakdownCore,+} from './modules/overview/overview.service';+import {+ getEntryExitPagesCore as pages_getEntryExitPagesCore,+ getPageConversionsCore as pages_getPageConversionsCore,+ getPagePerformanceCore as pages_getPagePerformanceCore,+ getTopPagesCore as pages_getTopPagesCore,+} from './modules/overview/pages.service';+import {+ adjustProfileProperty as profile_adjustProfileProperty,+ findProfilesCore as profile_findProfilesCore,+ getPowerUsers as profile_getPowerUsers,+ getProfileActivity as profile_getProfileActivity,+ getProfileById as profile_getProfileById,+ getProfileList as profile_getProfileList,+ getProfileListCount as profile_getProfileListCount,+ getProfileListPage as profile_getProfileListPage,+ getProfileMetrics as profile_getProfileMetrics,+ getProfileMetricsCore as profile_getProfileMetricsCore,+ getProfileMostEvents as profile_getProfileMostEvents,+ getProfilePopularRoutes as profile_getProfilePopularRoutes,+ getProfilePropertyKeys as profile_getProfilePropertyKeys,+ getProfilePropertyNames as profile_getProfilePropertyNames,+ getProfileSessionsCore as profile_getProfileSessionsCore,+ getProfiles as profile_getProfiles,+ getProfilesCached as profile_getProfilesCached,+ getProfileValues as profile_getProfileValues,+ getProfileWithEvents as profile_getProfileWithEvents,+ identifyProfile as profile_identifyProfile,+ upsertProfile as profile_upsertProfile,+} from './modules/profile/profile.service';+import {+ getRealtimeActiveSessions as realtime_getRealtimeActiveSessions,+ getRealtimeCoordinates as realtime_getRealtimeCoordinates,+ getRealtimeGeo as realtime_getRealtimeGeo,+ getRealtimeMapBadgeDetails as realtime_getRealtimeMapBadgeDetails,+ getRealtimePaths as realtime_getRealtimePaths,+ getRealtimeReferrals as realtime_getRealtimeReferrals,+} from './modules/realtime/realtime.service';import { createServices, type ServiceDeps, type Services } from './services';import type { ISetCookie } from './shared/cookie';@@ -170,6 +265,8 @@ export async function compatChHelpers(): Promise<{TABLE_NAMES: typeof import('@openpanel/db/src/clickhouse/client').TABLE_NAMES;chQuery: typeof import('@openpanel/db/src/clickhouse/client').chQuery;convertClickhouseDateToJs: typeof import('@openpanel/db/src/clickhouse/client').convertClickhouseDateToJs;+ formatClickhouseDate: typeof import('@openpanel/db/src/clickhouse/client').formatClickhouseDate;+ toNullIfDefaultMinDate: typeof import('@openpanel/db/src/clickhouse/client').toNullIfDefaultMinDate;clix: typeof import('@openpanel/db/src/clickhouse/query-builder').clix;}> {const [client, queryBuilder] = await Promise.all([@@ -180,6 +277,8 @@ export async function compatChHelpers(): Promise<{TABLE_NAMES: client.TABLE_NAMES,chQuery: client.chQuery,convertClickhouseDateToJs: client.convertClickhouseDateToJs,+ formatClickhouseDate: client.formatClickhouseDate,+ toNullIfDefaultMinDate: client.toNullIfDefaultMinDate,clix: queryBuilder.clix,};}@@ -772,3 +871,535 @@ export const deleteConversation: Services['conversation']['deleteConversation']services().then((container) =>container.conversation.deleteConversation(...args));++// M10-005: the runtime path's bare spellings. `event`, `profile`, `group`,+// `misc`, `overview`/`pages` and `realtime` all gained a `ServiceDeps` first+// parameter; `packages/trpc`'s routers and `@openpanel/queue`'s notification+// dispatch still call them bare, and reach them through here.++// --- event -------------------------------------------------------------------++export const createBotEvent: (+ ...args: Tail<Parameters<typeof event_createBotEvent>>+) => ReturnType<typeof event_createBotEvent> = (...args) =>+ compatServiceDeps().then((deps) => event_createBotEvent(deps, ...args));++export const createEvent: (+ ...args: Tail<Parameters<typeof event_createEvent>>+) => ReturnType<typeof event_createEvent> = (...args) =>+ compatServiceDeps().then((deps) => event_createEvent(deps, ...args));++export const getBotEventsPage: (+ ...args: Tail<Parameters<typeof event_getBotEventsPage>>+) => ReturnType<typeof event_getBotEventsPage> = (...args) =>+ compatServiceDeps().then((deps) => event_getBotEventsPage(deps, ...args));++export const getConversionEventNames: (+ ...args: Tail<Parameters<typeof event_getConversionEventNames>>+) => ReturnType<typeof event_getConversionEventNames> = (...args) =>+ compatServiceDeps().then((deps) =>+ event_getConversionEventNames(deps, ...args)+ );++export const getConversionListPage: (+ ...args: Tail<Parameters<typeof event_getConversionListPage>>+) => ReturnType<typeof event_getConversionListPage> = (...args) =>+ compatServiceDeps().then((deps) =>+ event_getConversionListPage(deps, ...args)+ );++export const getEventById: (+ ...args: Tail<Parameters<typeof event_getEventById>>+) => ReturnType<typeof event_getEventById> = (...args) =>+ compatServiceDeps().then((deps) => event_getEventById(deps, ...args));++export const getEventDetails: (+ ...args: Tail<Parameters<typeof event_getEventDetails>>+) => ReturnType<typeof event_getEventDetails> = (...args) =>+ compatServiceDeps().then((deps) => event_getEventDetails(deps, ...args));++export const getEventList: (+ ...args: Tail<Parameters<typeof event_getEventList>>+) => ReturnType<typeof event_getEventList> = (...args) =>+ compatServiceDeps().then((deps) => event_getEventList(deps, ...args));++export const getEventListPage: (+ ...args: Tail<Parameters<typeof event_getEventListPage>>+) => ReturnType<typeof event_getEventListPage> = (...args) =>+ compatServiceDeps().then((deps) => event_getEventListPage(deps, ...args));++export const getEventMetas: (+ ...args: Tail<Parameters<typeof event_getEventMetas>>+) => ReturnType<typeof event_getEventMetas> = (...args) =>+ compatServiceDeps().then((deps) => event_getEventMetas(deps, ...args));++export const getEventPropertyValuesCore: (+ ...args: Tail<Parameters<typeof event_getEventPropertyValuesCore>>+) => ReturnType<typeof event_getEventPropertyValuesCore> = (...args) =>+ compatServiceDeps().then((deps) =>+ event_getEventPropertyValuesCore(deps, ...args)+ );++export const getEvents: (+ ...args: Tail<Parameters<typeof event_getEvents>>+) => ReturnType<typeof event_getEvents> = (...args) =>+ compatServiceDeps().then((deps) => event_getEvents(deps, ...args));++export const getEventsCount: (+ ...args: Tail<Parameters<typeof event_getEventsCount>>+) => ReturnType<typeof event_getEventsCount> = (...args) =>+ compatServiceDeps().then((deps) => event_getEventsCount(deps, ...args));++export const getTopEventNames: (+ ...args: Tail<Parameters<typeof event_getTopEventNames>>+) => ReturnType<typeof event_getTopEventNames> = (...args) =>+ compatServiceDeps().then((deps) => event_getTopEventNames(deps, ...args));++export const getTopOrigins: (+ ...args: Tail<Parameters<typeof event_getTopOrigins>>+) => ReturnType<typeof event_getTopOrigins> = (...args) =>+ compatServiceDeps().then((deps) => event_getTopOrigins(deps, ...args));++export const getTopPages: (+ ...args: Tail<Parameters<typeof event_getTopPages>>+) => ReturnType<typeof event_getTopPages> = (...args) =>+ compatServiceDeps().then((deps) => event_getTopPages(deps, ...args));++export const listEventNamesCore: (+ ...args: Tail<Parameters<typeof event_listEventNamesCore>>+) => ReturnType<typeof event_listEventNamesCore> = (...args) =>+ compatServiceDeps().then((deps) => event_listEventNamesCore(deps, ...args));++export const listEventPropertiesCore: (+ ...args: Tail<Parameters<typeof event_listEventPropertiesCore>>+) => ReturnType<typeof event_listEventPropertiesCore> = (...args) =>+ compatServiceDeps().then((deps) =>+ event_listEventPropertiesCore(deps, ...args)+ );++export const queryEventsCore: (+ ...args: Tail<Parameters<typeof event_queryEventsCore>>+) => ReturnType<typeof event_queryEventsCore> = (...args) =>+ compatServiceDeps().then((deps) => event_queryEventsCore(deps, ...args));++export const updateEventMeta: (+ ...args: Tail<Parameters<typeof event_updateEventMeta>>+) => ReturnType<typeof event_updateEventMeta> = (...args) =>+ compatServiceDeps().then((deps) => event_updateEventMeta(deps, ...args));++// --- profile -----------------------------------------------------------------++export const adjustProfileProperty: (+ ...args: Tail<Parameters<typeof profile_adjustProfileProperty>>+) => ReturnType<typeof profile_adjustProfileProperty> = (...args) =>+ compatServiceDeps().then((deps) =>+ profile_adjustProfileProperty(deps, ...args)+ );++export const findProfilesCore: (+ ...args: Tail<Parameters<typeof profile_findProfilesCore>>+) => ReturnType<typeof profile_findProfilesCore> = (...args) =>+ compatServiceDeps().then((deps) => profile_findProfilesCore(deps, ...args));++export const getPowerUsers: (+ ...args: Tail<Parameters<typeof profile_getPowerUsers>>+) => ReturnType<typeof profile_getPowerUsers> = (...args) =>+ compatServiceDeps().then((deps) => profile_getPowerUsers(deps, ...args));++export const getProfileActivity: (+ ...args: Tail<Parameters<typeof profile_getProfileActivity>>+) => ReturnType<typeof profile_getProfileActivity> = (...args) =>+ compatServiceDeps().then((deps) => profile_getProfileActivity(deps, ...args));++export const getProfileById: (+ ...args: Tail<Parameters<typeof profile_getProfileById>>+) => ReturnType<typeof profile_getProfileById> = (...args) =>+ compatServiceDeps().then((deps) => profile_getProfileById(deps, ...args));++export const getProfileList: (+ ...args: Tail<Parameters<typeof profile_getProfileList>>+) => ReturnType<typeof profile_getProfileList> = (...args) =>+ compatServiceDeps().then((deps) => profile_getProfileList(deps, ...args));++export const getProfileListCount: (+ ...args: Tail<Parameters<typeof profile_getProfileListCount>>+) => ReturnType<typeof profile_getProfileListCount> = (...args) =>+ compatServiceDeps().then((deps) =>+ profile_getProfileListCount(deps, ...args)+ );++export const getProfileListPage: (+ ...args: Tail<Parameters<typeof profile_getProfileListPage>>+) => ReturnType<typeof profile_getProfileListPage> = (...args) =>+ compatServiceDeps().then((deps) => profile_getProfileListPage(deps, ...args));++export const getProfileMetrics: (+ ...args: Tail<Parameters<typeof profile_getProfileMetrics>>+) => ReturnType<typeof profile_getProfileMetrics> = (...args) =>+ compatServiceDeps().then((deps) => profile_getProfileMetrics(deps, ...args));++export const getProfileMetricsCore: (+ ...args: Tail<Parameters<typeof profile_getProfileMetricsCore>>+) => ReturnType<typeof profile_getProfileMetricsCore> = (...args) =>+ compatServiceDeps().then((deps) =>+ profile_getProfileMetricsCore(deps, ...args)+ );++export const getProfileMostEvents: (+ ...args: Tail<Parameters<typeof profile_getProfileMostEvents>>+) => ReturnType<typeof profile_getProfileMostEvents> = (...args) =>+ compatServiceDeps().then((deps) =>+ profile_getProfileMostEvents(deps, ...args)+ );++export const getProfilePopularRoutes: (+ ...args: Tail<Parameters<typeof profile_getProfilePopularRoutes>>+) => ReturnType<typeof profile_getProfilePopularRoutes> = (...args) =>+ compatServiceDeps().then((deps) =>+ profile_getProfilePopularRoutes(deps, ...args)+ );++export const getProfilePropertyKeys: (+ ...args: Tail<Parameters<typeof profile_getProfilePropertyKeys>>+) => ReturnType<typeof profile_getProfilePropertyKeys> = (...args) =>+ compatServiceDeps().then((deps) =>+ profile_getProfilePropertyKeys(deps, ...args)+ );++export const getProfilePropertyNames: (+ ...args: Tail<Parameters<typeof profile_getProfilePropertyNames>>+) => ReturnType<typeof profile_getProfilePropertyNames> = (...args) =>+ compatServiceDeps().then((deps) =>+ profile_getProfilePropertyNames(deps, ...args)+ );++export const getProfileSessionsCore: (+ ...args: Tail<Parameters<typeof profile_getProfileSessionsCore>>+) => ReturnType<typeof profile_getProfileSessionsCore> = (...args) =>+ compatServiceDeps().then((deps) =>+ profile_getProfileSessionsCore(deps, ...args)+ );++export const getProfileValues: (+ ...args: Tail<Parameters<typeof profile_getProfileValues>>+) => ReturnType<typeof profile_getProfileValues> = (...args) =>+ compatServiceDeps().then((deps) => profile_getProfileValues(deps, ...args));++export const getProfileWithEvents: (+ ...args: Tail<Parameters<typeof profile_getProfileWithEvents>>+) => ReturnType<typeof profile_getProfileWithEvents> = (...args) =>+ compatServiceDeps().then((deps) =>+ profile_getProfileWithEvents(deps, ...args)+ );++export const getProfiles: (+ ...args: Tail<Parameters<typeof profile_getProfiles>>+) => ReturnType<typeof profile_getProfiles> = (...args) =>+ compatServiceDeps().then((deps) => profile_getProfiles(deps, ...args));++/** `session.service.ts`'s `getSessionList` is still deps-free (its own task),+ * and this is the one cached read it makes. */+export const getProfilesCached: (+ ...args: Tail<Parameters<typeof profile_getProfilesCached>>+) => ReturnType<typeof profile_getProfilesCached> = (...args) =>+ compatServiceDeps().then((deps) => profile_getProfilesCached(deps, ...args));++export const identifyProfile: (+ ...args: Tail<Parameters<typeof profile_identifyProfile>>+) => ReturnType<typeof profile_identifyProfile> = (...args) =>+ compatServiceDeps().then((deps) => profile_identifyProfile(deps, ...args));++export const upsertProfile: (+ ...args: Tail<Parameters<typeof profile_upsertProfile>>+) => ReturnType<typeof profile_upsertProfile> = (...args) =>+ compatServiceDeps().then((deps) => profile_upsertProfile(deps, ...args));++// --- group -------------------------------------------------------------------++export const createGroup: (+ ...args: Tail<Parameters<typeof group_createGroup>>+) => ReturnType<typeof group_createGroup> = (...args) =>+ compatServiceDeps().then((deps) => group_createGroup(deps, ...args));++export const deleteGroup: (+ ...args: Tail<Parameters<typeof group_deleteGroup>>+) => ReturnType<typeof group_deleteGroup> = (...args) =>+ compatServiceDeps().then((deps) => group_deleteGroup(deps, ...args));++export const findGroupsCore: (+ ...args: Tail<Parameters<typeof group_findGroupsCore>>+) => ReturnType<typeof group_findGroupsCore> = (...args) =>+ compatServiceDeps().then((deps) => group_findGroupsCore(deps, ...args));++export const getGroupActivity: (+ ...args: Tail<Parameters<typeof group_getGroupActivity>>+) => ReturnType<typeof group_getGroupActivity> = (...args) =>+ compatServiceDeps().then((deps) => group_getGroupActivity(deps, ...args));++export const getGroupById: (+ ...args: Tail<Parameters<typeof group_getGroupById>>+) => ReturnType<typeof group_getGroupById> = (...args) =>+ compatServiceDeps().then((deps) => group_getGroupById(deps, ...args));++export const getGroupCore: (+ ...args: Tail<Parameters<typeof group_getGroupCore>>+) => ReturnType<typeof group_getGroupCore> = (...args) =>+ compatServiceDeps().then((deps) => group_getGroupCore(deps, ...args));++export const getGroupList: (+ ...args: Tail<Parameters<typeof group_getGroupList>>+) => ReturnType<typeof group_getGroupList> = (...args) =>+ compatServiceDeps().then((deps) => group_getGroupList(deps, ...args));++export const getGroupListCount: (+ ...args: Tail<Parameters<typeof group_getGroupListCount>>+) => ReturnType<typeof group_getGroupListCount> = (...args) =>+ compatServiceDeps().then((deps) => group_getGroupListCount(deps, ...args));++export const getGroupListPage: (+ ...args: Tail<Parameters<typeof group_getGroupListPage>>+) => ReturnType<typeof group_getGroupListPage> = (...args) =>+ compatServiceDeps().then((deps) => group_getGroupListPage(deps, ...args));++export const getGroupMemberGrowth: (+ ...args: Tail<Parameters<typeof group_getGroupMemberGrowth>>+) => ReturnType<typeof group_getGroupMemberGrowth> = (...args) =>+ compatServiceDeps().then((deps) => group_getGroupMemberGrowth(deps, ...args));++export const getGroupMemberProfiles: (+ ...args: Tail<Parameters<typeof group_getGroupMemberProfiles>>+) => ReturnType<typeof group_getGroupMemberProfiles> = (...args) =>+ compatServiceDeps().then((deps) =>+ group_getGroupMemberProfiles(deps, ...args)+ );++export const getGroupMemberProfilesPage: (+ ...args: Tail<Parameters<typeof group_getGroupMemberProfilesPage>>+) => ReturnType<typeof group_getGroupMemberProfilesPage> = (...args) =>+ compatServiceDeps().then((deps) =>+ group_getGroupMemberProfilesPage(deps, ...args)+ );++export const getGroupMetrics: (+ ...args: Tail<Parameters<typeof group_getGroupMetrics>>+) => ReturnType<typeof group_getGroupMetrics> = (...args) =>+ compatServiceDeps().then((deps) => group_getGroupMetrics(deps, ...args));++export const getGroupMostEvents: (+ ...args: Tail<Parameters<typeof group_getGroupMostEvents>>+) => ReturnType<typeof group_getGroupMostEvents> = (...args) =>+ compatServiceDeps().then((deps) => group_getGroupMostEvents(deps, ...args));++export const getGroupPopularRoutes: (+ ...args: Tail<Parameters<typeof group_getGroupPopularRoutes>>+) => ReturnType<typeof group_getGroupPopularRoutes> = (...args) =>+ compatServiceDeps().then((deps) =>+ group_getGroupPopularRoutes(deps, ...args)+ );++export const getGroupPropertyKeys: (+ ...args: Tail<Parameters<typeof group_getGroupPropertyKeys>>+) => ReturnType<typeof group_getGroupPropertyKeys> = (...args) =>+ compatServiceDeps().then((deps) => group_getGroupPropertyKeys(deps, ...args));++export const getGroupStats: (+ ...args: Tail<Parameters<typeof group_getGroupStats>>+) => ReturnType<typeof group_getGroupStats> = (...args) =>+ compatServiceDeps().then((deps) => group_getGroupStats(deps, ...args));++export const getGroupTypes: (+ ...args: Tail<Parameters<typeof group_getGroupTypes>>+) => ReturnType<typeof group_getGroupTypes> = (...args) =>+ compatServiceDeps().then((deps) => group_getGroupTypes(deps, ...args));++export const getGroupsByIds: (+ ...args: Tail<Parameters<typeof group_getGroupsByIds>>+) => ReturnType<typeof group_getGroupsByIds> = (...args) =>+ compatServiceDeps().then((deps) => group_getGroupsByIds(deps, ...args));++export const listGroupTypesCore: (+ ...args: Tail<Parameters<typeof group_listGroupTypesCore>>+) => ReturnType<typeof group_listGroupTypesCore> = (...args) =>+ compatServiceDeps().then((deps) => group_listGroupTypesCore(deps, ...args));++export const updateGroup: (+ ...args: Tail<Parameters<typeof group_updateGroup>>+) => ReturnType<typeof group_updateGroup> = (...args) =>+ compatServiceDeps().then((deps) => group_updateGroup(deps, ...args));++export const upsertGroup: (+ ...args: Tail<Parameters<typeof group_upsertGroup>>+) => ReturnType<typeof group_upsertGroup> = (...args) =>+ compatServiceDeps().then((deps) => group_upsertGroup(deps, ...args));++// --- overview ----------------------------------------------------------------++export const getAnalyticsOverviewCore: (+ ...args: Tail<Parameters<typeof overview_getAnalyticsOverviewCore>>+) => ReturnType<typeof overview_getAnalyticsOverviewCore> = (...args) =>+ compatServiceDeps().then((deps) =>+ overview_getAnalyticsOverviewCore(deps, ...args)+ );++export const getSegmentDailySeriesCore: (+ ...args: Tail<Parameters<typeof overview_getSegmentDailySeriesCore>>+) => ReturnType<typeof overview_getSegmentDailySeriesCore> = (...args) =>+ compatServiceDeps().then((deps) =>+ overview_getSegmentDailySeriesCore(deps, ...args)+ );++export const getTrafficBreakdownCore: (+ ...args: Tail<Parameters<typeof overview_getTrafficBreakdownCore>>+) => ReturnType<typeof overview_getTrafficBreakdownCore> = (...args) =>+ compatServiceDeps().then((deps) =>+ overview_getTrafficBreakdownCore(deps, ...args)+ );++// --- pages -------------------------------------------------------------------++export const getEntryExitPagesCore: (+ ...args: Tail<Parameters<typeof pages_getEntryExitPagesCore>>+) => ReturnType<typeof pages_getEntryExitPagesCore> = (...args) =>+ compatServiceDeps().then((deps) =>+ pages_getEntryExitPagesCore(deps, ...args)+ );++export const getPageConversionsCore: (+ ...args: Tail<Parameters<typeof pages_getPageConversionsCore>>+) => ReturnType<typeof pages_getPageConversionsCore> = (...args) =>+ compatServiceDeps().then((deps) =>+ pages_getPageConversionsCore(deps, ...args)+ );++export const getPagePerformanceCore: (+ ...args: Tail<Parameters<typeof pages_getPagePerformanceCore>>+) => ReturnType<typeof pages_getPagePerformanceCore> = (...args) =>+ compatServiceDeps().then((deps) =>+ pages_getPagePerformanceCore(deps, ...args)+ );++export const getTopPagesCore: (+ ...args: Tail<Parameters<typeof pages_getTopPagesCore>>+) => ReturnType<typeof pages_getTopPagesCore> = (...args) =>+ compatServiceDeps().then((deps) => pages_getTopPagesCore(deps, ...args));++// --- realtime ----------------------------------------------------------------++export const getRealtimeActiveSessions: (+ ...args: Tail<Parameters<typeof realtime_getRealtimeActiveSessions>>+) => ReturnType<typeof realtime_getRealtimeActiveSessions> = (...args) =>+ compatServiceDeps().then((deps) =>+ realtime_getRealtimeActiveSessions(deps, ...args)+ );++export const getRealtimeCoordinates: (+ ...args: Tail<Parameters<typeof realtime_getRealtimeCoordinates>>+) => ReturnType<typeof realtime_getRealtimeCoordinates> = (...args) =>+ compatServiceDeps().then((deps) =>+ realtime_getRealtimeCoordinates(deps, ...args)+ );++export const getRealtimeGeo: (+ ...args: Tail<Parameters<typeof realtime_getRealtimeGeo>>+) => ReturnType<typeof realtime_getRealtimeGeo> = (...args) =>+ compatServiceDeps().then((deps) => realtime_getRealtimeGeo(deps, ...args));++export const getRealtimeMapBadgeDetails: (+ ...args: Tail<Parameters<typeof realtime_getRealtimeMapBadgeDetails>>+) => ReturnType<typeof realtime_getRealtimeMapBadgeDetails> = (...args) =>+ compatServiceDeps().then((deps) =>+ realtime_getRealtimeMapBadgeDetails(deps, ...args)+ );++export const getRealtimePaths: (+ ...args: Tail<Parameters<typeof realtime_getRealtimePaths>>+) => ReturnType<typeof realtime_getRealtimePaths> = (...args) =>+ compatServiceDeps().then((deps) => realtime_getRealtimePaths(deps, ...args));++export const getRealtimeReferrals: (+ ...args: Tail<Parameters<typeof realtime_getRealtimeReferrals>>+) => ReturnType<typeof realtime_getRealtimeReferrals> = (...args) =>+ compatServiceDeps().then((deps) =>+ realtime_getRealtimeReferrals(deps, ...args)+ );++// --- misc --------------------------------------------------------------------++export const getStats: (+ ...args: Tail<Parameters<typeof misc_getStats>>+) => ReturnType<typeof misc_getStats> = (...args) =>+ compatServiceDeps().then((deps) => misc_getStats(deps, ...args));++export const insertPingRecord: (+ ...args: Tail<Parameters<typeof misc_insertPingRecord>>+) => ReturnType<typeof misc_insertPingRecord> = (...args) =>+ compatServiceDeps().then((deps) => misc_insertPingRecord(deps, ...args));++export const runPingCron: (+ ...args: Tail<Parameters<typeof misc_runPingCron>>+) => ReturnType<typeof misc_runPingCron> = (...args) =>+ compatServiceDeps().then((deps) => misc_runPingCron(deps, ...args));++// `packages/trpc`'s overview and event routers call these as OBJECTS+// (`overviewService.getMetrics(...)`, `pagesService.getTopPages(...)`), and+// one of them still does `.bind(overviewService)`. Both were module-singleton+// class instances before M10-005; here they are plain objects of bare+// wrappers, so the call sites read identically and `bind` stays a no-op.+export const overviewService = {+ isPageFilter: (...args: Parameters<Services['overview']['isPageFilter']>) =>+ isPageFilter(...args),+ getRawWhereClause: (+ ...args: Parameters<Services['overview']['getRawWhereClause']>+ ) => getRawWhereClause(...args),+ getMetrics: ((...args) =>+ services().then((container) =>+ container.overview.getMetrics(...args)+ )) as Services['overview']['getMetrics'],+ getTopPages: ((...args) =>+ services().then((container) =>+ container.overview.getTopPages(...args)+ )) as Services['overview']['getTopPages'],+ getTopEntryExit: ((...args) =>+ services().then((container) =>+ container.overview.getTopEntryExit(...args)+ )) as Services['overview']['getTopEntryExit'],+ getTopGeneric: ((...args) =>+ services().then((container) =>+ container.overview.getTopGeneric(...args)+ )) as Services['overview']['getTopGeneric'],+ getTopGenericSeries: ((...args) =>+ services().then((container) =>+ container.overview.getTopGenericSeries(...args)+ )) as Services['overview']['getTopGenericSeries'],+ getUserJourney: ((...args) =>+ services().then((container) =>+ container.overview.getUserJourney(...args)+ )) as Services['overview']['getUserJourney'],+ getTopEvents: ((...args) =>+ services().then((container) =>+ container.overview.getTopEvents(...args)+ )) as Services['overview']['getTopEvents'],+ getTopLinkOut: ((...args) =>+ services().then((container) =>+ container.overview.getTopLinkOut(...args)+ )) as Services['overview']['getTopLinkOut'],+ getMapData: ((...args) =>+ services().then((container) =>+ container.overview.getMapData(...args)+ )) as Services['overview']['getMapData'],+ getLiveData: ((...args) =>+ services().then((container) =>+ container.overview.getLiveData(...args)+ )) as Services['overview']['getLiveData'],+};++export const pagesService = {+ getTopPages: ((...args) =>+ services().then((container) =>+ container.pages.getTopPages(...args)+ )) as Services['pages']['getTopPages'],+ getPageTimeseries: ((...args) =>+ services().then((container) =>+ container.pages.getPageTimeseries(...args)+ )) as Services['pages']['getPageTimeseries'],+};