| 462 | } |
| 463 | |
| 464 | export async function stats(request: FastifyRequest, reply: FastifyReply) { |
| 465 | const res = await getCache('api:stats', 60 * 60, async () => { |
| 466 | const projects = await chQuery<{ project_id: string; count: number }>( |
| 467 | `SELECT project_id, count(*) as count from ${TABLE_NAMES.events} GROUP by project_id order by count()`, |
| 468 | ); |
| 469 | const last24h = await chQuery<{ count: number }>( |
| 470 | `SELECT count(*) as count from ${TABLE_NAMES.events} WHERE created_at > now() - interval '24 hours'`, |
| 471 | ); |
| 472 | return { projects, last24hCount: last24h[0]?.count || 0 }; |
| 473 | }); |
| 474 | |
| 475 | reply.status(200).send({ |
| 476 | projectsCount: res.projects.length, |
| 477 | eventsCount: res.projects.reduce((acc, { count }) => acc + count, 0), |
| 478 | eventsLast24hCount: res.last24hCount, |
| 479 | }); |
| 480 | } |
| 481 | |
| 482 | export async function getGeo(request: FastifyRequest, reply: FastifyReply) { |
| 483 | const { ip, header } = getClientIpFromHeaders(request.headers); |