Caching in progress
This commit is contained in:
@ -21,59 +21,32 @@ interface SentryPagination {
|
|||||||
}
|
}
|
||||||
export default class ReplaysController {
|
export default class ReplaysController {
|
||||||
|
|
||||||
public async search({ response }: HttpContext) {
|
public async stats({ request, response }: HttpContext) {
|
||||||
let results = await db.rawQuery(`
|
const {sendToWebhook} = request.qs()
|
||||||
SELECT
|
const cacheKey = `replays:sync:latest_version`
|
||||||
u.display_name,
|
const latestFetchVersion = await redis.get(`replays:fetch:latest_version`)
|
||||||
u.sessions,
|
const latestQueryVersion = await redis.get(`replays:stats:latest_version`)
|
||||||
u.total_time_seconds,
|
if (latestFetchVersion == latestQueryVersion) {
|
||||||
u.total_time_readable,
|
let results
|
||||||
u.average_session_time_readable,
|
results = await redis.get(`replays:sync:version:${latestQueryVersion}:results`)
|
||||||
u.average_time_seconds,
|
if (!results) {
|
||||||
r.id AS last_session_id,
|
console.log('no data in cache, updating')
|
||||||
r.finished_at AS last_session_time
|
results = await getResults()
|
||||||
|
await redis.set(`replays:sync:version:${latestQueryVersion}:results`, JSON.stringify(results))
|
||||||
|
|
||||||
FROM (
|
}
|
||||||
-- Aggregate sessions in the last 30 days
|
console.log('resultssdsdfds')
|
||||||
SELECT
|
return response.json(results)
|
||||||
"user" ->> 'display_name' AS display_name,
|
} else {
|
||||||
COUNT(duration) AS sessions,
|
let results = await getResults()
|
||||||
SUM(duration) AS total_time_seconds,
|
console.log('results quer', latestQueryVersion)
|
||||||
AVG(duration) AS average_time_seconds,
|
await redis.set(`replays:stats:version:${latestQueryVersion}:results`, JSON.stringify(results))
|
||||||
CONCAT(
|
await redis.set(`replays:stats:latest_version`, latestFetchVersion)
|
||||||
FLOOR(SUM(duration) / 86400), 'd ',
|
await redis.set(`replays:fetch:latest_version`, latestFetchVersion)
|
||||||
FLOOR(MOD(SUM(duration), 86400) / 3600), 'h ',
|
return response.json(results)
|
||||||
FLOOR(MOD(SUM(duration), 3600) / 60), 'm'
|
if (sendToWebhook) {
|
||||||
) AS total_time_readable,
|
|
||||||
CONCAT(
|
|
||||||
FLOOR(COUNT(duration) / 86400), 'd ',
|
|
||||||
FLOOR(MOD(COUNT(duration), 86400) / 3600), 'h ',
|
|
||||||
FLOOR(MOD(COUNT(duration), 3600) / 60), 'm'
|
|
||||||
) AS average_session_time_readable
|
|
||||||
FROM
|
|
||||||
replays
|
|
||||||
WHERE
|
|
||||||
finished_at >= NOW() - INTERVAL '30 days'
|
|
||||||
GROUP BY
|
|
||||||
"user" ->> 'display_name'
|
|
||||||
) u
|
|
||||||
|
|
||||||
-- LATERAL JOIN to get latest session (either within 30d or fallback to latest overall)
|
|
||||||
JOIN LATERAL (
|
|
||||||
SELECT id, finished_at
|
|
||||||
FROM replays
|
|
||||||
WHERE "user" ->> 'display_name' = u.display_name
|
|
||||||
ORDER BY
|
|
||||||
CASE WHEN finished_at >= NOW() - INTERVAL '30 days' THEN 0 ELSE 1 END,
|
|
||||||
finished_at DESC
|
|
||||||
LIMIT 1
|
|
||||||
) r ON true
|
|
||||||
|
|
||||||
ORDER BY
|
|
||||||
u.total_time_seconds DESC;`
|
|
||||||
)
|
|
||||||
try {
|
try {
|
||||||
|
console.log('syncing to webhook')
|
||||||
await fetch(env.get('WEBHOOK_URL'),
|
await fetch(env.get('WEBHOOK_URL'),
|
||||||
{
|
{
|
||||||
headers:
|
headers:
|
||||||
@ -87,6 +60,9 @@ ORDER BY
|
|||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.error('error sending webhook data', e)
|
console.error('error sending webhook data', e)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
response.json(results.rows)
|
response.json(results.rows)
|
||||||
}
|
}
|
||||||
public async list({ request, inertia }: HttpContext) {
|
public async list({ request, inertia }: HttpContext) {
|
||||||
@ -136,6 +112,13 @@ ORDER BY
|
|||||||
queryString = `?start=${start}&end=${end}`
|
queryString = `?start=${start}&end=${end}`
|
||||||
}
|
}
|
||||||
const replays = await fetchBatch(`https://sentry.io/api/0/organizations/${SENTRY_ORG}/replays/${queryString}`)
|
const replays = await fetchBatch(`https://sentry.io/api/0/organizations/${SENTRY_ORG}/replays/${queryString}`)
|
||||||
|
let latestVersion = await redis.get(`replays:fetch:latest_version`)
|
||||||
|
if (!latestVersion) {
|
||||||
|
redis.set('replays:fetch:latest_version', 1)
|
||||||
|
} else {
|
||||||
|
redis.set('replays:fetch:latest_version', ++latestVersion)
|
||||||
|
}
|
||||||
|
|
||||||
return response.json(replays)
|
return response.json(replays)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -231,3 +214,57 @@ function buildPaginationLinks(meta: { previousPageUrl: string, lastPage: number;
|
|||||||
|
|
||||||
return links
|
return links
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getResults(){
|
||||||
|
let results = await db.rawQuery(`
|
||||||
|
SELECT
|
||||||
|
u.display_name,
|
||||||
|
u.sessions,
|
||||||
|
u.total_time_seconds,
|
||||||
|
u.total_time_readable,
|
||||||
|
u.average_session_time_readable,
|
||||||
|
u.average_time_seconds,
|
||||||
|
r.id AS last_session_id,
|
||||||
|
r.finished_at AS last_session_time
|
||||||
|
|
||||||
|
FROM (
|
||||||
|
-- Aggregate sessions in the last 30 days
|
||||||
|
SELECT
|
||||||
|
"user" ->> 'display_name' AS display_name,
|
||||||
|
COUNT(duration) AS sessions,
|
||||||
|
SUM(duration) AS total_time_seconds,
|
||||||
|
AVG(duration) AS average_time_seconds,
|
||||||
|
CONCAT(
|
||||||
|
FLOOR(SUM(duration) / 86400), 'd ',
|
||||||
|
FLOOR(MOD(SUM(duration), 86400) / 3600), 'h ',
|
||||||
|
FLOOR(MOD(SUM(duration), 3600) / 60), 'm'
|
||||||
|
) AS total_time_readable,
|
||||||
|
CONCAT(
|
||||||
|
FLOOR(COUNT(duration) / 86400), 'd ',
|
||||||
|
FLOOR(MOD(COUNT(duration), 86400) / 3600), 'h ',
|
||||||
|
FLOOR(MOD(COUNT(duration), 3600) / 60), 'm'
|
||||||
|
) AS average_session_time_readable
|
||||||
|
FROM
|
||||||
|
replays
|
||||||
|
WHERE
|
||||||
|
finished_at >= NOW() - INTERVAL '30 days'
|
||||||
|
GROUP BY
|
||||||
|
"user" ->> 'display_name'
|
||||||
|
) u
|
||||||
|
|
||||||
|
-- LATERAL JOIN to get latest session (either within 30d or fallback to latest overall)
|
||||||
|
JOIN LATERAL (
|
||||||
|
SELECT id, finished_at
|
||||||
|
FROM replays
|
||||||
|
WHERE "user" ->> 'display_name' = u.display_name
|
||||||
|
ORDER BY
|
||||||
|
CASE WHEN finished_at >= NOW() - INTERVAL '30 days' THEN 0 ELSE 1 END,
|
||||||
|
finished_at DESC
|
||||||
|
LIMIT 1
|
||||||
|
) r ON true
|
||||||
|
|
||||||
|
ORDER BY
|
||||||
|
u.total_time_seconds DESC;`
|
||||||
|
)
|
||||||
|
return results
|
||||||
|
}
|
Reference in New Issue
Block a user