@@ -4,7 +4,6 @@ import * as schema from "../schema";
44import type { PageServerLoad } from "./$types" ;
55import { dayjs , likesCutoffSql } from "$lib/consts" ;
66import { error , redirect } from "@sveltejs/kit" ;
7- import { getLastWeek } from "$lib/data-processing/weekly" ;
87import { getStatsForDaysInTimePeriod } from "@/data-processing/days" ;
98
109const tz = "America/Argentina/Buenos_Aires" ;
@@ -25,12 +24,15 @@ function getStartingFrom(query: string) {
2524 }
2625}
2726
28- export const load : PageServerLoad = async ( { params , url, setHeaders } ) => {
27+ export const load : PageServerLoad = async ( { url, setHeaders } ) => {
2928 const query =
3029 url . searchParams . get ( "q" ) ?? "date:" + dayjs ( ) . tz ( tz ) . format ( "YYYY-MM-DD" ) ;
3130 const startingFrom = getStartingFrom ( query ) ;
3231 const endsAt = startingFrom . add ( 24 , "hour" ) ;
3332
33+ const heatmapStart = dayjs ( ) . tz ( tz ) . subtract ( 90 , "day" ) . startOf ( "day" ) ;
34+ const heatmapEnd = dayjs ( ) . tz ( tz ) . endOf ( "day" ) ;
35+
3436 const t0 = performance . now ( ) ;
3537 const [
3638 likedTweets ,
@@ -40,6 +42,8 @@ export const load: PageServerLoad = async ({ params, url, setHeaders }) => {
4042 firstLikedTweet ,
4143 monthData ,
4244 hasNextMonth ,
45+ heatLikes ,
46+ heatRetweets ,
4347 ] = await Promise . all ( [
4448 db . query . likedTweets . findMany ( {
4549 columns : {
@@ -112,10 +116,49 @@ export const load: PageServerLoad = async ({ params, url, setHeaders }) => {
112116 startingFrom . add ( 1 , "month" ) . startOf ( "month" ) . toDate ( ) ,
113117 ) ,
114118 } ) ,
119+ db . query . likedTweets . findMany ( {
120+ columns : { firstSeenAt : true } ,
121+ where : and (
122+ gte ( schema . likedTweets . firstSeenAt , heatmapStart . toDate ( ) ) ,
123+ lte ( schema . likedTweets . firstSeenAt , heatmapEnd . toDate ( ) ) ,
124+ likesCutoffSql ,
125+ ) ,
126+ orderBy : desc ( schema . likedTweets . firstSeenAt ) ,
127+ } ) ,
128+ db . query . retweets . findMany ( {
129+ columns : { retweetAt : true } ,
130+ where : and (
131+ gte ( schema . retweets . retweetAt , heatmapStart . toDate ( ) ) ,
132+ lte ( schema . retweets . retweetAt , heatmapEnd . toDate ( ) ) ,
133+ ) ,
134+ orderBy : desc ( schema . retweets . retweetAt ) ,
135+ } ) ,
115136 ] ) ;
116137 const t1 = performance . now ( ) ;
117138 console . log ( "queries" , t1 - t0 ) ;
118139
140+ console . time ( "heatmap" ) ;
141+ const hourHeatmap : number [ ] [ ] = Array . from ( { length : 7 } , ( ) =>
142+ Array ( 24 ) . fill ( 0 ) ,
143+ ) ;
144+ const addToHeat = ( d : Date ) => {
145+ // Assume fixed UTC-3 (America/Argentina/Buenos_Aires without DST)
146+ const utcHour = d . getUTCHours ( ) ;
147+ let hour = utcHour - 3 ;
148+ let dow = d . getUTCDay ( ) ; // 0 = Sunday
149+ if ( hour < 0 ) {
150+ hour += 24 ;
151+ dow = ( dow + 6 ) % 7 ; // previous day
152+ } else if ( hour >= 24 ) {
153+ hour -= 24 ;
154+ dow = ( dow + 1 ) % 7 ; // next day (not expected with -3 but kept generic)
155+ }
156+ hourHeatmap [ dow ] [ hour ] ++ ;
157+ } ;
158+ heatLikes . forEach ( ( t : { firstSeenAt : Date } ) => addToHeat ( t . firstSeenAt ) ) ;
159+ heatRetweets . forEach ( ( t : { retweetAt : Date } ) => addToHeat ( t . retweetAt ) ) ;
160+ console . timeEnd ( "heatmap" ) ;
161+
119162 if (
120163 likedTweets . length === 0 &&
121164 retweets . length === 0 &&
@@ -145,5 +188,6 @@ export const load: PageServerLoad = async ({ params, url, setHeaders }) => {
145188 monthData,
146189 hasNextMonth : ! ! hasNextMonth ,
147190 query,
191+ hourHeatmap,
148192 } ;
149193} ;
0 commit comments