Fix formatting
This commit is contained in:
@ -1,29 +1,29 @@
|
||||
import Replay from '#models/replay'
|
||||
import {parseSentryLinkHeader, SentryPagination} from './Sentry.js'
|
||||
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;
|
||||
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}`
|
||||
}
|
||||
Authorization: `Bearer ${SENTRY_TOKEN}`,
|
||||
},
|
||||
}
|
||||
const req = await fetch(url, options)
|
||||
if (!req.ok) {
|
||||
throw new Error(`Request failed with status ${req.status}`);
|
||||
throw new Error(`Request failed with status ${req.status}`)
|
||||
}
|
||||
|
||||
const resp = await req.json() as ApiResponse<Replay[]>;
|
||||
const replays = resp.data;
|
||||
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))
|
||||
const cleanedData = replays.map((record) => sanitizeInput(record, Replay.allowedFields))
|
||||
|
||||
let updated = await Replay.updateOrCreateMany('id', cleanedData)
|
||||
recordsUpdated = recordsUpdated + updated.length
|
||||
@ -39,12 +39,14 @@ export async function fetchBatch(url: string) {
|
||||
}
|
||||
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>)
|
||||
return allowedFields.reduce(
|
||||
(acc, key) => {
|
||||
if (key in data) acc[key] = data[key]
|
||||
return acc
|
||||
},
|
||||
{} as Record<string, any>
|
||||
)
|
||||
}
|
||||
|
@ -1,12 +1,11 @@
|
||||
|
||||
export interface SentryPagination {
|
||||
previous: string;
|
||||
hasPreviousResults: boolean;
|
||||
hasNextResults: boolean;
|
||||
previous: string
|
||||
hasPreviousResults: boolean
|
||||
hasNextResults: boolean
|
||||
next: string
|
||||
}
|
||||
export function parseSentryLinkHeader(header: string): SentryPagination {
|
||||
const links = header.split(',').map(part => part.trim())
|
||||
const links = header.split(',').map((part) => part.trim())
|
||||
|
||||
let result = {} as SentryPagination
|
||||
for (const link of links) {
|
||||
@ -25,4 +24,4 @@ export function parseSentryLinkHeader(header: string): SentryPagination {
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
@ -1,19 +1,21 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@ -6,9 +6,7 @@ import redis from '@adonisjs/redis/services/main'
|
||||
import { fetchBatch } from '../Helpers/Replays.js'
|
||||
import { sendDataToWebhook } from '../Helpers/Webhook.js'
|
||||
|
||||
|
||||
export default class ReplaysController {
|
||||
|
||||
public async stats({ request, response }: HttpContext) {
|
||||
const { sendToWebhook } = request.qs()
|
||||
const latestVersion = await redis.get(`replays:stats:latest_version`)
|
||||
@ -24,7 +22,12 @@ export default class ReplaysController {
|
||||
}
|
||||
}
|
||||
|
||||
let responseData = { version: results.version, updatedAt: results.updatedAt, numberOfRecords: results.rows.length, data: results.rows }
|
||||
let responseData = {
|
||||
version: results.version,
|
||||
updatedAt: results.updatedAt,
|
||||
numberOfRecords: results.rows.length,
|
||||
data: results.rows,
|
||||
}
|
||||
if (sendToWebhook) {
|
||||
await sendDataToWebhook(responseData)
|
||||
}
|
||||
@ -40,7 +43,7 @@ export default class ReplaysController {
|
||||
let paginated, meta, replays
|
||||
|
||||
if (data) {
|
||||
({ paginated, meta, replays } = JSON.parse(data))
|
||||
;({ paginated, meta, replays } = JSON.parse(data))
|
||||
} else {
|
||||
paginated = await Replay.query().paginate(page, perPage)
|
||||
paginated.baseUrl('/list')
|
||||
@ -49,7 +52,7 @@ export default class ReplaysController {
|
||||
|
||||
meta = {
|
||||
...json.meta,
|
||||
links: buildPaginationLinks(json.meta)
|
||||
links: buildPaginationLinks(json.meta),
|
||||
}
|
||||
|
||||
replays = json.data
|
||||
@ -60,17 +63,14 @@ export default class ReplaysController {
|
||||
return inertia.render('Replays/Index', {
|
||||
data: {
|
||||
replays,
|
||||
meta
|
||||
}
|
||||
meta,
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
}
|
||||
|
||||
async index({ request, response }: HttpContext) {
|
||||
const { statsPeriod, start, end } = request.qs()
|
||||
|
||||
let queryString: string = '?statsPeriod=24h'// Default in case none is provided
|
||||
let queryString: string = '?statsPeriod=24h' // Default in case none is provided
|
||||
if (statsPeriod) {
|
||||
queryString = `?statsPeriod=${statsPeriod}`
|
||||
} else if (start && end) {
|
||||
@ -82,25 +82,28 @@ export default class ReplaysController {
|
||||
|
||||
return response.json({ version: queryResults.latestVersion, ...queryResults })
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
function buildPaginationLinks(meta: { previousPageUrl: string, lastPage: number; currentPage: number; nextPageUrl: string }) {
|
||||
function buildPaginationLinks(meta: {
|
||||
previousPageUrl: string
|
||||
lastPage: number
|
||||
currentPage: number
|
||||
nextPageUrl: string
|
||||
}) {
|
||||
const links = []
|
||||
|
||||
// Previous
|
||||
links.push({
|
||||
url: meta.previousPageUrl,
|
||||
label: '« Prev',
|
||||
active: false
|
||||
active: false,
|
||||
})
|
||||
|
||||
for (let page = 1; page <= meta.lastPage; page++) {
|
||||
links.push({
|
||||
url: `/list?page=${page}`,
|
||||
label: page.toString(),
|
||||
active: page === meta.currentPage
|
||||
active: page === meta.currentPage,
|
||||
})
|
||||
}
|
||||
|
||||
@ -108,7 +111,7 @@ function buildPaginationLinks(meta: { previousPageUrl: string, lastPage: number;
|
||||
links.push({
|
||||
url: meta.nextPageUrl,
|
||||
label: 'Next »',
|
||||
active: false
|
||||
active: false,
|
||||
})
|
||||
|
||||
return links
|
||||
|
@ -5,7 +5,7 @@ import redis from '@adonisjs/redis/services/main'
|
||||
|
||||
export default class Replay extends BaseModel {
|
||||
public static async updateReplayStats() {
|
||||
let results = await db.rawQuery(`
|
||||
let results = await db.rawQuery(`
|
||||
SELECT
|
||||
u.display_name,
|
||||
u.sessions,
|
||||
@ -54,13 +54,12 @@ export default class Replay extends BaseModel {
|
||||
) r ON true
|
||||
|
||||
ORDER BY
|
||||
u.total_time_seconds DESC;`
|
||||
)
|
||||
const updatedVersion = await redis.incr('replays:stats:latest_version')
|
||||
results.version = updatedVersion
|
||||
results.updatedAt = Date.now()
|
||||
await redis.set(`replays:stats:version:${updatedVersion}:results`, JSON.stringify(results))
|
||||
return results
|
||||
u.total_time_seconds DESC;`)
|
||||
const updatedVersion = await redis.incr('replays:stats:latest_version')
|
||||
results.version = updatedVersion
|
||||
results.updatedAt = Date.now()
|
||||
await redis.set(`replays:stats:version:${updatedVersion}:results`, JSON.stringify(results))
|
||||
return results
|
||||
}
|
||||
@column({ isPrimary: true })
|
||||
declare id: string
|
||||
@ -72,14 +71,14 @@ export default class Replay extends BaseModel {
|
||||
prepare: (value) => {
|
||||
// The values from sentry are just arrays so convert them to json
|
||||
return JSON.stringify(value)
|
||||
}
|
||||
},
|
||||
})
|
||||
declare trace_ids: string[]
|
||||
|
||||
@column({
|
||||
prepare: (value) => {
|
||||
return JSON.stringify(value)
|
||||
}
|
||||
},
|
||||
})
|
||||
declare error_ids: string[]
|
||||
|
||||
@ -90,50 +89,44 @@ export default class Replay extends BaseModel {
|
||||
prepare: (value) => {
|
||||
// The values from sentry are just arrays so convert them to json
|
||||
return JSON.stringify(value)
|
||||
}
|
||||
},
|
||||
})
|
||||
declare tags: string[]
|
||||
|
||||
@column()
|
||||
declare user: string[]
|
||||
|
||||
|
||||
@column()
|
||||
declare sdk: any
|
||||
|
||||
|
||||
@column()
|
||||
declare os: any
|
||||
|
||||
|
||||
@column()
|
||||
declare browser: any
|
||||
|
||||
|
||||
@column()
|
||||
declare device: any
|
||||
|
||||
|
||||
@column()
|
||||
declare ota_updates: any
|
||||
|
||||
@column()
|
||||
declare is_archived: boolean | null
|
||||
|
||||
|
||||
@column({
|
||||
prepare: (value) => {
|
||||
// The values from sentry are just arrays so convert them to json
|
||||
return JSON.stringify(value)
|
||||
}
|
||||
},
|
||||
})
|
||||
declare urls: any
|
||||
|
||||
declare urls: any
|
||||
|
||||
@column({
|
||||
prepare: (value) => {
|
||||
// The values from sentry are just arrays so convert them to json
|
||||
return JSON.stringify(value)
|
||||
}
|
||||
},
|
||||
})
|
||||
declare clicks: any
|
||||
|
||||
@ -152,7 +145,7 @@ export default class Replay extends BaseModel {
|
||||
@column.dateTime()
|
||||
declare finished_at: DateTime | null
|
||||
|
||||
@column.dateTime({serializeAs: 'started_at'})
|
||||
@column.dateTime({ serializeAs: 'started_at' })
|
||||
declare started_at: DateTime | null
|
||||
|
||||
@column()
|
||||
@ -170,12 +163,11 @@ export default class Replay extends BaseModel {
|
||||
@column()
|
||||
declare platform: string | null
|
||||
|
||||
|
||||
@column({
|
||||
prepare: (value) => {
|
||||
// The values from sentry are just arrays so convert them to json
|
||||
return JSON.stringify(value)
|
||||
}
|
||||
},
|
||||
})
|
||||
declare releases: any
|
||||
|
||||
|
Reference in New Issue
Block a user