api: improve get events sql and setup simple caching

This commit is contained in:
Carl-Gerhard Lindesvärd
2023-12-12 15:54:10 +01:00
parent f82636a0d9
commit 3bd06b610e
4 changed files with 44 additions and 11 deletions

View File

@@ -125,9 +125,18 @@ mixan.screenView('Profile', {
mixan.clear();
```
## @mixan/backend
## @mixan/web
Self hosted service for collecting all events. Dockerfile and GUI will be added soon.
A nextjs web app. Collects all events and your gui to analyze your data.
### Setup cronjobs (optional)
Use of cronjobs is optional. Everything will work without them but they will enhance the events with more data. We also use cronjobs to warm up the cache to make the user experiance a bit better.
We use https://cron-job.org (free) to handle our cronjobs, you can use any provider you want.
- **https://domain.com/api/cron/cache/update** Will update the memory cache
- **https://domain.com/api/cron/events/enrich** Enrich events (adds duration etc)
## Screenshots

View File

@@ -0,0 +1,18 @@
import * as cache from '@/server/cache';
import { db } from '@/server/db';
import { getUniqueEvents } from '@/server/services/event.service';
import type { NextApiRequest, NextApiResponse } from 'next';
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const projects = await db.project.findMany();
for (const project of projects) {
const events = await getUniqueEvents({ projectId: project.id });
cache.set(`events_${project.id}`, 1000 * 60 * 60 * 24, events);
}
res.status(200).json({ ok: true });
}

View File

@@ -1,6 +1,7 @@
import { createTRPCRouter, protectedProcedure } from '@/server/api/trpc';
import * as cache from '@/server/cache';
import { db } from '@/server/db';
import { getUniqueEvents } from '@/server/services/event.service';
import { getProjectBySlug } from '@/server/services/project.service';
import type {
IChartEvent,
@@ -27,15 +28,8 @@ export const chartRouter = createTRPCRouter({
const project = await getProjectBySlug(projectSlug);
const events = await cache.getOr(
`events_${project.id}`,
1000 * 60 * 60,
() =>
db.event.findMany({
take: 500,
distinct: ['name'],
where: {
project_id: project.id,
},
})
1000 * 60 * 60 * 24,
() => getUniqueEvents({ projectId: project.id })
);
return events;

View File

@@ -0,0 +1,12 @@
import { db } from '../db';
export function getUniqueEvents({ projectId }: { projectId: string }) {
return db.event.findMany({
take: 500,
distinct: ['name'],
select: { name: true },
where: {
project_id: projectId,
},
});
}