mirror of
https://github.com/umami-software/umami.git
synced 2026-02-20 12:35:38 +01:00
Add collect limits.
This commit is contained in:
parent
f42cab8d83
commit
e487da72c3
13 changed files with 217 additions and 61 deletions
39
lib/cache.ts
39
lib/cache.ts
|
|
@ -1,6 +1,7 @@
|
|||
import { User, Website } from '@prisma/client';
|
||||
import { User, Website, Team } from '@prisma/client';
|
||||
import redis from '@umami/redis-client';
|
||||
import { getSession, getUser, getWebsite } from '../queries';
|
||||
import { lightFormat, startOfMonth } from 'date-fns';
|
||||
import { getAllWebsitesByUser, getSession, getUser, getViewTotals, getWebsite } from '../queries';
|
||||
|
||||
const DELETED = 'DELETED';
|
||||
|
||||
|
|
@ -32,7 +33,11 @@ async function deleteObject(key, soft = false) {
|
|||
return soft ? redis.set(key, DELETED) : redis.del(key);
|
||||
}
|
||||
|
||||
async function fetchWebsite(id): Promise<Website> {
|
||||
async function fetchWebsite(id): Promise<
|
||||
Website & {
|
||||
team?: Team;
|
||||
}
|
||||
> {
|
||||
return fetchObject(`website:${id}`, () => getWebsite({ id }));
|
||||
}
|
||||
|
||||
|
|
@ -58,6 +63,31 @@ async function storeUser(data) {
|
|||
return storeObject(key, data);
|
||||
}
|
||||
|
||||
async function fetchCollectLimit(userId): Promise<Number> {
|
||||
const monthDate = startOfMonth(new Date());
|
||||
const monthKey = lightFormat(monthDate, 'yyyy-MM');
|
||||
|
||||
return fetchObject(`collectLimit:${userId}:${monthKey}`, async () => {
|
||||
const websiteIds = (await getAllWebsitesByUser(userId)).map(a => a.id);
|
||||
|
||||
return getViewTotals(websiteIds, monthDate).then(data => data.views);
|
||||
});
|
||||
}
|
||||
|
||||
async function deleteCollectLimit(userId): Promise<Number> {
|
||||
const monthDate = startOfMonth(new Date());
|
||||
const monthKey = lightFormat(monthDate, 'yyyy-MM');
|
||||
|
||||
return deleteObject(`collectLimit:${userId}:${monthKey}`);
|
||||
}
|
||||
|
||||
async function incrementCollectLimit(userId): Promise<Number> {
|
||||
const monthDate = startOfMonth(new Date());
|
||||
const monthKey = lightFormat(monthDate, 'yyyy-MM');
|
||||
|
||||
return redis.incr(`collectLimit:${userId}:${monthKey}`);
|
||||
}
|
||||
|
||||
async function deleteUser(id) {
|
||||
return deleteObject(`user:${id}`);
|
||||
}
|
||||
|
|
@ -87,5 +117,8 @@ export default {
|
|||
fetchSession,
|
||||
storeSession,
|
||||
deleteSession,
|
||||
fetchCollectLimit,
|
||||
incrementCollectLimit,
|
||||
deleteCollectLimit,
|
||||
enabled: redis.enabled,
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue