1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/modules/newrelic/widget.go
2019-08-05 10:50:12 -07:00

96 lines
1.9 KiB
Go

package newrelic
import (
"fmt"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view"
"github.com/wtfutil/wtf/wtf"
nr "github.com/yfronto/newrelic"
)
type Widget struct {
view.TextWidget
client *Client
settings *Settings
}
func NewWidget(app *tview.Application, settings *Settings) *Widget {
widget := Widget{
TextWidget: view.NewTextWidget(app, settings.common, false),
settings: settings,
}
widget.client = NewClient(widget.settings.apiKey, widget.settings.applicationID)
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
}
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)
}
/* -------------------- 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 != "") && utils.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"),
utils.NameFromEmail(deploy.User),
)
revisions = append(revisions, deploy.Revision)
if len(revisions) == widget.settings.deployCount {
break
}
}
}
return str
}