From b80e97022026d382c13f8ca0fb2674641eb4e9fa Mon Sep 17 00:00:00 2001 From: Chris Cowan Date: Fri, 11 Aug 2017 01:49:54 +0200 Subject: [PATCH] Fixes #13246 - Add checks for empty annotations (#13422) * Fixes #13246 - Add checkes for empty annotations * Refactoring get_annotations to use async/await --- .../server/lib/vis_data/get_annotations.js | 34 +++++++++---------- .../server/lib/vis_data/get_panel_data.js | 2 +- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/core_plugins/metrics/server/lib/vis_data/get_annotations.js b/src/core_plugins/metrics/server/lib/vis_data/get_annotations.js index cdf84b26c0c55..9b90ac3cd2ee9 100644 --- a/src/core_plugins/metrics/server/lib/vis_data/get_annotations.js +++ b/src/core_plugins/metrics/server/lib/vis_data/get_annotations.js @@ -9,7 +9,7 @@ function validAnnotation(annotation) { annotation.template; } -export default (req, panel) => { +export default async (req, panel) => { const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('data'); const bodies = panel.annotations .filter(validAnnotation) @@ -31,22 +31,22 @@ export default (req, panel) => { }); if (!bodies.length) return { responses: [] }; - return callWithRequest(req, 'msearch', { - body: bodies.reduce((acc, item) => acc.concat(item), []) - }) - .then(resp => { - const results = {}; - panel.annotations - .filter(validAnnotation) - .forEach((annotation, index) => { - const data = resp.responses[index]; - results[annotation.id] = handleAnnotationResponse(data, annotation); - }); - return results; - }) - .catch(error => { - if (error.message === 'missing-indices') return {}; - throw error; + try { + const resp = await callWithRequest(req, 'msearch', { + body: bodies.reduce((acc, item) => acc.concat(item), []) }); + const results = {}; + panel.annotations + .filter(validAnnotation) + .forEach((annotation, index) => { + const data = resp.responses[index]; + results[annotation.id] = handleAnnotationResponse(data, annotation); + }); + return results; + } catch (error) { + if (error.message === 'missing-indices') return { responses: [] }; + throw error; + } + }; diff --git a/src/core_plugins/metrics/server/lib/vis_data/get_panel_data.js b/src/core_plugins/metrics/server/lib/vis_data/get_panel_data.js index dd00dc4dda6f3..d0528ea24df1a 100644 --- a/src/core_plugins/metrics/server/lib/vis_data/get_panel_data.js +++ b/src/core_plugins/metrics/server/lib/vis_data/get_panel_data.js @@ -21,7 +21,7 @@ export default function getPanelData(req) { }; }) .then(resp => { - if (!panel.annotations) return resp; + if (!panel.annotations || panel.annotations.length === 0) return resp; return getAnnotations(req, panel).then(annotations => { resp[panel.id].annotations = annotations; return resp;