fix(candles): serve chart from cache and stabilize any-source
This commit is contained in:
@@ -144,7 +144,14 @@ function chunkSecondsForBucket(bucketSeconds) {
|
||||
|
||||
function sqlUpsertCandlesFromTicks({ symbol, sourceKey, bucketSeconds, fromIso, toIso }) {
|
||||
return `
|
||||
WITH base AS (
|
||||
WITH chosen AS (
|
||||
SELECT source AS chosen_source
|
||||
FROM public.drift_ticks
|
||||
WHERE symbol = ${sqlLit(symbol)}
|
||||
ORDER BY ts DESC
|
||||
LIMIT 1
|
||||
),
|
||||
base AS (
|
||||
SELECT
|
||||
time_bucket(make_interval(secs => ${bucketSeconds}), ts) AS bucket,
|
||||
ts,
|
||||
@@ -154,7 +161,10 @@ function sqlUpsertCandlesFromTicks({ symbol, sourceKey, bucketSeconds, fromIso,
|
||||
WHERE symbol = ${sqlLit(symbol)}
|
||||
AND ts >= ${sqlLit(fromIso)}::timestamptz
|
||||
AND ts < ${sqlLit(toIso)}::timestamptz
|
||||
AND (${sqlLit(sourceKey)} = '' OR source = ${sqlLit(sourceKey)})
|
||||
AND (
|
||||
${sqlLit(sourceKey)} <> '' AND source = ${sqlLit(sourceKey)}
|
||||
OR ${sqlLit(sourceKey)} = '' AND source = COALESCE((SELECT chosen_source FROM chosen), source)
|
||||
)
|
||||
),
|
||||
agg AS (
|
||||
SELECT
|
||||
@@ -195,11 +205,26 @@ function sqlDeleteOlderCandles({ symbol, sourceKey, bucketSeconds, cutoffIso })
|
||||
}
|
||||
|
||||
async function getTickRange(cfg, { symbol, sourceKey }) {
|
||||
const sql = `
|
||||
const sql =
|
||||
String(sourceKey) === ''
|
||||
? `
|
||||
WITH chosen AS (
|
||||
SELECT source AS chosen_source
|
||||
FROM public.drift_ticks
|
||||
WHERE symbol=${sqlLit(symbol)}
|
||||
ORDER BY ts DESC
|
||||
LIMIT 1
|
||||
)
|
||||
SELECT min(ts) AS min_ts, max(ts) AS max_ts
|
||||
FROM public.drift_ticks
|
||||
WHERE symbol=${sqlLit(symbol)}
|
||||
AND (${sqlLit(sourceKey)} = '' OR source = ${sqlLit(sourceKey)});
|
||||
AND source = COALESCE((SELECT chosen_source FROM chosen), source);
|
||||
`
|
||||
: `
|
||||
SELECT min(ts) AS min_ts, max(ts) AS max_ts
|
||||
FROM public.drift_ticks
|
||||
WHERE symbol=${sqlLit(symbol)}
|
||||
AND source = ${sqlLit(sourceKey)};
|
||||
`;
|
||||
const out = await hasuraRunSql(cfg, sql, { readOnly: true });
|
||||
const row = Array.isArray(out?.result) && out.result.length >= 2 ? out.result[1] : null;
|
||||
|
||||
Reference in New Issue
Block a user