Skip to content

Commit

Permalink
Start implementing History
Browse files Browse the repository at this point in the history
While doing some refactoring in the main app, this patch starts
implementing the history feature as described in #5.

For now, it just lists the existing sessions using fyne widget.List.
The graphs will be generated using go-chart in the next patches.
  • Loading branch information
Cédric Jeanneret committed Mar 17, 2021
1 parent 0fd2ae9 commit 83bb7fd
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 35 deletions.
74 changes: 43 additions & 31 deletions gopolloplus.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import (
)

func main() {
var cfg *apolloUtils.ApolloConfig
var (
cfg *apolloUtils.ApolloConfig
history_file *os.File
)
standard_cfg, _ := homedir.Expand("~/.gopolloplus.ini")
_, err := os.Stat(standard_cfg)

Expand Down Expand Up @@ -63,11 +66,6 @@ func main() {
data_flow := make(chan *apolloMonitor.ApolloData, 5) // Make a buffered chan just in case
killWriter := make(chan bool)

hfile := apolloUtils.GetHistoryFile(cfg)
history_file, _ := os.OpenFile(hfile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
defer history_file.Close()
apolloUtils.CSVHeader(history_file)

if err != nil {
log.Fatal(err)
}
Expand All @@ -84,6 +82,10 @@ func main() {
window.SetFixedSize(true)
}

mainContainer := container.NewVBox()
dataContainer := container.NewVBox()
hfile := apolloUtils.GetHistoryFile(cfg)

// Define time things (clock and elapsed time)
clockLabel := apolloUI.TimeCanvas("Time")
val_elapsed := apolloUI.TimeCanvas("Elapsed")
Expand All @@ -96,16 +98,6 @@ func main() {
container.NewCenter(val_dist),
)

// Define buttons
button_quit := widget.NewButtonWithIcon("Quit", theme.CancelIcon(), func() {
killWriter <- true
monitor.Disconnect()
history_file.Close()
window.Close()
ui.Quit()
})


// Split canvas
lshift := float32(10)
split_title := &canvas.Text{Color: theme.TextColor(), Text: "Split Time",
Expand All @@ -132,7 +124,7 @@ func main() {
// SPM canvas
lshift = (2*apolloUI.GraphWidth)+10
spm_title := &canvas.Text{Color: theme.TextColor(),
Text: "Strokes per minutes",
Text: "Strokes per minute",
TextSize: apolloUI.TitleFontSize,
TextStyle: fyne.TextStyle{Bold: true}}

Expand All @@ -152,14 +144,33 @@ func main() {
spm_title, spm_current, spm_curr_txt,
spm_avg, spm_avg_txt,
spm_max, spm_max_txt)
var (
split_history = []uint64{}
power_history = []uint64{}
spm_history = []uint64{}
duration time.Duration
)

button_history := &widget.Button{Text: "History", Icon: theme.FolderOpenIcon(),}

// Define buttons
button_quit := widget.NewButtonWithIcon("Quit", theme.CancelIcon(), func() {
killWriter <- true
monitor.Disconnect()
history_file.Close()
window.Close()
ui.Quit()
})

button_reset := widget.NewButtonWithIcon("New Session", theme.DeleteIcon(), func() {
log.Print("Resetting remote monitor")
monitor.ResetSession()
history_file.Close()
// Prepare a new history file
hfile = apolloUtils.GetHistoryFile(cfg)
history_file, _ = os.OpenFile(hfile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
split_history = []uint64{}
power_history = []uint64{}
spm_history = []uint64{}
})

button_c2 := widget.NewButtonWithIcon("Send to log.C2", theme.MailForwardIcon(), func() {
Expand Down Expand Up @@ -188,16 +199,19 @@ func main() {
button_theme.SetChecked(true)
}

button_history.OnTapped = func () {apolloUI.ToggleHistory(ui, cfg)}
button_history.Refresh()

containerButtons := container.NewAdaptiveGrid(
4,
button_theme, button_reset, button_c2, button_quit,
5,
button_theme, button_reset, button_history, button_c2, button_quit,
)

mainContainer := container.NewVBox(
containerButtons,
containerTimes,
containerGraphs,
)
dataContainer.Add(containerTimes)
dataContainer.Add(containerGraphs)

mainContainer.Add(containerButtons)
mainContainer.Add(dataContainer)

window.SetContent(mainContainer)

Expand Down Expand Up @@ -233,12 +247,6 @@ func main() {
}()
go func() {
log.Print("Start chan reader")
var (
split_history = []uint64{}
power_history = []uint64{}
spm_history = []uint64{}
duration time.Duration
)
exit := false
for {
select {
Expand All @@ -261,6 +269,10 @@ func main() {
go apolloUI.ResizeCanvas(spm_history, spm_current, spm_avg, spm_max,
spm_curr_txt, spm_avg_txt, spm_max_txt, false)
go func() {
history_file, _ = os.OpenFile(hfile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
defer history_file.Close()
apolloUtils.CSVHeader(history_file)

history_file.Write([]byte(d.ToCSV()))
}()
default:
Expand Down
62 changes: 58 additions & 4 deletions pkg/apolloUI/apolloUI.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,35 @@ import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"github.com/cjeanneret/gopolloplus/pkg/apolloUtils"
"image/color"
"io/ioutil"
"log"
"sort"
"time"
)

const (
BarHeight = 50
GraphWidth = 300
TitleFontSize = 26
ValueFontSize = 18
TitleFontSize = 25
ValueFontSize = 30
)
var (
ValueColor = color.RGBA{3, 169, 244, 255}
CurrColor = color.Gray{Y: 110}
AVGColor = color.Gray{Y: 85}
MaxColor = color.Gray{Y: 65}
WhiteColor = color.Gray{Y: 255}
historyList = &widget.List{}
)

func TimeCanvas(title string) (c *canvas.Text) {
c = &canvas.Text{Color: theme.TextColor(),
TextSize: TitleFontSize,
TextSize: ValueFontSize,
Text: title,
TextStyle: fyne.TextStyle{Bold: true}}
return
Expand All @@ -42,7 +48,7 @@ func CreateCanvas(lshift, down float32, c color.Color) (rect *canvas.Rectangle,
rect_txt = &canvas.Text{Color: ValueColor,
TextSize: ValueFontSize,
TextStyle: fyne.TextStyle{Bold: true}}
rect_txt.Move(fyne.Position{lshift+10, down+20})
rect_txt.Move(fyne.Position{lshift+10, down+10})

return

Expand Down Expand Up @@ -84,3 +90,51 @@ func ResizeCanvas(values []uint64, curr, avg, max *canvas.Rectangle,
max_txt.Refresh()

}

func historyListing(cfg *apolloUtils.ApolloConfig) *widget.List {
files, err := ioutil.ReadDir(cfg.HistoryDir)
if err != nil {
log.Printf("historyListing: %v", err)
}
filenames := []string{}
for _, n := range files {
filenames = append(filenames, n.Name())
}
sort.Sort(sort.Reverse(sort.StringSlice(filenames)))

historyList = widget.NewList(
func() int { return len(filenames) },
func() fyne.CanvasObject { return widget.NewLabel("Sessions") },
func(i widget.ListItemID, o fyne.CanvasObject) { o.(*widget.Label).SetText(filenames[i]) },
)
historyList.OnSelected = func(i widget.ListItemID) { showSession(files[i].Name(), cfg) }
historyList.Resize(fyne.Size{Height: 500, Width: GraphWidth})

return historyList
}

func showSession(f string, cfg *apolloUtils.ApolloConfig) {
log.Print(f)
}

func ToggleHistory(ui fyne.App, cfg *apolloUtils.ApolloConfig) {
history := ui.NewWindow("History")
history.CenterOnScreen()
history.Resize(fyne.Size{Width:float32(GraphWidth*3 + 10), Height: 600})
history.SetFixedSize(true)

listing := historyListing(cfg)
listing.Move(fyne.Position{5, 40})
close_button := widget.NewButtonWithIcon("Close", theme.CancelIcon(), func() {
history.Close()
})

close_button.Resize(fyne.Size{Height: 35, Width: GraphWidth})
close_button.Move(fyne.Position{5, 5})

layout := container.NewWithoutLayout(close_button, listing)

history.SetContent(layout)

history.Show()
}

0 comments on commit 83bb7fd

Please sign in to comment.