196 lines
6.2 KiB
TypeScript
196 lines
6.2 KiB
TypeScript
import Replay from '#models/replay'
|
|
import env from '#start/env'
|
|
import type { HttpContext } from '@adonisjs/core/http'
|
|
const SENTRY_ORG = env.get('SENTRY_ORG')
|
|
import redis from '@adonisjs/redis/services/main'
|
|
import { fetchBatch } from '../Helpers/Replays.js'
|
|
import { sendDataToWebhook } from '../Helpers/Webhook.js'
|
|
import { faker } from '@faker-js/faker'
|
|
|
|
export default class ReplaysController {
|
|
public async faker({ request, response }: HttpContext) {
|
|
const { page } = await request.qs()
|
|
const sessions = Array.from({ length: 100 }, generateFakeSession)
|
|
const nextPage = +page + 1
|
|
await response.safeHeader(
|
|
'link',
|
|
`<http://localhost:3333/faker/?page=${page}>; rel="previous"; results="true"; cursor="0:1100:1", <http://localhost:3333/faker/?page=${nextPage}>; rel="next"; results="${page == 10 ? 'false' : 'true'}"; cursor="0:${page * 100}:0"`
|
|
)
|
|
return { data: sessions, count: sessions.length, page: page }
|
|
}
|
|
public async stats({ request, response }: HttpContext) {
|
|
const { sendToWebhook } = request.qs()
|
|
const latestVersion = await redis.get(`replays:stats:latest_version`)
|
|
let results
|
|
if (!latestVersion) {
|
|
console.log('Cache miss')
|
|
results = await Replay.updateReplayStats()
|
|
} else {
|
|
console.log('cache hit')
|
|
let data = await redis.get(`replays:stats:version:${latestVersion}:results`)
|
|
if (data) {
|
|
results = JSON.parse(data)
|
|
}
|
|
}
|
|
|
|
let responseData = {
|
|
version: results.version,
|
|
updatedAt: results.updatedAt,
|
|
numberOfRecords: results.rows.length,
|
|
data: results.rows,
|
|
}
|
|
if (sendToWebhook) {
|
|
await sendDataToWebhook(responseData)
|
|
}
|
|
return response.json(responseData)
|
|
}
|
|
|
|
public async home({ request, inertia }: HttpContext) {
|
|
const page = request.input('page', 1)
|
|
const perPage = 20
|
|
const cacheKey = `replays:page:${page}`
|
|
|
|
let data = await redis.get(cacheKey)
|
|
let paginated, meta, replays
|
|
|
|
if (data) {
|
|
;({ paginated, meta, replays } = JSON.parse(data))
|
|
} else {
|
|
paginated = await Replay.query().paginate(page, perPage)
|
|
paginated.baseUrl('/')
|
|
|
|
const json = paginated.toJSON()
|
|
|
|
meta = {
|
|
...json.meta,
|
|
links: buildPaginationLinks(json.meta),
|
|
}
|
|
|
|
replays = json.data
|
|
|
|
await redis.set(cacheKey, JSON.stringify({ paginated, meta, replays }), 'EX', 60)
|
|
}
|
|
|
|
return inertia.render('Replays/Index', {
|
|
data: {
|
|
replays,
|
|
meta,
|
|
},
|
|
})
|
|
}
|
|
|
|
async index({ request, response }: HttpContext) {
|
|
const { statsPeriod, start, end } = request.qs()
|
|
let queryString: string = '?statsPeriod=24h' // Default in case none is provided
|
|
if (statsPeriod) {
|
|
queryString = `?statsPeriod=${statsPeriod}`
|
|
} else if (start && end) {
|
|
queryString = `?start=${start}&end=${end}`
|
|
}
|
|
const queryFilter = env.get('QUERY_FILTER')
|
|
const baseUrl =
|
|
env.get('NODE_ENV') == 'production'
|
|
? `https://sentry.io/api/0/organizations/${SENTRY_ORG}/replays/${queryString}&field=id&field=user&field=duration&field=started_at&field=finished_at&query=${encodeURIComponent(queryFilter)}`
|
|
: 'http://localhost:3333/faker?page=1'
|
|
console.log('base', baseUrl)
|
|
await fetchBatch(baseUrl)
|
|
|
|
let queryResults = await Replay.updateReplayStats()
|
|
|
|
return response.json({ version: queryResults.latestVersion, ...queryResults })
|
|
}
|
|
}
|
|
|
|
function buildPaginationLinks(meta: {
|
|
previousPageUrl: string
|
|
lastPage: number
|
|
currentPage: number
|
|
nextPageUrl: string
|
|
}) {
|
|
const links = []
|
|
|
|
// Previous
|
|
links.push({
|
|
url: meta.previousPageUrl,
|
|
label: '« Prev',
|
|
active: false,
|
|
})
|
|
|
|
for (let page = 1; page <= meta.lastPage; page++) {
|
|
links.push({
|
|
url: `/?page=${page}`,
|
|
label: page.toString(),
|
|
active: page === meta.currentPage,
|
|
})
|
|
}
|
|
|
|
// Next
|
|
links.push({
|
|
url: meta.nextPageUrl,
|
|
label: 'Next »',
|
|
active: false,
|
|
})
|
|
|
|
return links
|
|
}
|
|
|
|
function generateFakeSession() {
|
|
const uuid = faker.string.uuid()
|
|
const browserName = faker.helpers.arrayElement(['Chrome', 'Firefox', 'Safari', 'Edge', 'Brave'])
|
|
const deviceBrand = faker.helpers.arrayElement(['Apple', 'Samsung', 'Google'])
|
|
const osName = faker.helpers.arrayElement(['iOS', 'Android', 'Windows', 'macOS'])
|
|
const platform = faker.helpers.arrayElement(['Sentry', 'Datadog', 'New Relic', 'Rollbar'])
|
|
const finishedAt = new Date(Date.now() - faker.number.int({ min: 0, max: 60 * 60 * 1000 }))
|
|
const displayName = faker.internet.email()
|
|
return {
|
|
activity: faker.number.int({ min: 1, max: 10 }),
|
|
browser: {
|
|
name: browserName,
|
|
version: faker.system.semver(),
|
|
},
|
|
count_dead_clicks: faker.number.int({ min: 0, max: 10 }),
|
|
count_rage_clicks: faker.number.int({ min: 0, max: 5 }),
|
|
count_errors: faker.number.int({ min: 0, max: 5 }),
|
|
count_segments: faker.number.int({ min: 0, max: 3 }),
|
|
count_urls: faker.number.int({ min: 1, max: 3 }),
|
|
device: {
|
|
brand: deviceBrand,
|
|
family: deviceBrand === 'Apple' ? 'iPhone' : deviceBrand,
|
|
model: faker.string.numeric({ length: 2 }),
|
|
name: `${deviceBrand} ${faker.string.alphanumeric({ length: 3 })}`,
|
|
},
|
|
dist: null,
|
|
duration: faker.number.int({ min: 100, max: 1000 }),
|
|
environment: faker.helpers.arrayElement(['production', 'staging', 'development']),
|
|
error_ids: [uuid],
|
|
finished_at: faker.date.between({ from: finishedAt, to: new Date() }).toISOString(),
|
|
has_viewed: faker.datatype.boolean(),
|
|
id: uuid,
|
|
is_archived: faker.datatype.boolean() ? null : false,
|
|
os: {
|
|
name: osName,
|
|
version: `${faker.number.int({ min: 10, max: 17 })}.${faker.number.int({ min: 0, max: 5 })}`,
|
|
},
|
|
platform: platform,
|
|
project_id: faker.string.numeric({ length: 6 }),
|
|
releases: [`version@${faker.system.semver()}`],
|
|
sdk: {
|
|
name: faker.hacker.noun(),
|
|
version: faker.system.semver(),
|
|
},
|
|
started_at: faker.date.recent().toISOString(),
|
|
tags: {
|
|
hello: ['world', faker.person.fullName()],
|
|
},
|
|
trace_ids: [uuid],
|
|
urls: [faker.internet.url()],
|
|
user: {
|
|
display_name: displayName,
|
|
email: displayName,
|
|
id: faker.string.numeric({ length: 8 }),
|
|
ip: faker.internet.ip(),
|
|
username: faker.internet.username(),
|
|
},
|
|
}
|
|
}
|