Skip to content

Commit

Permalink
Replaced update timing with a real timer
Browse files Browse the repository at this point in the history
  • Loading branch information
kbence committed Dec 20, 2016
1 parent 07c62ce commit 8bd953e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 68 deletions.
43 changes: 30 additions & 13 deletions pipeline/linechartpipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package pipeline
import (
"fmt"
"os"
"sync"
"time"

"github.com/kbence/logan/types"
"github.com/kbence/logan/utils"
"github.com/kbence/logan/utils/terminfo"
)

Expand All @@ -22,7 +23,7 @@ type LineChartPipeline struct {
settings LineChartSettings
chartSettings *types.ChartSettings
sampler *types.TimelineSampler
timer *utils.UpdateTimer
renderLock *sync.Mutex
}

func NewLineChartPipeline(input types.LogLineChannel, settings LineChartSettings) *LineChartPipeline {
Expand All @@ -43,7 +44,7 @@ func NewLineChartPipeline(input types.LogLineChannel, settings LineChartSettings
settings: settings,
chartSettings: chartSettings,
sampler: sampler,
timer: utils.NewUpdateTimer(),
renderLock: &sync.Mutex{},
}
}

Expand All @@ -53,20 +54,30 @@ func (p *LineChartPipeline) render() {
fmt.Println(chartRenderer.Render())
}

func (p *LineChartPipeline) updateIfNeeded() {
if !p.settings.FrequentUpdates {
return
}

if p.timer.IsUpdateNeeded() {
p.render()
fmt.Print(terminfo.GoUpBy(p.settings.Height - 1))
os.Stdout.Sync()
func (p *LineChartPipeline) updateLoop(exitChannel chan bool) {
for {
select {
case <-time.After(time.Second):
p.renderLock.Lock()
p.render()
fmt.Print(terminfo.GoUpBy(p.settings.Height - 1))
os.Stdout.Sync()
p.renderLock.Unlock()
break

case <-exitChannel:
return
}
}
}

func (p *LineChartPipeline) Start() chan bool {
exitChannel := make(chan bool)
updateExitChannel := make(chan bool)

if p.settings.FrequentUpdates {
go p.updateLoop(updateExitChannel)
}

go func() {
for {
Expand All @@ -77,10 +88,16 @@ func (p *LineChartPipeline) Start() chan bool {
}

p.sampler.Inc(line.Date, 1)
p.updateIfNeeded()
}

if p.settings.FrequentUpdates {
// Exit update goproc _before_ we start rendering
updateExitChannel <- true
}

p.renderLock.Lock()
p.render()
p.renderLock.Unlock()

exitChannel <- true
}()
Expand Down
53 changes: 35 additions & 18 deletions pipeline/uniquepipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package pipeline
import (
"fmt"
"strings"
"sync"
"time"
"unicode/utf8"

"github.com/kbence/logan/types"
"github.com/kbence/logan/utils"
"github.com/kbence/logan/utils/terminfo"
)

Expand All @@ -19,16 +20,16 @@ type UniquePipeline struct {
input types.LogLineChannel
counter *types.LogLineCounter
settings UniqueSettings
timer *utils.UpdateTimer
renderLock *sync.Mutex
lastPrintedLines int
}

func NewUniquePipeline(input types.LogLineChannel, settings UniqueSettings) *UniquePipeline {
return &UniquePipeline{
input: input,
counter: types.NewLogLineCounter(),
settings: settings,
timer: &utils.UpdateTimer{},
input: input,
counter: types.NewLogLineCounter(),
settings: settings,
renderLock: &sync.Mutex{},
}
}

Expand Down Expand Up @@ -102,26 +103,36 @@ func (p *UniquePipeline) clearLines(lines, width int) {
fmt.Println(terminfo.GoUpBy(lines))
}

func (p *UniquePipeline) updateIfNeeded() {
if p.settings.TopLimit <= 0 {
return
}
func (p *UniquePipeline) updateLoop(exitChannel chan bool) {
for {
select {
case <-time.After(time.Second):
p.renderLock.Lock()
p.clearLines(p.lastPrintedLines, p.settings.TerminalWidth)

if p.timer.IsUpdateNeeded() {
p.clearLines(p.lastPrintedLines, p.settings.TerminalWidth)
numPrintedLines := p.printUniqueLines()

numPrintedLines := p.printUniqueLines()
if numPrintedLines > 0 {
fmt.Print(terminfo.GoUpBy(numPrintedLines))
}

if numPrintedLines > 0 {
fmt.Print(terminfo.GoUpBy(numPrintedLines))
}
p.lastPrintedLines = numPrintedLines
p.renderLock.Unlock()
break

p.lastPrintedLines = numPrintedLines
case <-exitChannel:
return
}
}
}

func (p *UniquePipeline) Start() chan bool {
exitChannel := make(chan bool)
updateExitChannel := make(chan bool)

if p.settings.TopLimit > 0 {
go p.updateLoop(updateExitChannel)
}

go func() {
for {
Expand All @@ -132,10 +143,16 @@ func (p *UniquePipeline) Start() chan bool {
}

p.storeUniqueLine(line)
p.updateIfNeeded()
}

if p.settings.TopLimit > 0 {
updateExitChannel <- true
}

p.renderLock.Lock()
p.printUniqueLines()
p.renderLock.Unlock()

exitChannel <- true
}()

Expand Down
37 changes: 0 additions & 37 deletions utils/updatetimer.go

This file was deleted.

0 comments on commit 8bd953e

Please sign in to comment.