Merge pull request #14670 from influxdata/revert-14660-threshold_type

Revert "feat(notification/check): convert threshold type"
pull/14648/head
Michael Desa 2019-08-15 14:08:22 -04:00 committed by GitHub
commit 8e12b1cbb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 180 deletions

View File

@ -50,8 +50,6 @@ func TestService_handleGetChecks(t *testing.T) {
fl1 := 100.32
fl2 := 200.64
fl4 := 100.1
fl5 := 3023.2
tests := []struct {
name string
@ -82,15 +80,8 @@ func TestService_handleGetChecks(t *testing.T) {
Status: influxdb.Inactive,
},
Thresholds: []check.ThresholdConfig{
&check.Greater{
Value: fl1,
ThresholdConfigBase: check.ThresholdConfigBase{Level: notification.Critical},
},
&check.Lesser{
Value: fl2,
ThresholdConfigBase: check.ThresholdConfigBase{Level: notification.Info},
},
&check.Range{Min: fl4, Max: fl5},
{LowerBound: &fl1},
{UpperBound: &fl2},
},
},
}, 2, nil
@ -203,27 +194,17 @@ func TestService_handleGetChecks(t *testing.T) {
"thresholds": [
{
"allValues": false,
"level": "CRIT",
"type": "greater",
"value": 100.32
"level": "UNKNOWN",
"lowerBound": 100.32
},
{
"allValues": false,
"level": "INFO",
"type": "lesser",
"value": 200.64
},
{
"allValues": false,
"level": "UNKNOWN",
"max": 3023.2,
"min": 100.1,
"type": "range",
"within": false
}
"level": "UNKNOWN",
"upperBound": 200.64
}
],
"type": "threshold",
"labels": [
"labels": [
{
"id": "fc3dc670a4be9b9a",
"name": "label",

View File

@ -130,22 +130,22 @@ var typeToCheck = map[string](func() influxdb.Check){
"threshold": func() influxdb.Check { return &Threshold{} },
}
type rawJSON struct {
Type string `json:"type"`
type rawRuleJSON struct {
Typ string `json:"type"`
}
// UnmarshalJSON will convert
func UnmarshalJSON(b []byte) (influxdb.Check, error) {
var raw rawJSON
var raw rawRuleJSON
if err := json.Unmarshal(b, &raw); err != nil {
return nil, &influxdb.Error{
Msg: "unable to detect the check type from json",
}
}
convertedFunc, ok := typeToCheck[raw.Type]
convertedFunc, ok := typeToCheck[raw.Typ]
if !ok {
return nil, &influxdb.Error{
Msg: fmt.Sprintf("invalid check type %s", raw.Type),
Msg: fmt.Sprintf("invalid check type %s", raw.Typ),
}
}
converted := convertedFunc()

View File

@ -21,6 +21,12 @@ const (
id3 = "020f755c3c082002"
)
func numPtr(f float64) *float64 {
p := new(float64)
*p = f
return p
}
var goodBase = check.Base{
ID: influxTesting.MustIDBase16(id1),
Name: "name1",
@ -123,14 +129,12 @@ func TestValidCheck(t *testing.T) {
{
name: "bad thredshold",
src: &check.Threshold{
Base: goodBase,
Thresholds: []check.ThresholdConfig{
&check.Range{Min: 200, Max: 100},
},
Base: goodBase,
Thresholds: []check.ThresholdConfig{{}},
},
err: &influxdb.Error{
Code: influxdb.EInvalid,
Msg: "range threshold min can't be larger than max",
Msg: "threshold must have at least one lowerBound or upperBound value",
},
},
}
@ -204,9 +208,8 @@ func TestJSON(t *testing.T) {
},
},
Thresholds: []check.ThresholdConfig{
&check.Greater{ThresholdConfigBase: check.ThresholdConfigBase{AllValues: true}, Value: -1.36},
&check.Range{Min: -10000, Max: 500},
&check.Lesser{ThresholdConfigBase: check.ThresholdConfigBase{Level: notification.Critical}},
{AllValues: true, LowerBound: numPtr(-1.36)},
{LowerBound: numPtr(10000), UpperBound: numPtr(500)},
},
},
},

View File

@ -2,7 +2,6 @@ package check
import (
"encoding/json"
"fmt"
"github.com/influxdata/influxdb"
"github.com/influxdata/influxdb/notification"
@ -48,145 +47,21 @@ func (c Threshold) MarshalJSON() ([]byte, error) {
})
}
type thresholdDecode struct {
Base
Thresholds []thresholdConfigDecode `json:"thresholds"`
}
type thresholdConfigDecode struct {
ThresholdConfigBase
Type string `json:"type"`
Value float64 `json:"value"`
Min float64 `json:"min"`
Max float64 `json:"max"`
Within bool `json:"within"`
}
// UnmarshalJSON implement json.Unmarshaler interface.
func (c *Threshold) UnmarshalJSON(b []byte) error {
tdRaws := new(thresholdDecode)
if err := json.Unmarshal(b, tdRaws); err != nil {
return err
}
c.Base = tdRaws.Base
for _, tdRaw := range tdRaws.Thresholds {
switch tdRaw.Type {
case "lesser":
td := &Lesser{
ThresholdConfigBase: tdRaw.ThresholdConfigBase,
Value: tdRaw.Value,
}
c.Thresholds = append(c.Thresholds, td)
case "greater":
td := &Greater{
ThresholdConfigBase: tdRaw.ThresholdConfigBase,
Value: tdRaw.Value,
}
c.Thresholds = append(c.Thresholds, td)
case "range":
td := &Range{
ThresholdConfigBase: tdRaw.ThresholdConfigBase,
Min: tdRaw.Min,
Max: tdRaw.Max,
Within: tdRaw.Within,
}
c.Thresholds = append(c.Thresholds, td)
default:
return &influxdb.Error{
Msg: fmt.Sprintf("invalid threshold type %s", tdRaw.Type),
}
}
}
return nil
}
// ThresholdConfig is the base of all threshold config.
type ThresholdConfig interface {
MarshalJSON() ([]byte, error)
Valid() error
type ThresholdConfig struct {
// If true, only alert if all values meet threshold.
AllValues bool `json:"allValues"`
Level notification.CheckLevel `json:"level"`
LowerBound *float64 `json:"lowerBound,omitempty"`
UpperBound *float64 `json:"upperBound,omitempty"`
}
// Valid returns error if something is invalid.
func (c ThresholdConfigBase) Valid() error {
return nil
}
// ThresholdConfigBase is the base of all threshold config.
type ThresholdConfigBase struct {
// If true, only alert if all values meet threshold.
AllValues bool `json:"allValues"`
Level notification.CheckLevel `json:"level"`
}
// Lesser threshold type.
type Lesser struct {
ThresholdConfigBase
Value float64 `json:"value,omitempty"`
}
type lesserAlias Lesser
// MarshalJSON implement json.Marshaler interface.
func (td Lesser) MarshalJSON() ([]byte, error) {
return json.Marshal(
struct {
lesserAlias
Type string `json:"type"`
}{
lesserAlias: lesserAlias(td),
Type: "lesser",
})
}
// Greater threshold type.
type Greater struct {
ThresholdConfigBase
Value float64 `json:"value,omitempty"`
}
type greaterAlias Greater
// MarshalJSON implement json.Marshaler interface.
func (td Greater) MarshalJSON() ([]byte, error) {
return json.Marshal(
struct {
greaterAlias
Type string `json:"type"`
}{
greaterAlias: greaterAlias(td),
Type: "greater",
})
}
// Range threshold type.
type Range struct {
ThresholdConfigBase
Min float64 `json:"min,omitempty"`
Max float64 `json:"max,omitempty"`
Within bool `json:"within"`
}
type rangeAlias Range
// MarshalJSON implement json.Marshaler interface.
func (td Range) MarshalJSON() ([]byte, error) {
return json.Marshal(
struct {
rangeAlias
Type string `json:"type"`
}{
rangeAlias: rangeAlias(td),
Type: "range",
})
}
// Valid overwrite the base threshold.
func (td Range) Valid() error {
if td.Min > td.Max {
func (c ThresholdConfig) Valid() error {
if c.LowerBound == nil && c.UpperBound == nil {
return &influxdb.Error{
Code: influxdb.EInvalid,
Msg: "range threshold min can't be larger than max",
Msg: "threshold must have at least one lowerBound or upperBound value",
}
}
return nil

View File

@ -61,9 +61,8 @@ var threshold1 = &check.Threshold{
},
},
Thresholds: []check.ThresholdConfig{
&check.Lesser{Value: 1000},
&check.Greater{Value: 2000},
&check.Range{Min: 1500, Max: 1900, Within: true},
{LowerBound: FloatPtr(1000)},
{UpperBound: FloatPtr(2000)},
},
}
@ -283,9 +282,8 @@ func CreateCheck(
},
},
Thresholds: []check.ThresholdConfig{
&check.Lesser{Value: 1000},
&check.Greater{Value: 2000},
&check.Range{Min: 1500, Max: 1900, Within: true},
{LowerBound: FloatPtr(1000)},
{UpperBound: FloatPtr(2000)},
},
},
},