<commit_before>package zabbix

import (
	"context"
	"strings"
	"time"

	"github.com/alexanderzobnin/grafana-zabbix/pkg/zabbixapi"
	"github.com/bitly/go-simplejson"
	"github.com/grafana/grafana-plugin-sdk-go/backend"
	"github.com/grafana/grafana-plugin-sdk-go/backend/log"
)

// Zabbix is a wrapper for Zabbix API. It wraps Zabbix API queries and performs authentication, adds caching,
// deduplication and other performance optimizations.
type Zabbix struct {
	api     *zabbixapi.ZabbixAPI
	dsInfo  *backend.DataSourceInstanceSettings
	cache   *ZabbixCache
	version int
	logger  log.Logger
}

// New returns new instance of Zabbix client.
func New(dsInfo *backend.DataSourceInstanceSettings, zabbixAPI *zabbixapi.ZabbixAPI) (*Zabbix, error) {
	logger := log.New()

	zabbixSettings, err := readZabbixSettings(dsInfo)
	if err != nil {
		logger.Error("Error parsing Zabbix settings", "error", err)
		return nil, err
	}

	zabbixCache := NewZabbixCache(zabbixSettings.CacheTTL, 10*time.Minute)

	return &Zabbix{
		api:    zabbixAPI,
		dsInfo: dsInfo,
		cache:  zabbixCache,
		logger: logger,
	}, nil
}

func (zabbix *Zabbix) GetAPI() *zabbixapi.ZabbixAPI {
	return zabbix.api
}

// Request wraps request with cache
func (ds *Zabbix) Request(ctx context.Context, apiReq *ZabbixAPIRequest) (*simplejson.Json, error) {
	var resultJson *simplejson.Json
	var err error

	if ds.version == 0 {
		version, err := ds.GetVersion(ctx)
		if err != nil {
			ds.logger.Error("Error querying Zabbix version", "error", err)
		} else {
			ds.logger.Debug("Got Zabbix version", "version", version)
			ds.version = version
		}
	}

	cachedResult, queryExistInCache := ds.cache.GetAPIRequest(apiReq)
	if !queryExistInCache {
		resultJson, err = ds.request(ctx, apiReq.Method, apiReq.Params)
		if err != nil {
			return nil, err
		}

		if IsCachedRequest(apiReq.Method) {
			ds.logger.Debug("Writing result to cache", "method", apiReq.Method)
			ds.cache.SetAPIRequest(apiReq, resultJson)
		}
	} else {
		var ok bool
		resultJson, ok = cachedResult.(*simplejson.Json)
		if !ok {
			resultJson = simplejson.New()
		}
	}<commit_msg>Don't query version if error happened<commit_after>package zabbix

import (
	"context"
	"strings"
	"time"

	"github.com/alexanderzobnin/grafana-zabbix/pkg/zabbixapi"
	"github.com/bitly/go-simplejson"
	"github.com/grafana/grafana-plugin-sdk-go/backend"
	"github.com/grafana/grafana-plugin-sdk-go/backend/log"
)

// Zabbix is a wrapper for Zabbix API. It wraps Zabbix API queries and performs authentication, adds caching,
// deduplication and other performance optimizations.
type Zabbix struct {
	api     *zabbixapi.ZabbixAPI
	dsInfo  *backend.DataSourceInstanceSettings
	cache   *ZabbixCache
	version int
	logger  log.Logger
}

// New returns new instance of Zabbix client.
func New(dsInfo *backend.DataSourceInstanceSettings, zabbixAPI *zabbixapi.ZabbixAPI) (*Zabbix, error) {
	logger := log.New()

	zabbixSettings, err := readZabbixSettings(dsInfo)
	if err != nil {
		logger.Error("Error parsing Zabbix settings", "error", err)
		return nil, err
	}

	zabbixCache := NewZabbixCache(zabbixSettings.CacheTTL, 10*time.Minute)

	return &Zabbix{
		api:    zabbixAPI,
		dsInfo: dsInfo,
		cache:  zabbixCache,
		logger: logger,
	}, nil
}

func (zabbix *Zabbix) GetAPI() *zabbixapi.ZabbixAPI {
	return zabbix.api
}

// Request wraps request with cache
func (ds *Zabbix) Request(ctx context.Context, apiReq *ZabbixAPIRequest) (*simplejson.Json, error) {
	var resultJson *simplejson.Json
	var err error

	if ds.version == 0 {
		version, err := ds.GetVersion(ctx)
		if err != nil {
			ds.logger.Error("Error querying Zabbix version", "error", err)
			ds.version = -1
		} else {
			ds.logger.Debug("Got Zabbix version", "version", version)
			ds.version = version
		}
	}

	cachedResult, queryExistInCache := ds.cache.GetAPIRequest(apiReq)
	if !queryExistInCache {
		resultJson, err = ds.request(ctx, apiReq.Method, apiReq.Params)
		if err != nil {
			return nil, err
		}

		if IsCachedRequest(apiReq.Method) {
			ds.logger.Debug("Writing result to cache", "method", apiReq.Method)
			ds.cache.SetAPIRequest(apiReq, resultJson)
		}
	} else {
		var ok bool
		resultJson, ok = cachedResult.(*simplejson.Json)
		if !ok {
			resultJson = simplejson.New()
		}
	}
{'fraction_non_alphanumeric': 0.11767474699929395, 'fraction_numerical': 0.0016474464579901153, 'mean_word_length': 4.182926829268292, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7420600', 'n_tokens_mistral': 1634, 'n_tokens_neox': 1527, 'n_words': 361}