Refactor: remove unused types
This commit is contained in:
@@ -34,7 +34,6 @@ type ZabbixDatasourceInstance struct {
|
||||
zabbix *zabbix.Zabbix
|
||||
dsInfo *backend.DataSourceInstanceSettings
|
||||
Settings *ZabbixDatasourceSettings
|
||||
queryCache *DatasourceCache
|
||||
logger log.Logger
|
||||
}
|
||||
|
||||
@@ -79,7 +78,6 @@ func newZabbixDatasourceInstance(settings backend.DataSourceInstanceSettings) (i
|
||||
dsInfo: &settings,
|
||||
zabbix: zabbixClient,
|
||||
Settings: zabbixSettings,
|
||||
queryCache: NewDatasourceCache(zabbixSettings.CacheTTL, 10*time.Minute),
|
||||
logger: logger,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
package datasource
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"encoding/hex"
|
||||
"time"
|
||||
|
||||
"github.com/alexanderzobnin/grafana-zabbix/pkg/cache"
|
||||
)
|
||||
|
||||
// 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))
|
||||
}
|
||||
@@ -36,18 +36,6 @@ type ZabbixAPIResourceRequest struct {
|
||||
Params map[string]interface{} `json:"params,omitempty"`
|
||||
}
|
||||
|
||||
type ZabbixAPIRequest struct {
|
||||
Method string `json:"method"`
|
||||
Params ZabbixAPIParams `json:"params,omitempty"`
|
||||
}
|
||||
|
||||
func (r *ZabbixAPIRequest) String() string {
|
||||
jsonRequest, _ := json.Marshal(r.Params)
|
||||
return r.Method + string(jsonRequest)
|
||||
}
|
||||
|
||||
type ZabbixAPIParams = map[string]interface{}
|
||||
|
||||
type ZabbixAPIResourceResponse struct {
|
||||
Result interface{} `json:"result,omitempty"`
|
||||
}
|
||||
|
||||
@@ -1,79 +1,5 @@
|
||||
package datasource
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Items []Item
|
||||
|
||||
type Item struct {
|
||||
ID string `json:"itemid,omitempty"`
|
||||
Key string `json:"key_,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
ValueType int `json:"value_type,omitempty,string"`
|
||||
HostID string `json:"hostid,omitempty"`
|
||||
Hosts []ItemHost `json:"hosts,omitempty"`
|
||||
Status string `json:"status,omitempty"`
|
||||
State string `json:"state,omitempty"`
|
||||
}
|
||||
|
||||
func (item *Item) ExpandItem() string {
|
||||
name := item.Name
|
||||
key := item.Key
|
||||
|
||||
if strings.Index(key, "[") == -1 {
|
||||
return name
|
||||
}
|
||||
|
||||
keyRunes := []rune(item.Key)
|
||||
keyParamsStr := string(keyRunes[strings.Index(key, "[")+1 : strings.LastIndex(key, "]")])
|
||||
keyParams := splitKeyParams(keyParamsStr)
|
||||
|
||||
for i := len(keyParams); i >= 1; i-- {
|
||||
name = strings.ReplaceAll(name, fmt.Sprintf("$%v", i), keyParams[i-1])
|
||||
}
|
||||
|
||||
return name
|
||||
}
|
||||
|
||||
func splitKeyParams(paramStr string) []string {
|
||||
paramRunes := []rune(paramStr)
|
||||
params := []string{}
|
||||
quoted := false
|
||||
inArray := false
|
||||
splitSymbol := ","
|
||||
param := ""
|
||||
|
||||
for _, r := range paramRunes {
|
||||
symbol := string(r)
|
||||
if symbol == `"` && inArray {
|
||||
param += symbol
|
||||
} else if symbol == `"` && quoted {
|
||||
quoted = false
|
||||
} else if symbol == `"` && !quoted {
|
||||
quoted = true
|
||||
} else if symbol == "[" && !quoted {
|
||||
inArray = true
|
||||
} else if symbol == "]" && !quoted {
|
||||
inArray = false
|
||||
} else if symbol == splitSymbol && !quoted && !inArray {
|
||||
params = append(params, param)
|
||||
param = ""
|
||||
} else {
|
||||
param += symbol
|
||||
}
|
||||
}
|
||||
|
||||
params = append(params, param)
|
||||
return params
|
||||
}
|
||||
|
||||
type ItemHost struct {
|
||||
ID string `json:"hostid,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
}
|
||||
|
||||
type Trend []TrendPoint
|
||||
|
||||
type TrendPoint struct {
|
||||
|
||||
@@ -120,7 +120,7 @@ func (ds *ZabbixDatasourceInstance) getHistotyOrTrend(ctx context.Context, query
|
||||
itemids = append(itemids, m.ID)
|
||||
}
|
||||
|
||||
params := ZabbixAPIParams{
|
||||
params := zabbix.ZabbixAPIParams{
|
||||
"output": "extend",
|
||||
"sortfield": "clock",
|
||||
"sortorder": "ASC",
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
package datasource
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/alexanderzobnin/grafana-zabbix/pkg/cache"
|
||||
"github.com/alexanderzobnin/grafana-zabbix/pkg/zabbix"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
|
||||
@@ -18,7 +15,7 @@ var basicDatasourceInfo = &backend.DataSourceInstanceSettings{
|
||||
JSONData: []byte(`{"username":"username", "password":"password", "cacheTTL":"10m"}`),
|
||||
}
|
||||
|
||||
func mockZabbixQuery(method string, params ZabbixAPIParams) *zabbix.ZabbixAPIRequest {
|
||||
func mockZabbixQuery(method string, params zabbix.ZabbixAPIParams) *zabbix.ZabbixAPIRequest {
|
||||
return &zabbix.ZabbixAPIRequest{
|
||||
Method: method,
|
||||
Params: params,
|
||||
@@ -33,7 +30,6 @@ func MockZabbixDataSource(body string, statusCode int) *ZabbixDatasourceInstance
|
||||
dsInfo: basicDatasourceInfo,
|
||||
zabbix: zabbixClient,
|
||||
Settings: zabbixSettings,
|
||||
queryCache: NewDatasourceCache(cache.NoExpiration, 10*time.Minute),
|
||||
logger: log.New(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user