fix custom range queries

This commit is contained in:
keyan 2023-11-13 17:15:43 -06:00
parent bfa054c76c
commit 84b5475270
3 changed files with 6 additions and 5 deletions

View File

@ -24,7 +24,8 @@ export function withClause (range) {
return `
WITH range_values AS (
SELECT date_trunc('${unit}', $1) as minval,
date_trunc('${unit}', $2) as maxval),
date_trunc('${unit}', $2) as maxval
),
times AS (
SELECT generate_series(minval, maxval, interval '1 ${unit}') as time
FROM range_values
@ -35,7 +36,7 @@ export function withClause (range) {
export function intervalClause (range, table) {
const unit = timeUnitForRange(range)
return `"${table}".created_at >= date_trunc('${unit}', timezone('America/Chicago', $1)) AND "${table}".created_at <= date_trunc('${unit}', timezone('America/Chicago', $2)) `
return `date_trunc('${unit}', "${table}".created_at) >= date_trunc('${unit}', $1) AND date_trunc('${unit}', "${table}".created_at) <= date_trunc('${unit}', $2) `
}
export function viewIntervalClause (range, view) {
@ -63,7 +64,7 @@ export default {
`${withClause(range)}
SELECT time, json_build_array(
json_build_object('name', 'referrals', 'value', count("referrerId")),
json_build_object('name', 'organic', 'value', count(users.id) FILTER(WHERE id > ${PLACEHOLDERS_NUM}) - count("inviteId"))
json_build_object('name', 'organic', 'value', count(users.id) FILTER(WHERE id > ${PLACEHOLDERS_NUM}) - count("referrerId"))
) AS data
FROM times
LEFT JOIN users ON ${intervalClause(range, 'users')} AND time = date_trunc('${timeUnitForRange(range)}', created_at)

View File

@ -202,7 +202,7 @@ function whenClause (when, table) {
export function whenRange (when, from, to = new Date()) {
switch (when) {
case 'custom':
return [new Date(from), new Date(to)]
return [dayMonthYearToDate(from), dayMonthYearToDate(to)]
default:
return [dayMonthYearToDate(whenToFrom(when)), new Date(to)]
}

View File

@ -24,7 +24,7 @@ export default {
FROM users
WHERE ${intervalClause(range, 'users')}
AND "referrerId" = $3
`, ...range, Number(me.id))
`, ...range, Number(me.id))
const stats = await models.$queryRawUnsafe(
`${withClause(range)}