Skip to content

Commit

Permalink
Tweaks to status support
Browse files Browse the repository at this point in the history
  • Loading branch information
bitfield committed Aug 31, 2019
1 parent 056da22 commit 7270106
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 236 deletions.
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
)

var version = "0.11.0"
var version = "0.12.0"

var versionCmd = &cobra.Command{
Use: "version",
Expand Down
32 changes: 2 additions & 30 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,42 +1,14 @@
module github.com/bitfield/uptimerobot

require (
cloud.google.com/go v0.44.0 // indirect
github.com/coreos/bbolt v1.3.3 // indirect
github.com/coreos/etcd v3.3.13+incompatible // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f // indirect
github.com/go-kit/kit v0.9.0 // indirect
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 // indirect
github.com/google/go-cmp v0.3.1
github.com/google/pprof v0.0.0-20190723021845-34ac40c74b70 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.9.5 // indirect
github.com/hashicorp/golang-lru v0.5.3 // indirect
github.com/kisielk/errcheck v1.2.0 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/kr/pty v1.1.8 // indirect
github.com/magiconair/properties v1.8.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
github.com/pelletier/go-toml v1.4.0 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/prometheus/client_golang v1.1.0 // indirect
github.com/rogpeppe/fastuuid v1.2.0 // indirect
github.com/russross/blackfriday v2.0.0+incompatible // indirect
github.com/sirupsen/logrus v1.4.2 // indirect
github.com/spf13/afero v1.2.2 // indirect
github.com/spf13/cobra v0.0.5
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/viper v1.4.0
github.com/stretchr/objx v0.2.0 // indirect
github.com/ugorji/go v1.1.7 // indirect
go.etcd.io/bbolt v1.3.3 // indirect
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 // indirect
golang.org/x/mobile v0.0.0-20190806162312-597adff16ade // indirect
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 // indirect
github.com/stretchr/testify v1.3.0 // indirect
golang.org/x/sys v0.0.0-20190812073006-9eafafc0a87e // indirect
golang.org/x/tools v0.0.0-20190809145639-6d4652c779c4 // indirect
google.golang.org/grpc v1.22.1 // indirect
honnef.co/go/tools v0.0.1-2019.2.2 // indirect
golang.org/x/text v0.3.2 // indirect
)
163 changes: 0 additions & 163 deletions go.sum

Large diffs are not rendered by default.

49 changes: 13 additions & 36 deletions pkg/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,39 +49,16 @@ const StatusPaused = 0
// status when calling EditMonitor.
const StatusResumed = 1

// Status corresponds to the reported status of a monitor from the uptimerobot API
type Status int

// Status codes for each possible monitor status returned by uptimerobot
const (
Up Status = 2
Down Status = 9
MaybeDown Status = 8
Unknown Status = 1
Paused Status = 0
)

var statuses = map[Status]string{
Up: "Up",
Down: "Down",
MaybeDown: "Maybe Down",
Unknown: "Not Checked",
Paused: "Paused",
}

func (s Status) String() string {
statuses := map[Status]string{
Up: "Up",
Down: "Down",
MaybeDown: "Maybe Down",
Unknown: "Not Checked",
Paused: "Paused",
}

statusString, ok := statuses[s]
if !ok {
return "Unknown Status"
}
return statusString

}
// StatusUnknown is the status value indicating that the monitor status is
// currently unknown.
const StatusUnknown = 1

// StatusUp is the status value indicating that the monitor is currently up.
const StatusUp = 2

// StatusMaybeDown is the status value indicating that the monitor may be down,
// but this has not yet been confirmed.
const StatusMaybeDown = 8

// StatusDown is the status value indicating that the monitor is currently down.
const StatusDown = 9
29 changes: 23 additions & 6 deletions pkg/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ type Monitor struct {
Port int `json:"port"`
KeywordValue string `json:"keyword_value,omitempty"`
AlertContacts []string `json:"alert_contacts,omitempty"`
Status Status `json:"status"`
Status int `json:"status,omitempty"`
}

const monitorTemplate = `ID: {{ .ID }}
Name: {{ .FriendlyName }}
URL: {{ .URL -}}
URL: {{ .URL }}
Status: {{ .FriendlyStatus -}}
{{ if .Port }}{{ printf "\nPort: %d" .Port }}{{ end -}}
{{ if .Type }}{{ printf "\nType: %s" .FriendlyType }}{{ end -}}
Status: {{ .Status }}
{{ if .SubType }}{{ printf "\nSubtype: %s" .FriendlySubType }}{{ end -}}
{{ if .KeywordType }}{{ printf "\nKeywordType: %s" .FriendlyKeywordType }}{{ end -}}
{{ if .KeywordValue }}{{ printf "\nKeyword: %s" .KeywordValue }}{{ end }}`
Expand All @@ -48,7 +48,7 @@ func (m Monitor) FriendlyType() string {
case TypePort:
return "Port"
default:
return fmt.Sprintf("%v", m.Type)
return fmt.Sprintf("%d", m.Type)
}
}

