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

Merge pull request #394 from Seanstoppable/newrelicclient

Decouple newrelic client and widget
This commit is contained in:
Chris Cummer 2019-02-28 13:32:44 -08:00 committed by GitHub
commit ec6af9ddf8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 18 deletions

View File

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

View File

@ -2,6 +2,7 @@ package newrelic
import ( import (
"fmt" "fmt"
"os"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/wtf"
@ -10,11 +11,13 @@ import (
type Widget struct { type Widget struct {
wtf.TextWidget wtf.TextWidget
client *Client
} }
func NewWidget(app *tview.Application) *Widget { func NewWidget(app *tview.Application) *Widget {
widget := Widget{ widget := Widget{
TextWidget: wtf.NewTextWidget(app, "New Relic", "newrelic", false), TextWidget: wtf.NewTextWidget(app, "New Relic", "newrelic", false),
client: NewClient(apiKey(), wtf.Config.UInt("wtf.mods.newrelic.applicationId")),
} }
return &widget return &widget
@ -23,8 +26,8 @@ func NewWidget(app *tview.Application) *Widget {
/* -------------------- Exported Functions -------------------- */ /* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() { func (widget *Widget) Refresh() {
app, appErr := Application() app, appErr := widget.client.Application()
deploys, depErr := Deployments() deploys, depErr := widget.client.Deployments()
appName := "error" appName := "error"
if appErr == nil { if appErr == nil {
@ -86,3 +89,10 @@ func (widget *Widget) contentFrom(deploys []nr.ApplicationDeployment) string {
return str return str
} }
func apiKey() string {
return wtf.Config.UString(
"wtf.mods.newrelic.apiKey",
os.Getenv("WTF_NEW_RELIC_API_KEY"),
)
}