math-optim/report/pic.go
surtur 2d88ab9afc
All checks were successful
continuous-integration/drone/push Build is passing
go(report): add pic.go, pics.tmpl
2022-07-12 22:31:29 +02:00

72 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"
)
// Pic holds path to pic along with its caption.
type Pic struct {
Caption string
FilePath string
}
// PicList is a structure holding a slice of pics and {bench,algo} metadata.
type PicList struct {
Algo string
Bench string
Pics []Pic
}
// NewPic returns a new copy of Pic.
func NewPic() *Pic {
return &Pic{}
}
// NewPicList returns a new copy of PicList.
func NewPicList() *PicList {
return &PicList{}
}
// SavePicsToFile saves to file a pic list of a specified algo.
func SavePicsToFile(p PicList, algoName string) {
safeName := util.SanitiseFName(p.Algo + "-" + p.Bench)
texPicsFile := GetTexDir() + "pics-" + safeName + ".tex"
tmplPicsFile := "report/pics.tmpl"
tmplPics := template.Must(template.ParseFiles(tmplPicsFile))
// make sure the output dir exists, else die early.
if err := util.CreatePath(GetTexDir()); err != nil {
log.Fatalln(err)
}
f, err := os.Create(texPicsFile)
if err != nil {
log.Println(err)
}
defer f.Close()
err = tmplPics.Execute(f, struct {
Algo string
Bench string
Pics []Pic
Timestamp time.Time
}{
Algo: p.Algo,
Bench: p.Bench,
Pics: p.Pics,
Timestamp: time.Now(),
})
if err != nil {
log.Println(err)
}
}