@@ -196,29 +196,24 @@ export async function getServerSideProps({ req, res, query }) {
196196
197197 console . log ( `Fetching stats for page ${ page_id } from ${ date . toISOString ( ) } ` ) ;
198198
199- // Get current period stats
200- const {
201- // @ts -ignore
202- data : { page_views, visitors } ,
203- } = await supabaseAdmin
204- . rpc ( "page_view_stats" , {
205- pageid : String ( page_id ) ,
206- date : date . toISOString ( ) ,
207- } )
208- . maybeSingle ( )
209- . throwOnError ( ) ;
210-
211- // Get previous period stats for comparison
212- const {
213- // @ts -ignore
214- data : { page_views : prev_page_views , visitors : prev_visitors } ,
215- } = await supabaseAdmin
216- . rpc ( "page_view_stats" , {
217- pageid : String ( page_id ) ,
218- date : prevDate . toISOString ( ) ,
219- } )
220- . maybeSingle ( )
221- . throwOnError ( ) ;
199+ const { data : allPageViews } = await supabaseAdmin
200+ . from ( "page_views" )
201+ . select ( "created_at, visitor_id" )
202+ . eq ( "page_id" , page_id )
203+ . gte ( "created_at" , prevDate . toISOString ( ) ) ;
204+
205+ const currentPeriodViews = allPageViews ?. filter (
206+ ( view ) => new Date ( view . created_at ) >= date
207+ ) || [ ] ;
208+
209+ const previousPeriodViews = allPageViews ?. filter (
210+ ( view ) => new Date ( view . created_at ) >= prevDate && new Date ( view . created_at ) < date
211+ ) || [ ] ;
212+
213+ const page_views = currentPeriodViews . length ;
214+ const visitors = new Set ( currentPeriodViews . map ( v => v . visitor_id ) ) . size ;
215+ const prev_page_views = previousPeriodViews . length ;
216+ const prev_visitors = new Set ( previousPeriodViews . map ( v => v . visitor_id ) ) . size ;
222217
223218 // Calculate growth rates
224219 const pageViewsGrowth =
0 commit comments