diff --git a/internal/database/database.go b/internal/database/database.go index ae68305..496cb29 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -7,6 +7,7 @@ import ( _ "github.com/lib/pq" "github.com/sirupsen/logrus" "shiny-pancake/internal/model" + "shiny-pancake/logger" ) const datetime = `2006-01-02T15:04:05Z` @@ -57,8 +58,10 @@ func (db *Database) UserSessions() ([]*model.Session, error) { defer rows.Close() for rows.Next() { var sessCount model.Session + var l logger.Log err = rows.Scan(&sessCount.Username, &sessCount.Count) - + l = logger.Log{"info", logrus.Fields{"session" : sessCount.Count}, " count " + sessCount.Username + " users on host"} + logger.Lgr(&l) if err != nil { // handle this error panic(err) diff --git a/internal/gui/gui.go b/internal/gui/gui.go index 0a926c0..f18388d 100644 --- a/internal/gui/gui.go +++ b/internal/gui/gui.go @@ -3,14 +3,20 @@ package gui import ( "fmt" "github.com/sirupsen/logrus" + "shiny-pancake/internal/model" "shiny-pancake/logger" + "shiny-pancake/internal/database" "strings" - - tview "gitlab.com/tslocum/cview" + "github.com/rivo/tview" ) +type resources struct { + ActSess []*model.Session +} + + type panels struct { currentPanel int panel []panel @@ -32,7 +38,7 @@ func newState() *state { type Gui struct { app *tview.Application - panels *tview.Panels + pages *tview.Pages state *state db *database.Database } @@ -40,16 +46,26 @@ type Gui struct { func New(db *database.Database) *Gui { return &Gui{ app: tview.NewApplication(), - panels: tview.NewPanels(), + pages: tview.NewPages(), state: newState(), db: db, } } +func (g *Gui) sessionPanel() *activeSessions { + for _, panel := range g.state.panels.panel { + if panel.name() == `activeSessions` { + return panel.(*activeSessions) + } + } + return nil +} + // Start start application func (g *Gui) Start() error { g.initPanels() + g.startMonitoring() var l logger.Log l = logger.Log{"info", logrus.Fields{"init" : "ran"}, "got this far"} logger.Lgr(&l) @@ -62,46 +78,26 @@ func (g *Gui) Start() error { } func (g *Gui) Stop() { + g.stopMonitoring() g.app.Stop() } // Page "definitions" -func (g *Gui) sessionPanel() *activeSessions { - for _, panel := range g.state.panels.panel { - if panel.name() == `activeSessions` { - return panel.(*activeSessions) - } - } - return nil -} func (g *Gui) initPanels() { g.state.tabBar = newTabBar() - // Page definitions s := newSessions(g) + var l logger.Log l = logger.Log{"info", logrus.Fields{"initPanels" : "ran"}, "got this far"} logger.Lgr(&l) - /* - // NOTE: I would really like to get this working as it would be far neater. - // The issue is with the three panels being of different types. - // cannot use pg (type panel) as type tview.Primitive in argument to g.panels.AddPage: - // panel does not implement tview.Primitive (missing Blur method) - for idx, pg := range []panel{trips, cavers, caves} { - name := pg.name() - g.panels.AddPage(name, pg, true, idx == 0) - fmt.Fprintf(g.state.tabBar, ` %d ["%d"][darkcyan]%s[white][""] `, idx+1, idx, strings.Title(name)) - } - g.state.tabBar.Highlight("0") - */ - // Add panels to the "book" - g.panels.AddPanel(`activeSessions`, s, true, true) + g.pages.AddPage(`activeSessions`, s, true, true) fmt.Fprintf(g.state.tabBar, ` ["%d"]%d %s[""] `, 0, 1, strings.Title(s.name())) g.state.tabBar.Highlight("0") @@ -110,6 +106,15 @@ func (g *Gui) initPanels() { g.state.panels.panel = append(g.state.panels.panel, s) + layout := tview.NewFlex().SetDirection(tview.FlexRow). + AddItem(g.state.tabBar, 1, 1, false). + AddItem(g.pages, 0, 16, true) - g.app.SetRoot(g.panels, true) + g.app.SetRoot(layout, true) + g.goTo(`activeSessions`) } + +func (g *Gui) goTo(page string) { + g.pages.SwitchToPage(page) +} + diff --git a/internal/gui/monitoring.go b/internal/gui/monitoring.go index 2743dd7..bfd249e 100644 --- a/internal/gui/monitoring.go +++ b/internal/gui/monitoring.go @@ -1,15 +1,15 @@ package gui -//func (g *Gui) startMonitoring() { -// stop := make(chan int, 1) -// g.state.stopChans["activeSessions"] = stop -// go g.sessionPanel().monitoringSessions(g) -//} -// -//func (g *Gui) stopMonitoring() { -// g.state.stopChans["activeSessions"] <- 1 -// -//} +func (g *Gui) startMonitoring() { + stop := make(chan int, 1) + g.state.stopChans["activeSessions"] = stop + go g.sessionPanel().monitoringSessions(g) +} + +func (g *Gui) stopMonitoring() { + g.state.stopChans["activeSessions"] <- 1 + +} /*func (g *Gui) updateTask() { g.app.QueueUpdateDraw(func() { diff --git a/internal/gui/panel.go b/internal/gui/panel.go index 40ef44f..f608ec1 100644 --- a/internal/gui/panel.go +++ b/internal/gui/panel.go @@ -3,7 +3,8 @@ package gui type panel interface { name() string entries(*Gui) - //setEntries(*Gui) - //focus(*Gui) - //unfocus() + setEntries(*Gui) + updateEntries(*Gui) + focus(*Gui) + unfocus() } diff --git a/internal/gui/sessions.go b/internal/gui/sessions.go index 5aa2511..a040c8d 100644 --- a/internal/gui/sessions.go +++ b/internal/gui/sessions.go @@ -1,14 +1,13 @@ package gui import ( - tview "gitlab.com/tslocum/cview" + + "github.com/gdamore/tcell/v2" + "github.com/rivo/tview" "shiny-pancake/internal/model" + "time" ) -type resources struct { - ActSess []*model.Session - //statsLocations []*model.Statistic -} type activeSessions struct { @@ -44,3 +43,66 @@ func (t *activeSessions) entries(g *Gui) { g.state.resources.ActSess = sss } +func (t *activeSessions) setEntries(g *Gui) { + t.entries(g) + table := t.Clear() + + headers := []string{ + "user", + "session count", + } + + for i, header := range headers { + table.SetCell(0, i, &tview.TableCell{ + Text: header, + NotSelectable: true, + Align: tview.AlignLeft, + Color: tview.Styles.PrimaryTextColor, + BackgroundColor: tview.Styles.PrimitiveBackgroundColor, + Attributes: tcell.AttrBold, + }) + } + + for i, s := range g.state.resources.ActSess { + table.SetCell(i+1, 0, tview.NewTableCell(s.Username). + SetTextColor(tview.Styles.PrimaryTextColor). + SetMaxWidth(30). + SetExpansion(1)) + + table.SetCell(i+1, 1, tview.NewTableCell(string(s.Count)). + SetTextColor(tview.Styles.PrimaryTextColor). + SetMaxWidth(30). + SetExpansion(1)) + + } +} + +func (t *activeSessions) updateEntries(g *Gui) { + g.app.QueueUpdateDraw(func() { + t.setEntries(g) + }) +} + +func (t *activeSessions) focus(g *Gui) { + t.SetSelectable(true, false) + g.app.SetFocus(t) +} + +func (t *activeSessions) unfocus() { + t.SetSelectable(false, false) +} + +func (t *activeSessions) monitoringSessions(g *Gui) { + ticker := time.NewTicker(5 * time.Minute) + +LOOP: + for { + select { + case <-ticker.C: + t.updateEntries(g) + case <-g.state.stopChans["trips"]: + ticker.Stop() + break LOOP + } + } +} diff --git a/internal/gui/tabbar.go b/internal/gui/tabbar.go index 368e112..87fa0ea 100644 --- a/internal/gui/tabbar.go +++ b/internal/gui/tabbar.go @@ -1,10 +1,15 @@ package gui - import ( -tview "gitlab.com/tslocum/cview" + "github.com/rivo/tview" ) func newTabBar() *tview.TextView { - return tview.NewTextView() + return tview.NewTextView(). + SetDynamicColors(true). + SetRegions(true). + SetWrap(false)/*. + SetHighlightedFunc(func(added, removed, remaining []string) { + g.pages.SwitchToPage(added[0]) + })*/ } diff --git a/main b/main index 41e5daa..29181fd 100755 Binary files a/main and b/main differ