math-optim/report/table.go
surtur 621f56f5e3
All checks were successful
continuous-integration/drone/push Build is passing
go(stats): rework table saving,structure
* create tables as defined in the report pkg.
* add new funcs to calculate stats and prepare the necessary table
  structs.
* rework existing `stats` tests
* handle a weird test behaviour when tmpl at `report/table.tmpl` is not
  found when testing a `stats` func.
2022-07-13 17:35:18 +02:00

76 lines
1.4 KiB
Go

// Copyright 2022 wanderer <a_mirre at utb dot cz>
// SPDX-License-Identifier: GPL-3.0-or-later
package report
import (
"log"
"os"
"text/template"
"time"
"git.dotya.ml/wanderer/math-optim/util"
)
type Table struct {
Algo string
Header []string
ColLayout []string
Rows []Row
}
type Row struct {
// Title of the row should contain the settings used to get the Values.
Title string
Values []float64
}
func NewTable() *Table {
return &Table{}
}
func NewRow() *Row {
return &Row{}
}
func SaveTableToFile(t Table) {
safeName := util.SanitiseFName(t.Algo)
texTableFile := GetTexDir() + "table-" + safeName + ".tex"
tmplTableFile := "report/table.tmpl"
if _, err := os.Stat(tmplTableFile); err != nil {
// TODO(me): fix this.
// this block is relevant for the unit test path, somehow the file is
// not found as defined above.
log.Println(err, `, weird test behaviour , prepending "../"`)
tmplTableFile = "../" + tmplTableFile
}
tmplTable := template.Must(template.ParseFiles(tmplTableFile))
f, err := os.Create(texTableFile)
if err != nil {
log.Println(err)
}
defer f.Close()
err = tmplTable.Execute(f, struct {
Algo string
ColLayout []string
ColNames []string
Rs []Row
Timestamp time.Time
}{
Algo: t.Algo,
ColNames: t.Header,
ColLayout: t.ColLayout,
Rs: t.Rows,
Timestamp: time.Now(),
})
if err != nil {
log.Println(err)
}
}