refact logview

mine
derailed 2019-06-20 14:56:12 -06:00
parent a4d42aaf51
commit da43251c67
4 changed files with 44 additions and 48 deletions

View File

@ -72,7 +72,7 @@ func (v *logResourceView) showLogs(prev bool) {
if v.containerFn != nil { if v.containerFn != nil {
co = v.containerFn() co = v.containerFn()
} }
l.reload(co, v, v.list.GetName(), prev) l.reload(co, v, prev)
v.switchPage("logs") v.switchPage("logs")
} }

View File

@ -31,13 +31,10 @@ type (
logsView struct { logsView struct {
*tview.Pages *tview.Pages
app *appView app *appView
title string parent loggable
parent loggable actions keyActions
container string cancelFunc context.CancelFunc
actions keyActions
cancelFunc context.CancelFunc
showPrevious bool
} }
) )
@ -46,7 +43,6 @@ func newLogsView(title string, app *appView, parent loggable) *logsView {
app: app, app: app,
Pages: tview.NewPages(), Pages: tview.NewPages(),
parent: parent, parent: parent,
title: title,
} }
return &v return &v
@ -54,12 +50,11 @@ func newLogsView(title string, app *appView, parent loggable) *logsView {
// Protocol... // Protocol...
func (v *logsView) reload(co string, parent loggable, title string, prevLogs bool) { func (v *logsView) reload(co string, parent loggable, prevLogs bool) {
v.parent, v.title, v.showPrevious = parent, title, prevLogs v.parent = parent
v.deletePage() v.deletePage()
v.AddPage(co, newLogView(co, v.app, v.backCmd), true, true) v.AddPage("logs", newLogView(co, v.app, v.backCmd), true, true)
v.container = co v.load(co, prevLogs)
v.load()
} }
// SetActions to handle keyboard events. // SetActions to handle keyboard events.
@ -78,8 +73,7 @@ func (v *logsView) backFn() actionHandler {
} }
func (v *logsView) deletePage() { func (v *logsView) deletePage() {
v.RemovePage(v.container) v.RemovePage("logs")
v.container = ""
} }
func (v *logsView) stop() { func (v *logsView) stop() {
@ -91,8 +85,8 @@ func (v *logsView) stop() {
v.cancelFunc = nil v.cancelFunc = nil
} }
func (v *logsView) load() { func (v *logsView) load(container string, showPrevious bool) {
if err := v.doLoad(v.parent.getSelection(), v.container); err != nil { if err := v.doLoad(v.parent.getSelection(), container, showPrevious); err != nil {
v.app.flash().err(err) v.app.flash().err(err)
l := v.CurrentPage().Item.(*logView) l := v.CurrentPage().Item.(*logView)
l.logLine("😂 Doh! No logs are available at this time. Check again later on...") l.logLine("😂 Doh! No logs are available at this time. Check again later on...")
@ -101,7 +95,7 @@ func (v *logsView) load() {
v.app.SetFocus(v) v.app.SetFocus(v)
} }
func (v *logsView) doLoad(path, co string) error { func (v *logsView) doLoad(path, co string, showPrevious bool) error {
v.stop() v.stop()
maxBuff := int64(v.app.config.K9s.LogRequestSize) maxBuff := int64(v.app.config.K9s.LogRequestSize)
@ -118,30 +112,7 @@ func (v *logsView) doLoad(path, co string) error {
l.SetTitle(fmat) l.SetTitle(fmat)
c := make(chan string, 10) c := make(chan string, 10)
go func(l *logView) { go updateLogs(c, l)
buff, index := make([]string, logBuffSize), 0
for {
select {
case line, ok := <-c:
if !ok {
l.flush(index, buff)
index = 0
return
}
if index < logBuffSize {
buff[index] = line
index++
continue
}
l.flush(index, buff)
index = 0
buff[index] = line
case <-time.After(flushTimeout):
l.flush(index, buff)
index = 0
}
}
}(l)
ns, po := namespaced(path) ns, po := namespaced(path)
res, ok := v.parent.getList().Resource().(resource.Tailable) res, ok := v.parent.getList().Resource().(resource.Tailable)
@ -156,7 +127,7 @@ func (v *logsView) doLoad(path, co string) error {
Name: po, Name: po,
Container: co, Container: co,
Lines: maxBuff, Lines: maxBuff,
Previous: v.showPrevious, Previous: showPrevious,
} }
if err := res.Logs(ctx, c, opts); err != nil { if err := res.Logs(ctx, c, opts); err != nil {
v.cancelFunc() v.cancelFunc()
@ -166,6 +137,31 @@ func (v *logsView) doLoad(path, co string) error {
return nil return nil
} }
func updateLogs(c <-chan string, l *logView) {
buff, index := make([]string, logBuffSize), 0
for {
select {
case line, ok := <-c:
if !ok {
l.flush(index, buff)
index = 0
return
}
if index < logBuffSize {
buff[index] = line
index++
continue
}
l.flush(index, buff)
index = 0
buff[index] = line
case <-time.After(flushTimeout):
l.flush(index, buff)
index = 0
}
}
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Actions... // Actions...

View File

@ -158,7 +158,7 @@ func (v *podView) viewLogs(prev bool) bool {
} }
status := strings.TrimSpace(v.masterPage().GetCell(r, col).Text) status := strings.TrimSpace(v.masterPage().GetCell(r, col).Text)
if status == "Running" || status == "Completed" { if status == "Running" || status == "Completed" {
v.showLogs(v.selectedItem, "", v.list.GetName(), v, prev) v.showLogs(v.selectedItem, "", v, prev)
return true return true
} }
@ -166,9 +166,9 @@ func (v *podView) viewLogs(prev bool) bool {
return false return false
} }
func (v *podView) showLogs(path, co, view string, parent loggable, prev bool) { func (v *podView) showLogs(path, co string, parent loggable, prev bool) {
l := v.GetPrimitive("logs").(*logsView) l := v.GetPrimitive("logs").(*logsView)
l.reload(co, parent, view, prev) l.reload(co, parent, prev)
v.switchPage("logs") v.switchPage("logs")
} }

View File

@ -77,7 +77,7 @@ func (v *svcView) logsCmd(evt *tcell.EventKey) *tcell.EventKey {
} }
l := v.GetPrimitive("logs").(*logsView) l := v.GetPrimitive("logs").(*logsView)
l.reload("", v, v.list.GetName(), false) l.reload("", v, false)
v.switchPage("logs") v.switchPage("logs")
return nil return nil