Add faker endpoint for local dev
This commit is contained in:
@ -5,8 +5,19 @@ 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`)
|
||||
@ -77,9 +88,12 @@ export default class ReplaysController {
|
||||
queryString = `?start=${start}&end=${end}`
|
||||
}
|
||||
const queryFilter = env.get('QUERY_FILTER')
|
||||
await fetchBatch(
|
||||
`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)}`
|
||||
)
|
||||
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()
|
||||
|
||||
@ -119,3 +133,63 @@ function buildPaginationLinks(meta: {
|
||||
|
||||
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(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user