Fix backend linter errors
This commit is contained in:
@@ -145,6 +145,10 @@ func applyFunctionsPre(query *QueryModel, items []*zabbix.Item) error {
|
||||
|
||||
func applyGroupBy(series timeseries.TimeSeries, params ...interface{}) (timeseries.TimeSeries, error) {
|
||||
pInterval, err := MustString(params[0])
|
||||
if err != nil {
|
||||
return nil, errParsingFunctionParam(err)
|
||||
}
|
||||
|
||||
pAgg, err := MustString(params[1])
|
||||
if err != nil {
|
||||
return nil, errParsingFunctionParam(err)
|
||||
@@ -169,6 +173,10 @@ func applyGroupBy(series timeseries.TimeSeries, params ...interface{}) (timeseri
|
||||
|
||||
func applyPercentile(series timeseries.TimeSeries, params ...interface{}) (timeseries.TimeSeries, error) {
|
||||
pInterval, err := MustString(params[0])
|
||||
if err != nil {
|
||||
return nil, errParsingFunctionParam(err)
|
||||
}
|
||||
|
||||
percentile, err := MustFloat64(params[1])
|
||||
if err != nil {
|
||||
return nil, errParsingFunctionParam(err)
|
||||
@@ -275,6 +283,10 @@ func applyExponentialMovingAverage(series timeseries.TimeSeries, params ...inter
|
||||
|
||||
func applyAggregateBy(series []*timeseries.TimeSeriesData, params ...interface{}) ([]*timeseries.TimeSeriesData, error) {
|
||||
pInterval, err := MustString(params[0])
|
||||
if err != nil {
|
||||
return nil, errParsingFunctionParam(err)
|
||||
}
|
||||
|
||||
pAgg, err := MustString(params[1])
|
||||
if err != nil {
|
||||
return nil, errParsingFunctionParam(err)
|
||||
@@ -303,6 +315,10 @@ func applySumSeries(series []*timeseries.TimeSeriesData, params ...interface{})
|
||||
|
||||
func applyPercentileAgg(series []*timeseries.TimeSeriesData, params ...interface{}) ([]*timeseries.TimeSeriesData, error) {
|
||||
pInterval, err := MustString(params[0])
|
||||
if err != nil {
|
||||
return nil, errParsingFunctionParam(err)
|
||||
}
|
||||
|
||||
percentile, err := MustFloat64(params[1])
|
||||
if err != nil {
|
||||
return nil, errParsingFunctionParam(err)
|
||||
@@ -331,6 +347,10 @@ func applyPercentileAgg(series []*timeseries.TimeSeriesData, params ...interface
|
||||
|
||||
func applyTop(series []*timeseries.TimeSeriesData, params ...interface{}) ([]*timeseries.TimeSeriesData, error) {
|
||||
n, err := MustFloat64(params[0])
|
||||
if err != nil {
|
||||
return nil, errParsingFunctionParam(err)
|
||||
}
|
||||
|
||||
pAgg, err := MustString(params[1])
|
||||
if err != nil {
|
||||
return nil, errParsingFunctionParam(err)
|
||||
@@ -343,6 +363,10 @@ func applyTop(series []*timeseries.TimeSeriesData, params ...interface{}) ([]*ti
|
||||
|
||||
func applyBottom(series []*timeseries.TimeSeriesData, params ...interface{}) ([]*timeseries.TimeSeriesData, error) {
|
||||
n, err := MustFloat64(params[0])
|
||||
if err != nil {
|
||||
return nil, errParsingFunctionParam(err)
|
||||
}
|
||||
|
||||
pAgg, err := MustString(params[1])
|
||||
if err != nil {
|
||||
return nil, errParsingFunctionParam(err)
|
||||
@@ -413,7 +437,7 @@ func applyTimeShiftPost(series timeseries.TimeSeries, params ...interface{}) (ti
|
||||
if interval == 0 {
|
||||
return series, nil
|
||||
}
|
||||
if shiftForward == true {
|
||||
if shiftForward {
|
||||
interval = -interval
|
||||
}
|
||||
|
||||
|
||||
@@ -2,11 +2,13 @@ package datasource
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/alexanderzobnin/grafana-zabbix/pkg/zabbix"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend/resource/httpadapter"
|
||||
)
|
||||
|
||||
@@ -17,7 +19,11 @@ import (
|
||||
func (ds *ZabbixDatasource) RootHandler(rw http.ResponseWriter, req *http.Request) {
|
||||
ds.logger.Debug("Received resource call", "url", req.URL.String(), "method", req.Method)
|
||||
|
||||
rw.Write([]byte("Hello from Zabbix data source!"))
|
||||
_, err := rw.Write([]byte("Hello from Zabbix data source!"))
|
||||
if err != nil {
|
||||
ds.logger.Warn("Error writing response")
|
||||
}
|
||||
|
||||
rw.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
@@ -26,7 +32,7 @@ func (ds *ZabbixDatasource) ZabbixAPIHandler(rw http.ResponseWriter, req *http.R
|
||||
return
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(req.Body)
|
||||
body, err := io.ReadAll(req.Body)
|
||||
defer req.Body.Close()
|
||||
if err != nil || len(body) == 0 {
|
||||
writeError(rw, http.StatusBadRequest, err)
|
||||
@@ -67,7 +73,7 @@ func (ds *ZabbixDatasource) DBConnectionPostProcessingHandler(rw http.ResponseWr
|
||||
return
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(req.Body)
|
||||
body, err := io.ReadAll(req.Body)
|
||||
defer req.Body.Close()
|
||||
if err != nil || len(body) == 0 {
|
||||
writeError(rw, http.StatusBadRequest, err)
|
||||
@@ -95,6 +101,9 @@ func (ds *ZabbixDatasource) DBConnectionPostProcessingHandler(rw http.ResponseWr
|
||||
reqData.Query.TimeRange.To = time.Unix(reqData.TimeRange.To, 0)
|
||||
|
||||
frames, err := dsInstance.applyDataProcessing(req.Context(), &reqData.Query, reqData.Series, true)
|
||||
if err != nil {
|
||||
writeError(rw, http.StatusInternalServerError, err)
|
||||
}
|
||||
|
||||
resultJson, err := json.Marshal(frames)
|
||||
if err != nil {
|
||||
@@ -103,7 +112,11 @@ func (ds *ZabbixDatasource) DBConnectionPostProcessingHandler(rw http.ResponseWr
|
||||
|
||||
rw.Header().Add("Content-Type", "application/json")
|
||||
rw.WriteHeader(http.StatusOK)
|
||||
rw.Write(resultJson)
|
||||
|
||||
_, err = rw.Write(resultJson)
|
||||
if err != nil {
|
||||
ds.logger.Warn("Error writing response")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -115,7 +128,11 @@ func writeResponse(rw http.ResponseWriter, result *ZabbixAPIResourceResponse) {
|
||||
|
||||
rw.Header().Add("Content-Type", "application/json")
|
||||
rw.WriteHeader(http.StatusOK)
|
||||
rw.Write(resultJson)
|
||||
|
||||
_, err = rw.Write(resultJson)
|
||||
if err != nil {
|
||||
log.DefaultLogger.Warn("Error writing response")
|
||||
}
|
||||
}
|
||||
|
||||
func writeError(rw http.ResponseWriter, statusCode int, err error) {
|
||||
@@ -132,5 +149,9 @@ func writeError(rw http.ResponseWriter, statusCode int, err error) {
|
||||
|
||||
rw.Header().Add("Content-Type", "application/json")
|
||||
rw.WriteHeader(http.StatusInternalServerError)
|
||||
rw.Write(b)
|
||||
|
||||
_, err = rw.Write(b)
|
||||
if err != nil {
|
||||
log.DefaultLogger.Warn("Error writing response")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,6 +56,10 @@ func (ds *ZabbixDatasourceInstance) queryNumericItems(ctx context.Context, query
|
||||
var items []*zabbix.Item
|
||||
var err error
|
||||
zabbixVersion, err := ds.zabbix.GetVersion(ctx)
|
||||
if err != nil {
|
||||
ds.logger.Warn("Error getting Zabbix version")
|
||||
}
|
||||
|
||||
if zabbixVersion >= 54 {
|
||||
items, err = ds.zabbix.GetItems(ctx, groupFilter, hostFilter, itemTagFilter, itemFilter, "num", showDisabled)
|
||||
} else {
|
||||
|
||||
@@ -8,8 +8,6 @@ import (
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
|
||||
)
|
||||
|
||||
var emptyParams = map[string]interface{}{}
|
||||
|
||||
var basicDatasourceInfo = &backend.DataSourceInstanceSettings{
|
||||
ID: 1,
|
||||
Name: "TestDatasource",
|
||||
@@ -17,13 +15,6 @@ var basicDatasourceInfo = &backend.DataSourceInstanceSettings{
|
||||
JSONData: []byte(`{"username":"username", "password":"password", "cacheTTL":"10m", "authType":"token"}`),
|
||||
}
|
||||
|
||||
func mockZabbixQuery(method string, params zabbix.ZabbixAPIParams) *zabbix.ZabbixAPIRequest {
|
||||
return &zabbix.ZabbixAPIRequest{
|
||||
Method: method,
|
||||
Params: params,
|
||||
}
|
||||
}
|
||||
|
||||
func MockZabbixDataSource(body string, statusCode int) *ZabbixDatasourceInstance {
|
||||
zabbixSettings, _ := settings.ReadZabbixSettings(basicDatasourceInfo)
|
||||
zabbixClient, _ := zabbix.MockZabbixClient(basicDatasourceInfo, body, statusCode)
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
simplejson "github.com/bitly/go-simplejson"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
|
||||
@@ -14,6 +15,10 @@ import (
|
||||
// New creates new HTTP client.
|
||||
func New(dsInfo *backend.DataSourceInstanceSettings, timeout time.Duration) (*http.Client, error) {
|
||||
clientOptions, err := dsInfo.HTTPClientOptions()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
clientOptions.Timeouts.Timeout = timeout
|
||||
|
||||
tlsSkipVerify, err := getTLSSkipVerify(dsInfo)
|
||||
|
||||
@@ -96,7 +96,7 @@ func AggPercentile(n float64) AggregationFunc {
|
||||
return nil
|
||||
}
|
||||
|
||||
sort.Sort(sort.Float64Slice(values))
|
||||
sort.Float64s(values)
|
||||
percentileIndex := int(math.Floor(float64(len(values)) * n / 100))
|
||||
percentile := values[percentileIndex]
|
||||
return &percentile
|
||||
|
||||
@@ -129,12 +129,11 @@ func PrepareForStack(series []*TimeSeriesData) []*TimeSeriesData {
|
||||
p := s.TS[0]
|
||||
pNext := s.TS[1]
|
||||
interpolatedSeries := make([]TimePoint, 0)
|
||||
interpolatedTS := interpolatedTimeStamps[0]
|
||||
interpolatedTSIdx := 0
|
||||
|
||||
// Insert nulls before the first point
|
||||
for i := 0; i < len(interpolatedTimeStamps); i++ {
|
||||
interpolatedTS = interpolatedTimeStamps[i]
|
||||
interpolatedTS := interpolatedTimeStamps[i]
|
||||
if interpolatedTS.Before(p.Time) {
|
||||
interpolatedSeries = append(interpolatedSeries, TimePoint{Time: interpolatedTS, Value: nil})
|
||||
} else {
|
||||
|
||||
@@ -4,8 +4,6 @@ import (
|
||||
"math"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||
)
|
||||
|
||||
func NewTimeSeriesData() *TimeSeriesData {
|
||||
@@ -309,36 +307,3 @@ func findNearestLeft(series TimeSeries, pointIndex int) *TimePoint {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getTimeFieldIndex(frame *data.Frame) int {
|
||||
for i := 0; i < len(frame.Fields); i++ {
|
||||
if frame.Fields[i].Type() == data.FieldTypeTime {
|
||||
return i
|
||||
}
|
||||
}
|
||||
|
||||
return -1
|
||||
}
|
||||
|
||||
func getTimestampAt(frame *data.Frame, index int) *time.Time {
|
||||
timeFieldIdx := getTimeFieldIndex(frame)
|
||||
if timeFieldIdx < 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
tsValue := frame.Fields[timeFieldIdx].At(index)
|
||||
ts, ok := tsValue.(time.Time)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &ts
|
||||
}
|
||||
|
||||
func setTimeAt(frame *data.Frame, frameTs time.Time, index int) {
|
||||
for _, field := range frame.Fields {
|
||||
if field.Type() == data.FieldTypeTime {
|
||||
field.Insert(index, frameTs)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,6 +115,9 @@ func (ds *Zabbix) GetItems(
|
||||
itemTagFilter = strings.Join(tagStrs, ",")
|
||||
}
|
||||
allItems, err = ds.GetAllItems(ctx, hostids, nil, itemType, showDisabled, itemTagFilter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return filterItemsByQuery(allItems, itemFilter)
|
||||
}
|
||||
@@ -153,6 +156,9 @@ func (ds *Zabbix) GetItemsBefore54(
|
||||
} else if appFilter == "" && len(hostids) > 0 {
|
||||
allItems, err = ds.GetAllItems(ctx, hostids, nil, itemType, showDisabled, "")
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return filterItemsByQuery(allItems, itemFilter)
|
||||
}
|
||||
@@ -239,7 +245,11 @@ func (ds *Zabbix) GetItemTags(ctx context.Context, groupFilter string, hostFilte
|
||||
var allItems []*Item
|
||||
itemType := "num"
|
||||
showDisabled := false
|
||||
|
||||
allItems, err = ds.GetAllItems(ctx, hostids, nil, itemType, showDisabled, "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var allTags []ItemTag
|
||||
tagsMap := make(map[string]ItemTag)
|
||||
@@ -397,7 +407,7 @@ func (ds *Zabbix) GetAllItems(ctx context.Context, hostids []string, appids []st
|
||||
}
|
||||
}
|
||||
|
||||
if showDisabled == false {
|
||||
if !showDisabled {
|
||||
params["monitored"] = true
|
||||
}
|
||||
|
||||
@@ -521,12 +531,3 @@ func (ds *Zabbix) GetVersion(ctx context.Context) (int, error) {
|
||||
versionNum, err := strconv.Atoi(version)
|
||||
return versionNum, err
|
||||
}
|
||||
|
||||
func isAppMethodNotFoundError(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
message := err.Error()
|
||||
return message == `Method not found. Incorrect API "application".`
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ func (item *Item) ExpandItemName() string {
|
||||
name := item.Name
|
||||
key := item.Key
|
||||
|
||||
if strings.Index(key, "[") == -1 {
|
||||
if !strings.Contains(key, "[") {
|
||||
return name
|
||||
}
|
||||
|
||||
@@ -34,14 +34,13 @@ func expandItems(items []*Item) []*Item {
|
||||
}
|
||||
|
||||
func splitKeyParams(paramStr string) []string {
|
||||
paramRunes := []rune(paramStr)
|
||||
params := []string{}
|
||||
quoted := false
|
||||
inArray := false
|
||||
splitSymbol := ","
|
||||
param := ""
|
||||
|
||||
for _, r := range paramRunes {
|
||||
for _, r := range paramStr {
|
||||
symbol := string(r)
|
||||
if symbol == `"` && inArray {
|
||||
param += symbol
|
||||
|
||||
@@ -2,7 +2,7 @@ package zabbixapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
@@ -36,7 +36,7 @@ func MockZabbixAPI(body string, statusCode int) (*ZabbixAPI, error) {
|
||||
httpClient: NewTestClient(func(req *http.Request) *http.Response {
|
||||
return &http.Response{
|
||||
StatusCode: statusCode,
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString(body)),
|
||||
Body: io.NopCloser(bytes.NewBufferString(body)),
|
||||
Header: make(http.Header),
|
||||
}
|
||||
}),
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
@@ -214,7 +214,7 @@ func makeHTTPRequest(ctx context.Context, httpClient *http.Client, req *http.Req
|
||||
return nil, fmt.Errorf("request failed, status: %v", res.Status)
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user