api: improve get events sql and setup simple caching
This commit is contained in:
13
README.md
13
README.md
@@ -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
|
||||
|
||||
|
||||
18
apps/web/src/pages/api/cron/cache/update.tsx
vendored
Normal file
18
apps/web/src/pages/api/cron/cache/update.tsx
vendored
Normal 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 });
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
12
apps/web/src/server/services/event.service.ts
Normal file
12
apps/web/src/server/services/event.service.ts
Normal 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,
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user