Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

fix(export): /export/events date range truncated to calendar date, not exact timestamp (#477)

Kristo Kiis484effef03f8
diff --git a/packages/db/src/services/event.service.ts b/packages/db/src/services/event.service.ts
index f81cbfdec..7116b8817 100644
--- a/packages/db/src/services/event.service.ts
+++ b/packages/db/src/services/event.service.ts
@@ -490,6 +490,11 @@ export interface GetEventListOptions {
dateIntervalInDays?: number;
}
+/**
+ * Fetches a page of events matching the given filters/date range, ordered
+ * newest first. Falls back to a default recent-days cursor window when no
+ * cursor or explicit date bound is provided.
+ */
export async function getEventList(options: GetEventListOptions) {
const {
cursor,
@@ -527,7 +532,7 @@ export async function getEventList(options: GetEventListOptions) {
sb.where.cursor = `created_at < ${sqlstring.escape(formatClickhouseDate(cursor))}`;
}
- if (!(cursor || (startDate && endDate))) {
+ if (cursor === undefined && !startDate && !endDate) {
sb.where.cursorWindow = `created_at >= toDateTime64(${sqlstring.escape(formatClickhouseDate(new Date()))}, 3) - INTERVAL ${safeDateIntervalInDays} DAY`;
}
@@ -665,8 +670,11 @@ export async function getEventList(options: GetEventListOptions) {
sb.where.cohortId = `profile_id IN (SELECT profile_id FROM ${TABLE_NAMES.cohort_members} FINAL WHERE cohort_id = ${sqlstring.escape(cohortId)} AND project_id = ${sqlstring.escape(projectId)})`;
}
- if (startDate && endDate) {
- sb.where.created_at = `toDate(created_at) BETWEEN toDate('${formatClickhouseDate(startDate)}') AND toDate('${formatClickhouseDate(endDate)}')`;
+ if (startDate) {
+ sb.where.startDate = `created_at >= toDateTime64(${sqlstring.escape(startDate.toISOString().replace('T', ' ').replace('Z', ''))}, 3)`;
+ }
+ if (endDate) {
+ sb.where.endDate = `created_at <= toDateTime64(${sqlstring.escape(endDate.toISOString().replace('T', ' ').replace('Z', ''))}, 3)`;
}
if (events && events.length > 0) {
@@ -725,6 +733,10 @@ export async function getEventList(options: GetEventListOptions) {
return data;
}
+/**
+ * Counts events matching the given filters/date range, using the same
+ * where-clause construction as getEventList.
+ */
export async function getEventsCount({
projectId,
profileId,
@@ -749,8 +761,11 @@ export async function getEventsCount({
sb.where.cohortId = `profile_id IN (SELECT profile_id FROM ${TABLE_NAMES.cohort_members} FINAL WHERE cohort_id = ${sqlstring.escape(cohortId)} AND project_id = ${sqlstring.escape(projectId)})`;
}
- if (startDate && endDate) {
- sb.where.created_at = `toDate(created_at) BETWEEN toDate('${formatClickhouseDate(startDate)}') AND toDate('${formatClickhouseDate(endDate)}')`;
+ if (startDate) {
+ sb.where.startDate = `created_at >= toDateTime64(${sqlstring.escape(startDate.toISOString().replace('T', ' ').replace('Z', ''))}, 3)`;
+ }
+ if (endDate) {
+ sb.where.endDate = `created_at <= toDateTime64(${sqlstring.escape(endDate.toISOString().replace('T', ' ').replace('Z', ''))}, 3)`;
}
if (events && events.length > 0) {