( websiteId: string, column: string, filters: WebsiteQueryFilters )
| 575 | } |
| 576 | |
| 577 | export async function getWebsitePageviewMetrics( |
| 578 | websiteId: string, |
| 579 | column: string, |
| 580 | filters: WebsiteQueryFilters |
| 581 | ): Promise<{ x: string; y: number }[]> { |
| 582 | const eventType = |
| 583 | column === 'eventName' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView; |
| 584 | const { filterQuery, joinSession, params } = await parseWebsiteFilters( |
| 585 | websiteId, |
| 586 | { |
| 587 | ...filters, |
| 588 | }, |
| 589 | { joinSession: SESSION_COLUMNS.includes(column) } |
| 590 | ); |
| 591 | |
| 592 | let excludeDomain = Prisma.empty; |
| 593 | if (column === 'referrerDomain') { |
| 594 | excludeDomain = Prisma.sql`and ("WebsiteEvent"."referrerDomain" != ${params.websiteDomain} or "WebsiteEvent"."referrerDomain" is null)`; |
| 595 | } |
| 596 | |
| 597 | // ClickHouse fast path: only on WebsiteEvent without session join |
| 598 | if (env.clickhouse.enable && clickhouseHealthManager.isClickHouseHealthy()) { |
| 599 | try { |
| 600 | const chFilter = unwrapSQL(filterQuery) |
| 601 | .replace(/\"WebsiteEvent\"\./g, '') |
| 602 | .replace(/\"WebsiteSession\"\./g, '') |
| 603 | .replace(/\"/g, '') |
| 604 | .replace(/::[a-zA-Z]+/g, ''); |
| 605 | |
| 606 | const chExclude = |
| 607 | column === 'referrerDomain' |
| 608 | ? `and (referrerDomain != {websiteDomain:String} or referrerDomain is null)` |
| 609 | : ''; |
| 610 | |
| 611 | const chJoin = |
| 612 | joinSession === Prisma.empty |
| 613 | ? '' |
| 614 | : 'INNER JOIN WebsiteSession ON WebsiteEvent.sessionId = WebsiteSession.id'; |
| 615 | |
| 616 | const chQuery = ` |
| 617 | select ${column} as x, count(*) as y |
| 618 | from WebsiteEvent |
| 619 | ${chJoin} |
| 620 | where websiteId = {websiteId:String} |
| 621 | and createdAt between toDateTime({start:String}, 'UTC') and toDateTime({end:String}, 'UTC') |
| 622 | and eventType = {eventType:UInt64} |
| 623 | ${chExclude} |
| 624 | ${chFilter} |
| 625 | group by ${column} |
| 626 | order by y desc |
| 627 | limit 100 |
| 628 | `; |
| 629 | |
| 630 | const result = await clickhouse.query({ |
| 631 | query: chQuery, |
| 632 | query_params: { |
| 633 | websiteId, |
| 634 | websiteDomain: params.websiteDomain, |
no test coverage detected