Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(newrelic): Display deploy information from multiple apps #472

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,11 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.4/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lucasb-eyer/go-colorful v1.0.2 h1:mCMFu6PgSozg9tDNMMK3g18oJBX7oYGrC09mS6CXfO4=
github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s=
Expand Down
2 changes: 1 addition & 1 deletion maker/widget_maker.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func MakeWidget(
widget = nbascore.NewWidget(app, pages, settings)
case "newrelic":
settings := newrelic.NewSettingsFromYAML(widgetName, moduleConfig, globalConfig)
widget = newrelic.NewWidget(app, settings)
widget = newrelic.NewWidget(app, pages, settings)
case "opsgenie":
settings := opsgenie.NewSettingsFromYAML(widgetName, moduleConfig, globalConfig)
widget = opsgenie.NewWidget(app, settings)
Expand Down
74 changes: 74 additions & 0 deletions modules/newrelic/display.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package newrelic

import (
"fmt"

"github.com/wtfutil/wtf/wtf"
nr "github.com/yfronto/newrelic"
)

func (widget *Widget) display() {
client := widget.currentData()
if client == nil {
widget.Redraw(widget.CommonSettings.Title, " NewRelic data unavailable ", false)
return
}
app, appErr := client.Application()
deploys, depErr := client.Deployments()

appName := "error"
if appErr == nil {
appName = app.Name
}

var content string
title := fmt.Sprintf("%s - [green]%s[white]", widget.CommonSettings.Title, appName)
wrap := false
if depErr != nil {
wrap = true
content = depErr.Error()
} else {
content = widget.contentFrom(deploys)
}

widget.Redraw(title, content, wrap)
}

func (widget *Widget) contentFrom(deploys []nr.ApplicationDeployment) string {
str := fmt.Sprintf(
" %s\n",
"[red]Latest Deploys[white]",
)

revisions := []string{}

for _, deploy := range deploys {
if (deploy.Revision != "") && wtf.Exclude(revisions, deploy.Revision) {
lineColor := "white"
if wtf.IsToday(deploy.Timestamp) {
lineColor = "lightblue"
}

revLen := 8
if revLen > len(deploy.Revision) {
revLen = len(deploy.Revision)
}

str += fmt.Sprintf(
" [green]%s[%s] %s %-.16s[white]\n",
deploy.Revision[0:revLen],
lineColor,
deploy.Timestamp.Format("Jan 02 15:04 MST"),
wtf.NameFromEmail(deploy.User),
)

revisions = append(revisions, deploy.Revision)

if len(revisions) == widget.settings.deployCount {
break
}
}
}

return str
}
9 changes: 9 additions & 0 deletions modules/newrelic/keyboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package newrelic

import "github.com/gdamore/tcell"

func (widget *Widget) initializeKeyboardControls() {
widget.SetKeyboardChar("/", widget.ShowHelp, "Show/hide this help window")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you take a look at the JIRA module and add appropriate keyboard commands? r/j/k would be appreciated for consistency.

widget.SetKeyboardKey(tcell.KeyLeft, widget.PrevSource, "Select previous application")
widget.SetKeyboardKey(tcell.KeyRight, widget.NextSource, "Select next application")
}
12 changes: 6 additions & 6 deletions modules/newrelic/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ const defaultTitle = "NewRelic"
type Settings struct {
common *cfg.Common

apiKey string
applicationID int
deployCount int
apiKey string
deployCount int
applicationIDs []interface{}
}

func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *config.Config) *Settings {

settings := Settings{
common: cfg.NewCommonSettingsFromModule(name, defaultTitle, ymlConfig, globalConfig),

apiKey: ymlConfig.UString("apiKey", os.Getenv("WTF_NEW_RELIC_API_KEY")),
applicationID: ymlConfig.UInt("applicationID"),
deployCount: ymlConfig.UInt("deployCount", 5),
apiKey: ymlConfig.UString("apiKey", os.Getenv("WTF_NEW_RELIC_API_KEY")),
deployCount: ymlConfig.UInt("deployCount", 5),
applicationIDs: ymlConfig.UList("applicationIDs"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could keep applicationID for a non-breaking change. Either by appending it to applicationIDs, or doing something like the arrayifyProjects in the jira settings file

}

return &settings
Expand Down
90 changes: 33 additions & 57 deletions modules/newrelic/widget.go
Original file line number Diff line number Diff line change
@@ -1,93 +1,69 @@
package newrelic

import (
"fmt"
"sort"

"github.com/rivo/tview"
"github.com/wtfutil/wtf/wtf"
nr "github.com/yfronto/newrelic"
)

type Widget struct {
wtf.KeyboardWidget
wtf.MultiSourceWidget
wtf.TextWidget

client *Client
Clients []*Client

settings *Settings
}

func NewWidget(app *tview.Application, settings *Settings) *Widget {
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(app, settings.common, false),
KeyboardWidget: wtf.NewKeyboardWidget(app, pages, settings.common),
MultiSourceWidget: wtf.NewMultiSourceWidget(settings.common, "applicationID", "applicationIDs"),
TextWidget: wtf.NewTextWidget(app, settings.common, true),

settings: settings,
}

widget.client = NewClient(widget.settings.apiKey, widget.settings.applicationID)
widget.initializeKeyboardControls()
widget.View.SetInputCapture(widget.InputCapture)

widget.SetDisplayFunction(widget.display)

widget.KeyboardWidget.SetView(widget.View)

return &widget
}

/* -------------------- Exported Functions -------------------- */

func (widget *Widget) Refresh() {
app, appErr := widget.client.Application()
deploys, depErr := widget.client.Deployments()

appName := "error"
if appErr == nil {
appName = app.Name
for _, id := range wtf.ToInts(widget.settings.applicationIDs) {
widget.Clients = append(widget.Clients, NewClient(widget.settings.apiKey, id))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will create a new client that is calling the service each time, and this is probably not quite what you want. Say you had 3 applications, on the first refresh there would be 3, second would be 6, third would be 9..... eventually this will eat up a lot of resources.
It might make more sense to have the client accept applicationId as a parameter, and use that to iterate through requests and store the results in some struct, effectively caching the results. Otherwise, every time you change a page you will ALSO be making a web request to the newrelic API to get data, resulting in slower transitions. Minimally, I would create one set of clients up front, and Refresh will just call the display method

}

var content string
title := fmt.Sprintf("%s - [green]%s[white]", widget.CommonSettings.Title, appName)
wrap := false
if depErr != nil {
wrap = true
content = depErr.Error()
} else {
content = widget.contentFrom(deploys)
}
sort.Slice(widget.Clients, func(i, j int) bool {
return widget.Clients[i].applicationId < widget.Clients[j].applicationId
})

widget.Redraw(title, content, wrap)
widget.display()
}

func (widget *Widget) HelpText() string {
return widget.KeyboardWidget.HelpText()
}

/* -------------------- Unexported Functions -------------------- */

func (widget *Widget) contentFrom(deploys []nr.ApplicationDeployment) string {
str := fmt.Sprintf(
" %s\n",
"[red]Latest Deploys[white]",
)

revisions := []string{}

for _, deploy := range deploys {
if (deploy.Revision != "") && wtf.Exclude(revisions, deploy.Revision) {
lineColor := "white"
if wtf.IsToday(deploy.Timestamp) {
lineColor = "lightblue"
}

revLen := 8
if revLen > len(deploy.Revision) {
revLen = len(deploy.Revision)
}

str += fmt.Sprintf(
" [green]%s[%s] %s %-.16s[white]\n",
deploy.Revision[0:revLen],
lineColor,
deploy.Timestamp.Format("Jan 02 15:04 MST"),
wtf.NameFromEmail(deploy.User),
)

revisions = append(revisions, deploy.Revision)

if len(revisions) == widget.settings.deployCount {
break
}
}
func (widget *Widget) currentData() *Client {
if len(widget.Clients) == 0 {
return nil
}

if widget.Idx < 0 || widget.Idx >= len(widget.Clients) {
return nil
}

return str
return widget.Clients[widget.Idx]
}