// Copyright 2022 wanderer // SPDX-License-Identifier: GPL-3.0-or-later package report import ( "log" "os" "sort" "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 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" tmplPicsFile := "report/pics.tmpl" if _, err := os.Stat(tmplPicsFile); err != nil { // TODO(me): fix this, same as in table.go. // 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 "../"`) tmplPicsFile = "../" + tmplPicsFile } tmplPics := template.Must(template.ParseFiles(tmplPicsFile)) 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) } 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) } } ptf.FilePaths = paths allPics.TexFiles = append(allPics.TexFiles, ptf) }