improve(api): increase limit 1000, allow both projectId and project_id
This commit is contained in:
@@ -2,6 +2,7 @@ import { parseQueryString } from '@/utils/parse-zod-query-string';
|
|||||||
import type { FastifyReply, FastifyRequest } from 'fastify';
|
import type { FastifyReply, FastifyRequest } from 'fastify';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
import { HttpError } from '@/utils/errors';
|
||||||
import type { GetEventListOptions } from '@openpanel/db';
|
import type { GetEventListOptions } from '@openpanel/db';
|
||||||
import {
|
import {
|
||||||
ClientType,
|
ClientType,
|
||||||
@@ -10,11 +11,7 @@ import {
|
|||||||
getEventsCountCached,
|
getEventsCountCached,
|
||||||
} from '@openpanel/db';
|
} from '@openpanel/db';
|
||||||
import { getChart } from '@openpanel/trpc/src/routers/chart.helpers';
|
import { getChart } from '@openpanel/trpc/src/routers/chart.helpers';
|
||||||
import {
|
import { zChartEvent, zChartInput } from '@openpanel/validation';
|
||||||
zChartEvent,
|
|
||||||
zChartEventFilter,
|
|
||||||
zChartInput,
|
|
||||||
} from '@openpanel/validation';
|
|
||||||
import { omit } from 'ramda';
|
import { omit } from 'ramda';
|
||||||
|
|
||||||
async function getProjectId(
|
async function getProjectId(
|
||||||
@@ -33,11 +30,9 @@ async function getProjectId(
|
|||||||
request.client?.type === ClientType.read &&
|
request.client?.type === ClientType.read &&
|
||||||
request.client?.projectId !== projectId
|
request.client?.projectId !== projectId
|
||||||
) {
|
) {
|
||||||
reply.status(403).send({
|
throw new HttpError('You do not have access to this project', {
|
||||||
error: 'Forbidden',
|
status: 403,
|
||||||
message: 'You do not have access to this project',
|
|
||||||
});
|
});
|
||||||
return '';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const project = await db.project.findUnique({
|
const project = await db.project.findUnique({
|
||||||
@@ -48,11 +43,9 @@ async function getProjectId(
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!project) {
|
if (!project) {
|
||||||
reply.status(404).send({
|
throw new HttpError('Project not found', {
|
||||||
error: 'Not Found',
|
status: 404,
|
||||||
message: 'Project not found',
|
|
||||||
});
|
});
|
||||||
return '';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,11 +54,9 @@ async function getProjectId(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!projectId) {
|
if (!projectId) {
|
||||||
reply.status(400).send({
|
throw new HttpError('project_id or projectId is required', {
|
||||||
error: 'Bad Request',
|
status: 400,
|
||||||
message: 'project_id is required',
|
|
||||||
});
|
});
|
||||||
return '';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return projectId;
|
return projectId;
|
||||||
@@ -106,7 +97,7 @@ export async function events(
|
|||||||
const projectId = await getProjectId(request, reply);
|
const projectId = await getProjectId(request, reply);
|
||||||
const limit = query.data.limit;
|
const limit = query.data.limit;
|
||||||
const page = Math.max(query.data.page, 1);
|
const page = Math.max(query.data.page, 1);
|
||||||
const take = Math.max(Math.min(limit, 50), 1);
|
const take = Math.max(Math.min(limit, 1000), 1);
|
||||||
const cursor = page - 1;
|
const cursor = page - 1;
|
||||||
const options: GetEventListOptions = {
|
const options: GetEventListOptions = {
|
||||||
projectId,
|
projectId,
|
||||||
@@ -147,7 +138,6 @@ export async function events(
|
|||||||
const chartSchemeFull = zChartInput
|
const chartSchemeFull = zChartInput
|
||||||
.pick({
|
.pick({
|
||||||
breakdowns: true,
|
breakdowns: true,
|
||||||
projectId: true,
|
|
||||||
interval: true,
|
interval: true,
|
||||||
range: true,
|
range: true,
|
||||||
previous: true,
|
previous: true,
|
||||||
@@ -155,6 +145,8 @@ const chartSchemeFull = zChartInput
|
|||||||
endDate: true,
|
endDate: true,
|
||||||
})
|
})
|
||||||
.extend({
|
.extend({
|
||||||
|
project_id: z.string().optional(),
|
||||||
|
projectId: z.string().optional(),
|
||||||
events: z.array(
|
events: z.array(
|
||||||
z.object({
|
z.object({
|
||||||
name: z.string(),
|
name: z.string(),
|
||||||
@@ -181,10 +173,12 @@ export async function charts(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const projectId = await getProjectId(request, reply);
|
||||||
const { events, ...rest } = query.data;
|
const { events, ...rest } = query.data;
|
||||||
|
|
||||||
return getChart({
|
return getChart({
|
||||||
...rest,
|
...rest,
|
||||||
|
projectId,
|
||||||
events: events.map((event) => ({
|
events: events.map((event) => ({
|
||||||
...event,
|
...event,
|
||||||
segment: event.segment ?? 'event',
|
segment: event.segment ?? 'event',
|
||||||
|
|||||||
@@ -249,7 +249,9 @@ export async function getEvents(
|
|||||||
const events = await chQuery<IClickhouseEvent>(sql);
|
const events = await chQuery<IClickhouseEvent>(sql);
|
||||||
const projectId = events[0]?.project_id;
|
const projectId = events[0]?.project_id;
|
||||||
if (options.profile && projectId) {
|
if (options.profile && projectId) {
|
||||||
const ids = events.map((e) => e.profile_id);
|
const ids = events
|
||||||
|
.filter((e) => e.device_id !== e.profile_id)
|
||||||
|
.map((e) => e.profile_id);
|
||||||
const profiles = await getProfiles(ids, projectId);
|
const profiles = await getProfiles(ids, projectId);
|
||||||
|
|
||||||
const map = new Map<string, IServiceProfile>();
|
const map = new Map<string, IServiceProfile>();
|
||||||
|
|||||||
Reference in New Issue
Block a user