github.com/wtfutil/wtf@v0.43.0/modules/datadog/widget.go (about)

     1  package datadog
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/rivo/tview"
     7  	"github.com/wtfutil/wtf/utils"
     8  	"github.com/wtfutil/wtf/view"
     9  	datadog "github.com/zorkian/go-datadog-api"
    10  )
    11  
    12  type Widget struct {
    13  	view.ScrollableWidget
    14  
    15  	monitors []datadog.Monitor
    16  	settings *Settings
    17  	err      error
    18  }
    19  
    20  func NewWidget(tviewApp *tview.Application, redrawChan chan bool, pages *tview.Pages, settings *Settings) *Widget {
    21  	widget := Widget{
    22  		ScrollableWidget: view.NewScrollableWidget(tviewApp, redrawChan, pages, settings.Common),
    23  
    24  		settings: settings,
    25  	}
    26  
    27  	widget.SetRenderFunction(widget.Render)
    28  	widget.initializeKeyboardControls()
    29  
    30  	return &widget
    31  }
    32  
    33  /* -------------------- Exported Functions -------------------- */
    34  
    35  func (widget *Widget) Refresh() {
    36  	widget.err = nil
    37  	monitors, monitorErr := widget.Monitors()
    38  
    39  	if monitorErr != nil {
    40  		widget.monitors = nil
    41  		widget.err = monitorErr
    42  		widget.SetItemCount(0)
    43  		widget.Redraw(func() (string, string, bool) { return widget.CommonSettings().Title, monitorErr.Error(), true })
    44  		return
    45  	}
    46  	triggeredMonitors := []datadog.Monitor{}
    47  
    48  	for _, monitor := range monitors {
    49  		state := *monitor.OverallState
    50  		if state == "Alert" {
    51  			triggeredMonitors = append(triggeredMonitors, monitor)
    52  		}
    53  	}
    54  	widget.monitors = triggeredMonitors
    55  	widget.SetItemCount(len(widget.monitors))
    56  
    57  	widget.Render()
    58  }
    59  
    60  func (widget *Widget) Render() {
    61  	widget.Redraw(widget.content)
    62  }
    63  
    64  /* -------------------- Unexported Functions -------------------- */
    65  
    66  func (widget *Widget) content() (string, string, bool) {
    67  	triggeredMonitors := widget.monitors
    68  	var str string
    69  
    70  	title := widget.CommonSettings().Title
    71  
    72  	if widget.err != nil {
    73  		return title, widget.err.Error(), true
    74  	}
    75  
    76  	if len(triggeredMonitors) > 0 {
    77  		str += fmt.Sprintf(
    78  			" %s\n",
    79  			fmt.Sprintf(
    80  				"[%s]Triggered Monitors[white]",
    81  				widget.settings.Colors.Subheading,
    82  			),
    83  		)
    84  		for idx, triggeredMonitor := range triggeredMonitors {
    85  			row := fmt.Sprintf(`[%s][red] %s[%s]`,
    86  				widget.RowColor(idx),
    87  				*triggeredMonitor.Name,
    88  				widget.RowColor(idx),
    89  			)
    90  			str += utils.HighlightableHelper(widget.View, row, idx, len(*triggeredMonitor.Name))
    91  		}
    92  	} else {
    93  		str += fmt.Sprintf(
    94  			" %s\n",
    95  			"[green]No Triggered Monitors[white]",
    96  		)
    97  	}
    98  
    99  	return title, str, false
   100  }
   101  
   102  func (widget *Widget) openItem() {
   103  
   104  	sel := widget.GetSelected()
   105  	if sel >= 0 && widget.monitors != nil && sel < len(widget.monitors) {
   106  		item := &widget.monitors[sel]
   107  		utils.OpenFile(fmt.Sprintf("https://app.datadoghq.com/monitors/%d?q=*", *item.Id))
   108  	}
   109  }