From d2feeb84554376b0e60fb0b7ec99abfeecfacb18 Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Wed, 20 Apr 2022 11:14:57 -0400 Subject: [PATCH] PanelQueryRunner: use refId from results if the `key` value was not set in the packet (#47598) (#47647) (cherry picked from commit f80a0d2a9b0ac25649246711309954f560045f58) Co-authored-by: Ryan McKinley --- .../features/query/state/runRequest.test.ts | 36 ++++++++++++++++++- public/app/features/query/state/runRequest.ts | 4 ++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/public/app/features/query/state/runRequest.test.ts b/public/app/features/query/state/runRequest.test.ts index 6742d37d0bf..8a40ef7fd08 100644 --- a/public/app/features/query/state/runRequest.test.ts +++ b/public/app/features/query/state/runRequest.test.ts @@ -156,7 +156,7 @@ describe('runRequest', () => { }); }); - runRequestScenario('After tree responses, 2 with different keys', (ctx) => { + runRequestScenario('After three responses, 2 with different keys', (ctx) => { ctx.setup(() => { ctx.start(); ctx.emitPacket({ @@ -186,6 +186,40 @@ describe('runRequest', () => { }); }); + runRequestScenario('When the key is defined in refId', (ctx) => { + ctx.setup(() => { + ctx.start(); + ctx.emitPacket({ + data: [{ name: 'DataX-1', refId: 'X' } as DataFrame], + }); + ctx.emitPacket({ + data: [{ name: 'DataY-1', refId: 'Y' } as DataFrame], + }); + ctx.emitPacket({ + data: [{ name: 'DataY-2', refId: 'Y' } as DataFrame], + }); + }); + + it('should emit 3 separate results', () => { + expect(ctx.results.length).toBe(3); + }); + + it('should keep data for X and Y', () => { + expect(ctx.results[2].series).toMatchInlineSnapshot(` + Array [ + Object { + "name": "DataX-1", + "refId": "X", + }, + Object { + "name": "DataY-2", + "refId": "Y", + }, + ] + `); + }); + }); + runRequestScenario('After response with state Streaming', (ctx) => { ctx.setup(() => { ctx.start(); diff --git a/public/app/features/query/state/runRequest.ts b/public/app/features/query/state/runRequest.ts index 370df008e9b..76a7442ab3f 100644 --- a/public/app/features/query/state/runRequest.ts +++ b/public/app/features/query/state/runRequest.ts @@ -43,7 +43,9 @@ export function processResponsePacket(packet: DataQueryResponse, state: RunningQ ...state.packets, }; - packets[packet.key || 'A'] = packet; + // updates to the same key will replace previous values + const key = packet.key ?? packet.data?.[0]?.refId ?? 'A'; + packets[key] = packet; let loadingState = packet.state || LoadingState.Done; let error: DataQueryError | undefined = undefined;