use cache for alert queries
This commit is contained in:
@@ -36,9 +36,11 @@ type ZabbixAPIResourceRequest struct {
|
|||||||
|
|
||||||
type ZabbixAPIRequest struct {
|
type ZabbixAPIRequest struct {
|
||||||
Method string `json:"method"`
|
Method string `json:"method"`
|
||||||
Params map[string]interface{} `json:"params,omitempty"`
|
Params ZabbixAPIParams `json:"params,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ZabbixAPIParams = map[string]interface{}
|
||||||
|
|
||||||
type ZabbixAPIResourceResponse struct {
|
type ZabbixAPIResourceResponse struct {
|
||||||
Result interface{} `json:"result,omitempty"`
|
Result interface{} `json:"result,omitempty"`
|
||||||
}
|
}
|
||||||
@@ -153,8 +155,6 @@ func (p *zabbixParamOutput) UnmarshalJSON(data []byte) error {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ZabbixAPIParams = map[string]interface{}
|
|
||||||
|
|
||||||
type ZabbixAPIParamsLegacy struct {
|
type ZabbixAPIParamsLegacy struct {
|
||||||
Output *zabbixParamOutput `json:"output,omitempty"`
|
Output *zabbixParamOutput `json:"output,omitempty"`
|
||||||
SortField string `json:"sortfield,omitempty"`
|
SortField string `json:"sortfield,omitempty"`
|
||||||
|
|||||||
@@ -18,26 +18,37 @@ var NotCachedMethods = map[string]bool{
|
|||||||
"trend.get": true,
|
"trend.get": true,
|
||||||
}
|
}
|
||||||
|
|
||||||
// ZabbixAPIQuery handles query requests to Zabbix
|
// ZabbixQuery handles query requests to Zabbix
|
||||||
func (ds *ZabbixDatasourceInstance) ZabbixAPIQuery(ctx context.Context, apiReq *ZabbixAPIRequest) (*ZabbixAPIResourceResponse, error) {
|
func (ds *ZabbixDatasourceInstance) ZabbixQuery(ctx context.Context, apiReq *ZabbixAPIRequest) (*simplejson.Json, error) {
|
||||||
var result interface{}
|
var resultJson *simplejson.Json
|
||||||
var err error
|
var err error
|
||||||
var queryExistInCache bool
|
|
||||||
requestHash := HashString(apiReq.String())
|
requestHash := HashString(apiReq.String())
|
||||||
result, queryExistInCache = ds.queryCache.Get(requestHash)
|
|
||||||
|
|
||||||
|
cachedResult, queryExistInCache := ds.queryCache.Get(requestHash)
|
||||||
if !queryExistInCache {
|
if !queryExistInCache {
|
||||||
result, err = ds.ZabbixRequest(ctx, apiReq.Method, apiReq.Params)
|
resultJson, err = ds.ZabbixRequest(ctx, apiReq.Method, apiReq.Params)
|
||||||
|
|
||||||
if _, ok := NotCachedMethods[apiReq.Method]; !ok {
|
if _, ok := NotCachedMethods[apiReq.Method]; !ok {
|
||||||
ds.logger.Debug("Write result to cache", "method", apiReq.Method)
|
ds.logger.Debug("Write result to cache", "method", apiReq.Method)
|
||||||
ds.queryCache.Set(HashString(apiReq.String()), result)
|
ds.queryCache.Set(requestHash, resultJson)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
resultJson = cachedResult.(*simplejson.Json)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return resultJson, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZabbixAPIQuery handles query requests to Zabbix
|
||||||
|
func (ds *ZabbixDatasourceInstance) ZabbixAPIQuery(ctx context.Context, apiReq *ZabbixAPIRequest) (*ZabbixAPIResourceResponse, error) {
|
||||||
|
resultJson, err := ds.ZabbixQuery(ctx, apiReq)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
result := resultJson.Interface()
|
||||||
return BuildAPIResponse(&result)
|
return BuildAPIResponse(&result)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -251,7 +262,7 @@ func (ds *ZabbixDatasourceInstance) getAllItems(ctx context.Context, hostids []s
|
|||||||
filter["value_type"] = []int{1, 2, 4}
|
filter["value_type"] = []int{1, 2, 4}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ds.ZabbixRequest(ctx, "item.get", params)
|
return ds.ZabbixQuery(ctx, &ZabbixAPIRequest{Method: "item.get", Params: params})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ds *ZabbixDatasourceInstance) getAllApps(ctx context.Context, hostids []string) (*simplejson.Json, error) {
|
func (ds *ZabbixDatasourceInstance) getAllApps(ctx context.Context, hostids []string) (*simplejson.Json, error) {
|
||||||
@@ -260,7 +271,7 @@ func (ds *ZabbixDatasourceInstance) getAllApps(ctx context.Context, hostids []st
|
|||||||
"hostids": hostids,
|
"hostids": hostids,
|
||||||
}
|
}
|
||||||
|
|
||||||
return ds.ZabbixRequest(ctx, "application.get", params)
|
return ds.ZabbixQuery(ctx, &ZabbixAPIRequest{Method: "application.get", Params: params})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ds *ZabbixDatasourceInstance) getAllHosts(ctx context.Context, groupids []string) (*simplejson.Json, error) {
|
func (ds *ZabbixDatasourceInstance) getAllHosts(ctx context.Context, groupids []string) (*simplejson.Json, error) {
|
||||||
@@ -270,7 +281,7 @@ func (ds *ZabbixDatasourceInstance) getAllHosts(ctx context.Context, groupids []
|
|||||||
"groupids": groupids,
|
"groupids": groupids,
|
||||||
}
|
}
|
||||||
|
|
||||||
return ds.ZabbixRequest(ctx, "host.get", params)
|
return ds.ZabbixQuery(ctx, &ZabbixAPIRequest{Method: "host.get", Params: params})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ds *ZabbixDatasourceInstance) getAllGroups(ctx context.Context) (*simplejson.Json, error) {
|
func (ds *ZabbixDatasourceInstance) getAllGroups(ctx context.Context) (*simplejson.Json, error) {
|
||||||
@@ -280,7 +291,7 @@ func (ds *ZabbixDatasourceInstance) getAllGroups(ctx context.Context) (*simplejs
|
|||||||
"real_hosts": true,
|
"real_hosts": true,
|
||||||
}
|
}
|
||||||
|
|
||||||
return ds.ZabbixRequest(ctx, "hostgroup.get", params)
|
return ds.ZabbixQuery(ctx, &ZabbixAPIRequest{Method: "hostgroup.get", Params: params})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ds *ZabbixDatasourceInstance) queryNumericDataForItems(ctx context.Context, query *QueryModel, items zabbix.Items) (*data.Frame, error) {
|
func (ds *ZabbixDatasourceInstance) queryNumericDataForItems(ctx context.Context, query *QueryModel, items zabbix.Items) (*data.Frame, error) {
|
||||||
@@ -351,10 +362,10 @@ func (ds *ZabbixDatasourceInstance) getHistotyOrTrend(ctx context.Context, query
|
|||||||
var response *simplejson.Json
|
var response *simplejson.Json
|
||||||
var err error
|
var err error
|
||||||
if useTrend {
|
if useTrend {
|
||||||
response, err = ds.ZabbixRequest(ctx, "trend.get", params)
|
response, err = ds.ZabbixQuery(ctx, &ZabbixAPIRequest{Method: "trend.get", Params: params})
|
||||||
} else {
|
} else {
|
||||||
params["history"] = &k
|
params["history"] = &k
|
||||||
response, err = ds.ZabbixRequest(ctx, "history.get", params)
|
response, err = ds.ZabbixQuery(ctx, &ZabbixAPIRequest{Method: "history.get", Params: params})
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user