Search: Fix tags query (#55851)

* Search: fix tags query

* Search: fix type
This commit is contained in:
Artur Wierzbicki 2022-09-27 21:16:49 +04:00 committed by GitHub
parent 3409979288
commit 814211282a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 16 deletions

View File

@ -63,11 +63,12 @@ func (s *searchHTTPService) doQuery(c *models.ReqContext) response.Response {
return response.Error(500, "error handling search request", resp.Error)
}
if len(resp.Frames) != 1 {
return response.Error(500, "invalid search response", errors.New("invalid search response"))
if len(resp.Frames) == 0 {
msg := "invalid search response"
return response.Error(500, msg, errors.New(msg))
}
bytes, err := resp.Frames[0].MarshalJSON()
bytes, err := resp.MarshalJSON()
if err != nil {
return response.Error(500, "error marshalling response", err)
}

View File

@ -1,6 +1,7 @@
import {
ArrayVector,
DataFrame,
DataFrameJSON,
DataFrameView,
getDisplayProcessor,
SelectableValue,
@ -19,6 +20,10 @@ const loadingFrameName = 'Loading';
const searchURI = 'api/search-v2';
type SearchAPIResponse = {
frames: DataFrameJSON[];
};
export class BlugeSearcher implements GrafanaSearcher {
constructor(private fallbackSearcher: GrafanaSearcher) {}
@ -51,15 +56,19 @@ export class BlugeSearcher implements GrafanaSearcher {
limit: 1, // 0 would be better, but is ignored by the backend
};
const frame = toDataFrame(await getBackendSrv().post(searchURI, req));
const resp = await getBackendSrv().post<SearchAPIResponse>(searchURI, req);
const frames = resp.frames.map((f) => toDataFrame(f));
if (frame?.name === loadingFrameName) {
if (frames[0]?.name === loadingFrameName) {
return this.fallbackSearcher.tags(query);
}
if (frame.fields[0].name === 'tag') {
return getTermCountsFrom(frame);
for (const frame of frames) {
if (frame.fields[0].name === 'tag') {
return getTermCountsFrom(frame);
}
}
return [];
}
@ -92,9 +101,10 @@ export class BlugeSearcher implements GrafanaSearcher {
limit: query.limit ?? firstPageSize,
};
const rsp = await getBackendSrv().post(searchURI, req);
const rsp = await getBackendSrv().post<SearchAPIResponse>(searchURI, req);
const frames = rsp.frames.map((f) => toDataFrame(f));
const first = rsp ? toDataFrame(rsp) : { fields: [], length: 0 };
const first = frames.length ? toDataFrame(frames[0]) : { fields: [], length: 0 };
if (first.name === loadingFrameName) {
return this.fallbackSearcher.search(query);
@ -138,13 +148,12 @@ export class BlugeSearcher implements GrafanaSearcher {
if (from >= meta.count) {
return;
}
const frame = toDataFrame(
await getBackendSrv().post(searchURI, {
...(req ?? {}),
from,
limit: nextPageSizes,
})
);
const resp = await getBackendSrv().post<SearchAPIResponse>(searchURI, {
...(req ?? {}),
from,
limit: nextPageSizes,
});
const frame = toDataFrame(resp.frames[0]);
if (!frame) {
console.log('no results', frame);