math-optim/report/pic.go

92 lines
1.8 KiB
Go
Raw Normal View History

2022-07-12 22:31:29 +02:00
// Copyright 2022 wanderer <a_mirre at utb dot cz>
// SPDX-License-Identifier: GPL-3.0-or-later
package report
import (
2022-07-17 11:36:59 +02:00
_ "embed"
2022-07-12 22:31:29 +02:00
"log"
"os"
"sort"
2022-07-12 22:31:29 +02:00
"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
}
2022-07-17 11:36:59 +02:00
//go:embed pics.tmpl
var tmplPicsFile []byte
2022-07-12 22:31:29 +02:00
// 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 each pic list for all bench funcs of a specified algo
// to a file.
func SavePicsToFile(pls []PicList, algoName string) {
var paths []string
ptf := picTexFiles{Algo: algoName}
for _, p := range pls {
safeName := util.SanitiseFName(p.Algo + "-" + p.Bench)
texPicsFile := GetTexDir() + "pics-" + safeName + ".tex"
2022-07-17 11:36:59 +02:00
tmplPics := template.New("pics")
2022-07-17 11:36:59 +02:00
tmplPics = template.Must(tmplPics.Parse(string(tmplPicsFile)))
2022-07-12 22:31:29 +02:00
paths = append(paths, texPicsFile)
// keep the slice sorted.
sort.Strings(paths)
// make sure the output dir exists, else die early.
if err := util.CreatePath(GetTexDir()); err != nil {
log.Fatalln(err)
}
2022-07-12 22:31:29 +02:00
f, err := os.Create(texPicsFile)
if err != nil {
log.Println(err)
}
defer f.Close()
2022-07-12 22:31:29 +02:00
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(),
})
2022-07-12 22:31:29 +02:00
if err != nil {
log.Println(err)
}
2022-07-12 22:31:29 +02:00
}
ptf.FilePaths = paths
allPics.TexFiles = append(allPics.TexFiles, ptf)
2022-07-12 22:31:29 +02:00
}