summaryrefslogtreecommitdiff
path: root/machine.gui.go
blob: 5bd4501752eafbeefab811d4e3d7f70206580aba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package zoopb

import (
	"go.wit.com/gui"
	"go.wit.com/lib/protobuf/guipb"
	"go.wit.com/log"
)

func (x *Machines) NewTable(title string) *MachinesTable {
	t := new(MachinesTable)
	t.x = x
	pb := new(guipb.Table)
	pb.Title = title
	t.pb = pb
	return t
}

func (t *MachinesTable) AddStringFunc(title string, f func(*Machine) string) {
	t.pb.Order = append(t.pb.Order, title)

	sf := new(MachineStringFunc)
	sf.title = title
	sf.f = f
	t.stringFuncs = append(t.stringFuncs, sf)
}

func (t *MachinesTable) AddHostname() {
	log.Info("zoopb: GOT TO AddHostname()")
	t.pb.Order = append(t.pb.Order, "Hostname")
	t.pb.Order = append(t.pb.Order, "Cpus")
	t.pb.Order = append(t.pb.Order, "Memory")
}

func (mt *MachinesTable) doStringFunc(name string) {
	for _, sf := range mt.stringFuncs {
		if sf.title != name {
			continue
		}
		log.Info("zoopb: found stringfunc name:", name)
		r := new(guipb.StringRow)
		r.Header = new(guipb.Widget)
		r.Header.Name = name
		all := mt.x.All()
		for all.Scan() {
			m := all.Next()
			r.Vals = append(r.Vals, sf.f(m))
			log.Info("zoopb: adding", name, r.Vals)
		}
		mt.pb.StringRows = append(mt.pb.StringRows, r)
		return
	}
}

func (mt *MachinesTable) MakeTable() {
	for _, name := range mt.pb.Order {
		log.Info("zoopb: looking for row name()", name)
		switch name {
		case "Hostname":
			r := new(guipb.StringRow)
			r.Header = new(guipb.Widget)
			r.Header.Name = name
			all := mt.x.All()
			for all.Scan() {
				m := all.Next()
				r.Vals = append(r.Vals, m.Hostname)
				log.Info("zoopb: adding", name, r.Vals)
			}
			mt.pb.StringRows = append(mt.pb.StringRows, r)
			continue
		case "Cpus":
			i := new(guipb.IntRow)
			i.Header = new(guipb.Widget)
			i.Header.Name = name
			all := mt.x.All()
			for all.Scan() {
				m := all.Next()
				i.Vals = append(i.Vals, m.Cpus)
				log.Info("zoopb: adding", name, i.Vals)
			}
			mt.pb.IntRows = append(mt.pb.IntRows, i)
			continue
		case "Memory":
			i := new(guipb.IntRow)
			i.Header = new(guipb.Widget)
			i.Header.Name = name
			all := mt.x.All()
			for all.Scan() {
				m := all.Next()
				i.Vals = append(i.Vals, m.Memory)
				log.Info("zoopb: adding", name, i.Vals)
			}
			mt.pb.IntRows = append(mt.pb.IntRows, i)
			continue
		default:
			// mt.addFuncRow(name)
		}
		log.Info("zoopb: didn't find name. trying StringFuncs", name)
		mt.doStringFunc(name)
	}
}

/*
func (mt *MachinesTable) addRow(name string) {
	i := new(guipb.IntRow)
	i.Header = new(guipb.Widget)
	i.Header.Name = "Memories"
	all := t.x.All()
	for all.Scan() {
		m := all.Next()
		i.Vals = append(i.Vals, m.Memory)
		log.Info("zoopb: adding cpus", i.Vals)
	}
	t.pb.IntRows = append(t.pb.IntRows, i)
}
*/

func (mt *MachinesTable) ShowTable() {
	log.Info("zoopb.ShowTable() SENDING TO GUI")
	mt.MakeTable()
	gui.ShowTable(mt.pb)
}

type MachineStringFunc struct {
	title string
	f     func(*Machine) string
}

type MachinesTable struct {
	// gt        *gui.NodeTable
	pb          *guipb.Table
	x           *Machines
	hostnames   []string
	stringFuncs []*MachineStringFunc
	// columns   []*gui.NodeColumn
	// order     []*gui.NodeColumn
}