data endpoint for entity 90008 (aka. a website)
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor: make each visit timestamped separately so we can filter them out properly

dusk 78d7752f 8bc6d136

+14 -7
+13 -6
src/lib/visits.ts
··· 8 8 const visitCountFile = `${env.WEBSITE_DATA_DIR}/visitcount` 9 9 const visitCount = writable(parseInt(existsSync(visitCountFile) ? readFileSync(visitCountFile).toString() : '0')) 10 10 11 - type Visitor = { count: number, since: number } 11 + type Visitor = { visits: number[] } 12 12 const lastVisitors = writable<Map<string, Visitor>>(new Map()) 13 13 const VISITOR_EXPIRY_SECONDS = 60 * 60 * 1 14 14 ··· 47 47 // filter out old entries 48 48 visitors = new Map( 49 49 visitors.entries().filter( 50 - ([_, value]) => 51 - { return currentTime - value.since < 1000 * VISITOR_EXPIRY_SECONDS } 50 + ([_, visitor]) => 51 + { return currentTime - visitor.visits[0] < 1000 * VISITOR_EXPIRY_SECONDS } 52 + ).map( 53 + ([id, visitor]) => { 54 + visitor.visits = visitor.visits.filter((since) => { 55 + return currentTime - since < 1000 * VISITOR_EXPIRY_SECONDS 56 + }) 57 + return [id, visitor] 58 + } 52 59 ) 53 60 ) 54 61 // check whether the request is from a bot or not (this doesnt need to be accurate we just want to filter out honest bots) ··· 63 70 console.log(`new client visitor id ${visitorId}`) 64 71 } 65 72 // update the entry 66 - let visitorEntry = visitors.get(visitorId) || {count: 0, since: 0} 67 - visitorEntry.count += 1 68 - visitorEntry.since = currentTime 73 + let visitorEntry = visitors.get(visitorId) || {visits: []} 74 + // put new visit in the front 75 + visitorEntry.visits = [currentTime].concat(visitorEntry.visits) 69 76 visitors.set(visitorId, visitorEntry); 70 77 return visitors 71 78 }
+1 -1
src/routes/+layout.svelte
··· 43 43 $: title = getTitle(data.route); 44 44 45 45 $: recentVisitCount = data.lastVisitors.values().reduce( 46 - (total, visitor) => { return total + visitor.count; }, 0 46 + (total, visitor) => { return total + visitor.visits.length; }, 0 47 47 ) 48 48 49 49 const svgSquiggles = [[2], [3], [2], [3], [1]];