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
import (
"os"
"github.com/wtfutil/wtf/wtf"
nr "github.com/yfronto/newrelic"
)
func Application() (*nr.Application, error) {
client := nr.NewClient(apiKey())
type Client struct {
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 {
return nil, err
}
@ -18,21 +27,13 @@ func Application() (*nr.Application, error) {
return application, nil
}
func Deployments() ([]nr.ApplicationDeployment, error) {
client := nr.NewClient(apiKey())
func (client *Client) Deployments() ([]nr.ApplicationDeployment, error) {
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 {
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

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