1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Fix merge conflict

Signed-off-by: Chris Cummer <chriscummer@me.com>
This commit is contained in:
Chris Cummer 2020-07-27 17:17:17 -07:00
commit a0e34507db
4 changed files with 69 additions and 22 deletions

View File

@ -102,6 +102,7 @@ install:
## lint: runs a number of code quality checks against the source code ## lint: runs a number of code quality checks against the source code
lint: lint:
golangci-lint cache clean
golangci-lint run golangci-lint run
# lint: # lint:

View File

@ -6,6 +6,10 @@ import (
"github.com/PagerDuty/go-pagerduty" "github.com/PagerDuty/go-pagerduty"
) )
const (
queryTimeFmt = "2006-01-02T15:04:05Z07:00"
)
// GetOnCalls returns a list of people currently on call // GetOnCalls returns a list of people currently on call
func GetOnCalls(apiKey string, scheduleIDs []string) ([]pagerduty.OnCall, error) { func GetOnCalls(apiKey string, scheduleIDs []string) ([]pagerduty.OnCall, error) {
client := pagerduty.NewClient(apiKey) client := pagerduty.NewClient(apiKey)
@ -14,10 +18,8 @@ func GetOnCalls(apiKey string, scheduleIDs []string) ([]pagerduty.OnCall, error)
var queryOpts pagerduty.ListOnCallOptions var queryOpts pagerduty.ListOnCallOptions
queryOpts.ScheduleIDs = scheduleIDs queryOpts.ScheduleIDs = scheduleIDs
queryOpts.Since = time.Now().Format(queryTimeFmt)
timeFmt := "2006-01-02T15:04:05Z07:00" queryOpts.Until = time.Now().Format(queryTimeFmt)
queryOpts.Since = time.Now().Format(timeFmt)
queryOpts.Until = time.Now().Format(timeFmt)
oncalls, err := client.ListOnCalls(queryOpts) oncalls, err := client.ListOnCalls(queryOpts)
if err != nil { if err != nil {

View File

@ -21,6 +21,7 @@ type Settings struct {
myName string `help:"The name to highlight when on-call in PagerDuty."` myName string `help:"The name to highlight when on-call in PagerDuty."`
scheduleIDs []interface{} `help:"An array of schedule IDs you want to restrict the OnCalls query to."` scheduleIDs []interface{} `help:"An array of schedule IDs you want to restrict the OnCalls query to."`
showIncidents bool `help:"Whether or not to list incidents." optional:"true"` showIncidents bool `help:"Whether or not to list incidents." optional:"true"`
showOnCallEnd bool `help:"Whether or not to display the date the oncall schedule ends." optional:"true"`
showSchedules bool `help:"Whether or not to show schedules." optional:"true"` showSchedules bool `help:"Whether or not to show schedules." optional:"true"`
teamIDs []interface{} `help:"An array of team IDs to restrict the incidents query to" optional:"true"` teamIDs []interface{} `help:"An array of team IDs to restrict the incidents query to" optional:"true"`
userIDs []interface{} `help:"An array of user IDs to restrict the incidents query to" optional:"true"` userIDs []interface{} `help:"An array of user IDs to restrict the incidents query to" optional:"true"`
@ -36,6 +37,7 @@ func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *co
myName: ymlConfig.UString("myName"), myName: ymlConfig.UString("myName"),
scheduleIDs: ymlConfig.UList("scheduleIDs", []interface{}{}), scheduleIDs: ymlConfig.UList("scheduleIDs", []interface{}{}),
showIncidents: ymlConfig.UBool("showIncidents", true), showIncidents: ymlConfig.UBool("showIncidents", true),
showOnCallEnd: ymlConfig.UBool("showOnCallEnd", false),
showSchedules: ymlConfig.UBool("showSchedules", true), showSchedules: ymlConfig.UBool("showSchedules", true),
teamIDs: ymlConfig.UList("teamIDs", []interface{}{}), teamIDs: ymlConfig.UList("teamIDs", []interface{}{}),
userIDs: ymlConfig.UList("userIDs", []interface{}{}), userIDs: ymlConfig.UList("userIDs", []interface{}{}),

View File

@ -3,6 +3,7 @@ package pagerduty
import ( import (
"fmt" "fmt"
"sort" "sort"
"time"
"github.com/PagerDuty/go-pagerduty" "github.com/PagerDuty/go-pagerduty"
"github.com/rivo/tview" "github.com/rivo/tview"
@ -10,12 +11,18 @@ import (
"github.com/wtfutil/wtf/view" "github.com/wtfutil/wtf/view"
) )
const (
onCallTimeAPILayout = "2006-01-02T15:04:05Z"
onCallTimeDisplayLayout = "Jan 2, 2006"
)
type Widget struct { type Widget struct {
view.TextWidget view.TextWidget
settings *Settings settings *Settings
} }
// NewWidget creates and returns an instance of PagerDuty widget
func NewWidget(app *tview.Application, settings *Settings) *Widget { func NewWidget(app *tview.Application, settings *Settings) *Widget {
widget := Widget{ widget := Widget{
TextWidget: view.NewTextWidget(app, settings.common), TextWidget: view.NewTextWidget(app, settings.common),
@ -69,63 +76,79 @@ func (widget *Widget) Refresh() {
func (widget *Widget) contentFrom(onCalls []pagerduty.OnCall, incidents []pagerduty.Incident) string { func (widget *Widget) contentFrom(onCalls []pagerduty.OnCall, incidents []pagerduty.Incident) string {
var str string var str string
// Incidents
if widget.settings.showIncidents { if widget.settings.showIncidents {
str += "[yellow] Incidents[white]" str += fmt.Sprintf("[%s] Incidents[white]\n", widget.settings.common.Colors.Subheading)
if len(incidents) > 0 { if len(incidents) > 0 {
for _, incident := range incidents { for _, incident := range incidents {
str += fmt.Sprintf("\n [%s]%s[white]\n", widget.settings.common.Colors.Subheading, incident.Summary) str += fmt.Sprintf("\n [%s]%s[white]\n", widget.settings.common.Colors.Label, tview.Escape(incident.Summary))
str += fmt.Sprintf(" Status: %s\n", incident.Status) str += fmt.Sprintf(" Status: %s\n", incident.Status)
str += fmt.Sprintf(" Service: %s\n", incident.Service.Summary) str += fmt.Sprintf(" Service: %s\n", incident.Service.Summary)
str += fmt.Sprintf(" Escalation: %s\n", incident.EscalationPolicy.Summary) str += fmt.Sprintf(" Escalation: %s\n", incident.EscalationPolicy.Summary)
str += fmt.Sprintf(" Link: %s\n", incident.HTMLURL)
} }
} else { } else {
str += "\n No open incidents\n" str += "\n No open incidents\n"
} }
str += "\n"
} }
tree := make(map[string][]pagerduty.OnCall) onCallTree := make(map[string][]pagerduty.OnCall)
filter := make(map[string]bool) filter := make(map[string]bool)
for _, item := range widget.settings.escalationFilter { for _, item := range widget.settings.escalationFilter {
filter[item.(string)] = true filter[item.(string)] = true
} }
// OnCalls
for _, onCall := range onCalls { for _, onCall := range onCalls {
key := onCall.EscalationPolicy.Summary summary := onCall.EscalationPolicy.Summary
if len(widget.settings.escalationFilter) == 0 || filter[key] { if len(widget.settings.escalationFilter) == 0 || filter[summary] {
tree[key] = append(tree[key], onCall) onCallTree[summary] = append(onCallTree[summary], onCall)
} }
} }
// We want to sort our escalation policies for predictability/ease of finding // We want to sort our escalation policies for predictability/ease of finding
keys := make([]string, 0, len(tree)) keys := make([]string, 0, len(onCallTree))
for k := range tree { for k := range onCallTree {
keys = append(keys, k) keys = append(keys, k)
} }
sort.Strings(keys) sort.Strings(keys)
if len(keys) > 0 { if len(keys) > 0 {
str += fmt.Sprintf("\n[%s] Schedules[white]\n", widget.settings.common.Colors.Subheading) str += fmt.Sprintf("[%s] Schedules[white]\n", widget.settings.common.Colors.Subheading)
// Print out policies, and escalation order of users // Print out policies, and escalation order of users
for _, key := range keys { for _, key := range keys {
str += fmt.Sprintf( str += fmt.Sprintf(
"\n [%s]%s\n", "\n [%s]%s\n",
widget.settings.common.Colors.Subheading, widget.settings.common.Colors.Label,
key, key,
) )
values := tree[key] values := onCallTree[key]
sort.Sort(ByEscalationLevel(values)) sort.Sort(ByEscalationLevel(values))
for _, item := range values { for _, onCall := range values {
str += fmt.Sprintf( str += fmt.Sprintf(
" [%s]%d - %s\n", " [%s]%d - %s\n",
widget.settings.common.Colors.Text, widget.settings.common.Colors.Text,
item.EscalationLevel, onCall.EscalationLevel,
widget.userSummary(item), widget.userSummary(onCall),
) )
onCallEnd := widget.onCallEndSummary(onCall)
if onCallEnd != "" {
str += fmt.Sprintf(
" %s\n",
onCallEnd,
)
}
} }
} }
} }
@ -133,8 +156,27 @@ func (widget *Widget) contentFrom(onCalls []pagerduty.OnCall, incidents []pagerd
return str return str
} }
func (widget *Widget) userSummary(item pagerduty.OnCall) string { // onCallEndSummary may or may not return the date that the specified onCall schedule ends
summary := item.User.Summary func (widget *Widget) onCallEndSummary(onCall pagerduty.OnCall) string {
if !widget.settings.showOnCallEnd {
return ""
}
if onCall.End == "" {
return ""
}
end, err := time.Parse(onCallTimeAPILayout, onCall.End)
if err != nil {
return ""
}
return end.Format(onCallTimeDisplayLayout)
}
// userSummary returns the name of the person assigned to the specified onCall schedule
func (widget *Widget) userSummary(onCall pagerduty.OnCall) string {
summary := onCall.User.Summary
if summary == widget.settings.myName { if summary == widget.settings.myName {
summary = fmt.Sprintf("[::b]%s", summary) summary = fmt.Sprintf("[::b]%s", summary)