mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 07:48:37 +01:00
update endpoints to support domains
This commit is contained in:
@@ -3,16 +3,18 @@ type RefOrPrimitive<T> = T | Ref<T> | ComputedRef<T>
|
|||||||
|
|
||||||
export type CustomOptions = {
|
export type CustomOptions = {
|
||||||
useSnapshotDates?: boolean,
|
useSnapshotDates?: boolean,
|
||||||
|
useActiveDomain?: boolean,
|
||||||
useActivePid?: boolean,
|
useActivePid?: boolean,
|
||||||
useTimeOffset?: boolean,
|
useTimeOffset?: boolean,
|
||||||
slice?: RefOrPrimitive<string>,
|
slice?: RefOrPrimitive<string>,
|
||||||
limit?: RefOrPrimitive<number | string>,
|
limit?: RefOrPrimitive<number | string>,
|
||||||
custom?: Record<string, RefOrPrimitive<string>>
|
custom?: Record<string, RefOrPrimitive<string>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
const { token } = useAccessToken();
|
const { token } = useAccessToken();
|
||||||
const { projectId } = useProject();
|
const { projectId } = useProject();
|
||||||
const { safeSnapshotDates } = useSnapshot()
|
const { safeSnapshotDates } = useSnapshot()
|
||||||
|
const { domain } = useDomain();
|
||||||
|
|
||||||
function getValueFromRefOrPrimitive<T>(data?: T | Ref<T> | ComputedRef<T>) {
|
function getValueFromRefOrPrimitive<T>(data?: T | Ref<T> | ComputedRef<T>) {
|
||||||
if (!data) return;
|
if (!data) return;
|
||||||
@@ -24,6 +26,7 @@ export function useComputedHeaders(customOptions?: CustomOptions) {
|
|||||||
const useSnapshotDates = customOptions?.useSnapshotDates || true;
|
const useSnapshotDates = customOptions?.useSnapshotDates || true;
|
||||||
const useActivePid = customOptions?.useActivePid || true;
|
const useActivePid = customOptions?.useActivePid || true;
|
||||||
const useTimeOffset = customOptions?.useTimeOffset || true;
|
const useTimeOffset = customOptions?.useTimeOffset || true;
|
||||||
|
const useActiveDomain = customOptions?.useActiveDomain || true;
|
||||||
|
|
||||||
const headers = computed<Record<string, string>>(() => {
|
const headers = computed<Record<string, string>>(() => {
|
||||||
// console.trace('Computed recalculated');
|
// console.trace('Computed recalculated');
|
||||||
@@ -41,6 +44,7 @@ export function useComputedHeaders(customOptions?: CustomOptions) {
|
|||||||
'x-time-offset': useTimeOffset ? (new Date().getTimezoneOffset().toString()) : '',
|
'x-time-offset': useTimeOffset ? (new Date().getTimezoneOffset().toString()) : '',
|
||||||
'x-slice': getValueFromRefOrPrimitive(customOptions?.slice) ?? '',
|
'x-slice': getValueFromRefOrPrimitive(customOptions?.slice) ?? '',
|
||||||
'x-limit': getValueFromRefOrPrimitive(customOptions?.limit)?.toString() ?? '',
|
'x-limit': getValueFromRefOrPrimitive(customOptions?.limit)?.toString() ?? '',
|
||||||
|
'x-domain': useActiveDomain ? (domain.value ?? '') : '',
|
||||||
...parsedCustom
|
...parsedCustom
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ const selfhosted = useSelfhosted();
|
|||||||
<DashboardActionableChart :key="refreshKey"></DashboardActionableChart>
|
<DashboardActionableChart :key="refreshKey"></DashboardActionableChart>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
<div class="flex w-full justify-center mt-6 px-6">
|
<div class="flex w-full justify-center mt-6 px-6">
|
||||||
<div class="flex w-full gap-6 flex-col xl:flex-row">
|
<div class="flex w-full gap-6 flex-col xl:flex-row">
|
||||||
<div class="flex-1">
|
<div class="flex-1">
|
||||||
@@ -77,6 +78,7 @@ const selfhosted = useSelfhosted();
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
<div class="flex w-full justify-center mt-6 px-6">
|
<div class="flex w-full justify-center mt-6 px-6">
|
||||||
<div class="flex w-full gap-6 flex-col xl:flex-row">
|
<div class="flex w-full gap-6 flex-col xl:flex-row">
|
||||||
<div class="flex-1">
|
<div class="flex-1">
|
||||||
|
|||||||
@@ -1,16 +1,15 @@
|
|||||||
|
|
||||||
import { VisitModel } from "@schema/metrics/VisitSchema";
|
import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||||
import { Redis } from "~/server/services/CacheService";
|
import { Redis } from "~/server/services/CacheService";
|
||||||
import { getRequestDataOld } from "~/server/utils/getRequestData";
|
|
||||||
|
|
||||||
export default defineEventHandler(async event => {
|
export default defineEventHandler(async event => {
|
||||||
|
|
||||||
const data = await getRequestDataOld(event, { requireSchema: false });
|
const data = await getRequestData(event, ['GUEST', 'RANGE', 'GUEST', 'DOMAIN']);
|
||||||
if (!data) return;
|
if (!data) return;
|
||||||
|
|
||||||
const { pid, from, to, project_id, limit } = data;
|
const { pid, from, to, project_id, limit, domain } = data;
|
||||||
|
|
||||||
const cacheKey = `countries:${pid}:${limit}:${from}:${to}`;
|
const cacheKey = `countries:${pid}:${limit}:${from}:${to}:${domain}`;
|
||||||
const cacheExp = 60;
|
const cacheExp = 60;
|
||||||
|
|
||||||
return await Redis.useCacheV2(cacheKey, cacheExp, async () => {
|
return await Redis.useCacheV2(cacheKey, cacheExp, async () => {
|
||||||
@@ -19,7 +18,8 @@ export default defineEventHandler(async event => {
|
|||||||
{
|
{
|
||||||
$match: {
|
$match: {
|
||||||
project_id,
|
project_id,
|
||||||
created_at: { $gte: new Date(from), $lte: new Date(to) }
|
created_at: { $gte: new Date(from), $lte: new Date(to) },
|
||||||
|
website: domain
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{ $group: { _id: "$country", count: { $sum: 1, } } },
|
{ $group: { _id: "$country", count: { $sum: 1, } } },
|
||||||
|
|||||||
@@ -1,16 +1,15 @@
|
|||||||
|
|
||||||
import { VisitModel } from "@schema/metrics/VisitSchema";
|
import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||||
import { Redis } from "~/server/services/CacheService";
|
import { Redis } from "~/server/services/CacheService";
|
||||||
import { getRequestDataOld } from "~/server/utils/getRequestData";
|
|
||||||
|
|
||||||
export default defineEventHandler(async event => {
|
export default defineEventHandler(async event => {
|
||||||
|
|
||||||
const data = await getRequestDataOld(event, { requireSchema: false });
|
const data = await getRequestData(event, ['GUEST', 'RANGE', 'GUEST', 'DOMAIN']);
|
||||||
if (!data) return;
|
if (!data) return;
|
||||||
|
|
||||||
const { pid, from, to, project_id, limit } = data;
|
const { pid, from, to, project_id, limit, domain } = data;
|
||||||
|
|
||||||
const cacheKey = `devices:${pid}:${limit}:${from}:${to}`;
|
const cacheKey = `devices:${pid}:${limit}:${from}:${to}:${domain}`;
|
||||||
const cacheExp = 60;
|
const cacheExp = 60;
|
||||||
|
|
||||||
return await Redis.useCacheV2(cacheKey, cacheExp, async () => {
|
return await Redis.useCacheV2(cacheKey, cacheExp, async () => {
|
||||||
@@ -19,7 +18,8 @@ export default defineEventHandler(async event => {
|
|||||||
{
|
{
|
||||||
$match: {
|
$match: {
|
||||||
project_id,
|
project_id,
|
||||||
created_at: { $gte: new Date(from), $lte: new Date(to) }
|
created_at: { $gte: new Date(from), $lte: new Date(to) },
|
||||||
|
website: domain
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{ $group: { _id: "$device", count: { $sum: 1, } } },
|
{ $group: { _id: "$device", count: { $sum: 1, } } },
|
||||||
|
|||||||
@@ -1,16 +1,15 @@
|
|||||||
|
|
||||||
import { VisitModel } from "@schema/metrics/VisitSchema";
|
import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||||
import { Redis } from "~/server/services/CacheService";
|
import { Redis } from "~/server/services/CacheService";
|
||||||
import { getRequestDataOld } from "~/server/utils/getRequestData";
|
|
||||||
|
|
||||||
export default defineEventHandler(async event => {
|
export default defineEventHandler(async event => {
|
||||||
|
|
||||||
const data = await getRequestDataOld(event, { requireSchema: false });
|
const data = await getRequestData(event, ['OFFSET', 'RANGE', 'GUEST', 'DOMAIN']);
|
||||||
if (!data) return;
|
if (!data) return;
|
||||||
|
|
||||||
const { pid, from, to, project_id, limit } = data;
|
const { pid, from, to, project_id, limit, domain } = data;
|
||||||
|
|
||||||
const cacheKey = `referrers:${pid}:${limit}:${from}:${to}`;
|
const cacheKey = `referrers:${pid}:${limit}:${from}:${to}:${domain}`;
|
||||||
const cacheExp = 60;
|
const cacheExp = 60;
|
||||||
|
|
||||||
return await Redis.useCacheV2(cacheKey, cacheExp, async () => {
|
return await Redis.useCacheV2(cacheKey, cacheExp, async () => {
|
||||||
@@ -19,7 +18,8 @@ export default defineEventHandler(async event => {
|
|||||||
{
|
{
|
||||||
$match: {
|
$match: {
|
||||||
project_id,
|
project_id,
|
||||||
created_at: { $gte: new Date(from), $lte: new Date(to) }
|
created_at: { $gte: new Date(from), $lte: new Date(to) },
|
||||||
|
website: domain,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{ $group: { _id: "$referrer", count: { $sum: 1, } } },
|
{ $group: { _id: "$referrer", count: { $sum: 1, } } },
|
||||||
@@ -27,6 +27,7 @@ export default defineEventHandler(async event => {
|
|||||||
{ $limit: limit }
|
{ $limit: limit }
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
||||||
return result as { _id: string, count: number }[];
|
return result as { _id: string, count: number }[];
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { VisitModel } from "@schema/metrics/VisitSchema";
|
|||||||
|
|
||||||
export default defineEventHandler(async event => {
|
export default defineEventHandler(async event => {
|
||||||
|
|
||||||
const data = await getRequestData(event, ['GUEST', 'LIVEMODE']);
|
const data = await getRequestData(event, ['GUEST']);
|
||||||
if (!data) return;
|
if (!data) return;
|
||||||
|
|
||||||
const { project_id } = data;
|
const { project_id } = data;
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ export type GetRequestDataOptions = {
|
|||||||
/** @default false */ requireOffset?: boolean,
|
/** @default false */ requireOffset?: boolean,
|
||||||
}
|
}
|
||||||
|
|
||||||
export type RequestDataScope = 'GUEST' | 'SCHEMA' | 'LIVEMODE' | 'SLICE' | 'RANGE' | 'OFFSET';
|
export type RequestDataScope = 'GUEST' | 'SCHEMA' | 'ANON' | 'SLICE' | 'RANGE' | 'OFFSET' | 'DOMAIN';
|
||||||
|
|
||||||
async function hasAccessToProject(user_id: string, project: TProject) {
|
async function hasAccessToProject(user_id: string, project: TProject) {
|
||||||
if (!project) return [false, 'NONE'];
|
if (!project) return [false, 'NONE'];
|
||||||
@@ -48,14 +48,20 @@ export async function getRequestData(event: H3Event<EventHandlerRequest>, requir
|
|||||||
|
|
||||||
const requireSchema = required_scopes.includes('SCHEMA');
|
const requireSchema = required_scopes.includes('SCHEMA');
|
||||||
const allowGuests = required_scopes.includes('GUEST');
|
const allowGuests = required_scopes.includes('GUEST');
|
||||||
const allowLitlyx = required_scopes.includes('LIVEMODE');
|
const allowAnon = required_scopes.includes('ANON');
|
||||||
const requireSlice = required_scopes.includes('SLICE');
|
const requireSlice = required_scopes.includes('SLICE');
|
||||||
const requireRange = required_scopes.includes('RANGE');
|
const requireRange = required_scopes.includes('RANGE');
|
||||||
const requireOffset = required_scopes.includes('OFFSET');
|
const requireOffset = required_scopes.includes('OFFSET');
|
||||||
|
const requireDomain = required_scopes.includes('DOMAIN');
|
||||||
|
|
||||||
const pid = getHeader(event, 'x-pid');
|
const pid = getHeader(event, 'x-pid');
|
||||||
if (!pid) return setResponseStatus(event, 400, 'x-pid is required');
|
if (!pid) return setResponseStatus(event, 400, 'x-pid is required');
|
||||||
|
|
||||||
|
const domain = getHeader(event, 'x-domain');
|
||||||
|
if (requireDomain) {
|
||||||
|
if (domain == null || domain == undefined || domain.length == 0) return setResponseStatus(event, 400, 'x-domain is required');
|
||||||
|
}
|
||||||
|
|
||||||
const slice = getHeader(event, 'x-slice') as Slice;
|
const slice = getHeader(event, 'x-slice') as Slice;
|
||||||
if (!slice && requireSlice) return setResponseStatus(event, 400, 'x-slice is required');
|
if (!slice && requireSlice) return setResponseStatus(event, 400, 'x-slice is required');
|
||||||
|
|
||||||
@@ -95,18 +101,16 @@ export async function getRequestData(event: H3Event<EventHandlerRequest>, requir
|
|||||||
const project = await ProjectModel.findById(project_id);
|
const project = await ProjectModel.findById(project_id);
|
||||||
if (!project) return setResponseStatus(event, 400, 'project not found');
|
if (!project) return setResponseStatus(event, 400, 'project not found');
|
||||||
|
|
||||||
|
if (!allowAnon) {
|
||||||
if (pid !== LITLYX_PROJECT_ID) {
|
|
||||||
const [hasAccess, role] = await hasAccessToProject(user.id, project);
|
const [hasAccess, role] = await hasAccessToProject(user.id, project);
|
||||||
if (!hasAccess) return setResponseStatus(event, 400, 'no access to project');
|
if (!hasAccess) return setResponseStatus(event, 400, 'no access to project');
|
||||||
if (role === 'GUEST' && !allowGuests) return setResponseStatus(event, 403, 'only owner can access this');
|
if (role === 'GUEST' && !allowGuests) return setResponseStatus(event, 403, 'only owner can access this');
|
||||||
} else {
|
|
||||||
if (!allowLitlyx) return setResponseStatus(event, 400, 'no access to project');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
from: from as string,
|
from: from as string,
|
||||||
to: to as string,
|
to: to as string,
|
||||||
|
domain: domain as string,
|
||||||
pid, project_id, project, user, limit, slice, schemaName, model, timeOffset: offset
|
pid, project_id, project, user, limit, slice, schemaName, model, timeOffset: offset
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user