mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Merge branch 'feat/nr-multi' of github.com:Seanstoppable/wtf into Seanstoppable-feat/nr-multi
This commit is contained in:
commit
7dd4e4909b
@ -167,7 +167,7 @@ func MakeWidget(
|
||||
widget = nbascore.NewWidget(app, pages, settings)
|
||||
case "newrelic":
|
||||
settings := newrelic.NewSettingsFromYAML(moduleName, moduleConfig, config)
|
||||
widget = newrelic.NewWidget(app, settings)
|
||||
widget = newrelic.NewWidget(app, pages, settings)
|
||||
case "opsgenie":
|
||||
settings := opsgenie.NewSettingsFromYAML(moduleName, moduleConfig, config)
|
||||
widget = opsgenie.NewWidget(app, settings)
|
||||
|
74
modules/newrelic/display.go
Normal file
74
modules/newrelic/display.go
Normal file
@ -0,0 +1,74 @@
|
||||
package newrelic
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/wtfutil/wtf/utils"
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
nr "github.com/yfronto/newrelic"
|
||||
)
|
||||
|
||||
func (widget *Widget) content() (string, string, bool) {
|
||||
client := widget.currentData()
|
||||
if client == nil {
|
||||
return widget.CommonSettings().Title, " NewRelic data unavailable ", false
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
return 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 != "") && utils.DoesNotInclude(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"),
|
||||
utils.NameFromEmail(deploy.User),
|
||||
)
|
||||
|
||||
revisions = append(revisions, deploy.Revision)
|
||||
|
||||
if len(revisions) == widget.settings.deployCount {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return str
|
||||
}
|
9
modules/newrelic/keyboard.go
Normal file
9
modules/newrelic/keyboard.go
Normal file
@ -0,0 +1,9 @@
|
||||
package newrelic
|
||||
|
||||
import "github.com/gdamore/tcell"
|
||||
|
||||
func (widget *Widget) initializeKeyboardControls() {
|
||||
widget.SetKeyboardChar("/", widget.ShowHelp, "Show/hide this help window")
|
||||
widget.SetKeyboardKey(tcell.KeyLeft, widget.PrevSource, "Select previous application")
|
||||
widget.SetKeyboardKey(tcell.KeyRight, widget.NextSource, "Select next application")
|
||||
}
|
@ -13,8 +13,8 @@ type Settings struct {
|
||||
common *cfg.Common
|
||||
|
||||
apiKey string `help:"Your New Relic API token."`
|
||||
applicationID int `help:"The integer ID of the New Relic application you wish to report on."`
|
||||
deployCount int `help:"The number of past deploys to display on screen." optional:"true"`
|
||||
applicationIDs []interface{} `help:"The integer ID of the New Relic application you wish to report on."`
|
||||
}
|
||||
|
||||
func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *config.Config) *Settings {
|
||||
@ -22,9 +22,9 @@ func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *co
|
||||
settings := Settings{
|
||||
common: cfg.NewCommonSettingsFromModule(name, defaultTitle, ymlConfig, globalConfig),
|
||||
|
||||
apiKey: ymlConfig.UString("apiKey", ymlConfig.UString("apikey", os.Getenv("WTF_NEW_RELIC_API_KEY"))),
|
||||
applicationID: ymlConfig.UInt("applicationID"),
|
||||
apiKey: ymlConfig.UString("apiKey", os.Getenv("WTF_NEW_RELIC_API_KEY")),
|
||||
deployCount: ymlConfig.UInt("deployCount", 5),
|
||||
applicationIDs: ymlConfig.UList("applicationIDs"),
|
||||
}
|
||||
|
||||
return &settings
|
||||
|
@ -1,29 +1,46 @@
|
||||
package newrelic
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"github.com/rivo/tview"
|
||||
"github.com/wtfutil/wtf/utils"
|
||||
"github.com/wtfutil/wtf/view"
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
)
|
||||
|
||||
type Widget struct {
|
||||
view.KeyboardWidget
|
||||
view.MultiSourceWidget
|
||||
view.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: view.NewTextWidget(app, settings.common, false),
|
||||
KeyboardWidget: view.NewKeyboardWidget(app, pages, settings.common),
|
||||
MultiSourceWidget: view.NewMultiSourceWidget(settings.common, "applicationID", "applicationIDs"),
|
||||
TextWidget: view.NewTextWidget(app, settings.common, true),
|
||||
|
||||
settings: settings,
|
||||
}
|
||||
|
||||
widget.client = NewClient(widget.settings.apiKey, widget.settings.applicationID)
|
||||
widget.initializeKeyboardControls()
|
||||
widget.View.SetInputCapture(widget.InputCapture)
|
||||
|
||||
for _, id := range utils.ToInts(widget.settings.applicationIDs) {
|
||||
widget.Clients = append(widget.Clients, NewClient(widget.settings.apiKey, id))
|
||||
}
|
||||
|
||||
sort.Slice(widget.Clients, func(i, j int) bool {
|
||||
return widget.Clients[i].applicationId < widget.Clients[j].applicationId
|
||||
})
|
||||
|
||||
widget.SetDisplayFunction(widget.Refresh)
|
||||
|
||||
widget.KeyboardWidget.SetView(widget.View)
|
||||
|
||||
return &widget
|
||||
}
|
||||
@ -36,57 +53,20 @@ func (widget *Widget) Refresh() {
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) content() (string, string, bool) {
|
||||
app, appErr := widget.client.Application()
|
||||
deploys, depErr := widget.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 += fmt.Sprintf(
|
||||
" %s\n",
|
||||
"[red]Latest Deploys[white]",
|
||||
)
|
||||
|
||||
revisions := []string{}
|
||||
|
||||
for _, deploy := range deploys {
|
||||
if (deploy.Revision != "") && utils.DoesNotInclude(revisions, deploy.Revision) {
|
||||
lineColor := "white"
|
||||
if wtf.IsToday(deploy.Timestamp) {
|
||||
lineColor = "lightblue"
|
||||
}
|
||||
|
||||
revLen := 8
|
||||
if revLen > len(deploy.Revision) {
|
||||
revLen = len(deploy.Revision)
|
||||
}
|
||||
|
||||
content += fmt.Sprintf(
|
||||
" [green]%s[%s] %s %-.16s[white]\n",
|
||||
deploy.Revision[0:revLen],
|
||||
lineColor,
|
||||
deploy.Timestamp.Format("Jan 02 15:04 MST"),
|
||||
utils.NameFromEmail(deploy.User),
|
||||
)
|
||||
|
||||
revisions = append(revisions, deploy.Revision)
|
||||
|
||||
if len(revisions) == widget.settings.deployCount {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return title, content, wrap
|
||||
func (widget *Widget) HelpText() string {
|
||||
return widget.KeyboardWidget.HelpText()
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) currentData() *Client {
|
||||
if len(widget.Clients) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if widget.Idx < 0 || widget.Idx >= len(widget.Clients) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return widget.Clients[widget.Idx]
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user