Fix trend values count and sum
This commit is contained in:
@@ -157,17 +157,9 @@ func convertHistoryToDataFrame(history zabbix.History, items []*zabbix.Item) *da
|
|||||||
func convertTrendToHistory(trend zabbix.Trend, valueType string) (zabbix.History, error) {
|
func convertTrendToHistory(trend zabbix.Trend, valueType string) (zabbix.History, error) {
|
||||||
history := make([]zabbix.HistoryPoint, 0)
|
history := make([]zabbix.HistoryPoint, 0)
|
||||||
for _, point := range trend {
|
for _, point := range trend {
|
||||||
valueStr := point.ValueAvg
|
value, err := getTrendPointValue(point, valueType)
|
||||||
switch valueType {
|
|
||||||
case "min":
|
|
||||||
valueStr = point.ValueMin
|
|
||||||
case "max":
|
|
||||||
valueStr = point.ValueMax
|
|
||||||
}
|
|
||||||
|
|
||||||
value, err := strconv.ParseFloat(valueStr, 64)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error parsing trend value: %s", err)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
history = append(history, zabbix.HistoryPoint{
|
history = append(history, zabbix.HistoryPoint{
|
||||||
@@ -179,3 +171,41 @@ func convertTrendToHistory(trend zabbix.Trend, valueType string) (zabbix.History
|
|||||||
|
|
||||||
return history, nil
|
return history, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getTrendPointValue(point zabbix.TrendPoint, valueType string) (float64, error) {
|
||||||
|
if valueType == "avg" || valueType == "min" || valueType == "max" || valueType == "count" {
|
||||||
|
valueStr := point.ValueAvg
|
||||||
|
switch valueType {
|
||||||
|
case "min":
|
||||||
|
valueStr = point.ValueMin
|
||||||
|
case "max":
|
||||||
|
valueStr = point.ValueMax
|
||||||
|
case "count":
|
||||||
|
valueStr = point.Num
|
||||||
|
}
|
||||||
|
|
||||||
|
value, err := strconv.ParseFloat(valueStr, 64)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("error parsing trend value: %s", err)
|
||||||
|
}
|
||||||
|
return value, nil
|
||||||
|
} else if valueType == "sum" {
|
||||||
|
avgStr := point.ValueAvg
|
||||||
|
avg, err := strconv.ParseFloat(avgStr, 64)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("error parsing trend value: %s", err)
|
||||||
|
}
|
||||||
|
countStr := point.Num
|
||||||
|
count, err := strconv.ParseFloat(countStr, 64)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("error parsing trend value: %s", err)
|
||||||
|
}
|
||||||
|
if count > 0 {
|
||||||
|
return avg * count, nil
|
||||||
|
} else {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0, fmt.Errorf("failed to get trend value, unknown value type: %s", valueType)
|
||||||
|
}
|
||||||
|
|||||||
@@ -61,11 +61,11 @@ func (ds *ZabbixDatasourceInstance) queryNumericItems(ctx context.Context, query
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ds *ZabbixDatasourceInstance) queryNumericDataForItems(ctx context.Context, query *QueryModel, items []*zabbix.Item) ([]*data.Frame, error) {
|
func (ds *ZabbixDatasourceInstance) queryNumericDataForItems(ctx context.Context, query *QueryModel, items []*zabbix.Item) ([]*data.Frame, error) {
|
||||||
valueType := ds.getTrendValueType(query)
|
trendValueType := ds.getTrendValueType(query)
|
||||||
consolidateBy := ds.getConsolidateBy(query)
|
consolidateBy := ds.getConsolidateBy(query)
|
||||||
|
|
||||||
if consolidateBy == "" {
|
if consolidateBy != "" {
|
||||||
consolidateBy = valueType
|
trendValueType = consolidateBy
|
||||||
}
|
}
|
||||||
|
|
||||||
err := applyFunctionsPre(query, items)
|
err := applyFunctionsPre(query, items)
|
||||||
@@ -73,7 +73,7 @@ func (ds *ZabbixDatasourceInstance) queryNumericDataForItems(ctx context.Context
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
history, err := ds.getHistotyOrTrend(ctx, query, items, consolidateBy)
|
history, err := ds.getHistotyOrTrend(ctx, query, items, trendValueType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user