Expand All @@ -71,7 +71,7 @@ func (m Monitor) FriendlySubType() string {
case SubTypeCustomPort:
return fmt.Sprintf("Custom port (%d)", m.Port)
default:
return fmt.Sprintf("%v", m.SubType)
return fmt.Sprintf("%d", m.SubType)
}
}

Expand All @@ -83,7 +83,24 @@ func (m Monitor) FriendlyKeywordType() string {
case KeywordNotExists:
return "NotExists"
default:
return fmt.Sprintf("%v", m.KeywordType)
return fmt.Sprintf("%d", m.KeywordType)
}
}

func (m Monitor) FriendlyStatus() string {
switch m.Status {
case StatusPaused:
return "Paused"
case StatusUnknown:
return "Unknown"
case StatusUp:
return "Up"
case StatusMaybeDown:
return "MaybeDown"
case StatusDown:
return "Down"
default:
return fmt.Sprintf("%d", m.Status)
}
}

Expand Down
1 change: 1 addition & 0 deletions pkg/testdata/monitor_http.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ID: 777749809
Name: Google
URL: http://www.google.com
Status: Up
Port: 80
Type: HTTP
1 change: 1 addition & 0 deletions pkg/testdata/monitor_keyword.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ID: 777749810
Name: Google
URL: http://www.google.com
Status: MaybeDown
Port: 80
Type: Keyword
KeywordType: Exists
Expand Down
1 change: 1 addition & 0 deletions pkg/testdata/monitor_keyword_notexists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ID: 777749811
Name: Google
URL: http://www.google.com
Status: Paused
Port: 80
Type: Keyword
KeywordType: NotExists
Expand Down
1 change: 1 addition & 0 deletions pkg/testdata/monitor_subtype.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ID: 777749812
Name: Google
URL: http://www.google.com
Status: Paused
Port: 80
Type: Port
Subtype: FTP (21)
11 changes: 11 additions & 0 deletions pkg/uptimerobot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func TestUnmarshalMonitor(t *testing.T) {
URL: "http://www.google.com",
Type: TypeHTTP,
Port: 80,
Status: StatusUnknown,
}
data, err := ioutil.ReadFile("testdata/unmarshal.json")
if err != nil {
Expand Down Expand Up @@ -206,6 +207,7 @@ func TestGetMonitorByID(t *testing.T) {
URL: "http://www.google.com",
Type: TypeHTTP,
Port: 80,
Status: StatusUnknown,
}
got, err := client.GetMonitor(want.ID)
if err != nil {
Expand Down Expand Up @@ -274,12 +276,14 @@ func TestGetMonitors(t *testing.T) {
URL: "http://www.google.com",
Type: TypeHTTP,
Port: 80,
Status: StatusUnknown,
},
{
ID: 777712827,
FriendlyName: "My Web Page",
URL: "http://mywebpage.com/",
Type: TypeHTTP,
Status: StatusUp,
},
{
ID: 777559666,
Expand All @@ -288,6 +292,7 @@ func TestGetMonitors(t *testing.T) {
Type: TypePort,
SubType: SubTypeFTP,
Port: 21,
Status: StatusUp,
},
{
ID: 781397847,
Expand All @@ -296,6 +301,7 @@ func TestGetMonitors(t *testing.T) {
Type: TypePort,
SubType: SubTypeCustomPort,
Port: 8000,
Status: StatusUnknown,
},
}
got, err := client.AllMonitors()
Expand All @@ -320,6 +326,7 @@ func TestGetMonitorsBySearch(t *testing.T) {
FriendlyName: "My Web Page",
URL: "http://mywebpage.com/",
Type: TypeHTTP,
Status: StatusUp,
},
}
got, err := client.SearchMonitors("My Web Page")
Expand Down Expand Up @@ -425,6 +432,7 @@ func TestRenderMonitor(t *testing.T) {
Type: TypeHTTP,
Port: 0,
AlertContacts: []string{"3", "5", "7"},
Status: StatusUp,
},
wantFile: "testdata/monitor_http.txt",
},
Expand All @@ -438,6 +446,7 @@ func TestRenderMonitor(t *testing.T) {
KeywordType: KeywordExists,
KeywordValue: "bogus",
Port: 80,
Status: StatusMaybeDown,
},
wantFile: "testdata/monitor_keyword.txt",
},
Expand All @@ -451,6 +460,7 @@ func TestRenderMonitor(t *testing.T) {
KeywordType: KeywordNotExists,
KeywordValue: "bogus",
Port: 80,
Status: StatusUnknown,
},
wantFile: "testdata/monitor_keyword_notexists.txt",
},
Expand All @@ -463,6 +473,7 @@ func TestRenderMonitor(t *testing.T) {
Type: TypePort,
SubType: SubTypeFTP,
Port: 80,
Status: StatusPaused,
},
wantFile: "testdata/monitor_subtype.txt",
},
Expand Down

0 comments on commit 7270106

Please sign in to comment.