-
Notifications
You must be signed in to change notification settings - Fork 0
/
reportModel.go
154 lines (133 loc) · 3.94 KB
/
reportModel.go
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package tui
import (
"fmt"
"strconv"
"time"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/buonotti/apisense/errors"
"github.com/buonotti/apisense/validation/pipeline"
)
var (
validatedEndpointRows []table.Row
selectedReport pipeline.Report
allowReportSelection bool
)
type reportModel struct {
keymap keymap
table table.Model
validationEndpointModel tea.Model
}
func ReportModel() tea.Model {
r, err := pipeline.Reports()
errors.CheckErr(err)
reports = r
t := table.New(
table.WithColumns(getReportColumns()),
table.WithRows(getReportRows(reports)),
table.WithFocused(true),
table.WithHeight(7),
)
s := table.DefaultStyles()
s.Selected = s.Selected.
Foreground(lipgloss.Color("#F38BA8")).
Background(lipgloss.Color("#1e1e2e")).
Bold(false)
t.SetStyles(s)
return reportModel{
keymap: DefaultKeyMap,
table: t,
validationEndpointModel: ValidationEndpointModel(),
}
}
func (r reportModel) Init() tea.Cmd {
return nil
}
func (r reportModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
var cmdModel tea.Cmd
if choiceReportModel != "" {
if directoryUpdate {
t := table.New(
table.WithColumns(getReportColumns()),
table.WithRows(getReportRows(reports)),
table.WithFocused(true),
table.WithHeight(7),
)
s := table.DefaultStyles()
s.Selected = s.Selected.
Foreground(lipgloss.Color("#F38BA8")).
Background(lipgloss.Color("#1e1e2e")).
Bold(false)
t.SetStyles(s)
r.table = t
directoryUpdate = false
}
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, r.keymap.back):
if choiceReportModel == "reportModel" {
choiceReportModel = ""
choiceMainMenu = ""
}
case key.Matches(msg, r.keymap.quit):
return r, tea.Quit
case key.Matches(msg, r.keymap.choose):
if allowReportSelection {
i, err := strconv.Atoi(r.table.SelectedRow()[0])
errors.CheckErr(err)
rep, err := getSelectedReport(reports, i)
errors.CheckErr(err)
selectedReport = rep
validatedEndpointRows = getValidatedEndpointRows(selectedReport)
if choiceReportModel != "reportModel" {
r.validationEndpointModel, cmdModel = r.validationEndpointModel.Update(msg)
r.table, cmd = r.table.Update(msg)
return r, tea.Batch(cmd, cmdModel)
}
choiceReportModel = "validatedEndpointModel"
r.table, cmd = r.table.Update(msg)
}
return r, cmd
}
}
r.validationEndpointModel, cmdModel = r.validationEndpointModel.Update(msg)
r.table, cmd = r.table.Update(msg)
return r, tea.Batch(cmd, cmdModel)
}
return r, nil
}
func (r reportModel) View() string {
if choiceReportModel != "reportModel" {
return styleContentCenter.Copy().MarginLeft(1).MarginRight(1).BorderStyle(lipgloss.RoundedBorder()).Render(r.validationEndpointModel.View())
}
return styleContentCenter.Copy().MarginLeft(1).MarginRight(1).BorderStyle(lipgloss.RoundedBorder()).Render(r.table.View())
}
func getReportRows(reports []pipeline.Report) []table.Row {
rows := make([]table.Row, 0)
for i, report := range reports {
rows = append(rows, table.Row{fmt.Sprintf("%v", i), report.Id, fmt.Sprintf("%v", time.Time(report.Time).Format("2006-01-02 15:04:05"))})
}
allowReportSelection = true
if len(rows) < 1 {
rows = append(rows, table.Row{"", "No reports", "found. Make sure the deamon is running correctly"})
allowReportSelection = false
}
return rows
}
func getReportColumns() []table.Column {
return []table.Column{
{Title: "", Width: 3},
{Title: "Id", Width: 10},
{Title: "Timestamp", Width: 62},
}
}
func getSelectedReport(reports []pipeline.Report, index int) (pipeline.Report, error) {
if index > len(reports) || index < 0 {
return pipeline.Report{}, errors.ModelError.New("Index out of range")
}
return reports[index], nil
}