diff --git a/public/app/overrides/services/tags.ts b/public/app/overrides/services/tags.ts index cc589d9b5..4d124ffc5 100644 --- a/public/app/overrides/services/tags.ts +++ b/public/app/overrides/services/tags.ts @@ -1,7 +1,61 @@ -import { Result } from '@webapp/util/fp'; +import { parseResponse, request } from '@webapp/services/base'; +import { z } from 'zod'; + +const seriesLabelsSchema = z.preprocess( + (a: any) => { + if ('labelsSet' in a) { + return a; + } + + return { labelsSet: [{ labels: [] }] }; + }, + z.object({ + labelsSet: z.array( + z.object({ + labels: z.array( + z.object({ + name: z.string(), + value: z.string(), + }) + ), + }) + ), + }) +); + +async function fetchLabelsSeries( + query: string, + transformFn: (t: { name: string; value: string }[]) => T +) { + const profileTypeID = query.replace(/\{.*/g, ''); + const response = await request('/querier.v1.QuerierService/Series', { + method: 'POST', + body: JSON.stringify({ + matchers: [`{__profile_type__=\"${profileTypeID}\"}`], + }), + headers: { + 'content-type': 'application/json', + }, + }); + const isMetaTag = (tag: string) => tag.startsWith('__') && tag.endsWith('__'); + + return parseResponse( + response, + seriesLabelsSchema + .transform((res) => { + return res.labelsSet + .flatMap((a) => a.labels) + .filter((a) => !isMetaTag(a.name)); + }) + .transform(transformFn) + ); +} export async function fetchTags(query: string, from: number, until: number) { - return Result.ok([]); + return fetchLabelsSeries(query, function (t) { + const labelNames = t.map((a) => a.name); + return Array.from(new Set(labelNames)); + }); } export async function fetchLabelValues( @@ -10,7 +64,8 @@ export async function fetchLabelValues( from: number, until: number ) { - return Result.err({ - message: 'TODO: implement ', + return fetchLabelsSeries(query, function (t) { + const labelValues = t.filter((l) => label === l.name).map((a) => a.value); + return Array.from(new Set(labelValues)); }); } diff --git a/scripts/webpack/webpack.dev.js b/scripts/webpack/webpack.dev.js index 777e8599d..7637bb58f 100644 --- a/scripts/webpack/webpack.dev.js +++ b/scripts/webpack/webpack.dev.js @@ -8,6 +8,7 @@ module.exports = merge(common, { port: 4040, proxy: { '/pyroscope': 'http://localhost:4100', + '/querier.v1.QuerierService': 'http://localhost:4100', }, }, optimization: {