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

Migrate all modules to their own subfolder

Handles #375
This commit is contained in:
Sean Smith
2019-02-18 11:16:34 -05:00
parent c28c31aedb
commit 8030380f89
123 changed files with 51 additions and 51 deletions

View File

@@ -0,0 +1,38 @@
package newrelic
import (
"os"
"github.com/wtfutil/wtf/wtf"
nr "github.com/yfronto/newrelic"
)
func Application() (*nr.Application, error) {
client := nr.NewClient(apiKey())
application, err := client.GetApplication(wtf.Config.UInt("wtf.mods.newrelic.applicationId"))
if err != nil {
return nil, err
}
return application, nil
}
func Deployments() ([]nr.ApplicationDeployment, error) {
client := nr.NewClient(apiKey())
opts := &nr.ApplicationDeploymentOptions{Page: 1}
deployments, err := client.GetApplicationDeployments(wtf.Config.UInt("wtf.mods.newrelic.applicationId"), opts)
if err != nil {
return nil, err
}
return deployments, nil
}
func apiKey() string {
return wtf.Config.UString(
"wtf.mods.newrelic.apiKey",
os.Getenv("WTF_NEW_RELIC_API_KEY"),
)
}

View File

@@ -0,0 +1,88 @@
package newrelic
import (
"fmt"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/wtf"
nr "github.com/yfronto/newrelic"
)
type Widget struct {
wtf.TextWidget
}
func NewWidget(app *tview.Application) *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(app, "New Relic", "newrelic", false),
}
return &widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
app, appErr := Application()
deploys, depErr := Deployments()
appName := "error"
if appErr == nil {
appName = app.Name
}
widget.View.SetTitle(widget.ContextualTitle(fmt.Sprintf("%s - [green]%s[white]", widget.Name, appName)))
widget.View.Clear()
var content string
if depErr != nil {
widget.View.SetWrap(true)
content = depErr.Error()
} else {
widget.View.SetWrap(false)
content = widget.contentFrom(deploys)
}
widget.View.SetText(content)
}
/* -------------------- 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 = 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) == wtf.Config.UInt("wtf.mods.newrelic.deployCount", 5) {
break
}
}
}
return str
}