Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CI: Enable and fix some checks - Pt2 #32

Merged
merged 13 commits into from
Dec 18, 2024
Merged
33 changes: 16 additions & 17 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,18 @@ linters:
disable:
- exportloopref # Deprecated
- execinquery # Deprecated
- gomnd # Deprecated name of mnd
- exhaustruct # Very noisy, hard to disable when want zero values
- forcetypeassert # If its wrong, we want to know
- nlreturn # Not desired
- gci
- gocritic
- godox
- gomnd
- godox # Yes. The project is in progress
- depguard # Not desired
- varnamelen # Not desired
- gochecknoglobals
- gosec
- mnd
- nonamedreturns
- paralleltest
- predeclared
- testpackage
- thelper
- varnamelen
- wrapcheck
- wsl
- depguard
- cyclop
- revive
- funlen
- unparam
- exhaustive
- inamedparam

linters-settings:
ireturn:
Expand All @@ -37,3 +24,15 @@ linters-settings:
- stdlib
- empty
- generic
gci:
sections:
- standard
- default
- localmodule
mnd:
ignored-files:
- 'monitor/.*.go' # UI code has many magic layout numbers...
- 'main.go' # Main file allowed, as it is example code
paralleltest:
# eCAL uses global state, any test using Initialize can't be run in parallel
ignore-missing: true
17 changes: 10 additions & 7 deletions cmd/monitor/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"errors"
"fmt"

"github.com/DownerCase/ecal-go/ecal/monitoring"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"

"github.com/DownerCase/ecal-go/ecal/monitoring"
)

