feat: company names in fixed order
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Marvin Preuss 2023-04-14 08:14:17 +00:00
parent 6b69f825bf
commit ca06b84106
2 changed files with 68 additions and 8 deletions

View File

@ -5,6 +5,7 @@ import (
"math" "math"
"os" "os"
"regexp" "regexp"
"sort"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -29,7 +30,9 @@ type Value struct {
Value float64 Value float64
} }
type Categories map[string][]Value type Values []Value
type Categories map[string]Values
func (c Categories) Insert(company, value string) error { func (c Categories) Insert(company, value string) error {
fValue, err := strconv.ParseFloat(value, 32) fValue, err := strconv.ParseFloat(value, 32)
@ -133,19 +136,16 @@ func Create(out, dir string) error {
return fmt.Errorf("failed to create new sheet: %w", err) return fmt.Errorf("failed to create new sheet: %w", err)
} }
m := map[string]float64{} item = CalcAndSort(item)
for _, i := range item {
m[i.Company] += i.Value
}
valueLength := 0 valueLength := 0
for k, v := range m { for _, i := range item {
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), i.Company); err != nil {
return fmt.Errorf("failed to set cell: %w", err) return fmt.Errorf("failed to set cell: %w", err)
} }
if err := f.SetCellFloat(cat, fmt.Sprintf("B%d", valueLength+1), v, 2, 64); err != nil { if err := f.SetCellFloat(cat, fmt.Sprintf("B%d", valueLength+1), i.Value, 2, 64); err != nil {
return fmt.Errorf("failed to set cell: %w", err) return fmt.Errorf("failed to set cell: %w", err)
} }
@ -220,3 +220,32 @@ func createCategoryChart(category string, length int, f *excelize.File) error {
return nil return nil
} }
func CalcAndSort(v Values) Values {
n := Values{}
for _, i := range v {
added := false
for k, v := range n {
if i.Company == v.Company {
n[k].Value += i.Value
added = true
break
}
}
if !added {
n = append(n, i)
}
}
// Sorting for company names.
sort.SliceStable(n, func(i, j int) bool {
return n[i].Company < n[j].Company
})
return n
}

View File

@ -124,3 +124,34 @@ 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 TestCalcAndSort(t *testing.T) {
tables := []struct {
name string
input dirtoexcel.Values
expected dirtoexcel.Values
}{
{
"00",
dirtoexcel.Values{
dirtoexcel.Value{Company: "BAR", Value: 3},
dirtoexcel.Value{Company: "ALDI", Value: 1},
dirtoexcel.Value{Company: "ALDI", Value: 2},
},
dirtoexcel.Values{
dirtoexcel.Value{Company: "ALDI", Value: 3},
dirtoexcel.Value{Company: "BAR", Value: 3},
},
},
}
is := is.New(t)
for _, tt := range tables {
tt := tt
t.Run(tt.name, func(t *testing.T) {
is.Equal(dirtoexcel.CalcAndSort(tt.input), tt.expected)
})
}
}