Add redis and basic frontend for viewing data

This commit is contained in:
Mike Conrad
2025-05-19 11:07:03 -04:00
parent f12cdbf76a
commit a96b9f2c8b
10 changed files with 258 additions and 20 deletions

View File

@ -4,6 +4,7 @@ import type { HttpContext } from '@adonisjs/core/http'
const SENTRY_TOKEN = env.get('SENTRY_TOKEN')
const SENTRY_ORG = env.get('SENTRY_ORG')
let recordsUpdated = 0
import redis from '@adonisjs/redis/services/main'
interface ApiResponse<T> {
@ -18,7 +19,42 @@ interface SentryPagination {
next: string
}
export default class ReplaysController {
public async list({ 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('/list')
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()
@ -97,4 +133,32 @@ function parseSentryLinkHeader(header:string): SentryPagination {
if (key in data) acc[key] = data[key]
return acc
}, {} as Record<string, any>)
}
}
function buildPaginationLinks(meta) {
const links = []
// Previous
links.push({
url: meta.previousPageUrl,
label: '&laquo; Prev',
active: false
})
for (let page = 1; page <= meta.lastPage; page++) {
links.push({
url: `/list?page=${page}`,
label: page.toString(),
active: page === meta.currentPage
})
}
// Next
links.push({
url: meta.nextPageUrl,
label: 'Next &raquo;',
active: false
})
return links
}