github.com/wtfutil/wtf@v0.43.0/modules/newrelic/display.go (about)

     1  package newrelic
     2  
     3  import (
     4  	"fmt"
     5  
     6  	nr "github.com/wtfutil/wtf/modules/newrelic/client"
     7  	"github.com/wtfutil/wtf/utils"
     8  	"github.com/wtfutil/wtf/wtf"
     9  )
    10  
    11  func (widget *Widget) content() (string, string, bool) {
    12  	client := widget.currentData()
    13  	if client == nil {
    14  		return widget.CommonSettings().Title, " NewRelic data unavailable ", false
    15  	}
    16  	app, appErr := client.Application()
    17  	deploys, depErr := client.Deployments()
    18  
    19  	appName := "error"
    20  	if appErr == nil {
    21  		appName = app.Name
    22  	}
    23  
    24  	var content string
    25  	title := fmt.Sprintf("%s - [green]%s[white]", widget.CommonSettings().Title, appName)
    26  	wrap := false
    27  	if depErr != nil {
    28  		wrap = true
    29  		content = depErr.Error()
    30  	} else {
    31  		content = widget.contentFrom(deploys)
    32  	}
    33  
    34  	return title, content, wrap
    35  }
    36  
    37  func (widget *Widget) contentFrom(deploys []nr.ApplicationDeployment) string {
    38  	str := fmt.Sprintf(
    39  		" %s\n",
    40  		fmt.Sprintf(
    41  			"[%s]Latest Deploys[white]",
    42  			widget.settings.Colors.Subheading,
    43  		),
    44  	)
    45  
    46  	revisions := []string{}
    47  
    48  	for _, deploy := range deploys {
    49  		if (deploy.Revision != "") && utils.DoesNotInclude(revisions, deploy.Revision) {
    50  			lineColor := "white"
    51  			if wtf.IsToday(deploy.Timestamp) {
    52  				lineColor = "lightblue"
    53  			}
    54  
    55  			revLen := 8
    56  			if revLen > len(deploy.Revision) {
    57  				revLen = len(deploy.Revision)
    58  			}
    59  
    60  			str += fmt.Sprintf(
    61  				" [green]%s[%s] %s %-.16s[white]\n",
    62  				deploy.Revision[0:revLen],
    63  				lineColor,
    64  				deploy.Timestamp.Format("Jan 02 15:04 MST"),
    65  				utils.NameFromEmail(deploy.User),
    66  			)
    67  
    68  			revisions = append(revisions, deploy.Revision)
    69  
    70  			if len(revisions) == widget.settings.deployCount {
    71  				break
    72  			}
    73  		}
    74  	}
    75  
    76  	return str
    77  }