Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 13 additions & 19 deletions apps/api/src/controllers/export.controller.ts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ async function getProjectId(
return projectId;
}

const preprocessCommaSeparatedArray = (arg: unknown) => {
if (arg == null) return undefined;
if (Array.isArray(arg)) return arg;
if (typeof arg === 'string')
return arg.split(',').map((s) => s.trim()).filter(Boolean);
return arg;
};

export const eventsScheme = z.object({
project_id: z.string().optional(),
projectId: z.string().optional(),
Expand All @@ -83,24 +91,8 @@ export const eventsScheme = z.object({
return value;
}, z.array(zChartEventFilter))
.optional(),
includes: z
.preprocess(
(arg) => {
if (arg == null) {
return undefined;
}
if (Array.isArray(arg)) {
return arg;
}
if (typeof arg === 'string') {
const parts = arg.split(',').map((s) => s.trim()).filter(Boolean);
return parts;
}
return arg;
},
z.array(z.string())
)
.optional(),
includes: z.preprocess(preprocessCommaSeparatedArray, z.array(z.string())).optional(),
property_keys: z.preprocess(preprocessCommaSeparatedArray, z.array(z.string())).optional(),
});

export async function events(
Expand All @@ -110,7 +102,7 @@ export async function events(
reply: FastifyReply
) {
const projectId = await getProjectId(request);
const { limit, page: rawPage, event, start, end, profileId, includes, filters } = request.query;
const { limit, page: rawPage, event, start, end, profileId, includes, filters, property_keys } = request.query;
const take = Math.max(Math.min(limit, 1000), 1);
const cursor = Math.max(rawPage, 1) - 1;
const options: GetEventListOptions = {
Expand All @@ -122,9 +114,11 @@ export async function events(
take,
profileId,
filters,
propertyKeys: property_keys,
select: {
profile: false,
meta: false,
properties: true,
...includes?.reduce((acc, key) => ({ ...acc, [key]: true }), {}),
},
};
Expand Down
9 changes: 8 additions & 1 deletion packages/db/src/services/event.service.ts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ export interface GetEventListOptions {
select?: SelectHelper<IServiceEvent>;
custom?: (sb: SqlBuilderObject) => void;
dateIntervalInDays?: number;
propertyKeys?: string[];
}

export async function getEventList(options: GetEventListOptions) {
Expand All @@ -484,6 +485,7 @@ export async function getEventList(options: GetEventListOptions) {
custom,
select: incomingSelect,
dateIntervalInDays = 0.5,
propertyKeys,
} = options;
const { sb, getSql, join } = createSqlBuilder();

Expand Down Expand Up @@ -548,7 +550,12 @@ export async function getEventList(options: GetEventListOptions) {
sb.select.sessionId = 'session_id';
}
if (select.properties) {
sb.select.properties = 'properties';
if (propertyKeys && propertyKeys.length > 0) {
const keys = [...new Set(propertyKeys)].map((k) => sqlstring.escape(k)).join(', ');
sb.select.properties = `mapFilter((k, v) -> k IN (${keys}), properties) AS properties`;
} else {
sb.select.properties = 'properties';
}
}
if (select.country) {
sb.select.country = 'country';
Expand Down