Skip to content

Commit

Permalink
chore: refactor bits of checks and its dependencies for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
jsteenb2 committed Dec 18, 2019
1 parent 409a438 commit fa1a9b6
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 32 deletions.
37 changes: 19 additions & 18 deletions http/check_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"

"github.com/influxdata/httprouter"
Expand Down Expand Up @@ -369,47 +370,47 @@ type decodeStatus struct {
Status influxdb.Status `json:"status"`
}

func decodePostCheckRequest(ctx context.Context, r *http.Request) (postCheckRequest, error) {
var req postCheckRequest

buf := new(bytes.Buffer)
_, err := buf.ReadFrom(r.Body)
func decodePostCheckRequest(r *http.Request) (postCheckRequest, error) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return req, &influxdb.Error{
return postCheckRequest{}, &influxdb.Error{
Code: influxdb.EInvalid,
Err: err,
}
}
defer r.Body.Close()
chk, err := check.UnmarshalJSON(buf.Bytes())

chk, err := check.UnmarshalJSON(b)
if err != nil {
return req, &influxdb.Error{
return postCheckRequest{}, &influxdb.Error{
Code: influxdb.EInvalid,
Err: err,
}
}

var ds decodeStatus
err = json.Unmarshal(buf.Bytes(), &ds)
if err != nil {
return req, &influxdb.Error{
if err := json.Unmarshal(b, &ds); err != nil {
return postCheckRequest{}, &influxdb.Error{
Code: influxdb.EInvalid,
Err: err,
}
}

var dl decodeLabels
err = json.Unmarshal(buf.Bytes(), &dl)
if err != nil {
return req, &influxdb.Error{
if err := json.Unmarshal(b, &dl); err != nil {
return postCheckRequest{}, &influxdb.Error{
Code: influxdb.EInvalid,
Err: err,
}
}

req = postCheckRequest{CheckCreate: influxdb.CheckCreate{Check: chk, Status: ds.Status}, Labels: dl.Labels}

return req, nil
return postCheckRequest{
CheckCreate: influxdb.CheckCreate{
Check: chk,
Status: ds.Status,
},
Labels: dl.Labels,
}, nil
}

func decodePutCheckRequest(ctx context.Context, r *http.Request) (influxdb.CheckCreate, error) {
Expand Down Expand Up @@ -514,7 +515,7 @@ func decodePatchCheckRequest(ctx context.Context, r *http.Request) (*patchCheckR
// handlePostCheck is the HTTP handler for the POST /api/v2/checks route.
func (h *CheckHandler) handlePostCheck(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
chk, err := decodePostCheckRequest(ctx, r)
chk, err := decodePostCheckRequest(r)
if err != nil {
h.log.Debug("Failed to decode request", zap.Error(err))
h.HandleHTTPError(ctx, err, w)
Expand Down
2 changes: 1 addition & 1 deletion http/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10083,7 +10083,7 @@ components:
query:
description: URL to retrieve flux script for this check
$ref: "#/components/schemas/Link"
required: [name, type, orgID, query]
required: [name, orgID, query]
ThresholdCheck:
allOf:
- $ref: "#/components/schemas/CheckBase"
Expand Down
12 changes: 5 additions & 7 deletions notification/check/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,22 +187,20 @@ var typeToCheck = map[string](func() influxdb.Check){
"threshold": func() influxdb.Check { return &Threshold{} },
}

type rawRuleJSON struct {
Typ string `json:"type"`
}

// UnmarshalJSON will convert
func UnmarshalJSON(b []byte) (influxdb.Check, error) {
var raw rawRuleJSON
var raw struct {
Type string `json:"type"`
}
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.Typ]
convertedFunc, ok := typeToCheck[raw.Type]
if !ok {
return nil, &influxdb.Error{
Msg: fmt.Sprintf("invalid check type %s", raw.Typ),
Msg: fmt.Sprintf("invalid check type %s", raw.Type),
}
}
converted := convertedFunc()
Expand Down
9 changes: 3 additions & 6 deletions notification/check/threshold.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,9 @@ func (td Lesser) Type() string {
return "lesser"
}

type lesserAlias Lesser

// MarshalJSON implement json.Marshaler interface.
func (td Lesser) MarshalJSON() ([]byte, error) {
type lesserAlias Lesser
return json.Marshal(
struct {
lesserAlias
Expand All @@ -412,10 +411,9 @@ func (td Greater) Type() string {
return "greater"
}

type greaterAlias Greater

// MarshalJSON implement json.Marshaler interface.
func (td Greater) MarshalJSON() ([]byte, error) {
type greaterAlias Greater
return json.Marshal(
struct {
greaterAlias
Expand All @@ -439,10 +437,9 @@ func (td Range) Type() string {
return "range"
}

type rangeAlias Range

// MarshalJSON implement json.Marshaler interface.
func (td Range) MarshalJSON() ([]byte, error) {
type rangeAlias Range
return json.Marshal(
struct {
rangeAlias
Expand Down
9 changes: 9 additions & 0 deletions notification/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,12 @@ func (d *Duration) UnmarshalJSON(b []byte) error {

return nil
}

// FromTimeDuration converts a time.Duration to a notification.Duration type.
func FromTimeDuration(d time.Duration) (Duration, error) {
dur, err := parser.ParseDuration(d.String())
if err != nil {
return Duration{}, err
}
return Duration(*dur), nil
}
2 changes: 2 additions & 0 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const (
TaskDefaultPageSize = 100
TaskMaxPageSize = 500

// TODO(jsteenb2): make these constants of type Status

TaskStatusActive = "active"
TaskStatusInactive = "inactive"
)
Expand Down

0 comments on commit fa1a9b6

Please sign in to comment.