Exemplo n.º 1
0
func updateMemCPU(gcpu, gmem *ui.Gauge) {
	done := make(chan struct{})
	memInfoChan, errc := daemon.TotalMemory(done, daemon.Delay)
	cpuInfoChan, errc := daemon.TotalCPU(done, daemon.Delay)
	timeout := time.After(2 * daemon.Delay)
	defer func() {
		close(done)
		// Necessary to read from error channel to prevent sending goroutine going into deadlock
		<-errc
	}()
	for {
		select {
		case memInfo := <-memInfoChan:
			pc := int(100 * (float64(memInfo.MemTotal-memInfo.MemFree) / float64(memInfo.MemTotal)))
			gmem.Percent = pc
			ui.Render(ui.Body)
		case cpuInfo := <-cpuInfoChan:
			pc := int(cpuInfo.AverageUtilization)
			gcpu.Percent = pc
			ui.Render(ui.Body)
		case err := <-errc:
			if err != nil {
			}
			return
		case <-timeout:
		}
	}
}
Exemplo n.º 2
0
func UpdateGenericGauge(g *ui.Gauge, updateFunc func() float64) {
	for {
		g.Percent = int(updateFunc())
		time.Sleep(500 * time.Millisecond)
	}
}