-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_results_page.go
157 lines (144 loc) · 3.63 KB
/
query_results_page.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
155
156
157
package tessen
import (
"fmt"
"regexp"
ui "github.com/gizak/termui"
)
type QueryResult struct {
Id string
Summary string
Data interface{}
}
type QueryResultsPage struct {
BaseListPage
CommandBarFragment
StatusBarFragment
ActiveQuery Query
}
func GetQueryResults(query Query) []interface{} {
s := FindSourceByName(query.Source.Name)
if s == nil {
return nil
}
if s.Provider == "uchiwa" {
return GetFilteredListOfUchiwaEvents(query, &s.CachedData)
} else if s.Provider == "pagerduty" {
return GetFilteredListOfPagerDutyEvents(query, &s.CachedData)
} else if s.Provider == "chronos" {
return GetFilteredListOfChronosJobs(query, &s.CachedData)
} else {
log.Errorf("Unsupported provider %q", s.Provider)
return nil
}
}
func (p *QueryResultsPage) GetSelectedQueryResultId() string {
qr := p.cachedResults.([]interface{})[p.selectedLine]
return qr.(QueryResult).Id
}
func (p *QueryResultsPage) Search() {
s := p.ActiveSearch
n := len(p.cachedResults.([]interface{}))
if s.command == "" {
return
}
increment := 1
if s.directionUp {
increment = -1
}
// we use modulo here so we can loop through every line.
// adding 'n' means we never have '-1 % n'.
startLine := (p.selectedLine + n + increment) % n
for i := startLine; i != p.selectedLine; i = (i + increment + n) % n {
cr := p.cachedResults.([]interface{})[i]
if s.re.MatchString(cr.(QueryResult).Summary) {
p.SetSelectedLine(i)
p.Update()
break
}
}
}
func (p *QueryResultsPage) SelectItem() {
if len(p.cachedResults.([]interface{})) == 0 {
return
}
id := p.GetSelectedQueryResultId()
if id == "" {
return
}
q := new(ShowDetailPage)
q.EventId = id
q.Source = p.ActiveQuery.Source
currentPage = q
q.Create()
changePage()
}
func (p *QueryResultsPage) GoBack() {
currentPage = queryPage
changePage()
}
func (p *QueryResultsPage) Update() {
ls := p.uiList
log.Debugf("QueryResultsPage.Update(): self: %s (%p), ls: (%p)", p.Id(), p, ls)
p.markActiveLine()
ls.Items = p.displayLines[p.firstDisplayLine:]
ui.Render(ls)
p.statusBar.Update()
p.commandBar.Update()
}
func (p *QueryResultsPage) Refresh() {
pDeref := &p
q := *pDeref
q.cachedResults = nil
queryResultsPage = q
changePage()
q.Create()
}
func (p *QueryResultsPage) markActiveLine() {
if p.cachedResults == nil {
return
}
for i, v := range p.cachedResults.([]interface{}) {
selected := ""
s := v.(QueryResult).Summary
if i == p.selectedLine {
selected = "fg-white,bg-blue"
if s == "" {
s = " "
} else if ok, _ := regexp.MatchString(`\[.+\]\((fg|bg)-[a-z]{1,6}\)`, s); ok {
r := regexp.MustCompile(`\[(.*?)\]\((fg|bg)-[a-z]{1,6}\)`)
s = r.ReplaceAllString(s, `$1`)
}
p.displayLines[i] = fmt.Sprintf("[%s](%s)", s, selected)
} else {
p.displayLines[i] = s
}
}
}
func (p *QueryResultsPage) Create() {
log.Debugf("QueryResultsPage.Create(): self: %s (%p)", p.Id(), p)
log.Debugf("QueryResultsPage.Create(): currentPage: %s (%p)", currentPage.Id(), currentPage)
ui.Clear()
ls := ui.NewList()
p.uiList = ls
if p.statusBar == nil {
p.statusBar = new(StatusBar)
}
if p.commandBar == nil {
p.commandBar = commandBar
}
if p.cachedResults == nil {
p.cachedResults = GetQueryResults(p.ActiveQuery)
}
if p.selectedLine >= len(p.cachedResults.([]interface{})) {
p.selectedLine = len(p.cachedResults.([]interface{})) - 1
}
p.displayLines = make([]string, len(p.cachedResults.([]interface{})))
ls.ItemFgColor = ui.ColorYellow
ls.BorderLabel = fmt.Sprintf("%s: %s", p.ActiveQuery.Name, p.ActiveQuery.Filter)
ls.Height = ui.TermHeight() - 2
ls.Width = ui.TermWidth()
ls.Y = 0
p.statusBar.Create()
p.commandBar.Create()
p.Update()
}