fix order by and revert getDateSQL

This commit is contained in:
Francis Cao 2024-08-23 16:43:38 -07:00
parent 8500669ed0
commit 004ccdc22f
4 changed files with 13 additions and 4 deletions

View File

@ -19,6 +19,14 @@ const MYSQL_DATE_FORMATS = {
year: '%Y-01-01T00:00:00Z',
};
const POSTGRESQL_DATE_FORMATS = {
minute: 'YYYY-MM-DD HH24:MI:00',
hour: 'YYYY-MM-DD HH24:00:00',
day: 'YYYY-MM-DD',
month: 'YYYY-MM-01',
year: 'YYYY-01-01',
};
function getAddIntervalQuery(field: string, interval: string): string {
const db = getDatabaseType();
@ -60,18 +68,16 @@ function getDateSQL(field: string, unit: string, timezone?: string): string {
if (db === POSTGRESQL) {
if (timezone) {
return `date_trunc('${unit}', ${field} at time zone '${timezone}')`;
return `to_char(date_trunc('${unit}', ${field} at time zone '${timezone}'), '${POSTGRESQL_DATE_FORMATS[unit]}')`;
}
return `date_trunc('${unit}', ${field})`;
return `to_char(date_trunc('${unit}', ${field}), '${POSTGRESQL_DATE_FORMATS[unit]}')`;
}
if (db === MYSQL) {
if (timezone) {
const tz = moment.tz(timezone).format('Z');
return `date_format(convert_tz(${field},'+00:00','${tz}'), '${MYSQL_DATE_FORMATS[unit]}')`;
}
return `date_format(${field}, '${MYSQL_DATE_FORMATS[unit]}')`;
}
}

View File

@ -31,6 +31,7 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {
and event_type = {{eventType}}
${filterQuery}
group by 1
order by 1
`,
params,
);

View File

@ -24,6 +24,7 @@ async function relationalQuery(
createdAt: { gte: startDate, lte: endDate },
},
take: 500,
orderBy: { createdAt: 'desc' },
});
}

View File

@ -31,6 +31,7 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {
and event_type = {{eventType}}
${filterQuery}
group by 1
order by 1
`,
params,
);