From db7764cdcb4964354579d8f0b04f552ec0fd2a16 Mon Sep 17 00:00:00 2001 From: akvlad Date: Mon, 27 Nov 2023 13:46:11 +0200 Subject: [PATCH] fix: get metrics in the clusterized env --- promql/index.js | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/promql/index.js b/promql/index.js index 1d9cf8aa..0268758d 100644 --- a/promql/index.js +++ b/promql/index.js @@ -101,7 +101,7 @@ const getIdxSubquery = (conds, fromMs, toMs) => { const toS = Math.floor(toMs / 1000) return (new Sql.Select()) .select('fingerprint') - .from('time_series_gin') + .from([DATABASE_NAME() + '.time_series_gin', 'time_series_gin']) .where(Sql.And( Sql.Or(...conds), Sql.Gte('date', new Sql.Raw(`toDate(fromUnixTimestamp(${fromS}))`)), @@ -118,15 +118,21 @@ module.exports.getData = async (matchers, fromMs, toMs) => { const db = DATABASE_NAME() const matches = getMatchersIdxCond(matchers) const idx = getIdxSubquery(matches, fromMs, toMs) - const withIdx = new Sql.With('idx', idx, !!clusterName) + const timeSeries = (new Sql.Select()) + .select( + 'fingerprint', + [new Sql.Raw('arraySort(JSONExtractKeysAndValues(labels, \'String\'))'), 'labels'] + ).from(DATABASE_NAME() + '.time_series') + .where(new Sql.In('fingerprint', 'in', new Sql.WithReference(withIdx))) + const withTimeSeries = new Sql.With('timeSeries', timeSeries, !!clusterName) const raw = (new Sql.Select()) .with(withIdx) .select( [new Sql.Raw('argMaxMerge(last)'), 'value'], 'fingerprint', [new Sql.Raw('intDiv(timestamp_ns, 15000000000) * 15000'), 'timestamp_ms']) - .from(`metrics_15s${_dist}`) + .from([`metrics_15s${_dist}`, 'metrics_15s']) .where( new Sql.And( new Sql.In('fingerprint', 'in', new Sql.WithReference(withIdx)), @@ -135,27 +141,31 @@ module.exports.getData = async (matchers, fromMs, toMs) => { ) ).groupBy('fingerprint', 'timestamp_ms') .orderBy('fingerprint', 'timestamp_ms') - const timeSeries = (new Sql.Select()) - .select( - 'fingerprint', - [new Sql.Raw('arraySort(JSONExtractKeysAndValues(labels, \'String\'))'), 'labels'] - ).from('time_series') - .where(new Sql.In('fingerprint', 'in', new Sql.WithReference(withIdx))) - const withRaw = new Sql.With('raw', raw, false) - const withTimeSeries = new Sql.With('timeSeries', timeSeries, false) + if (clusterName) { + raw.select([new Sql.Raw('min(time_series.labels)'), 'labels']).join( + [new Sql.WithReference(withTimeSeries), 'time_series'], + 'any left', + Sql.Eq('time_series.fingerprint', new Sql.Raw('metrics_15s.fingerprint')) + ) + } + const withRaw = new Sql.With('raw', raw, !!clusterName) const res = (new Sql.Select()) - .with(withRaw, withTimeSeries) + .with(withRaw) .select( [new Sql.Raw('any(labels)'), 'stream'], [new Sql.Raw('arraySort(groupArray((raw.timestamp_ms, raw.value)))'), 'values'] ).from([new Sql.WithReference(withRaw), 'raw']) - .join( - [new Sql.WithReference(withTimeSeries), 'time_series'], - 'any left', - Sql.Eq('time_series.fingerprint', new Sql.Raw('raw.fingerprint')) - ).groupBy('raw.fingerprint') + .groupBy('raw.fingerprint') .orderBy('raw.fingerprint') - + if (!clusterName) { + res.with(withTimeSeries) + .join( + [new Sql.WithReference(withTimeSeries), 'time_series'], + 'any left', + Sql.Eq('time_series.fingerprint', new Sql.Raw('raw.fingerprint')) + ) + } + console.log(res.toString()) const data = await rawRequest(res.toString() + ' FORMAT RowBinary', null, db, { responseType: 'arraybuffer' }) return new Uint8Array(data.data)