Created
April 16, 2023 01:32
-
-
Save vorpus/29eaf40c23a612b825ab33d1e9c15a00 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
url: '/render-maddening-month/excuse-shy-request', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515043737792 | |
}, | |
{ | |
url: '/study-boorish-time', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515046287916 | |
}, | |
{ | |
url: '/enlist-swanky-hand', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515046878683 | |
}, | |
{ | |
url: '/sneeze-rainy-protest', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515048652158 | |
}, | |
{ | |
url: '/purchase-productive-car/freeze-overrated-leaf', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515048698794 | |
}, | |
{ | |
url: '/misunderstand-receptive-aunt/say-rural-dad', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515049047113 | |
}, | |
{ | |
url: '/help-nauseating-copper/suspend-truculent-morning', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515049771888 | |
}, | |
{ | |
url: '/operate-square-cover/handle-encouraging-drug', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515050182968 | |
}, | |
{ | |
url: '/study-boorish-time', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515054148273 | |
}, | |
{ | |
url: '/stand-heartbreaking-brass/mug-envious-library', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515056612539 | |
}, | |
{ | |
url: '/thrive-defective-cellar', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515057960326 | |
}, | |
{ | |
url: '/misunderstand-receptive-aunt/say-rural-dad', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515058895386 | |
}, | |
{ | |
url: '/cover-plain-animal/inject-difficult-produce', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515059057946 | |
}, | |
{ | |
url: '/stand-heartbreaking-brass/mug-envious-library', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515059206546 | |
}, | |
{ | |
url: '/bust-sassy-parcel', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515059994386 | |
}, | |
{ | |
url: '/roll-abnormal-river/spare-ugliest-boy', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515061193862 | |
}, | |
{ | |
url: '/fix-devilish-son/mentor-fair-sofa', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515063854086 | |
}, | |
{ | |
url: '/locate-abrupt-shame/navigate-seemly-basket', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515068561776 | |
}, | |
{ | |
url: '/kick-tested-stone/freeze-glorious-birthday', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515071744924 | |
}, | |
{ | |
url: '/thrive-defective-cellar', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515074492233 | |
}, | |
{ | |
url: '/study-boorish-time', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515074937635 | |
}, | |
{ | |
url: '/render-maddening-month/excuse-shy-request', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515075747581 | |
}, | |
{ | |
url: '/search-daffy-chicken', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515076721982 | |
}, | |
{ | |
url: '/bust-sassy-parcel', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515077645437 | |
}, | |
{ | |
url: '/cover-plain-animal/inject-difficult-produce', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515077955419 | |
}, | |
{ | |
url: '/operate-square-cover/handle-encouraging-drug', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515080294419 | |
}, | |
{ | |
url: '/stain-light-quiet/delay-precious-arithmetic', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515081309678 | |
}, | |
{ | |
url: '/render-maddening-month/excuse-shy-request', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515081710485 | |
}, | |
{ | |
url: '/handwrite-aloof-vegetable', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515082160475 | |
}, | |
{ | |
url: '/sneeze-rainy-protest', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515082531037 | |
}, | |
{ | |
url: '/roll-abnormal-river/spare-ugliest-boy', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515083650379 | |
}, | |
{ | |
url: '/blush-tough-friction', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515083999665 | |
}, | |
{ | |
url: '/help-nauseating-copper/suspend-truculent-morning', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515086883380 | |
}, | |
{ | |
url: '/handwrite-aloof-vegetable', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515088147294 | |
}, | |
{ | |
url: '/reply-dapper-show/deliver-entertaining-fight', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515088149579 | |
}, | |
{ | |
url: '/misunderstand-receptive-aunt/say-rural-dad', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515088949459 | |
}, | |
{ | |
url: '/study-boorish-time', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515089727127 | |
}, | |
{ | |
url: '/handwrite-aloof-vegetable', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515090653298 | |
}, | |
{ | |
url: '/kick-tested-stone/freeze-glorious-birthday', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515091257008 | |
}, | |
{ | |
url: '/stand-heartbreaking-brass/mug-envious-library', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515091416528 | |
}, | |
{ | |
url: '/purchase-productive-car/freeze-overrated-leaf', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515091747067 | |
}, | |
{ | |
url: '/acquire-petite-sun/split-aback-river', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515092449256 | |
}, | |
{ | |
url: '/kick-tested-stone/freeze-glorious-birthday', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515093255594 | |
}, | |
{ | |
url: '/swell-jolly-beast/beam-heavy-ladybug', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515094071845 | |
}, | |
{ | |
url: '/acquire-petite-sun/split-aback-river', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515094369571 | |
}, | |
{ | |
url: '/swell-jolly-beast/beam-heavy-ladybug', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515095668264 | |
}, | |
{ | |
url: '/acquire-petite-sun/split-aback-river', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515097605358 | |
}, | |
{ | |
url: '/conduct-idiotic-grain/shine-jolly-activity', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515097735042 | |
}, | |
{ | |
url: '/handwrite-aloof-vegetable', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515098280584 | |
}, | |
{ | |
url: '/fix-devilish-son/mentor-fair-sofa', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515098804442 | |
}, | |
{ | |
url: '/represent-alleged-gold', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515098840145 | |
}, | |
{ | |
url: '/fix-devilish-son/mentor-fair-sofa', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515100320785 | |
}, | |
{ | |
url: '/conduct-idiotic-grain/shine-jolly-activity', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515105006146 | |
}, | |
{ | |
url: '/conduct-idiotic-grain/shine-jolly-activity', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515106055448 | |
}, | |
{ | |
url: '/swell-jolly-beast/beam-heavy-ladybug', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515106226001 | |
}, | |
{ | |
url: '/intensify-damaging-umbrella', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515109408299 | |
}, | |
{ | |
url: '/split-nasty-bottle/groan-exclusive-taste', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515109428036 | |
}, | |
{ | |
url: '/search-daffy-chicken', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515111149362 | |
}, | |
{ | |
url: '/thrive-defective-cellar', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515112274074 | |
}, | |
{ | |
url: '/conduct-idiotic-grain/shine-jolly-activity', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515114045590 | |
}, | |
{ | |
url: '/hurry-nauseating-hall/hunt-zany-laborer', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515115454989 | |
}, | |
{ | |
url: '/locate-abrupt-shame/navigate-seemly-basket', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515115568475 | |
}, | |
{ | |
url: '/stand-heartbreaking-brass/mug-envious-library', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515116575496 | |
}, | |
{ | |
url: '/like-economic-horse/bite-ad hoc-holiday', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515119325808 | |
}, | |
{ | |
url: '/thrive-defective-cellar', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515119337443 | |
}, | |
{ | |
url: '/study-boorish-time', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515125479348 | |
}, | |
{ | |
url: '/hurry-nauseating-hall/hunt-zany-laborer', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515127065357 | |
}, | |
{ | |
url: '/roll-abnormal-river/spare-ugliest-boy', | |
visitorId: 'edb4de8d-b28b-3b75-b7bb-ed4bb599ec11', | |
timestamp: 1515127154917 | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { EVENT_DATA_API, SUBMIT_API } from './const/endpoints' | |
const fetchFromApi = async () => { | |
try { | |
const response = await fetch(EVENT_DATA_API); | |
const body = await response.json(); | |
return body; | |
} catch (error) { | |
console.error(error); | |
} | |
} | |
const postToApi = async (data: any) => { | |
const postResponse = await fetch(SUBMIT_API, { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json' | |
}, | |
body: JSON.stringify(data) | |
}) | |
return postResponse.json() | |
} | |
interface Event { | |
url: string; | |
visitorId: string; | |
timestamp: number; | |
} | |
interface Session { | |
duration: number; | |
pages: string[]; | |
startTime: number; | |
} | |
interface UserSession { | |
[key: string]: [Session]; | |
} | |
const getSessions = (events: [Event]) => { | |
const sessions: any = [] | |
// sort events by timestamp | |
events.sort((a: Event, b: Event) => a.timestamp - b.timestamp) | |
// console.log(events) | |
let sessionPages: any = [] | |
for (let i = 0; i < events.length; i++) { | |
if (sessionPages.length === 0) { | |
sessionPages.push(events[i]); | |
} else if (events[i].timestamp - sessionPages[sessionPages.length - 1].timestamp > 1000*60*10) { | |
sessions.push({ | |
duration: sessionPages[sessionPages.length-1].timestamp - sessionPages[0].timestamp, | |
pages: sessionPages.map((session: any) => session.url), | |
startTime: sessionPages[0].timestamp | |
}) | |
sessionPages = [events[i]] | |
} else { | |
sessionPages.push(events[i]) | |
} | |
} | |
if (sessionPages.length) { | |
sessions.push({ | |
duration: 0, | |
pages: [sessionPages[0].url], | |
startTime: sessionPages[0].timestamp | |
}) | |
} | |
// console.log(sessions) | |
return sessions | |
// return sessions | |
} | |
const main = async () => { | |
const {events} = await fetchFromApi(); | |
// partition events by visitorId | |
const eventsByVisitorId: { [key: string]: [Event] } = {} | |
events.forEach((event: Event) => { | |
if (eventsByVisitorId[event.visitorId]) { | |
eventsByVisitorId[event.visitorId].push(event) | |
} else { | |
eventsByVisitorId[event.visitorId] = [event] | |
} | |
}) | |
// console.log(eventsByVisitorId) | |
console.log(eventsByVisitorId['edb4de8d-b28b-3b75-b7bb-ed4bb599ec11']) | |
const sessionsById:any = {} | |
// for each key in eventsByVisitorId, sort the events by timestamp | |
// for (const visitorId in eventsByVisitorId) { | |
// sessionsById[visitorId] = getSessions(eventsByVisitorId[visitorId]) | |
// } | |
// console.log(getSessions(eventsByVisitorId['edb4de8d-b28b-3b75-b7bb-ed4bb599ec11'])) | |
// console.log(sessionsById['edb4de8d-b28b-3b75-b7bb-ed4bb599ec11']) | |
// console.log(sessionsById) | |
// const postResponse = await postToApi(sessionsById) | |
// console.log(postResponse); | |
// console.log(sessionsById) | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment