93 lines
2.5 KiB
Go
93 lines
2.5 KiB
Go
package ledger
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"slices"
|
|
"time"
|
|
)
|
|
|
|
func RegisterWithContributionPrediction(reg map[string]Balances, windowAsPercentOfTotalDuration float64) (map[string]Balances, error) {
|
|
times, err := registerTimesInUnix(reg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
datesToPredict := func() []string {
|
|
result := make([]string, 0)
|
|
windowDuration := time.Second * time.Duration(float64(slices.Max(times)-slices.Min(times))*windowAsPercentOfTotalDuration)
|
|
|
|
lastReal := time.Unix(slices.Max(times), 0)
|
|
lastPredicted := lastReal
|
|
for lastPredicted.Before(lastReal.Add(windowDuration)) {
|
|
lastPredicted = lastPredicted.Add(time.Hour * 24 * time.Duration((45 - lastPredicted.Day())))
|
|
result = append(result, lastPredicted.Format("2006-01"))
|
|
}
|
|
return result
|
|
}()
|
|
log.Print(datesToPredict)
|
|
|
|
namesDatesBalance := func() map[string]map[string]Balance {
|
|
result := make(map[string]map[string]Balance)
|
|
for date, balances := range reg {
|
|
for name, balance := range balances {
|
|
if _, ok := result[name]; !ok {
|
|
result[name] = make(map[string]Balance)
|
|
}
|
|
result[name][date] = balance
|
|
}
|
|
}
|
|
return result
|
|
}()
|
|
|
|
result := make(map[string]Balances)
|
|
for name, datesBalance := range namesDatesBalance {
|
|
result[name] = func() Balances {
|
|
dates := func() []string {
|
|
result := make([]string, 0)
|
|
for k := range datesBalance {
|
|
result = append(result, k)
|
|
}
|
|
return result
|
|
}()
|
|
slices.Sort(dates)
|
|
half := dates[len(dates)/2]
|
|
threeQuarter := dates[int(3.0*(len(dates)/4.0))]
|
|
sevenEighths := dates[int(7.0*(len(dates)/8.0))]
|
|
sum:= func(start, stop string) balance
|
|
panic(fmt.Sprintf("%s: %s .. %s .. %s (%v)", name, half, threeQuarter, sevenEighths, dates))
|
|
}()
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func RegisterWithCompoundingInterestPrediction(reg map[string]Balances, windowAsPercentOfTotalDuration float64, name string, rate float64) (map[string]Balances, error) {
|
|
result := make(map[string]Balances)
|
|
return result, io.EOF
|
|
}
|
|
|
|
func registerTimesInUnix(reg map[string]Balances) ([]int64, error) {
|
|
result := make([]int64, 0, len(reg))
|
|
for k := range reg {
|
|
v, err := registerTime(k)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result = append(result, v.Unix())
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func registerTime(s string) (time.Time, error) {
|
|
for _, layout := range []string{
|
|
"2006-01-02",
|
|
"2006-01",
|
|
} {
|
|
if t, err := time.ParseInLocation(layout, s, time.Local); err == nil {
|
|
return t, err
|
|
}
|
|
}
|
|
return time.Time{}, fmt.Errorf("no layout matching %q", s)
|
|
}
|