mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Display OpsGenie oncall data for one specific schedule
This commit is contained in:
committed by
Chris Cummer
parent
62502c24d0
commit
945dfc8db7
@@ -1,33 +1,55 @@
|
||||
package opsgenie
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
ogcli "github.com/opsgenie/opsgenie-go-sdk/client"
|
||||
sch "github.com/opsgenie/opsgenie-go-sdk/schedule"
|
||||
)
|
||||
|
||||
func Fetch() string {
|
||||
apiKey := os.Getenv("WTF_OPS_GENIE_API_KEY")
|
||||
|
||||
cli := new(ogcli.OpsGenieClient)
|
||||
cli.SetAPIKey(apiKey)
|
||||
|
||||
scheduler, err := cli.Schedule()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
request := sch.ListSchedulesRequest{}
|
||||
response, err := scheduler.List(request)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var str string
|
||||
for _, schedule := range response.Schedules {
|
||||
str = str + schedule.Name + "\n"
|
||||
}
|
||||
|
||||
return ""
|
||||
type Data struct {
|
||||
OnCallRecipients []string `json:"onCallRecipients"`
|
||||
Parent Parent `json:"_parent"`
|
||||
}
|
||||
type OnCallData struct {
|
||||
Data Data `json:"data"`
|
||||
Message string `json:"message"`
|
||||
RequestID string `json:"requestId"`
|
||||
Took float32 `json:"took"`
|
||||
}
|
||||
|
||||
type Parent struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Enabled bool `json:"enabled"`
|
||||
}
|
||||
|
||||
func Fetch() *OnCallData {
|
||||
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)
|
||||
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", fmt.Sprintf("GenieKey %s", apiKey))
|
||||
|
||||
client := &http.Client{}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var onCallData OnCallData
|
||||
|
||||
if err := json.NewDecoder(resp.Body).Decode(&onCallData); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return &onCallData
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package opsgenie
|
||||
|
||||
import (
|
||||
//"fmt"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/rivo/tview"
|
||||
@@ -31,13 +32,13 @@ func NewWidget() *Widget {
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) Refresh() {
|
||||
//data := Fetch()
|
||||
data := Fetch()
|
||||
|
||||
widget.View.SetTitle(" OpsGenie ")
|
||||
widget.RefreshedAt = time.Now()
|
||||
|
||||
widget.View.Clear()
|
||||
//fmt.Fprintf(widget.View, "%s", widget.contentFrom(data))
|
||||
fmt.Fprintf(widget.View, "%s", widget.contentFrom(data))
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
@@ -52,6 +53,10 @@ func (widget *Widget) addView() {
|
||||
widget.View = view
|
||||
}
|
||||
|
||||
func (widget *Widget) contentFrom(data string) string {
|
||||
return data
|
||||
func (widget *Widget) contentFrom(onCallData *OnCallData) 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, ", "))
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user