Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: get metrics in the clusterized env #396

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 28 additions & 18 deletions promql/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}))`)),
Expand All @@ -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)),
Expand All @@ -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)
Expand Down
Loading