Add redis and basic frontend for viewing data
This commit is contained in:
@ -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: '« 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 »',
|
||||
active: false
|
||||
})
|
||||
|
||||
return links
|
||||
}
|
Reference in New Issue
Block a user