diff options
author | matthieu <matthieu.cneude@gmail.com> | 2020-09-02 12:12:53 +0200 |
---|---|---|
committer | matthieu <matthieu.cneude@gmail.com> | 2020-09-02 12:13:31 +0200 |
commit | d7683cc842825daa370a5f61aab1c4e0ca327e3a (patch) | |
tree | 96d45a7285f61473510b542f5f31e95126c34cf2 | |
parent | eb7ba582bf42a01008b92906f1871ab4e47e2c6e (diff) |
Add alternative to concurency in the code - need to add an option for
that
-rw-r--r-- | cmd/devdash.go | 3 | ||||
-rw-r--r-- | internal/project.go | 96 |
2 files changed, 85 insertions, 14 deletions
diff --git a/cmd/devdash.go b/cmd/devdash.go index 4c6af98..7878c3b 100644 --- a/cmd/devdash.go +++ b/cmd/devdash.go @@ -133,7 +133,10 @@ func build(file string, tui *internal.Tui) { } project.WithLocalhost(localhost) + // TODO choice between concurency and non concurency + // renderFuncs := project.CreateNonConcWidgets() renderFuncs := project.CreateWidgets() + fmt.Println(renderFuncs) if !debug { project.Render(renderFuncs) } diff --git a/internal/project.go b/internal/project.go index cf2d94f..18a46a1 100644 --- a/internal/project.go +++ b/internal/project.go @@ -1,5 +1,9 @@ package internal +// TODO it's a mess in there between concurrent / non concurrent ways of getting widget render function. +// Initially it was made to go around the limitation of concurrent connection for google analytics. +// To refactor! + import ( "github.com/pkg/errors" ) @@ -155,7 +159,7 @@ func mapServiceName(serviceID string) (string, error) { // Create all the widgets and populate them with data. // Return channels with render functions -func (p *project) CreateWidgets() [][][]chan func() error { +func (p *project) CreateWidgets() [][][]func() error { // TODO: use display.box instead of this shortcut err := p.addTitle(p.tui) if err != nil { @@ -189,18 +193,31 @@ func (p *project) CreateWidgets() [][][]chan func() error { continue } - go createWidgets(service, serviceName, w, p.tui, ch) + go getChannelRenderers(service, serviceName, w, p.tui, ch) } } } - return chs + funcs := make([][][]func() error, len(chs)) + for r, _ := range chs { + for c, _ := range chs[r] { + funcs[r] = append(funcs[r], []func() error{}) + for _, chann := range chs[r][c] { + f, ok := <-chann + if ok { + funcs[r][c] = append(funcs[r][c], f) + } + } + } + } + + return funcs } -// createWidgets and fetch information via different ways depending on Widget (API / SSH / ...) +// getConcurentRenderers and fetch information via different ways depending on Widget (API / SSH / ...) // A function to display the widget will be send to a channel. // One channel per widget to keep the widget order in a slice. -func createWidgets(s service, name string, w Widget, tui *Tui, c chan<- func() error) { +func getChannelRenderers(s service, name string, w Widget, tui *Tui, c chan<- func() error) { if s == nil { c <- DisplayError(tui, errors.Errorf("can't use widget %s without service %s.", w.Name, name)) } else { @@ -214,17 +231,68 @@ func createWidgets(s service, name string, w Widget, tui *Tui, c chan<- func() e close(c) } -func (p *project) Render(chs [][][]chan func() error) { +// Create all the widgets and populate them with data. +// Return channels with render functions +func (p *project) CreateNonConcWidgets() [][][]func() error { + // TODO: use display.box instead of this shortcut + err := p.addTitle(p.tui) + if err != nil { + err = errors.Wrapf(err, "can't add project title %s", p.name) + DisplayError(p.tui, err)() + } + + funcs := make([][][]func() error, len(p.widgets)) + + for ir, row := range p.widgets { + for ic, col := range row { + funcs[ir] = append(funcs[ir], []func() error{}) + for _, w := range col { + w = p.addDefaultTheme(w) + + service, err := p.mapServiceID(w.serviceID()) + if err != nil { + funcs[ir][ic] = append(funcs[ir][ic], DisplayError(p.tui, err)) + continue + } + + serviceName, err := mapServiceName(w.serviceID()) + if err != nil { + funcs[ir][ic] = append(funcs[ir][ic], DisplayError(p.tui, err)) + continue + } + + funcs[ir][ic] = append(funcs[ir][ic], getFuncRenderers(service, serviceName, w, p.tui)) + } + } + } + + return funcs +} + +func getFuncRenderers(s service, name string, w Widget, tui *Tui) (f func() error) { + if s == nil { + f = DisplayError(tui, errors.Errorf( + "Configuration error - you can't use the widget %s without the service %s.", + w.Name, + name, + )) + } + + f, err := s.CreateWidgets(w, tui) + if err != nil { + f = DisplayError(tui, err) + } + + return f +} + +func (p *project) Render(funcs [][][]func() error) { for r, row := range p.widgets { for c, col := range row { - cs := chs[r][c] - for _, chann := range cs { - f, ok := <-chann - if ok { - err := f() - if err != nil { - DisplayError(p.tui, err)() - } + for _, f := range funcs[r][c] { + err := f() + if err != nil { + DisplayError(p.tui, err)() } } if len(col) > 0 { |