MCPcopy
hub / github.com/msgbyte/tianji / getWorkspaceWebsiteStats

Function getWorkspaceWebsiteStats

src/server/model/website/index.ts:725–839  ·  view source on GitHub ↗
(
  websiteId: string,
  filters: WebsiteQueryFilters
)

Source from the content-addressed store, hash-verified

723}
724
725export 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;

Callers 2

getWebsitePublicStatsFunction · 0.85
website.tsFile · 0.85

Calls 9

parseWebsiteFiltersFunction · 0.85
unwrapSQLFunction · 0.85
isClickHouseHealthyMethod · 0.80
warnMethod · 0.80
catchMethod · 0.80
forceHealthCheckMethod · 0.80
replaceMethod · 0.65
getDateQueryFunction · 0.50

Tested by

no test coverage detected