Reduce controller logic scope
This commit is contained in:
50
app/Helpers/Replays.ts
Normal file
50
app/Helpers/Replays.ts
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
import Replay from '#models/replay'
|
||||||
|
import {parseSentryLinkHeader, SentryPagination} from './Sentry.js'
|
||||||
|
|
||||||
|
import env from '#start/env'
|
||||||
|
let recordsUpdated = 0
|
||||||
|
const SENTRY_TOKEN = env.get('SENTRY_TOKEN')
|
||||||
|
interface ApiResponse<T> {
|
||||||
|
data: T;
|
||||||
|
// optionally, you can define `meta`, `errors`, etc. if your API returns them
|
||||||
|
}
|
||||||
|
export async function fetchBatch(url: string) {
|
||||||
|
const options: RequestInit = {
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${SENTRY_TOKEN}`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const req = await fetch(url, options)
|
||||||
|
if (!req.ok) {
|
||||||
|
throw new Error(`Request failed with status ${req.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const resp = await req.json() as ApiResponse<Replay[]>;
|
||||||
|
const replays = resp.data;
|
||||||
|
const headers = req.headers
|
||||||
|
|
||||||
|
const cleanedData = replays.map(record => sanitizeInput(record, Replay.allowedFields))
|
||||||
|
|
||||||
|
let updated = await Replay.updateOrCreateMany('id', cleanedData)
|
||||||
|
recordsUpdated = recordsUpdated + updated.length
|
||||||
|
const linkHeader = headers.get('link')
|
||||||
|
if (!linkHeader) {
|
||||||
|
return { error: 'link header missing from Sentry API response' }
|
||||||
|
}
|
||||||
|
const pagination: SentryPagination = parseSentryLinkHeader(linkHeader)
|
||||||
|
|
||||||
|
if (pagination.hasNextResults == true) {
|
||||||
|
console.log('fetching', pagination.next)
|
||||||
|
await fetchBatch(pagination.next)
|
||||||
|
}
|
||||||
|
console.log('no more results')
|
||||||
|
return { recordsUpdated }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function sanitizeInput(data: Record<string, any>, allowedFields: string[]) {
|
||||||
|
return allowedFields.reduce((acc, key) => {
|
||||||
|
if (key in data) acc[key] = data[key]
|
||||||
|
return acc
|
||||||
|
}, {} as Record<string, any>)
|
||||||
|
}
|
28
app/Helpers/Sentry.ts
Normal file
28
app/Helpers/Sentry.ts
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
|
||||||
|
export interface SentryPagination {
|
||||||
|
previous: string;
|
||||||
|
hasPreviousResults: boolean;
|
||||||
|
hasNextResults: boolean;
|
||||||
|
next: string
|
||||||
|
}
|
||||||
|
export function parseSentryLinkHeader(header: string): SentryPagination {
|
||||||
|
const links = header.split(',').map(part => part.trim())
|
||||||
|
|
||||||
|
let result = {} as SentryPagination
|
||||||
|
for (const link of links) {
|
||||||
|
const match = link.match(/<([^>]+)>;\s*rel="([^"]+)";\s*results="([^"]+)";\s*cursor="([^"]+)"/)
|
||||||
|
if (!match) continue
|
||||||
|
|
||||||
|
const [, url, rel, results] = match
|
||||||
|
|
||||||
|
if (rel === 'previous') {
|
||||||
|
result.previous = url
|
||||||
|
result.hasPreviousResults = results === 'true'
|
||||||
|
} else if (rel === 'next') {
|
||||||
|
result.next = url
|
||||||
|
result.hasNextResults = results === 'true'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
19
app/Helpers/Webhook.ts
Normal file
19
app/Helpers/Webhook.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import env from '#start/env'
|
||||||
|
|
||||||
|
export async function sendDataToWebhook(responseData:{ version: number, updatedAt: Date, numberOfRecords: number, data: unknown}) {
|
||||||
|
try {
|
||||||
|
console.log('syncing to webhook')
|
||||||
|
await fetch(env.get('WEBHOOK_URL'),
|
||||||
|
{
|
||||||
|
headers:
|
||||||
|
{
|
||||||
|
'content-type': 'application/json'
|
||||||
|
},
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify(responseData)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
} catch (e) {
|
||||||
|
console.error('error sending webhook data', e)
|
||||||
|
}
|
||||||
|
}
|
@ -1,62 +1,34 @@
|
|||||||
import Replay from '#models/replay'
|
import Replay from '#models/replay'
|
||||||
import env from '#start/env'
|
import env from '#start/env'
|
||||||
import type { HttpContext } from '@adonisjs/core/http'
|
import type { HttpContext } from '@adonisjs/core/http'
|
||||||
import db from '@adonisjs/lucid/services/db'
|
|
||||||
const SENTRY_TOKEN = env.get('SENTRY_TOKEN')
|
|
||||||
const SENTRY_ORG = env.get('SENTRY_ORG')
|
const SENTRY_ORG = env.get('SENTRY_ORG')
|
||||||
let recordsUpdated = 0
|
|
||||||
import redis from '@adonisjs/redis/services/main'
|
import redis from '@adonisjs/redis/services/main'
|
||||||
|
import { fetchBatch } from '../Helpers/Replays.js'
|
||||||
|
import { sendDataToWebhook } from '../Helpers/Webhook.js'
|
||||||
|
|
||||||
|
|
||||||
interface ApiResponse<T> {
|
|
||||||
data: T;
|
|
||||||
// optionally, you can define `meta`, `errors`, etc. if your API returns them
|
|
||||||
}
|
|
||||||
|
|
||||||
interface SentryPagination {
|
|
||||||
previous: string;
|
|
||||||
hasPreviousResults: boolean;
|
|
||||||
hasNextResults: boolean;
|
|
||||||
next: string
|
|
||||||
}
|
|
||||||
export default class ReplaysController {
|
export default class ReplaysController {
|
||||||
|
|
||||||
public async stats({ request, response }: HttpContext) {
|
public async stats({ request, response }: HttpContext) {
|
||||||
const {sendToWebhook} = request.qs()
|
const { sendToWebhook } = request.qs()
|
||||||
const latestVersion = await redis.get(`replays:stats:latest_version`)
|
const latestVersion = await redis.get(`replays:stats:latest_version`)
|
||||||
|
let results
|
||||||
if (!latestVersion) {
|
if (!latestVersion) {
|
||||||
//
|
|
||||||
console.log('Cache miss')
|
console.log('Cache miss')
|
||||||
const queryResults = await Replay.updateReplayStats()
|
results = await Replay.updateReplayStats()
|
||||||
queryResults.latest_version = 1
|
|
||||||
queryResults.updatedAt = Date.now()
|
|
||||||
await redis.set(`replays:stats:version:1:results`, JSON.stringify(queryResults))
|
|
||||||
await redis.set(`replays:stats:latest_version`, 1)
|
|
||||||
return response.json(queryResults)
|
|
||||||
} else {
|
} else {
|
||||||
console.log('cache hit')
|
console.log('cache hit')
|
||||||
const results = await redis.get(`replays:stats:version:${latestVersion}:results`)
|
let data = await redis.get(`replays:stats:version:${latestVersion}:results`)
|
||||||
return response.json(JSON.parse(results))
|
if (data) {
|
||||||
|
results = JSON.parse(data)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
await redis.set(`replays:stats:version:${latestQueryVersion}:results`, JSON.stringify(results))
|
|
||||||
|
|
||||||
|
let responseData = { version: results.version, updatedAt: results.updatedAt, numberOfRecords: results.rows.length, data: results.rows }
|
||||||
if (sendToWebhook) {
|
if (sendToWebhook) {
|
||||||
try {
|
await sendDataToWebhook(responseData)
|
||||||
console.log('syncing to webhook')
|
|
||||||
await fetch(env.get('WEBHOOK_URL'),
|
|
||||||
{
|
|
||||||
headers:
|
|
||||||
{
|
|
||||||
'content-type': 'application/json'
|
|
||||||
},
|
|
||||||
method: 'POST',
|
|
||||||
body: JSON.stringify(results.rows)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
} catch(e) {
|
|
||||||
console.error('error sending webhook data', e)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return response.json(responseData)
|
||||||
}
|
}
|
||||||
|
|
||||||
public async list({ request, inertia }: HttpContext) {
|
public async list({ request, inertia }: HttpContext) {
|
||||||
@ -97,7 +69,6 @@ export default class ReplaysController {
|
|||||||
|
|
||||||
async index({ request, response }: HttpContext) {
|
async index({ request, response }: HttpContext) {
|
||||||
const { statsPeriod, start, end } = request.qs()
|
const { statsPeriod, start, end } = request.qs()
|
||||||
recordsUpdated = 0
|
|
||||||
|
|
||||||
let queryString: string = '?statsPeriod=24h'// Default in case none is provided
|
let queryString: string = '?statsPeriod=24h'// Default in case none is provided
|
||||||
if (statsPeriod) {
|
if (statsPeriod) {
|
||||||
@ -105,78 +76,16 @@ export default class ReplaysController {
|
|||||||
} else if (start && end) {
|
} else if (start && end) {
|
||||||
queryString = `?start=${start}&end=${end}`
|
queryString = `?start=${start}&end=${end}`
|
||||||
}
|
}
|
||||||
const replays = await fetchBatch(`https://sentry.io/api/0/organizations/${SENTRY_ORG}/replays/${queryString}`)
|
await fetchBatch(`https://sentry.io/api/0/organizations/${SENTRY_ORG}/replays/${queryString}`)
|
||||||
|
|
||||||
let queryResults = await Replay.updateReplayStats()
|
let queryResults = await Replay.updateReplayStats()
|
||||||
|
|
||||||
return response.json({version: queryResults.latestVersion, ...queryResults})
|
return response.json({ version: queryResults.latestVersion, ...queryResults })
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async function fetchBatch(url: string) {
|
|
||||||
const options: RequestInit = {
|
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${SENTRY_TOKEN}`
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const req = await fetch(url, options)
|
|
||||||
if (!req.ok) {
|
|
||||||
throw new Error(`Request failed with status ${req.status}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const resp = await req.json() as ApiResponse<Replay[]>;
|
|
||||||
const replays = resp.data;
|
|
||||||
const headers = req.headers
|
|
||||||
|
|
||||||
const cleanedData = replays.map(record => sanitizeInput(record, Replay.allowedFields))
|
|
||||||
|
|
||||||
let updated = await Replay.updateOrCreateMany('id', cleanedData)
|
|
||||||
recordsUpdated = recordsUpdated + updated.length
|
|
||||||
const linkHeader = headers.get('link')
|
|
||||||
if (!linkHeader) {
|
|
||||||
return { error: 'link header missing from Sentry API response' }
|
|
||||||
}
|
|
||||||
const pagination: SentryPagination = parseSentryLinkHeader(linkHeader)
|
|
||||||
|
|
||||||
if (pagination.hasNextResults == true) {
|
|
||||||
console.log('fetching', pagination.next)
|
|
||||||
await fetchBatch(pagination.next)
|
|
||||||
}
|
|
||||||
console.log('no more results')
|
|
||||||
return { recordsUpdated }
|
|
||||||
|
|
||||||
}
|
|
||||||
function parseSentryLinkHeader(header: string): SentryPagination {
|
|
||||||
const links = header.split(',').map(part => part.trim())
|
|
||||||
|
|
||||||
let result = {} as SentryPagination
|
|
||||||
for (const link of links) {
|
|
||||||
const match = link.match(/<([^>]+)>;\s*rel="([^"]+)";\s*results="([^"]+)";\s*cursor="([^"]+)"/)
|
|
||||||
if (!match) continue
|
|
||||||
|
|
||||||
const [, url, rel, results] = match
|
|
||||||
|
|
||||||
if (rel === 'previous') {
|
|
||||||
result.previous = url
|
|
||||||
result.hasPreviousResults = results === 'true'
|
|
||||||
} else if (rel === 'next') {
|
|
||||||
result.next = url
|
|
||||||
result.hasNextResults = results === 'true'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
function sanitizeInput(data: Record<string, any>, allowedFields: string[]) {
|
|
||||||
return allowedFields.reduce((acc, key) => {
|
|
||||||
if (key in data) acc[key] = data[key]
|
|
||||||
return acc
|
|
||||||
}, {} as Record<string, any>)
|
|
||||||
}
|
|
||||||
|
|
||||||
function buildPaginationLinks(meta: { previousPageUrl: string, lastPage: number; currentPage: number; nextPageUrl: string }) {
|
function buildPaginationLinks(meta: { previousPageUrl: string, lastPage: number; currentPage: number; nextPageUrl: string }) {
|
||||||
const links = []
|
const links = []
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user