broken
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
Marvin Preuss 2023-04-13 11:33:03 +00:00
parent 9bf0b05ec6
commit 8eea6b08ab
2 changed files with 60 additions and 7 deletions

View File

@ -5,6 +5,7 @@ import (
"math" "math"
"os" "os"
"regexp" "regexp"
"sort"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -22,7 +23,7 @@ func init() {
}.NewHandler(os.Stderr))) }.NewHandler(os.Stderr)))
} }
const re = `^(?P<company>[a-zA-Z0-9-]+)(?:_[a-zA-Z]+)?_(?P<value>\d+\.\d{1,2})` const re = `^(?P<company>[a-zA-Z0-9-\s]+)(?:_[a-zA-Z0-9]+)?_(?P<value>\d+\.\d{1,2})`
type Value struct { type Value struct {
Company string Company string
@ -102,6 +103,8 @@ func Create(out, dir string) error {
res := r.FindAllStringSubmatch(i.Name(), -1) res := r.FindAllStringSubmatch(i.Name(), -1)
if res == nil { if res == nil {
log.Error("couldnt find match", "file", i.Name())
return fmt.Errorf("nothing found") return fmt.Errorf("nothing found")
} }
@ -136,9 +139,12 @@ func Create(out, dir string) error {
m[i.Company] += i.Value m[i.Company] += i.Value
} }
sm := SortMap(m)
valueLength := 0 valueLength := 0
for k, v := range m { for k, v := range sm {
log.Info("KEY", "key", k, "category", cat)
if err := f.SetCellStr(cat, fmt.Sprintf("A%d", valueLength+1), k); err != nil { if err := f.SetCellStr(cat, fmt.Sprintf("A%d", valueLength+1), k); err != nil {
return fmt.Errorf("failed to set cell: %w", err) return fmt.Errorf("failed to set cell: %w", err)
} }
@ -218,3 +224,21 @@ func createCategoryChart(category string, length int, f *excelize.File) error {
return nil return nil
} }
func SortMap[V any](m map[string]V) map[string]V {
r := map[string]V{}
keys := make([]string, 0, len(m))
for key := range m {
keys = append(keys, key)
}
sort.Strings(keys)
for _, k := range keys {
r[k] = m[k]
}
return r
}

View File

@ -30,6 +30,7 @@ func TestCreate(t *testing.T) {
"aldi_10.98.jpg", "aldi_10.98.jpg",
"aldi_10.00.jpg", "aldi_10.00.jpg",
"edeka_10.11.jpg", "edeka_10.11.jpg",
"casa espana_20221216_62.19.jpg",
}, },
map[string][]want{ map[string][]want{
"Food": { "Food": {
@ -53,6 +54,10 @@ func TestCreate(t *testing.T) {
"BMA-ZON", "BMA-ZON",
"12.34", "12.34",
}, },
{
"CASA ESPANA",
"62.19",
},
}, },
}, },
}, },
@ -78,11 +83,11 @@ func TestCreate(t *testing.T) {
// NOTE: Uncomment this to view excel file. // NOTE: Uncomment this to view excel file.
// //
// eFile, err := os.ReadFile(outfile) eFile, err := os.ReadFile(outfile)
// is.NoErr(err) is.NoErr(err)
//
// err = os.WriteFile(path.Join("/home/marv/wip/amseltools", "foo.xlsx"), eFile, 0o644) err = os.WriteFile(path.Join("/home/marv/wip/amseltools", "foo.xlsx"), eFile, 0o644)
// is.NoErr(err) is.NoErr(err)
// Read the excel. // Read the excel.
f, err := excelize.OpenFile(outfile) f, err := excelize.OpenFile(outfile)
@ -119,3 +124,27 @@ func TestCategoriesInsert(t *testing.T) {
is.Equal(dirtoexcel.Categories{"NonFood": []dirtoexcel.Value{{"AMAZON", 11.11}}}, m) is.Equal(dirtoexcel.Categories{"NonFood": []dirtoexcel.Value{{"AMAZON", 11.11}}}, m)
} }
func TestSortMap(t *testing.T) {
tables := []struct {
name string
input map[string]int
expected map[string]int
}{
{
"00",
map[string]int{"FOO": 1, "BAR": 2, "ZONK": 3},
map[string]int{"BAR": 2, "FOO": 1, "ZONK": 3},
},
}
is := is.New(t)
for _, tt := range tables {
tt := tt
t.Run(tt.name, func(t *testing.T) {
is.Equal(dirtoexcel.SortMap(tt.input), tt.expected)
})
}
}