mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 07:48:37 +01:00
add selfhosted env + start fix dates
This commit is contained in:
@@ -56,6 +56,8 @@ async function exportToGoogle(data: string, user_id: string) {
|
||||
}
|
||||
}
|
||||
|
||||
const { SELFHOSTED } = useRuntimeConfig();
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const data = await getRequestData(event, { requireSchema: false });
|
||||
@@ -63,9 +65,12 @@ export default defineEventHandler(async event => {
|
||||
|
||||
const { project, project_id, user } = data;
|
||||
|
||||
const PREMIUM_TYPE = project.premium_type;
|
||||
|
||||
if (PREMIUM_TYPE === 0) return setResponseStatus(event, 400, 'Project not premium');
|
||||
if (SELFHOSTED !== 'TRUE') {
|
||||
const PREMIUM_TYPE = project.premium_type;
|
||||
if (PREMIUM_TYPE === 0) return setResponseStatus(event, 400, 'Project not premium');
|
||||
}
|
||||
|
||||
|
||||
const { mode, slice } = getQuery(event);
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
import DateService from "@services/DateService";
|
||||
|
||||
import { checkSliceValidity, generateDateSlices } from "~/server/services/TimelineService";
|
||||
import { checkSliceValidity } from "~/server/services/TimelineService";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
@@ -23,7 +23,7 @@ export default defineEventHandler(async event => {
|
||||
const [sliceValid, errorOrDays] = checkSliceValidity(from, to, slice);
|
||||
if (!sliceValid) throw Error(errorOrDays);
|
||||
|
||||
const allDates = generateDateSlices(slice, new Date(from), new Date(to));
|
||||
const allDates = DateService.generateDateSlices(slice, new Date(from), new Date(to));
|
||||
|
||||
const result: { _id: string, count: number }[] = [];
|
||||
|
||||
|
||||
@@ -13,12 +13,15 @@ export default defineEventHandler(async event => {
|
||||
const cacheExp = 60;
|
||||
|
||||
return await Redis.useCacheV2(cacheKey, cacheExp, async () => {
|
||||
|
||||
const timelineData = await executeTimelineAggregation({
|
||||
projectId: project_id,
|
||||
model: EventModel,
|
||||
from, to, slice,
|
||||
});
|
||||
|
||||
const timelineFilledMerged = fillAndMergeTimelineAggregationV2(timelineData, slice, from, to);
|
||||
|
||||
return timelineFilledMerged;
|
||||
|
||||
});
|
||||
|
||||
@@ -12,7 +12,7 @@ export default defineEventHandler(async event => {
|
||||
const cacheKey = `timeline:visits:${pid}:${slice}:${from}:${to}`;
|
||||
const cacheExp = 60;
|
||||
|
||||
return await Redis.useCacheV2(cacheKey, cacheExp, async () => {
|
||||
return await Redis.useCacheV2(cacheKey, cacheExp, async () => {
|
||||
|
||||
const timelineData = await executeTimelineAggregation({
|
||||
projectId: project_id,
|
||||
@@ -23,7 +23,7 @@ export default defineEventHandler(async event => {
|
||||
const timelineFilledMerged = fillAndMergeTimelineAggregationV2(timelineData, slice, from, to);
|
||||
return timelineFilledMerged;
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -89,57 +89,11 @@ export function fillAndMergeTimelineAggregation(timeline: { _id: string, count:
|
||||
}
|
||||
|
||||
export function fillAndMergeTimelineAggregationV2(timeline: { _id: string, count: number }[], slice: Slice, from: string, to: string) {
|
||||
const allDates = generateDateSlices(slice, new Date(from), new Date(to));
|
||||
const merged = mergeDates(timeline, allDates, slice);
|
||||
const allDates = DateService.generateDateSlices(slice, new Date(from), new Date(to));
|
||||
const merged = DateService.mergeDates(timeline, allDates, slice);
|
||||
return merged;
|
||||
}
|
||||
|
||||
export function checkSliceValidity(from: string | number | Date, to: string | number | Date, slice: Slice): [false, string] | [true, number] {
|
||||
return DateService.canUseSlice(from, to, slice);
|
||||
}
|
||||
|
||||
export function generateDateSlices(slice: Slice, fromDate: Date, toDate: Date) {
|
||||
const slices: Date[] = [];
|
||||
let currentDate = fromDate;
|
||||
const addFunctions: { [key in Slice]: any } = { hour: fns.addHours, day: fns.addDays, week: fns.addWeeks, month: fns.addMonths, year: fns.addYears };
|
||||
const addFunction = addFunctions[slice];
|
||||
if (!addFunction) { throw new Error(`Invalid slice: ${slice}`); }
|
||||
while (fns.isBefore(currentDate, toDate) || currentDate.getTime() === toDate.getTime()) {
|
||||
slices.push(currentDate);
|
||||
currentDate = addFunction(currentDate, 1);
|
||||
}
|
||||
return slices;
|
||||
}
|
||||
|
||||
function mergeDates(timeline: { _id: string, count: number }[], dates: Date[], slice: Slice) {
|
||||
|
||||
const result: { _id: string, count: number }[] = [];
|
||||
|
||||
const isSames: { [key in Slice]: any } = { hour: fns.isSameHour, day: fns.isSameDay, week: fns.isSameWeek, month: fns.isSameMonth, year: fns.isSameYear, }
|
||||
|
||||
const isSame = isSames[slice];
|
||||
|
||||
if (!isSame) {
|
||||
throw new Error(`Invalid slice: ${slice}`);
|
||||
}
|
||||
|
||||
for (const element of timeline) {
|
||||
const elementDate = new Date(element._id);
|
||||
for (const date of dates) {
|
||||
if (isSame(elementDate, date)) {
|
||||
const existingEntry = result.find(item => isSame(new Date(item._id), date));
|
||||
|
||||
if (existingEntry) {
|
||||
existingEntry.count += element.count;
|
||||
} else {
|
||||
result.push({
|
||||
_id: date.toISOString(),
|
||||
count: element.count,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user