fix custom range queries
This commit is contained in:
parent
bfa054c76c
commit
84b5475270
|
@ -24,7 +24,8 @@ export function withClause (range) {
|
||||||
return `
|
return `
|
||||||
WITH range_values AS (
|
WITH range_values AS (
|
||||||
SELECT date_trunc('${unit}', $1) as minval,
|
SELECT date_trunc('${unit}', $1) as minval,
|
||||||
date_trunc('${unit}', $2) as maxval),
|
date_trunc('${unit}', $2) as maxval
|
||||||
|
),
|
||||||
times AS (
|
times AS (
|
||||||
SELECT generate_series(minval, maxval, interval '1 ${unit}') as time
|
SELECT generate_series(minval, maxval, interval '1 ${unit}') as time
|
||||||
FROM range_values
|
FROM range_values
|
||||||
|
@ -35,7 +36,7 @@ export function withClause (range) {
|
||||||
export function intervalClause (range, table) {
|
export function intervalClause (range, table) {
|
||||||
const unit = timeUnitForRange(range)
|
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) {
|
export function viewIntervalClause (range, view) {
|
||||||
|
@ -63,7 +64,7 @@ export default {
|
||||||
`${withClause(range)}
|
`${withClause(range)}
|
||||||
SELECT time, json_build_array(
|
SELECT time, json_build_array(
|
||||||
json_build_object('name', 'referrals', 'value', count("referrerId")),
|
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
|
) AS data
|
||||||
FROM times
|
FROM times
|
||||||
LEFT JOIN users ON ${intervalClause(range, 'users')} AND time = date_trunc('${timeUnitForRange(range)}', created_at)
|
LEFT JOIN users ON ${intervalClause(range, 'users')} AND time = date_trunc('${timeUnitForRange(range)}', created_at)
|
||||||
|
|
|
@ -202,7 +202,7 @@ function whenClause (when, table) {
|
||||||
export function whenRange (when, from, to = new Date()) {
|
export function whenRange (when, from, to = new Date()) {
|
||||||
switch (when) {
|
switch (when) {
|
||||||
case 'custom':
|
case 'custom':
|
||||||
return [new Date(from), new Date(to)]
|
return [dayMonthYearToDate(from), dayMonthYearToDate(to)]
|
||||||
default:
|
default:
|
||||||
return [dayMonthYearToDate(whenToFrom(when)), new Date(to)]
|
return [dayMonthYearToDate(whenToFrom(when)), new Date(to)]
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ export default {
|
||||||
FROM users
|
FROM users
|
||||||
WHERE ${intervalClause(range, 'users')}
|
WHERE ${intervalClause(range, 'users')}
|
||||||
AND "referrerId" = $3
|
AND "referrerId" = $3
|
||||||
`, ...range, Number(me.id))
|
`, ...range, Number(me.id))
|
||||||
|
|
||||||
const stats = await models.$queryRawUnsafe(
|
const stats = await models.$queryRawUnsafe(
|
||||||
`${withClause(range)}
|
`${withClause(range)}
|
||||||
|
|
Loading…
Reference in New Issue