From ff994cb7930ec447258035928704bf0374bcdfef Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Tue, 3 Apr 2018 03:44:57 -0700 Subject: [PATCH] OpsGenie on-call schedules into dashboard --- opsgenie/client.go | 27 +++++++++++++-------------- opsgenie/widget.go | 15 ++++++++++----- wtf.go | 12 ++++++------ 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/opsgenie/client.go b/opsgenie/client.go index 97c7c300..49a1c027 100644 --- a/opsgenie/client.go +++ b/opsgenie/client.go @@ -7,15 +7,16 @@ import ( "os" ) -type Data struct { - OnCallRecipients []string `json:"onCallRecipients"` - Parent Parent `json:"_parent"` +type OnCallResponse struct { + OnCallData []OnCallData `json:"data"` + Message string `json:"message"` + RequestID string `json:"requestId"` + Took float32 `json:"took"` } + type OnCallData struct { - Data Data `json:"data"` - Message string `json:"message"` - RequestID string `json:"requestId"` - Took float32 `json:"took"` + Recipients []string `json:"onCallRecipients"` + Parent Parent `json:"_parent"` } type Parent struct { @@ -24,11 +25,10 @@ type Parent struct { Enabled bool `json:"enabled"` } -func Fetch() *OnCallData { +func Fetch() *OnCallResponse { apiKey := os.Getenv("WTF_OPS_GENIE_API_KEY") - scheduleName := "Oversight" - url := fmt.Sprintf("https://api.opsgenie.com/v2/schedules/%s/on-calls?scheduleIdentifierType=name&flat=true", scheduleName) + url := "https://api.opsgenie.com/v2/schedules/on-calls?flat=true" req, err := http.NewRequest("GET", url, nil) if err != nil { @@ -45,11 +45,10 @@ func Fetch() *OnCallData { } defer resp.Body.Close() - var onCallData OnCallData - - if err := json.NewDecoder(resp.Body).Decode(&onCallData); err != nil { + var onCallResponse OnCallResponse + if err := json.NewDecoder(resp.Body).Decode(&onCallResponse); err != nil { panic(err) } - return &onCallData + return &onCallResponse } diff --git a/opsgenie/widget.go b/opsgenie/widget.go index fc4ca394..5e626197 100644 --- a/opsgenie/widget.go +++ b/opsgenie/widget.go @@ -20,7 +20,7 @@ func NewWidget() *Widget { BaseWidget: wtf.BaseWidget{ Name: "OpsGenie", RefreshedAt: time.Now(), - RefreshInt: 28800, + RefreshInt: 21600, }, } @@ -35,7 +35,7 @@ func NewWidget() *Widget { func (widget *Widget) Refresh() { data := Fetch() - widget.View.SetTitle(" OpsGenie ") + widget.View.SetTitle(" ⏰ On Call ") widget.RefreshedAt = time.Now() widget.View.Clear() @@ -51,14 +51,19 @@ func (widget *Widget) addView() { view.SetBorderColor(tcell.ColorGray) view.SetDynamicColors(true) view.SetTitle(widget.Name) + view.SetWrap(false) widget.View = view } -func (widget *Widget) contentFrom(onCallData *OnCallData) string { +func (widget *Widget) contentFrom(onCallResponse *OnCallResponse) string { str := "\n" - str = str + fmt.Sprintf(" [red]%s[white]\n", onCallData.Data.Parent.Name) - str = str + fmt.Sprintf(" %s\n", strings.Join(onCallData.Data.OnCallRecipients, ", ")) + + for _, data := range onCallResponse.OnCallData { + str = str + fmt.Sprintf(" [green]%s[white]\n", data.Parent.Name) + str = str + fmt.Sprintf(" %s\n", strings.Join(data.Recipients, ", ")) + str = str + "\n" + } return str } diff --git a/wtf.go b/wtf.go index 36e348a5..7f7ab720 100644 --- a/wtf.go +++ b/wtf.go @@ -55,18 +55,18 @@ func main() { weather.Refresh() grid := tview.NewGrid() - grid.SetRows(9, 9, 9, 9, 15, 3) // How _high_ the row is, in terminal rows - grid.SetColumns(40, 40) // How _wide_ the column is, in terminal columns + grid.SetRows(9, 9, 9, 9, 9, 6, 3) // How _high_ the row is, in terminal rows + grid.SetColumns(40, 40) // How _wide_ the column is, in terminal columns grid.SetBorder(false) grid.AddItem(bamboo.View, 0, 0, 2, 1, 0, 0, false) - grid.AddItem(cal.View, 2, 0, 3, 1, 0, 0, false) + grid.AddItem(cal.View, 2, 1, 4, 1, 0, 0, false) grid.AddItem(git.View, 0, 2, 3, 1, 0, 0, false) grid.AddItem(weather.View, 0, 1, 1, 1, 0, 0, false) grid.AddItem(sec.View, 1, 1, 1, 1, 0, 0, false) - grid.AddItem(opsgenie.View, 2, 1, 1, 1, 0, 0, false) - grid.AddItem(jira.View, 3, 1, 1, 1, 0, 0, false) - grid.AddItem(stat.View, 5, 0, 3, 3, 0, 0, false) + grid.AddItem(opsgenie.View, 2, 0, 3, 1, 0, 0, false) + grid.AddItem(jira.View, 5, 0, 1, 1, 0, 0, false) + grid.AddItem(stat.View, 6, 0, 3, 3, 0, 0, false) app := tview.NewApplication()