var (
Expand All @@ -26,38 +27,40 @@ func NewTable(columns []table.Column) table.Model {
type NavKeyMap map[string]func() tea.Cmd

func (navKeys NavKeyMap) HandleMsg(msg tea.Msg) (cmd tea.Cmd, navigated bool) {
switch msg := msg.(type) {
case tea.KeyMsg:
if msg, ok := msg.(tea.KeyMsg); ok {
if f, ok := navKeys[msg.String()]; ok {
return f(), true
}
}

return nil, false
}

type topicType int
type TopicType int

const (
topicTypeSubscriber topicType = iota
topicTypeSubscriber TopicType = iota
topicTypePublisher
)

func getTopicMonitoring(topicType topicType) []monitoring.TopicMon {
func getTopicMonitoring(topicType TopicType) []monitoring.TopicMon {
switch topicType {
case topicTypeSubscriber:
return monitoring.GetMonitoring(monitoring.MonitorSubscriber).Subscribers
case topicTypePublisher:
return monitoring.GetMonitoring(monitoring.MonitorPublisher).Publishers
}

return nil
}

func getTopicFromID(topicType topicType, id string) (monitoring.TopicMon, error) {
func getTopicFromID(topicType TopicType, id string) (monitoring.TopicMon, error) {
topicList := getTopicMonitoring(topicType)
for _, topic := range topicList {
if topic.TopicID == id {
return topic, nil
}
}

return monitoring.TopicMon{}, fmt.Errorf("[getTopicFromId]: %w", errNoTopic)
}
16 changes: 9 additions & 7 deletions cmd/monitor/config_page.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
package main

import (
"github.com/DownerCase/ecal-go/ecal"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"

"github.com/DownerCase/ecal-go/ecal"
)

type modelConfig struct {
type ModelConfig struct {
viewport viewport.Model
}

func NewConfigModel() *modelConfig {
func NewConfigModel() *ModelConfig {
viewport := viewport.New(85, 10)
viewport.SetContent(ecal.GetConfig())
viewport.Style = baseStyle
return &modelConfig{

return &ModelConfig{
viewport: viewport,
}
}

func (m *modelConfig) Refresh() {}
func (m *ModelConfig) Refresh() {}

func (m *modelConfig) Update(msg tea.Msg) (cmd tea.Cmd) {
func (m *ModelConfig) Update(msg tea.Msg) (cmd tea.Cmd) {
m.viewport, cmd = m.viewport.Update(msg)
return cmd
}

func (m *modelConfig) View() string {
func (m *ModelConfig) View() string {
return m.viewport.View()
}
34 changes: 20 additions & 14 deletions cmd/monitor/hosts_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package main
import (
"strconv"

"github.com/DownerCase/ecal-go/ecal/monitoring"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"

"github.com/DownerCase/ecal-go/ecal/monitoring"
)

type modelHosts struct {
type ModelHosts struct {
table table.Model
}

func NewHostsModel() *modelHosts {
func NewHostsModel() *ModelHosts {
columns := []table.Column{
{Title: "Host", Width: 28},
{Title: "Processes", Width: 9},
Expand All @@ -22,21 +23,21 @@ func NewHostsModel() *modelHosts {
{Title: "Clients", Width: 7},
}

return &modelHosts{
return &ModelHosts{
table: NewTable(columns),
}
}

func (m *modelHosts) Update(msg tea.Msg) tea.Cmd {
m.updateTable(nil)
func (m *ModelHosts) Update(msg tea.Msg) tea.Cmd {
m.updateTable(msg)
return nil
}

func (m *modelHosts) View() string {
func (m *ModelHosts) View() string {
return baseStyle.Render(m.table.View()) + "\n" + m.table.HelpView()
}

func (m *modelHosts) Refresh() {
func (m *ModelHosts) Refresh() {
m.updateTable(nil)
}

Expand All @@ -48,33 +49,37 @@ type hostInfo struct {
Processes int
}

func (m *modelHosts) updateTable(msg tea.Msg) {
func (m *ModelHosts) updateTable(msg tea.Msg) {
mon := monitoring.GetMonitoring(monitoring.MonitorAll)

hosts := make(map[string]hostInfo)
for _, pub := range mon.Publishers {
host := hosts[pub.HostName]
host.Publishers += 1
host.Publishers++
hosts[pub.HostName] = host
}

for _, sub := range mon.Subscribers {
host := hosts[sub.HostName]
host.Subscribers += 1
host.Subscribers++
hosts[sub.HostName] = host
}

for _, client := range mon.Clients {
host := hosts[client.HostName]
host.Clients += 1
host.Clients++
hosts[client.HostName] = host
}

for _, server := range mon.Servers {
host := hosts[server.HostName]
host.Servers += 1
host.Servers++
hosts[server.HostName] = host
}

for _, proc := range mon.Processes {
host := hosts[proc.HostName]
host.Processes += 1
host.Processes++
hosts[proc.HostName] = host
}

Expand All @@ -93,5 +98,6 @@ func hostsToRows(hosts map[string]hostInfo) (rows []table.Row) {
strconv.FormatInt(int64(hostInfo.Clients), 10),
})
}

return
}
27 changes: 14 additions & 13 deletions cmd/monitor/log_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package main
import (
"time"

"github.com/DownerCase/ecal-go/ecal/logging"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"

"github.com/DownerCase/ecal-go/ecal/logging"
)

type LoggingPage int
Expand All @@ -22,7 +23,7 @@ type logsKeyMap struct {
Clear key.Binding
}

type modelLogs struct {
type ModelLogs struct {
table table.Model
subpage LoggingPage
help help.Model
Expand All @@ -48,29 +49,28 @@ func (km logsKeyMap) FullHelp() [][]key.Binding {
return append([][]key.Binding{{km.Clear}}, km.KeyMap.FullHelp()...)
}

func NewLogsModel() *modelLogs {
func NewLogsModel() *ModelLogs {
columns := []table.Column{
{Title: "Time", Width: 10},
{Title: "Level", Width: 6},
{Title: "Unit", Width: 15},
{Title: "Message", Width: 46},
}

return &modelLogs{
return &ModelLogs{
table: NewTable(columns),
subpage: subpageLoggingMain,
help: help.New(),
keymap: newLogsKeyMap(),
}
}

func (m *modelLogs) Update(msg tea.Msg) tea.Cmd {
func (m *ModelLogs) Update(msg tea.Msg) tea.Cmd {
var cmd tea.Cmd

switch m.subpage {
case subpageLoggingMain:
switch msg := msg.(type) {
case tea.KeyMsg:
if msg, ok := msg.(tea.KeyMsg); ok {
switch {
case key.Matches(msg, m.keymap.Clear):
m.table.SetRows([]table.Row{})
Expand All @@ -80,37 +80,38 @@ func (m *modelLogs) Update(msg tea.Msg) tea.Cmd {
}
}
case subpageLoggingDetailed:
// cmd = m.model_detailed.Update(msg)
}

return cmd
}

func (m *modelLogs) View() string {
func (m *ModelLogs) View() string {
switch m.subpage {
case subpageLoggingMain:
return baseStyle.Render(m.table.View()) + "\n" + m.help.View(m.keymap)
case subpageLoggingDetailed:
// return m.model_detailed.View()
}

return "Invalid page"
}

func (m *modelLogs) Refresh() {
func (m *ModelLogs) Refresh() {
switch m.subpage {
case subpageLoggingDetailed:
// m.model_detailed.Refresh()
default:
case subpageLoggingMain:
m.updateTable(nil)
}
}

func (m *modelLogs) updateTable(msg tea.Msg) {
func (m *ModelLogs) updateTable(msg tea.Msg) {
rows := []table.Row{}
logs := logging.GetLogging().Messages

for _, log := range logs {
rows = append(rows, logToRow(log))
}

m.table.SetRows(append(m.table.Rows(), rows...))
m.table, _ = m.table.Update(msg)
}
Expand Down
Loading
Loading