Beispiel #1
0
// updateFooter updates the footer contents based on any encountered errors.
func updateFooter(ctx *cli.Context, err error, footer *termui.Par) {
	// Generate the basic footer
	refresh := time.Duration(ctx.Int(monitorCommandRefreshFlag.Name)) * time.Second
	footer.Text = fmt.Sprintf("Press Ctrl+C to quit. Refresh interval: %v.", refresh)
	footer.TextFgColor = termui.Theme().ParTextFg | termui.AttrBold

	// Append any encountered errors
	if err != nil {
		footer.Text = fmt.Sprintf("Error: %v.", err)
		footer.TextFgColor = termui.ColorRed | termui.AttrBold
	}
}
Beispiel #2
0
// Init creates widgets, sets sizes and labels.
func (t *TermUI) Init(data UIData) error {
	err := termui.Init()
	if err != nil {
		return err
	}

	termui.UseTheme("helloworld")
	theme := termui.Theme()
	theme.BodyBg = termui.ColorDefault
	theme.BlockBg = termui.ColorDefault
	theme.BorderBg = termui.ColorDefault
	theme.BorderFg = termui.ColorBlack
	theme.BorderLabelTextBg = termui.ColorDefault
	theme.BorderLabelTextFg = termui.ColorCyan
	theme.ListItemBg = termui.ColorDefault
	theme.ParTextBg = termui.ColorDefault

	termui.SetTheme(theme)

	t.Title = func() *termui.Par {
		p := termui.NewPar("")
		p.Height = 3
		p.TextFgColor = termui.ColorWhite
		p.Border.Label = "Services Monitor"
		p.Border.FgColor = termui.ColorBlack
		return p
	}()
	t.Status = func() *termui.Par {
		p := termui.NewPar("")
		p.Height = 3
		p.TextFgColor = termui.ColorWhite
		p.Border.Label = "Status"
		p.Border.LabelFgColor = termui.ColorCyan
		p.Border.FgColor = termui.ColorBlack
		return p
	}()
	t.Services = func() *termui.List {
		list := termui.NewList()
		list.ItemFgColor = termui.ColorYellow
		list.Border.LabelFgColor = termui.ColorCyan | termui.AttrBold
		list.Border.Label = "Services"
		list.Height = len(data.Services) + 2
		return list
	}()

	t.Lists = make([]*termui.List, len(data.Vars))
	for i, name := range data.Vars {
		list := termui.NewList()
		list.ItemFgColor = colorByKind(name.Kind())
		list.Border.Label = name.Short()
		list.Border.LabelFgColor = termui.ColorCyan
		list.Height = len(data.Services) + 2
		t.Lists[i] = list
	}

	makeSparkline := func(name VarName) *termui.Sparklines {
		var sparklines []termui.Sparkline
		for _, service := range data.Services {
			spl := termui.NewSparkline()
			spl.Height = 1
			spl.TitleColor = termui.ColorGreen
			spl.LineColor = termui.ColorGreen
			spl.Title = service.Name
			sparklines = append(sparklines, spl)
		}

		s := termui.NewSparklines(sparklines...)
		s.Height = 2*len(data.Services) + 2
		s.HasBorder = true
		s.Border.Label = fmt.Sprintf("Monitoring %s", name.Long())
		return s
	}
	t.Sparkline1 = makeSparkline(data.Vars[0])
	if len(data.Vars) > 1 {
		t.Sparkline2 = makeSparkline(data.Vars[1])
	}

	t.Relayout()

	return nil
}
Beispiel #3
0
// Metrics prints out metrics for a given service or if the service is not
// specified, metrics for the entire environment are printed.
func Metrics(serviceLabel string, jsonFlag bool, csvFlag bool, sparkFlag bool, streamFlag bool, mins int, settings *models.Settings) {
	if streamFlag && (jsonFlag || csvFlag || mins != 1) {
		fmt.Println("--stream cannot be used with a custom format and multiple records")
		os.Exit(1)
	}
	var singleRetriever func(mins int, settings *models.Settings) *models.Metrics
	if serviceLabel != "" {
		service := helpers.RetrieveServiceByLabel(serviceLabel, settings)
		if service == nil {
			fmt.Printf("Could not find a service with the label \"%s\"\n", serviceLabel)
			os.Exit(1)
		}
		settings.ServiceID = service.ID
		singleRetriever = helpers.RetrieveServiceMetrics
	}
	var transformer Transformer
	redraw := make(chan bool)
	if jsonFlag {
		transformer = Transformer{
			SingleRetriever: singleRetriever,
			DataTransformer: &JSONTransformer{},
		}
	} else if csvFlag {
		buffer := &bytes.Buffer{}
		transformer = Transformer{
			SingleRetriever: singleRetriever,
			DataTransformer: &CSVTransformer{
				HeadersWritten: false,
				GroupMode:      serviceLabel == "",
				Buffer:         buffer,
				Writer:         csv.NewWriter(buffer),
			},
		}
	} else if sparkFlag {
		// the spark lines interface stays up until closed by the user, so
		// we might as well keep updating it as long as it is there
		streamFlag = true
		mins = 60
		err := ui.Init()
		if err != nil {
			fmt.Println(err.Error())
			os.Exit(1)
		}
		defer ui.Close()
		ui.UseTheme("helloworld")

		p := ui.NewPar("PRESS q TO QUIT")
		p.HasBorder = false
		p.TextFgColor = ui.Theme().SparklineTitle
		ui.Body.AddRows(
			ui.NewRow(ui.NewCol(12, 0, p)),
		)

		transformer = Transformer{
			SingleRetriever: singleRetriever,
			DataTransformer: &SparkTransformer{
				Redraw:     redraw,
				SparkLines: make(map[string]*ui.Sparklines),
			},
		}
	} else {
		transformer = Transformer{
			SingleRetriever: singleRetriever,
			DataTransformer: &TextTransformer{},
		}
	}
	transformer.GroupRetriever = helpers.RetrieveEnvironmentMetrics
	transformer.Stream = streamFlag
	transformer.GroupMode = serviceLabel == ""
	transformer.Mins = mins
	transformer.settings = settings

	helpers.SignIn(settings)

	if sparkFlag {
		go transformer.process()

		ui.Body.Align()
		ui.Render(ui.Body)

		quit := make(chan bool)
		go maintainSparkLines(redraw, quit)
		<-quit
	} else {
		transformer.process()
	}
}
Beispiel #4
0
// Init creates widgets, sets sizes and labels.
func (t *TermUISingle) Init(data UIData) error {
	err := termui.Init()
	if err != nil {
		return err
	}

	t.Sparklines = make(map[VarName]*termui.Sparkline)

	termui.UseTheme("helloworld")
	theme := termui.Theme()
	theme.BodyBg = termui.ColorDefault
	theme.BlockBg = termui.ColorDefault
	theme.BorderBg = termui.ColorDefault
	theme.BorderFg = termui.ColorBlack
	theme.BorderLabelTextBg = termui.ColorDefault
	theme.BorderLabelTextFg = termui.ColorCyan
	theme.ListItemBg = termui.ColorDefault
	theme.ParTextBg = termui.ColorDefault
	termui.SetTheme(theme)

	t.Title = func() *termui.Par {
		p := termui.NewPar("")
		p.Height = 3
		p.BgColor = termui.ColorDefault
		p.TextBgColor = termui.ColorDefault
		p.TextFgColor = termui.ColorWhite
		p.Border.Label = "Services Monitor"
		p.Border.LabelBgColor = termui.ColorDefault
		p.Border.LabelFgColor = termui.ColorCyan
		p.Border.FgColor = termui.ColorBlack
		p.Border.BgColor = termui.ColorDefault
		return p
	}()
	t.Status = func() *termui.Par {
		p := termui.NewPar("")
		p.Height = 3
		p.BgColor = termui.ColorDefault
		p.TextBgColor = termui.ColorDefault
		p.TextFgColor = termui.ColorWhite
		p.Border.Label = "Status"
		p.Border.LabelBgColor = termui.ColorDefault
		p.Border.LabelFgColor = termui.ColorCyan
		p.Border.FgColor = termui.ColorBlack
		p.Border.BgColor = termui.ColorDefault
		return p
	}()

	t.Pars = make([]*termui.Par, len(data.Vars))
	for i, name := range data.Vars {
		par := termui.NewPar("")
		par.BgColor = termui.ColorDefault
		par.TextFgColor = colorByKind(name.Kind())
		par.TextBgColor = termui.ColorDefault
		par.Border.FgColor = termui.ColorBlack
		par.Border.BgColor = termui.ColorDefault
		par.Border.Label = name.Short()
		par.Border.LabelFgColor = termui.ColorCyan
		par.Border.LabelBgColor = termui.ColorDefault
		par.Height = 3
		t.Pars[i] = par
	}

	var sparklines []termui.Sparkline
	for _, name := range data.Vars {
		spl := termui.NewSparkline()
		spl.Height = 1

		spl.TitleColor = colorByKind(name.Kind())
		spl.LineColor = colorByKind(name.Kind())
		spl.Title = name.Long()
		sparklines = append(sparklines, spl)
	}

	t.Sparkline = func() *termui.Sparklines {
		s := termui.NewSparklines(sparklines...)
		s.Height = 2*len(sparklines) + 2
		s.HasBorder = true
		s.BgColor = termui.ColorDefault
		s.Border.FgColor = termui.ColorBlack
		s.Border.BgColor = termui.ColorDefault
		s.Border.Label = fmt.Sprintf("Monitoring")
		s.Border.LabelFgColor = termui.ColorCyan
		s.Border.LabelBgColor = termui.ColorDefault
		return s
	}()

	t.Relayout()

	return nil
}