move cache to datasource_cache
This commit is contained in:
@@ -27,7 +27,7 @@ type ZabbixDatasourceInstance struct {
|
||||
zabbixAPI *zabbixapi.ZabbixAPI
|
||||
dsInfo *backend.DataSourceInstanceSettings
|
||||
Settings *ZabbixDatasourceSettings
|
||||
queryCache *cache.Cache
|
||||
queryCache *DatasourceCache
|
||||
logger log.Logger
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ func NewZabbixDatasourceInstance(dsInfo *backend.DataSourceInstanceSettings) (*Z
|
||||
dsInfo: dsInfo,
|
||||
zabbixAPI: zabbixAPI,
|
||||
Settings: zabbixSettings,
|
||||
queryCache: cache.NewCache(zabbixSettings.CacheTTL, 10*time.Minute),
|
||||
queryCache: NewDatasourceCache(zabbixSettings.CacheTTL, 10*time.Minute),
|
||||
logger: log.New(),
|
||||
}, nil
|
||||
}
|
||||
@@ -121,11 +121,11 @@ func (ds *ZabbixDatasource) GetDatasource(pluginContext backend.PluginContext) (
|
||||
dsSettings := pluginContext.DataSourceInstanceSettings
|
||||
dsKey := fmt.Sprintf("%d-%d", pluginContext.OrgID, dsSettings.ID)
|
||||
// Get hash to check if settings changed
|
||||
dsInfoHash := cache.HashDatasourceInfo(dsSettings)
|
||||
dsInfoHash := HashDatasourceInfo(dsSettings)
|
||||
|
||||
if cachedData, ok := ds.datasourceCache.Get(dsKey); ok {
|
||||
if cachedDS, ok := cachedData.(*ZabbixDatasourceInstance); ok {
|
||||
cachedDSHash := cache.HashDatasourceInfo(cachedDS.dsInfo)
|
||||
cachedDSHash := HashDatasourceInfo(cachedDS.dsInfo)
|
||||
if cachedDSHash == dsInfoHash {
|
||||
return cachedDS, nil
|
||||
}
|
||||
|
||||
51
pkg/datasource/datasource_cache.go
Normal file
51
pkg/datasource/datasource_cache.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package datasource
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/alexanderzobnin/grafana-zabbix/pkg/cache"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
)
|
||||
|
||||
// DatasourceCache is a cache for datasource instance.
|
||||
type DatasourceCache struct {
|
||||
cache *cache.Cache
|
||||
}
|
||||
|
||||
// NewDatasourceCache creates a DatasourceCache with expiration(ttl) time and cleanupInterval.
|
||||
func NewDatasourceCache(ttl time.Duration, cleanupInterval time.Duration) *DatasourceCache {
|
||||
return &DatasourceCache{
|
||||
cache.NewCache(ttl, cleanupInterval),
|
||||
}
|
||||
}
|
||||
|
||||
// GetAPIRequest gets request response from cache
|
||||
func (c *DatasourceCache) GetAPIRequest(request *ZabbixAPIRequest) (interface{}, bool) {
|
||||
requestHash := HashString(request.String())
|
||||
return c.cache.Get(requestHash)
|
||||
}
|
||||
|
||||
// SetAPIRequest writes request response to cache
|
||||
func (c *DatasourceCache) SetAPIRequest(request *ZabbixAPIRequest, response interface{}) {
|
||||
requestHash := HashString(request.String())
|
||||
c.cache.Set(requestHash, response)
|
||||
}
|
||||
|
||||
// HashString converts the given text string to hash string
|
||||
func HashString(text string) string {
|
||||
hash := sha1.New()
|
||||
hash.Write([]byte(text))
|
||||
return hex.EncodeToString(hash.Sum(nil))
|
||||
}
|
||||
|
||||
// HashDatasourceInfo converts the given datasource info to hash string
|
||||
func HashDatasourceInfo(dsInfo *backend.DataSourceInstanceSettings) string {
|
||||
digester := sha1.New()
|
||||
if err := json.NewEncoder(digester).Encode(dsInfo); err != nil {
|
||||
panic(err) // This shouldn't be possible but just in case DatasourceInfo changes
|
||||
}
|
||||
return hex.EncodeToString(digester.Sum(nil))
|
||||
}
|
||||
100
pkg/datasource/datasource_cache_test.go
Normal file
100
pkg/datasource/datasource_cache_test.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package datasource
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
"gotest.tools/assert"
|
||||
)
|
||||
|
||||
func TestHashDatasourceInfo(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
dsInfoBefore *backend.DataSourceInstanceSettings
|
||||
dsInfoAfter *backend.DataSourceInstanceSettings
|
||||
equal bool
|
||||
}{
|
||||
{
|
||||
name: "Same datasource settings",
|
||||
dsInfoBefore: &backend.DataSourceInstanceSettings{
|
||||
ID: 1,
|
||||
Name: "Zabbix",
|
||||
URL: "https://localhost:3306/zabbix/api_jsonrpc.php",
|
||||
JSONData: []byte("{}"),
|
||||
DecryptedSecureJSONData: map[string]string{
|
||||
"username": "grafanaZabbixUser",
|
||||
"password": "$uper$ecr3t!!!",
|
||||
},
|
||||
},
|
||||
dsInfoAfter: &backend.DataSourceInstanceSettings{
|
||||
ID: 1,
|
||||
Name: "Zabbix",
|
||||
URL: "https://localhost:3306/zabbix/api_jsonrpc.php",
|
||||
JSONData: []byte("{}"),
|
||||
DecryptedSecureJSONData: map[string]string{
|
||||
"username": "grafanaZabbixUser",
|
||||
"password": "$uper$ecr3t!!!",
|
||||
},
|
||||
},
|
||||
equal: true,
|
||||
},
|
||||
{
|
||||
name: "Password changed",
|
||||
dsInfoBefore: &backend.DataSourceInstanceSettings{
|
||||
ID: 1,
|
||||
Name: "Zabbix",
|
||||
URL: "https://localhost:3306/zabbix/api_jsonrpc.php",
|
||||
JSONData: []byte("{}"),
|
||||
DecryptedSecureJSONData: map[string]string{
|
||||
"username": "grafanaZabbixUser",
|
||||
"password": "$uper$ecr3t!!!",
|
||||
},
|
||||
},
|
||||
dsInfoAfter: &backend.DataSourceInstanceSettings{
|
||||
ID: 1,
|
||||
Name: "Zabbix",
|
||||
URL: "https://localhost:3306/zabbix/api_jsonrpc.php",
|
||||
JSONData: []byte("{}"),
|
||||
DecryptedSecureJSONData: map[string]string{
|
||||
"username": "grafanaZabbixUser",
|
||||
"password": "new$uper$ecr3t!!!",
|
||||
},
|
||||
},
|
||||
equal: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
before := HashDatasourceInfo(tt.dsInfoBefore)
|
||||
after := HashDatasourceInfo(tt.dsInfoAfter)
|
||||
got := before == after
|
||||
assert.Equal(t, tt.equal, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkHashDatasourceInfo(b *testing.B) {
|
||||
benches := []struct {
|
||||
name string
|
||||
dsInfo *backend.DataSourceInstanceSettings
|
||||
}{
|
||||
{
|
||||
"Normal Datasource Info",
|
||||
&backend.DataSourceInstanceSettings{
|
||||
ID: 4,
|
||||
Name: "MyZabbixDatasource",
|
||||
URL: "https://localhost:3306/zabbix/api_jsonrpc.php",
|
||||
JSONData: []byte(`{ "addThresholds": true, "disableReadOnlyUsersAck": true }`),
|
||||
DecryptedSecureJSONData: map[string]string{
|
||||
"username": "grafanaZabbixUser",
|
||||
"password": "$uper$ecr3t!!!",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, bt := range benches {
|
||||
b.Run(bt.name, func(b *testing.B) {
|
||||
HashDatasourceInfo(bt.dsInfo)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"github.com/alexanderzobnin/grafana-zabbix/pkg/cache"
|
||||
"github.com/alexanderzobnin/grafana-zabbix/pkg/zabbixapi"
|
||||
simplejson "github.com/bitly/go-simplejson"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
@@ -28,9 +27,8 @@ var CachedMethods = map[string]bool{
|
||||
func (ds *ZabbixDatasourceInstance) ZabbixQuery(ctx context.Context, apiReq *ZabbixAPIRequest) (*simplejson.Json, error) {
|
||||
var resultJson *simplejson.Json
|
||||
var err error
|
||||
requestHash := cache.HashString(apiReq.String())
|
||||
|
||||
cachedResult, queryExistInCache := ds.queryCache.Get(requestHash)
|
||||
cachedResult, queryExistInCache := ds.queryCache.GetAPIRequest(apiReq)
|
||||
if !queryExistInCache {
|
||||
resultJson, err = ds.ZabbixRequest(ctx, apiReq.Method, apiReq.Params)
|
||||
if err != nil {
|
||||
@@ -39,7 +37,7 @@ func (ds *ZabbixDatasourceInstance) ZabbixQuery(ctx context.Context, apiReq *Zab
|
||||
|
||||
if _, ok := CachedMethods[apiReq.Method]; ok {
|
||||
ds.logger.Debug("Write result to cache", "method", apiReq.Method)
|
||||
ds.queryCache.Set(requestHash, resultJson)
|
||||
ds.queryCache.SetAPIRequest(apiReq, resultJson)
|
||||
}
|
||||
} else {
|
||||
var ok bool
|
||||
|
||||
@@ -50,7 +50,7 @@ func MockZabbixDataSource(body string, statusCode int) *ZabbixDatasourceInstance
|
||||
dsInfo: basicDatasourceInfo,
|
||||
zabbixAPI: zabbixAPI,
|
||||
Settings: zabbixSettings,
|
||||
queryCache: cache.NewCache(cache.NoExpiration, 10*time.Minute),
|
||||
queryCache: NewDatasourceCache(cache.NoExpiration, 10*time.Minute),
|
||||
logger: log.New(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user