diff --git a/config.yml b/config.yml index e0cfd9f3..a0a5133a 100644 --- a/config.yml +++ b/config.yml @@ -19,6 +19,10 @@ wtf: username: "senorprogrammer" jira: refreshInterval: 900 + newrelic: + applicationId: 10549735 + deployCount: 5 + refreshInterval: 900 opsgenie: refreshInterval: 21600 security: diff --git a/newrelic/client.go b/newrelic/client.go new file mode 100644 index 00000000..b3d27c44 --- /dev/null +++ b/newrelic/client.go @@ -0,0 +1,30 @@ +package newrelic + +import ( + "os" + + nr "github.com/yfronto/newrelic" +) + +func Application() (*nr.Application, error) { + client := nr.NewClient(os.Getenv("WTF_NEW_RELIC_API_KEY")) + + application, err := client.GetApplication(Config.UInt("wtf.newrelic.applicationId")) + if err != nil { + return nil, err + } + + return application, nil +} + +func Deployments() ([]nr.ApplicationDeployment, error) { + client := nr.NewClient(os.Getenv("WTF_NEW_RELIC_API_KEY")) + + opts := &nr.ApplicationDeploymentOptions{Page: 1} + deployments, err := client.GetApplicationDeployments(Config.UInt("wtf.newrelic.applicationId"), opts) + if err != nil { + return nil, err + } + + return deployments, nil +} diff --git a/newrelic/widget.go b/newrelic/widget.go new file mode 100644 index 00000000..8f021da7 --- /dev/null +++ b/newrelic/widget.go @@ -0,0 +1,87 @@ +package newrelic + +import ( + "fmt" + "time" + + "github.com/gdamore/tcell" + "github.com/olebedev/config" + "github.com/rivo/tview" + "github.com/senorprogrammer/wtf/wtf" + nr "github.com/yfronto/newrelic" +) + +var Config *config.Config + +type Widget struct { + wtf.BaseWidget + + View *tview.TextView +} + +func NewWidget() *Widget { + widget := Widget{ + BaseWidget: wtf.BaseWidget{ + Name: "New Relic", + RefreshedAt: time.Now(), + RefreshInt: Config.UInt("wtf.newrelic.refreshInterval", 900), + }, + } + + widget.addView() + + return &widget +} + +/* -------------------- Exported Functions -------------------- */ + +func (widget *Widget) Refresh() { + app, _ := Application() + deploys, _ := Deployments() + + widget.View.SetTitle(fmt.Sprintf(" New Relic: [green]%s[white] ", app.Name)) + widget.RefreshedAt = time.Now() + + widget.View.Clear() + fmt.Fprintf(widget.View, "%s", widget.contentFrom(deploys)) +} + +/* -------------------- Unexported Functions -------------------- */ + +func (widget *Widget) addView() { + view := tview.NewTextView() + + view.SetBorder(true) + view.SetBorderColor(tcell.ColorGray) + view.SetDynamicColors(true) + view.SetTitle(widget.Name) + view.SetWrap(false) + + widget.View = view +} + +func (widget *Widget) contentFrom(deploys []nr.ApplicationDeployment) string { + str := "\n" + str = str + " [red]Latest Deploys[white]\n" + + revisions := []string{} + + for _, deploy := range deploys { + if (deploy.Revision != "") && wtf.Exclude(revisions, deploy.Revision) { + str = str + fmt.Sprintf( + " [green]%4s[white] %s %s\n", + deploy.Revision[len(deploy.Revision)-4:], + deploy.Timestamp.Format("Jan 2 15:04"), + wtf.NameFromEmail(deploy.User), + ) + + revisions = append(revisions, deploy.Revision) + + if len(revisions) == Config.UInt("wtf.newrelic.deployCount", 5) { + break + } + } + } + + return str +} diff --git a/opsgenie/widget.go b/opsgenie/widget.go index e7c7a44d..6083b8bb 100644 --- a/opsgenie/widget.go +++ b/opsgenie/widget.go @@ -67,7 +67,7 @@ func (widget *Widget) contentFrom(onCallResponse *OnCallResponse) string { if len(data.Recipients) == 0 { str = str + " [gray]no one[white]\n" } else { - str = str + fmt.Sprintf(" %s\n", strings.Join(widget.namesFromEmails(data.Recipients), ", ")) + str = str + fmt.Sprintf(" %s\n", strings.Join(wtf.NamesFromEmails(data.Recipients), ", ")) } str = str + "\n" @@ -79,14 +79,3 @@ func (widget *Widget) contentFrom(onCallResponse *OnCallResponse) string { func (widget *Widget) cleanScheduleName(schedule string) string { return strings.Replace(schedule, "_", " ", -1) } - -func (widget *Widget) namesFromEmails(emails []string) []string { - names := []string{} - - for _, email := range emails { - parts := strings.Split(email, "@") - names = append(names, strings.Title(strings.Replace(parts[0], ".", " ", -1))) - } - - return names -} diff --git a/wtf.go b/wtf.go index a715c1d5..22d2a8c6 100644 --- a/wtf.go +++ b/wtf.go @@ -9,6 +9,7 @@ import ( "github.com/senorprogrammer/wtf/git" "github.com/senorprogrammer/wtf/github" "github.com/senorprogrammer/wtf/jira" + "github.com/senorprogrammer/wtf/newrelic" "github.com/senorprogrammer/wtf/opsgenie" "github.com/senorprogrammer/wtf/security" "github.com/senorprogrammer/wtf/status" @@ -63,6 +64,11 @@ func main() { jira.Refresh() go wtf.Schedule(jira) + newrelic.Config = Config + newrelic := newrelic.NewWidget() + newrelic.Refresh() + go wtf.Schedule(newrelic) + opsgenie.Config = Config opsgenie := opsgenie.NewWidget() opsgenie.Refresh() @@ -91,6 +97,7 @@ func main() { grid.AddItem(cal.View, 2, 1, 4, 1, 0, 0, false) grid.AddItem(git.View, 0, 2, 2, 3, 0, 0, false) grid.AddItem(github.View, 2, 2, 2, 3, 0, 0, false) + grid.AddItem(newrelic.View, 4, 2, 1, 3, 0, 0, false) grid.AddItem(weather.View, 0, 1, 1, 1, 0, 0, false) grid.AddItem(sec.View, 5, 0, 1, 1, 0, 0, false) grid.AddItem(opsgenie.View, 2, 0, 2, 1, 0, 0, false) diff --git a/wtf/utils.go b/wtf/utils.go index da098444..7d185504 100644 --- a/wtf/utils.go +++ b/wtf/utils.go @@ -4,6 +4,7 @@ import ( "fmt" "io/ioutil" "os/exec" + "strings" "time" ) @@ -34,6 +35,30 @@ func ExecuteCommand(cmd *exec.Cmd) string { return str } +func Exclude(strs []string, val string) bool { + for _, str := range strs { + if val == str { + return false + } + } + return true +} + +func NameFromEmail(email string) string { + parts := strings.Split(email, "@") + return strings.Title(strings.Replace(parts[0], ".", " ", -1)) +} + +func NamesFromEmails(emails []string) []string { + names := []string{} + + for _, email := range emails { + names = append(names, NameFromEmail(email)) + } + + return names +} + func PrettyDate(dateStr string) string { newTime, _ := time.Parse(DateFormat, dateStr) return fmt.Sprint(newTime.Format("Jan 2, 2006"))