2025-02-20 05:12:36 -06:00
|
|
|
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by the GPL 3.0
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2025-02-20 05:27:53 -06:00
|
|
|
|
|
|
|
"go.wit.com/log"
|
2025-02-20 05:12:36 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// this file is named poorly. It has more than Sort()
|
|
|
|
|
|
|
|
func (pb *Files) makeGuiFile(pf *File) error {
|
|
|
|
newf, _ := os.OpenFile(pf.Filebase+".gui.pb.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
|
|
|
defer newf.Close()
|
|
|
|
|
|
|
|
headerGui(newf, pf)
|
|
|
|
|
|
|
|
fmt.Fprintf(newf, "// START GUI\n")
|
|
|
|
fmt.Fprintf(newf, "\n")
|
|
|
|
|
2025-02-20 05:27:53 -06:00
|
|
|
guiMain(newf, pf.Bases.Name, pf.Base.Name)
|
|
|
|
guiStringFuncs(newf, pf.Package, pf.Bases.Name, pf.Base.Name)
|
|
|
|
|
|
|
|
for _, v := range pf.Base.Vars {
|
|
|
|
// log.Printf("Found %s %s\n", v.VarType, v.VarName)
|
|
|
|
if v.VarType == "string" {
|
|
|
|
log.Printf("make Add function here %s %s %s\n", pf.Bases.Name, pf.Base.Name, v.VarName)
|
|
|
|
guiAddFunc(newf, pf.Bases.Name, pf.Base.Name, v.VarName)
|
|
|
|
// log.Printf("make Add function here %s %s\n", v.VarType, v.VarName)
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
continue
|
|
|
|
if !v.HasSort {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if v.IsRepeated {
|
|
|
|
// can't work against slices
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if v.VarType != "string" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
VARNAME := v.VarName
|
|
|
|
funcdef := msg.newSortType(wSort, PARENT, VARNAME)
|
|
|
|
log.Printf("Adding %s\n", funcdef)
|
|
|
|
*/
|
|
|
|
}
|
2025-02-20 05:12:36 -06:00
|
|
|
|
|
|
|
fmt.Fprintf(newf, "\n")
|
|
|
|
fmt.Fprintf(newf, "// END GUI\n")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func headerGui(w io.Writer, pf *File) {
|
|
|
|
// header must come first
|
|
|
|
headerComment(w)
|
|
|
|
|
|
|
|
fmt.Fprintf(w, "package %s\n", pf.Package)
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, "import (")
|
|
|
|
fmt.Fprintln(w, " \"time\"")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, " \"go.wit.com/gui\"")
|
|
|
|
fmt.Fprintln(w, " \"go.wit.com/lib/protobuf/guipb\"")
|
|
|
|
fmt.Fprintln(w, " \"go.wit.com/log\"")
|
|
|
|
fmt.Fprintln(w, " timestamppb \"google.golang.org/protobuf/types/known/timestamppb\"")
|
|
|
|
fmt.Fprintln(w, ")")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func guiMain(w io.Writer, FRUITS string, FRUIT string) {
|
|
|
|
fmt.Fprintln(w, "func (x *"+FRUITS+") NewTable(title string) *"+FRUITS+"Table {")
|
|
|
|
fmt.Fprintln(w, " t := new("+FRUITS+"Table)")
|
|
|
|
fmt.Fprintln(w, " t.x = x")
|
|
|
|
fmt.Fprintln(w, " pb := new(guipb.Table)")
|
|
|
|
fmt.Fprintln(w, " pb.Title = title")
|
|
|
|
fmt.Fprintln(w, " t.pb = pb")
|
|
|
|
fmt.Fprintln(w, " return t")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, "func (t *"+FRUITS+"Table) AddStringFunc(title string, f func(*"+FRUIT+") string) {")
|
|
|
|
fmt.Fprintln(w, " t.pb.Order = append(t.pb.Order, title)")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, " sf := new("+FRUIT+"StringFunc)")
|
|
|
|
fmt.Fprintln(w, " sf.title = title")
|
|
|
|
fmt.Fprintln(w, " sf.f = f")
|
|
|
|
fmt.Fprintln(w, " t.stringFuncs = append(t.stringFuncs, sf)")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
|
|
|
|
fmt.Fprintln(w, "func (t *"+FRUITS+"Table) AddIntFunc(title string, f func(*"+FRUIT+") int) {")
|
|
|
|
fmt.Fprintln(w, " t.pb.Order = append(t.pb.Order, title)")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, " sf := new("+FRUIT+"IntFunc)")
|
|
|
|
fmt.Fprintln(w, " sf.title = title")
|
|
|
|
fmt.Fprintln(w, " sf.f = f")
|
|
|
|
fmt.Fprintln(w, " t.intFuncs = append(t.intFuncs, sf)")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, "func (t *"+FRUITS+"Table) AddTimeFunc(title string, f func(*"+FRUIT+") time.Time) {")
|
|
|
|
fmt.Fprintln(w, " t.pb.Order = append(t.pb.Order, title)")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, " sf := new("+FRUIT+"TimeFunc)")
|
|
|
|
fmt.Fprintln(w, " sf.title = title")
|
|
|
|
fmt.Fprintln(w, " sf.f = f")
|
|
|
|
fmt.Fprintln(w, " t.timeFuncs = append(t.timeFuncs, sf)")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, "func (mt *"+FRUITS+"Table) ShowTable() {")
|
|
|
|
fmt.Fprintln(w, " log.Info(\"ShowTable() SENDING TO GUI\")")
|
|
|
|
fmt.Fprintln(w, " // mt.MakeTable()")
|
|
|
|
fmt.Fprintln(w, " gui.ShowTable(mt.pb)")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, "type "+FRUIT+"StringFunc struct {")
|
|
|
|
fmt.Fprintln(w, " title string")
|
|
|
|
fmt.Fprintln(w, " f func(*"+FRUIT+") string")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, "type "+FRUIT+"IntFunc struct {")
|
|
|
|
fmt.Fprintln(w, " title string")
|
|
|
|
fmt.Fprintln(w, " f func(*"+FRUIT+") int")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, "type "+FRUIT+"TimeFunc struct {")
|
|
|
|
fmt.Fprintln(w, " title string")
|
|
|
|
fmt.Fprintln(w, " f func(*"+FRUIT+") time.Time")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, "type "+FRUITS+"Table struct {")
|
|
|
|
fmt.Fprintln(w, " // gt *gui.NodeTable")
|
|
|
|
fmt.Fprintln(w, " pb *guipb.Table")
|
|
|
|
fmt.Fprintln(w, " x *"+FRUITS+"")
|
|
|
|
fmt.Fprintln(w, " hostnames []string")
|
|
|
|
fmt.Fprintln(w, " stringFuncs []*"+FRUIT+"StringFunc")
|
|
|
|
fmt.Fprintln(w, " intFuncs []*"+FRUIT+"IntFunc")
|
|
|
|
fmt.Fprintln(w, " timeFuncs []*"+FRUIT+"TimeFunc")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
}
|
|
|
|
|
|
|
|
func guiStringFuncs(w io.Writer, ZOOPB string, FRUITS string, FRUIT string) {
|
|
|
|
fmt.Fprintln(w, "func (mt *"+FRUITS+"Table) doStringFunc(name string) bool {")
|
|
|
|
fmt.Fprintln(w, " for _, sf := range mt.stringFuncs {")
|
|
|
|
fmt.Fprintln(w, " if sf.title != name {")
|
|
|
|
fmt.Fprintln(w, " continue")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, " log.Info(\""+ZOOPB+": found stringfunc name:\", name)")
|
|
|
|
fmt.Fprintln(w, " r := new(guipb.StringRow)")
|
|
|
|
fmt.Fprintln(w, " r.Header = new(guipb.Widget)")
|
|
|
|
fmt.Fprintln(w, " r.Header.Name = name")
|
|
|
|
fmt.Fprintln(w, " all := mt.x.All()")
|
|
|
|
fmt.Fprintln(w, " for all.Scan() {")
|
|
|
|
fmt.Fprintln(w, " m := all.Next()")
|
|
|
|
fmt.Fprintln(w, " r.Vals = append(r.Vals, sf.f(m))")
|
|
|
|
fmt.Fprintln(w, " log.Info(\""+ZOOPB+": adding\", name, r.Vals)")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, " mt.pb.StringRows = append(mt.pb.StringRows, r)")
|
|
|
|
fmt.Fprintln(w, " return true")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, " return false")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, "func (mt *"+FRUITS+"Table) doIntFunc(name string) bool {")
|
|
|
|
fmt.Fprintln(w, " for _, sf := range mt.intFuncs {")
|
|
|
|
fmt.Fprintln(w, " if sf.title != name {")
|
|
|
|
fmt.Fprintln(w, " continue")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, " log.Info(\""+ZOOPB+": found intfunc name:\", name)")
|
|
|
|
fmt.Fprintln(w, " r := new(guipb.IntRow)")
|
|
|
|
fmt.Fprintln(w, " r.Header = new(guipb.Widget)")
|
|
|
|
fmt.Fprintln(w, " r.Header.Name = name")
|
|
|
|
fmt.Fprintln(w, " all := mt.x.All()")
|
|
|
|
fmt.Fprintln(w, " for all.Scan() {")
|
|
|
|
fmt.Fprintln(w, " m := all.Next()")
|
|
|
|
fmt.Fprintln(w, " r.Vals = append(r.Vals, int64(sf.f(m)))")
|
|
|
|
fmt.Fprintln(w, " log.Info(\""+ZOOPB+": adding\", name, r.Vals)")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, " mt.pb.IntRows = append(mt.pb.IntRows, r)")
|
|
|
|
fmt.Fprintln(w, " return true")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, " return false")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, "func (mt *"+FRUITS+"Table) doTimeFunc(name string) bool {")
|
|
|
|
fmt.Fprintln(w, " for _, sf := range mt.timeFuncs {")
|
|
|
|
fmt.Fprintln(w, " if sf.title != name {")
|
|
|
|
fmt.Fprintln(w, " continue")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, " log.Info(\""+ZOOPB+": found timefunc name:\", name)")
|
|
|
|
fmt.Fprintln(w, " r := new(guipb.TimeRow)")
|
|
|
|
fmt.Fprintln(w, " r.Header = new(guipb.Widget)")
|
|
|
|
fmt.Fprintln(w, " r.Header.Name = name")
|
|
|
|
fmt.Fprintln(w, " all := mt.x.All()")
|
|
|
|
fmt.Fprintln(w, " for all.Scan() {")
|
|
|
|
fmt.Fprintln(w, " m := all.Next()")
|
|
|
|
fmt.Fprintln(w, " t := sf.f(m)")
|
|
|
|
fmt.Fprintln(w, " r.Vals = append(r.Vals, timestamppb.New(t)) // convert to protobuf time")
|
|
|
|
fmt.Fprintln(w, " log.Info(\""+ZOOPB+": adding\", name, r.Vals)")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, " mt.pb.TimeRows = append(mt.pb.TimeRows, r)")
|
|
|
|
fmt.Fprintln(w, " return true")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, " return false")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, "func (mt *"+FRUITS+"Table) MakeTable() {")
|
|
|
|
fmt.Fprintln(w, " for _, name := range mt.pb.Order {")
|
|
|
|
fmt.Fprintln(w, " log.Info(\""+ZOOPB+": looking for row name()\", name)")
|
|
|
|
fmt.Fprintln(w, " if mt.doStringFunc(name) {")
|
|
|
|
fmt.Fprintln(w, " continue")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, " if mt.doIntFunc(name) {")
|
|
|
|
fmt.Fprintln(w, " continue")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, " if mt.doTimeFunc(name) {")
|
|
|
|
fmt.Fprintln(w, " continue")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
}
|
2025-02-20 05:27:53 -06:00
|
|
|
|
|
|
|
func guiAddFunc(w io.Writer, FRUITS string, FRUIT string, BRAND string) {
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, "func (t *"+FRUITS+"Table) Add"+BRAND+"() {")
|
|
|
|
fmt.Fprintln(w, " // t.pb.Order = append(t.pb.Order, "+BRAND+")")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, " t.AddStringFunc(\""+BRAND+"\", func(m *"+FRUIT+") string {")
|
|
|
|
fmt.Fprintln(w, " return m."+BRAND+"")
|
|
|
|
fmt.Fprintln(w, " })")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
/*
|
|
|
|
fmt.Fprintln(w, "func (t *"+FRUITS+"Table) AddMemory() {")
|
|
|
|
fmt.Fprintln(w, " t.pb.Order = append(t.pb.Order, \"Memory\")")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, "func (t *"+FRUITS+"Table) AddCpus() {")
|
|
|
|
fmt.Fprintln(w, " t.pb.Order = append(t.pb.Order, \"Cpus\")")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
*/
|
|
|
|
}
|