Skip to content

Commit

Permalink
show indices and docs in index
Browse files Browse the repository at this point in the history
  • Loading branch information
jtsunne committed Dec 22, 2022
1 parent 2484719 commit d639e06
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 11 deletions.
24 changes: 24 additions & 0 deletions Structs/EsIndices.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package Structs

import "encoding/json"

type EsIndices struct {
Health string `json:"health"`
Status string `json:"status"`
Expand All @@ -20,3 +22,25 @@ type EsIndexAlias struct {
RoutingIndex string `json:"routing.index"`
RoutingSearch string `json:"routing.search"`
}

type EsDocs struct {
Took int `json:"took"`
TimedOut bool `json:"timed_out"`
Shards struct {
Total int `json:"total"`
Successful int `json:"successful"`
Skipped int `json:"skipped"`
Failed int `json:"failed"`
} `json:"_shards"`
Hits struct {
Total int `json:"total"`
MaxScore float64 `json:"max_score"`
Hits []struct {
Index string `json:"_index"`
Type string `json:"_type"`
Id string `json:"_id"`
Score float64 `json:"_score"`
Source json.RawMessage `json:"_source,omitempty"`
} `json:"hits"`
} `json:"hits"`
}
12 changes: 12 additions & 0 deletions Utils/Common.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ func GetJson(url string, target interface{}) error {
return json.NewDecoder(r.Body).Decode(target)
}

func PostJson(url string, body string, target interface{}) error {
req, _ := http.NewRequest(http.MethodPost, url, strings.NewReader(body))
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
r, err := c.Do(req)
if err != nil {
return err
}
defer r.Body.Close()

return json.NewDecoder(r.Body).Decode(target)
}

func ParseEsUrl(u string) string {
schema := "http"
port := "9200"
Expand Down
2 changes: 1 addition & 1 deletion Views/HelpPage.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Global HotKeys:
Indices Page HotKeys:
i - Sort indices by Index Name
o - Sort indices by Documents Count
Ctrl+\ - Set filter for the Indices view
? - Set filter for the Indices view
Ctrl+E - Remove selected Index
Ctrl+P - Set Replicas Count for the selected index
Enter - Show Index Settings
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ go 1.19

require (
github.com/gdamore/tcell/v2 v2.5.3
github.com/rivo/tview v0.0.0-20221214142745-4802e03c3898
github.com/rivo/tview v0.0.0-20221217182043-ccce554c3803
)

require (
github.com/epiclabs-io/winman v0.0.0-20220901164457-3d8c4b3ae090 // indirect
github.com/gdamore/encoding v1.0.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/epiclabs-io/winman v0.0.0-20220901164457-3d8c4b3ae090 h1:sEzis2zS+9e9kGElINXWL5oSea1xdeYp4V7wMC7eBcs=
github.com/epiclabs-io/winman v0.0.0-20220901164457-3d8c4b3ae090/go.mod h1:m/93UpwDqZIYvwcDC2Ipag4XIdqBKvBhk/9GFZD+8g4=
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1 h1:QqwPZCwh/k1uYqq6uXSb9TRDhTkfQbO80v8zhnIe5zM=
Expand All @@ -10,24 +12,31 @@ github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/rivo/tview v0.0.0-20220812085834-0e6b21a48e96/go.mod h1:hyzpnqn4KWzZopTEjL1AxvlzOLMH1IuKo4lTw6vyOQc=
github.com/rivo/tview v0.0.0-20220916081518-2e69b7385a37 h1:cTzFg1FfTXwXuODi7Doz70hsW+dAye1OBwAFWHCqmww=
github.com/rivo/tview v0.0.0-20220916081518-2e69b7385a37/go.mod h1:YX2wUZOcJGOIycErz2s9KvDaP0jnWwRCirQMPLPpQ+Y=
github.com/rivo/tview v0.0.0-20221214142745-4802e03c3898 h1:iLHpXlN1wrXyHmT13UUrroUzl3ujn59cAbEXFlMdRV4=
github.com/rivo/tview v0.0.0-20221214142745-4802e03c3898/go.mod h1:YX2wUZOcJGOIycErz2s9KvDaP0jnWwRCirQMPLPpQ+Y=
github.com/rivo/tview v0.0.0-20221217182043-ccce554c3803 h1:gaknGRzW4g4I+5sGu4X81BZbROJ0j96ap9xnEbcZhXA=
github.com/rivo/tview v0.0.0-20221217182043-ccce554c3803/go.mod h1:YX2wUZOcJGOIycErz2s9KvDaP0jnWwRCirQMPLPpQ+Y=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.3.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rivo/uniseg v0.4.2 h1:YwD0ulJSJytLpiaWua0sBDusfsCZohxjxzVTYjwxfV8=
github.com/rivo/uniseg v0.4.2/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2 h1:46ULzRKLh1CwgRq2dC5SlBzEqqNCi8rreOZnNrbqcIY=
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220318055525-2edf467146b5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d h1:SZxvLBoTP5yHO3Frd4z4vrF+DBX9vMVanchswa69toE=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.3.0 h1:qoo4akIqOcDME5bhc/NgxUdovd6BSS2uMsVjB56q1xI=
golang.org/x/term v0.3.0/go.mod h1:q750SLmJuPmVoN1blW3UFBPREJfb1KmY3vwxfr+nFDA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
72 changes: 63 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ var (
clusterNodesTags []Structs.EsClusterNodeTags
clusterRepos []Structs.EsClusterRepository
clusterSnapshots map[string][]Structs.EsSnapshot
docs Structs.EsDocs
app = tview.NewApplication()
pages = tview.NewPages()
helpPage = tview.NewTextView()
tvNodes = tview.NewTable()
tvIndices = tview.NewTable()
tvDocsTable = tview.NewTable()
repoTable = tview.NewTable()
snapshotTable = tview.NewTable()
header = tview.NewTextView()
Expand Down Expand Up @@ -69,6 +71,11 @@ func init() {
pages.SwitchToPage("indices")
}
})
tvDocsTable.SetDoneFunc(func(k tcell.Key) {
if k == tcell.KeyEscape {
pages.SwitchToPage("indices")
}
})

tvIndices.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
Expand All @@ -84,6 +91,12 @@ func init() {
SetReplicasMessageBox(name.Text)
RefreshData()
return nil
case tcell.KeyCtrlBackslash:
r, _ := tvIndices.GetSelection()
name := tvIndices.GetCell(r, 0)
GetDocsFromIndex(name.Text)
pages.SwitchToPage("docs")
return nil
}
switch event.Rune() {
case 'I':
Expand All @@ -98,6 +111,9 @@ func init() {
case 'o':
SortData("docCount")
return nil
case '?':
app.SetFocus(filter)
return nil
}
return event
})
Expand Down Expand Up @@ -146,17 +162,23 @@ func init() {
AddItem(header, 3, 1, false).
AddItem(helpPage, 0, 1, true),
true, true)
pages.AddPage("nodes",
pages.AddPage("indices",
tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(header, 3, 1, false).
AddItem(tvNodes, 0, 1, true).
AddItem(filter, 3, 1, false).
AddItem(tvIndices, 0, 1, true).
AddItem(footer, 3, 1, false),
true, true)
pages.AddPage("indices",
pages.AddPage("docs",
tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(header, 3, 1, false).
AddItem(filter, 3, 1, false).
AddItem(tvIndices, 0, 1, true).
AddItem(tvDocsTable, 0, 1, true).
AddItem(footer, 3, 1, false),
true, true)
pages.AddPage("nodes",
tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(header, 3, 1, false).
AddItem(tvNodes, 0, 1, true).
AddItem(footer, 3, 1, false),
true, true)
}
Expand Down Expand Up @@ -202,6 +224,36 @@ func RefreshData() {
footer.SetText("Data refreshed @ " + dt.Format(time.ANSIC))
}

func GetDocsFromIndex(idxName string) {
b := []byte(`{"query": { "match_all": {} }, "size": 100}`)
Utils.PostJson(fmt.Sprintf("%s/%s/_search", EsUrl, idxName), string(b), &docs)
tvDocsTable.Clear()
tvDocsTable.SetBorder(true)
tvDocsTable.SetCell(0, 0, tview.NewTableCell("_id").
SetTextColor(tcell.ColorYellow).SetAlign(tview.AlignCenter))
tvDocsTable.SetCell(0, 1, tview.NewTableCell("_source").
SetTextColor(tcell.ColorYellow).SetAlign(tview.AlignCenter))
for i, item := range docs.Hits.Hits {
tvDocsTable.SetCellSimple(i+1, 0, item.Id)
tvDocsTable.SetCellSimple(i+1, 1, string(item.Source))
}
tvDocsTable.SetFixed(2, 1)
tvDocsTable.Select(2, 1)
tvDocsTable.SetSelectable(true, false)
tvDocsTable.SetSelectedFunc(func(row, column int) {
r, _ := tvDocsTable.GetSelection()
name := tvDocsTable.GetCell(r, 1)
tvInfo.SetText(Utils.ColorizeJson(name.Text))
pages.SwitchToPage("info")
})
tvInfo.SetDoneFunc(func(k tcell.Key) {
if k == tcell.KeyEscape {
pages.SwitchToPage("docs")
}
})

}

func SortData(sortBy string) {

if sortBy == "docCount" {
Expand Down Expand Up @@ -326,7 +378,7 @@ func FillIndices(idxs []Structs.EsIndices, t *tview.Table) {
t.SetCell(0, 0, tview.NewTableCell("Index").
SetTextColor(tcell.ColorYellow).
SetAlign(tview.AlignCenter))
t.SetCell(0, 1, tview.NewTableCell("Index").
t.SetCell(0, 1, tview.NewTableCell("Alias").
SetTextColor(tcell.ColorYellow).
SetAlign(tview.AlignCenter))
t.SetCell(0, 2, tview.NewTableCell("Health").
Expand Down Expand Up @@ -472,6 +524,11 @@ func selectedIndexFunc(row int, _ int, tbl *tview.Table) {
body, _ := io.ReadAll(r.Body)
tvInfo.SetText(Utils.ColorizeJson(string(body)))
pages.SwitchToPage("info")
tvInfo.SetDoneFunc(func(k tcell.Key) {
if k == tcell.KeyEscape {
pages.SwitchToPage("indices")
}
})
}

func tableDoneFunc(k tcell.Key, tbl *tview.Table) {
Expand Down Expand Up @@ -537,9 +594,6 @@ func main() {
tvIndices.Clear()
RefreshData()
FilterData(filter.GetText())
case tcell.KeyCtrlBackslash:
footer.SetText("KeyBS pressed")
app.SetFocus(filter)
case tcell.KeyCtrlQ:
app.Stop()
case tcell.KeyF1:
Expand Down

0 comments on commit d639e06

Please sign in to comment.