mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
73
modules/pagerduty/client.go
Normal file
73
modules/pagerduty/client.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package pagerduty
|
||||
|
||||
import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/PagerDuty/go-pagerduty"
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
)
|
||||
|
||||
// GetOnCalls returns a list of people currently on call
|
||||
func GetOnCalls() ([]pagerduty.OnCall, error) {
|
||||
client := pagerduty.NewClient(apiKey())
|
||||
|
||||
var results []pagerduty.OnCall
|
||||
|
||||
var queryOpts pagerduty.ListOnCallOptions
|
||||
queryOpts.Since = time.Now().Format("2006-01-02T15:04:05Z07:00")
|
||||
queryOpts.Until = time.Now().Format("2006-01-02T15:04:05Z07:00")
|
||||
|
||||
oncalls, err := client.ListOnCalls(queryOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
results = append(results, oncalls.OnCalls...)
|
||||
|
||||
for oncalls.APIListObject.More == true {
|
||||
queryOpts.APIListObject.Offset = oncalls.APIListObject.Offset
|
||||
oncalls, err = client.ListOnCalls(queryOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results = append(results, oncalls.OnCalls...)
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// GetIncidents returns a list of people currently on call
|
||||
func GetIncidents() ([]pagerduty.Incident, error) {
|
||||
client := pagerduty.NewClient(apiKey())
|
||||
|
||||
var results []pagerduty.Incident
|
||||
|
||||
var queryOpts pagerduty.ListIncidentsOptions
|
||||
queryOpts.DateRange = "all"
|
||||
queryOpts.Statuses = []string{"triggered", "acknowledged"}
|
||||
|
||||
items, err := client.ListIncidents(queryOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results = append(results, items.Incidents...)
|
||||
|
||||
for items.APIListObject.More == true {
|
||||
queryOpts.APIListObject.Offset = items.APIListObject.Offset
|
||||
items, err = client.ListIncidents(queryOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results = append(results, items.Incidents...)
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func apiKey() string {
|
||||
return wtf.Config.UString(
|
||||
"wtf.mods.pagerduty.apiKey",
|
||||
os.Getenv("WTF_PAGERDUTY_API_KEY"),
|
||||
)
|
||||
}
|
||||
12
modules/pagerduty/sort.go
Normal file
12
modules/pagerduty/sort.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package pagerduty
|
||||
|
||||
import "github.com/PagerDuty/go-pagerduty"
|
||||
|
||||
type ByEscalationLevel []pagerduty.OnCall
|
||||
|
||||
func (s ByEscalationLevel) Len() int { return len(s) }
|
||||
func (s ByEscalationLevel) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||
|
||||
func (s ByEscalationLevel) Less(i, j int) bool {
|
||||
return s[i].EscalationLevel < s[j].EscalationLevel
|
||||
}
|
||||
113
modules/pagerduty/widget.go
Normal file
113
modules/pagerduty/widget.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package pagerduty
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"github.com/PagerDuty/go-pagerduty"
|
||||
"github.com/rivo/tview"
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
)
|
||||
|
||||
type Widget struct {
|
||||
wtf.TextWidget
|
||||
}
|
||||
|
||||
func NewWidget(app *tview.Application) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(app, "PagerDuty", "pagerduty", false),
|
||||
}
|
||||
|
||||
return &widget
|
||||
}
|
||||
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) Refresh() {
|
||||
var onCalls []pagerduty.OnCall
|
||||
var incidents []pagerduty.Incident
|
||||
|
||||
var err1 error
|
||||
var err2 error
|
||||
|
||||
if wtf.Config.UBool("wtf.mods.pagerduty.showSchedules", true) {
|
||||
onCalls, err1 = GetOnCalls()
|
||||
}
|
||||
|
||||
if wtf.Config.UBool("wtf.mods.pagerduty.showIncidents") {
|
||||
incidents, err2 = GetIncidents()
|
||||
}
|
||||
|
||||
widget.View.SetTitle(widget.ContextualTitle(fmt.Sprintf("%s", widget.Name)))
|
||||
widget.View.Clear()
|
||||
|
||||
var content string
|
||||
if err1 != nil || err2 != nil {
|
||||
widget.View.SetWrap(true)
|
||||
if err1 != nil {
|
||||
content = content + err1.Error()
|
||||
}
|
||||
if err2 != nil {
|
||||
content = content + err2.Error()
|
||||
}
|
||||
} else {
|
||||
widget.View.SetWrap(false)
|
||||
content = widget.contentFrom(onCalls, incidents)
|
||||
}
|
||||
|
||||
widget.View.SetText(content)
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) contentFrom(onCalls []pagerduty.OnCall, incidents []pagerduty.Incident) string {
|
||||
var str string
|
||||
|
||||
if len(incidents) > 0 {
|
||||
str = str + "[yellow]Incidents[white]\n"
|
||||
for _, incident := range incidents {
|
||||
str = str + fmt.Sprintf("[red]%s[white]\n", incident.Summary)
|
||||
str = str + fmt.Sprintf("Status: %s\n", incident.Status)
|
||||
str = str + fmt.Sprintf("Service: %s\n", incident.Service.Summary)
|
||||
str = str + fmt.Sprintf("Escalation: %s\n", incident.EscalationPolicy.Summary)
|
||||
}
|
||||
}
|
||||
|
||||
tree := make(map[string][]pagerduty.OnCall)
|
||||
|
||||
filtering := wtf.Config.UList("wtf.mods.pagerduty.escalationFilter")
|
||||
filter := make(map[string]bool)
|
||||
for _, item := range filtering {
|
||||
filter[item.(string)] = true
|
||||
}
|
||||
|
||||
for _, onCall := range onCalls {
|
||||
key := onCall.EscalationPolicy.Summary
|
||||
if len(filtering) == 0 || filter[key] {
|
||||
tree[key] = append(tree[key], onCall)
|
||||
}
|
||||
}
|
||||
|
||||
// We want to sort our escalation policies for predictability/ease of finding
|
||||
keys := make([]string, 0, len(tree))
|
||||
for k := range tree {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
|
||||
sort.Strings(keys)
|
||||
|
||||
if len(keys) > 0 {
|
||||
str = str + "[yellow]Schedules[white]\n"
|
||||
// Print out policies, and escalation order of users
|
||||
for _, key := range keys {
|
||||
str = str + fmt.Sprintf("[red]%s\n", key)
|
||||
values := tree[key]
|
||||
sort.Sort(ByEscalationLevel(values))
|
||||
for _, item := range values {
|
||||
str = str + fmt.Sprintf("[white]%d - %s\n", item.EscalationLevel, item.User.Summary)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return str
|
||||
}
|
||||
Reference in New Issue
Block a user