( websiteId: string, filters: WebsiteQueryFilters )
| 723 | } |
| 724 | |
| 725 | export async function getWorkspaceWebsiteStats( |
| 726 | websiteId: string, |
| 727 | filters: WebsiteQueryFilters |
| 728 | ): Promise<any> { |
| 729 | const { filterQuery, joinSession, params } = await parseWebsiteFilters( |
| 730 | websiteId, |
| 731 | { |
| 732 | ...filters, |
| 733 | } |
| 734 | ); |
| 735 | |
| 736 | // Prefer ClickHouse when enabled and healthy; translate filterQuery for CH; require no session join |
| 737 | if ( |
| 738 | env.clickhouse.enable && |
| 739 | clickhouseHealthManager.isClickHouseHealthy() && |
| 740 | joinSession === Prisma.empty |
| 741 | ) { |
| 742 | try { |
| 743 | const chFilter = unwrapSQL(filterQuery) |
| 744 | .replace(/\"WebsiteEvent\"\./g, '') |
| 745 | .replace(/\"WebsiteSession\"\./g, '') |
| 746 | .replace(/\"Website\"\./g, '') |
| 747 | .replace(/\"/g, '') |
| 748 | .replace(/::[a-zA-Z]+/g, ''); |
| 749 | |
| 750 | const chQuery = ` |
| 751 | select |
| 752 | sum(t.c) as pageviews, |
| 753 | uniqExact(t.sessionId) as uniques, |
| 754 | sum(if(t.c = 1, 1, 0)) as bounces, |
| 755 | sum(t.time) as totaltime |
| 756 | from ( |
| 757 | select |
| 758 | sessionId, |
| 759 | toStartOfHour(createdAt) as bucket, |
| 760 | count(*) as c, |
| 761 | dateDiff('second', min(createdAt), max(createdAt)) as time |
| 762 | from WebsiteEvent |
| 763 | where websiteId = {websiteId:String} |
| 764 | and createdAt between toDateTime({start:String}, 'UTC') and toDateTime({end:String}, 'UTC') |
| 765 | and eventType = {eventType:UInt64} |
| 766 | ${chFilter} |
| 767 | group by sessionId, bucket |
| 768 | ) as t |
| 769 | `; |
| 770 | |
| 771 | const result = await clickhouse.query({ |
| 772 | query: chQuery, |
| 773 | query_params: { |
| 774 | websiteId: params.websiteId, |
| 775 | start: dayjs(params.startDate).utc().format('YYYY-MM-DD HH:mm:ss'), |
| 776 | end: dayjs(params.endDate).utc().format('YYYY-MM-DD HH:mm:ss'), |
| 777 | eventType: EVENT_TYPE.pageView, |
| 778 | }, |
| 779 | }); |
| 780 | const json = await result.json<any>(); |
| 781 | const rows = (json?.data ?? []) as { |
| 782 | pageviews: number; |
no test coverage detected