From b79f398d88ccd63bd459ed275ae2f598bbb9ae7b Mon Sep 17 00:00:00 2001 From: rozetko Date: Fri, 28 Feb 2020 12:10:16 +0300 Subject: [PATCH 1/2] Import analytic units: display import errors #406 (#408) --- src/panel/graph_panel/graph_ctrl.ts | 39 +++++++++++++++---- .../graph_panel/services/analytic_service.ts | 24 +++++++----- 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/src/panel/graph_panel/graph_ctrl.ts b/src/panel/graph_panel/graph_ctrl.ts index dd879e3..2536750 100644 --- a/src/panel/graph_panel/graph_ctrl.ts +++ b/src/panel/graph_panel/graph_ctrl.ts @@ -578,12 +578,37 @@ class GraphCtrl extends MetricsPanelCtrl { panelId: string, datasourceUrl: string ) => { - // TODO: use template variables from form - await this.importPanel(JSON.parse(panelTemplate), { - grafanaUrl: this._grafanaUrl, - panelId: this._panelId, - datasourceUrl: this._datasourceRequest.url - }); + try { + // TODO: use template variables from form (or not?) + await this.importPanel(JSON.parse(panelTemplate), { + grafanaUrl: this._grafanaUrl, + panelId: this._panelId, + datasourceUrl: this._datasourceRequest.url + }); + + appEvents.emit( + 'alert-success', + [ + // TODO: analytic units count? + 'Analytic units are imported successfully' + ] + ); + + await this.onHasticDatasourceChange(); + } catch(e) { + let status = e.status !== undefined ? `Status code: ${e.status}` : ''; + let message = e.message; + if(e.data !== undefined && e.data.message !== undefined) { + message = e.data.message; + } + appEvents.emit( + 'alert-error', + [ + `Error while importing analytic units. ${status}`, + message + ] + ); + } }; this.publishAppEvent('show-modal', { @@ -594,7 +619,7 @@ class GraphCtrl extends MetricsPanelCtrl { async importPanel(panelTemplate: PanelTemplate, templateVariables: TemplateVariables): Promise { // TODO: show import errors properly - await this.analyticsController.importPanel(panelTemplate, templateVariables); + return this.analyticsController.importPanel(panelTemplate, templateVariables); } // getAnnotationsByTag(tag) { diff --git a/src/panel/graph_panel/services/analytic_service.ts b/src/panel/graph_panel/services/analytic_service.ts index 7888f38..51abb66 100644 --- a/src/panel/graph_panel/services/analytic_service.ts +++ b/src/panel/graph_panel/services/analytic_service.ts @@ -74,7 +74,8 @@ export class AnalyticService { panelTemplate: PanelTemplate, templateVariables: TemplateVariables ): Promise { - await this.post('/panels/template', { panelTemplate, templateVariables }); + const rawError = true; + return this.post('/panels/template', { panelTemplate, templateVariables }, rawError); } async postNewAnalyticUnit( @@ -282,7 +283,7 @@ export class AnalyticService { return this.post('/analyticUnits/detect', { ids, from, to }); } - private async _analyticRequest(method: string, url: string, data?: any) { + private async _analyticRequest(method: string, url: string, data?: any, rawError: boolean = false) { try { method = method.toUpperCase(); url = this._hasticDatasourceURL + url; @@ -296,6 +297,9 @@ export class AnalyticService { this._isUp = true; return response.data; } catch(error) { + if(rawError) { + throw error; + } // xhrStatus may be one of: ('complete', 'error', 'timeout' or 'abort') // See: https://github.com/angular/angular.js/blob/55075b840c9194b8524627a293d6166528b9a1c2/src/ng/http.js#L919-L920 if(error.xhrStatus !== 'complete' || error.status > 500) { @@ -336,20 +340,20 @@ export class AnalyticService { return true; } - private async get(url, params?) { - return this._analyticRequest('GET', url, params); + private async get(url: string, params?: any, rawError: boolean = false): Promise { + return this._analyticRequest('GET', url, params, rawError); } - private async post(url, data?) { - return this._analyticRequest('POST', url, data); + private async post(url: string, data?: any, rawError: boolean = false): Promise { + return this._analyticRequest('POST', url, data, rawError); } - private async patch(url, data?) { - return this._analyticRequest('PATCH', url, data); + private async patch(url: string, data?: any, rawError: boolean = false): Promise { + return this._analyticRequest('PATCH', url, data, rawError); } - private async delete(url, data?) { - return this._analyticRequest('DELETE', url, data); + private async delete(url: string, data?: any, rawError: boolean = false): Promise { + return this._analyticRequest('DELETE', url, data, rawError); } private _displayNoConnectionAlert(statusText: string): void { From 6298d3d7cd1d419e5df7d60895d6982256469bc3 Mon Sep 17 00:00:00 2001 From: Alexander Velikiy <39257464+VargBurz@users.noreply.github.com> Date: Fri, 28 Feb 2020 12:10:45 +0300 Subject: [PATCH 2/2] TypeError: Cannot read property 'isUp' of undefined #398 (#416) --- src/panel/graph_panel/graph_ctrl.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/panel/graph_panel/graph_ctrl.ts b/src/panel/graph_panel/graph_ctrl.ts index 2536750..95fd7cd 100644 --- a/src/panel/graph_panel/graph_ctrl.ts +++ b/src/panel/graph_panel/graph_ctrl.ts @@ -338,11 +338,21 @@ class GraphCtrl extends MetricsPanelCtrl { this._analyticUnitsToShow ); - if(this.analyticService.isUp) { - await this.analyticsController.init(); + if(this.analyticService === undefined) { + appEvents.emit( + 'alert-error', + [ + 'Select Hastic datasource', + 'Panel config -> Visualization' + ] + ); + } else { + if(this.analyticService.isUp) { + await this.analyticsController.init(); - this._updatePanelInfo(); - this.analyticsController.updateServerInfo(); + this._updatePanelInfo(); + this.analyticsController.updateServerInfo(); + } } this._graphRenderer = new GraphRenderer(