From 6e80b491205168a2744970bfd0403a76447e580b Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Thu, 26 Sep 2019 21:35:00 +0300 Subject: [PATCH] backend: handle response on the fronted --- pkg/zabbix_api.go | 18 +++++++++++++++++- .../zabbix_api/zabbixAPIConnector.js | 13 ++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/pkg/zabbix_api.go b/pkg/zabbix_api.go index 3890540..0e98b47 100644 --- a/pkg/zabbix_api.go +++ b/pkg/zabbix_api.go @@ -90,7 +90,23 @@ func (ds *ZabbixDatasource) ZabbixAPIQuery(ctx context.Context, tsdbReq *datasou resultByte, err := result.MarshalJSON() ds.logger.Debug("ZabbixAPIQuery", "result", string(resultByte)) - return nil, errors.New("ZabbixAPIQuery is not implemented yet") + return ds.BuildResponse(result) +} + +func (ds *ZabbixDatasource) BuildResponse(result *simplejson.Json) (*datasource.DatasourceResponse, error) { + resultByte, err := result.MarshalJSON() + if err != nil { + return nil, err + } + + return &datasource.DatasourceResponse{ + Results: []*datasource.QueryResult{ + &datasource.QueryResult{ + RefId: "zabbixAPI", + MetaJson: string(resultByte), + }, + }, + }, nil } func (ds *ZabbixDatasource) ZabbixRequest(ctx context.Context, dsInfo *datasource.DatasourceInfo, method string, params *simplejson.Json) (*simplejson.Json, error) { diff --git a/src/datasource-zabbix/zabbix/connectors/zabbix_api/zabbixAPIConnector.js b/src/datasource-zabbix/zabbix/connectors/zabbix_api/zabbixAPIConnector.js index 6ad3831..f9229bf 100644 --- a/src/datasource-zabbix/zabbix/connectors/zabbix_api/zabbixAPIConnector.js +++ b/src/datasource-zabbix/zabbix/connectors/zabbix_api/zabbixAPIConnector.js @@ -40,7 +40,10 @@ export class ZabbixAPIConnector { ////////////////////////// request(method, params) { - this.tsdbRequest(method, params); + this.tsdbRequest(method, params).then(response => { + const result = this.handleTsdbResponse(response); + console.log(result); + }); return this.zabbixAPICore.request(this.url, method, params, this.requestOptions, this.auth) .catch(error => { @@ -79,6 +82,14 @@ export class ZabbixAPIConnector { }); } + handleTsdbResponse(response) { + if (!response || !response.data || !response.data.results) { + return []; + } + + return response.data.results['zabbixAPI'].meta; + } + /** * When API unauthenticated or auth token expired each request produce login() * call. But auth token is common to all requests. This function wraps login() method