Skip to content

Commit

Permalink
Reverted getDateQuery logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecao committed Jul 21, 2022
1 parent 358c725 commit 250cdce
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 18 deletions.
16 changes: 2 additions & 14 deletions lib/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,12 @@ export function getDatabase() {
return type;
}

export function getDateStringQuery(data, unit) {
const db = getDatabase();

if (db === POSTGRESQL) {
return `to_char(${data}, '${POSTGRESQL_DATE_FORMATS[unit]}')`;
}

if (db === MYSQL) {
return `DATE_FORMAT(${data}, '${MYSQL_DATE_FORMATS[unit]}')`;
}
}

export function getDateQuery(field, unit, timezone) {
const db = getDatabase();

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})`;
}
Expand All @@ -40,7 +28,7 @@ export function getDateQuery(field, unit, timezone) {
if (timezone) {
const tz = moment.tz(timezone).format('Z');

return `convert_tz(${field},'+00:00','${tz}')`;
return `date_format(convert_tz(${field},'+00:00','${tz}', '${MYSQL_DATE_FORMATS[unit]}')`;
}

return `${field}`;
Expand Down
4 changes: 2 additions & 2 deletions queries/analytics/event/getEventMetrics.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getDateQuery, getDateStringQuery, getFilterQuery, rawQuery } from 'lib/queries';
import { getDateQuery, getFilterQuery, rawQuery } from 'lib/queries';

export function getEventMetrics(
website_id,
Expand All @@ -14,7 +14,7 @@ export function getEventMetrics(
`
select
event_value x,
${getDateStringQuery(getDateQuery('created_at', unit, timezone), unit)} t,
${getDateQuery('created_at', unit, timezone)} t,
count(*) y
from event
where website_id=$1
Expand Down
4 changes: 2 additions & 2 deletions queries/analytics/pageview/getPageviewStats.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parseFilters, rawQuery, getDateQuery, getDateStringQuery } from 'lib/queries';
import { parseFilters, rawQuery, getDateQuery } from 'lib/queries';

export function getPageviewStats(
website_id,
Expand All @@ -20,7 +20,7 @@ export function getPageviewStats(
return rawQuery(
`
select
${getDateStringQuery('g.t', unit)} as t,
g.t as t,
g.y as y
from
(select ${getDateQuery('pageview.created_at', unit, timezone)} t,
Expand Down

0 comments on commit 250cdce

Please sign in to comment.