move cache to datasource_cache
This commit is contained in:
22
pkg/cache/cache.go
vendored
22
pkg/cache/cache.go
vendored
@@ -1,12 +1,8 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
cache "github.com/patrickmn/go-cache"
|
||||
)
|
||||
|
||||
@@ -19,6 +15,8 @@ const (
|
||||
DefaultExpiration time.Duration = 0
|
||||
)
|
||||
|
||||
// TODO: since all methods moved to datasource_cache, this can be removed and replaced by go-cache
|
||||
|
||||
// Cache is a abstraction over go-cache.
|
||||
type Cache struct {
|
||||
cache *cache.Cache
|
||||
@@ -40,19 +38,3 @@ func (c *Cache) Set(request string, response interface{}) {
|
||||
func (c *Cache) Get(request string) (interface{}, bool) {
|
||||
return c.cache.Get(request)
|
||||
}
|
||||
|
||||
// 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/cache/cache_test.go
vendored
100
pkg/cache/cache_test.go
vendored
@@ -1,100 +0,0 @@
|
||||
package cache
|
||||
|
||||
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)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user