Skip to content

Commit

Permalink
Balance history fixes (day format instead of month) (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-myles authored Jul 18, 2024
1 parent b3c7b2f commit af4c5fe
Show file tree
Hide file tree
Showing 6 changed files with 1,226 additions and 74 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/ethereum/go-ethereum v1.14.7
github.com/goccy/go-json v0.10.3
github.com/hashicorp/go-multierror v1.1.1
github.com/ice-blockchain/eskimo v1.372.0
github.com/ice-blockchain/eskimo v1.373.0
github.com/ice-blockchain/go-tarantool-client v0.0.0-20230327200757-4fc71fa3f7bb
github.com/ice-blockchain/wintr v1.147.0
github.com/imroc/req/v3 v3.43.7
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO
github.com/huin/goupnp v1.3.0 h1:UvLUlWDNpoUdYzb2TCn+MuTWtcjXKSza2n6CBdQ0xXc=
github.com/huin/goupnp v1.3.0/go.mod h1:gnGPsThkYa7bFi/KWmEysQRf48l2dvR5bxr2OFckNX8=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ice-blockchain/eskimo v1.372.0 h1:T5JfwtVMn3ep/1NW98sUZ99nRKOSKHgEUT+dtQk/T/w=
github.com/ice-blockchain/eskimo v1.372.0/go.mod h1:etbbw8VRonltuR2fq4Rviput/FAItkatddbzj+eGxY8=
github.com/ice-blockchain/eskimo v1.373.0 h1:JhW7fWXQz1Ju/q/78l4RxCA1elA1z+k557FFmtKhvnI=
github.com/ice-blockchain/eskimo v1.373.0/go.mod h1:etbbw8VRonltuR2fq4Rviput/FAItkatddbzj+eGxY8=
github.com/ice-blockchain/go-tarantool-client v0.0.0-20230327200757-4fc71fa3f7bb h1:8TnFP3mc7O+tc44kv2e0/TpZKnEVUaKH+UstwfBwRkk=
github.com/ice-blockchain/go-tarantool-client v0.0.0-20230327200757-4fc71fa3f7bb/go.mod h1:ZsQU7i3mxhgBBu43Oev7WPFbIjP4TniN/b1UPNGbrq8=
github.com/ice-blockchain/wintr v1.147.0 h1:VQxvK3FWFIbm+X6obrecx7n07cBZoQDf/dQX8gfCcTc=
Expand Down
21 changes: 16 additions & 5 deletions tokenomics/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ func (r *repository) GetBalanceHistory( //nolint:funlen,gocognit,revive,gocyclo,
factor = 1
}
dates, notBeforeTime, notAfterTime := r.calculateDates(limit, offset, start, end, factor)
if len(dates) == 0 {
return []*BalanceHistoryEntry{}, nil
}
id, gErr := GetOrInitInternalID(ctx, r.db, userID)
if gErr != nil {
return nil, errors.Wrapf(gErr, "failed to getOrInitInternalID for userID:%v", userID)
Expand All @@ -115,14 +118,17 @@ func (r *repository) calculateDates(limit, offset uint64, start, end *time.Time,
} else {
beforeStartPadding = uint64(start.Add(stdlibtime.Duration(-calculatedLimit*uint64(stdlibtime.Minute))).Minute()) + 1
}
} else {
if offset > 0 { // Hack not to load extra records from FE.
return nil, nil, nil
}
}
mappedLimit := calculatedLimit + beforeStartPadding + afterStartPadding
if r.cfg.GlobalAggregationInterval.Child == stdlibtime.Minute {
mappedOffset = (offset / hoursInADay) * uint64(r.cfg.GlobalAggregationInterval.Parent/r.cfg.GlobalAggregationInterval.Child)
}
dates = make([]stdlibtime.Time, 0, mappedLimit)
firstDayOfStartMonth := stdlibtime.Date(start.Year(), start.Month(), 1, 0, 0, 0, 0, stdlibtime.UTC)
lastDayOfEndMonth := stdlibtime.Date(end.Year(), end.Month(), int(daysInMonth(end)), 0, 0, 0, 0, stdlibtime.UTC)

if factor > 0 {
if r.cfg.GlobalAggregationInterval.Child == stdlibtime.Minute {
for ix := stdlibtime.Duration(mappedOffset); ix < stdlibtime.Duration(mappedLimit+mappedOffset); ix++ {
Expand All @@ -133,8 +139,10 @@ func (r *repository) calculateDates(limit, offset uint64, start, end *time.Time,
} else {
notBeforeTime = start
notAfterTime = end
for ix := 0; ix <= int(lastDayOfEndMonth.Sub(firstDayOfStartMonth).Hours()/hoursInADay); ix++ {
dates = append(dates, firstDayOfStartMonth.Add(stdlibtime.Duration(ix)*hoursInADay*stdlibtime.Hour))
firstDayOfEndMonth := stdlibtime.Date(end.Year(), end.Month(), 1, 0, 0, 0, 0, stdlibtime.UTC)
lastDayOfStartMonth := stdlibtime.Date(start.Year(), start.Month(), int(daysInMonth(start)), 0, 0, 0, 0, stdlibtime.UTC)
for ix := 0; ix <= int(lastDayOfStartMonth.Sub(firstDayOfEndMonth).Hours()/hoursInADay); ix++ {
dates = append(dates, firstDayOfEndMonth.Add(stdlibtime.Duration(ix)*hoursInADay*stdlibtime.Hour))
}
}
} else {
Expand All @@ -147,6 +155,8 @@ func (r *repository) calculateDates(limit, offset uint64, start, end *time.Time,
} else {
notBeforeTime = end
notAfterTime = start
firstDayOfStartMonth := stdlibtime.Date(start.Year(), start.Month(), 1, 0, 0, 0, 0, stdlibtime.UTC)
lastDayOfEndMonth := stdlibtime.Date(end.Year(), end.Month(), int(daysInMonth(end)), 0, 0, 0, 0, stdlibtime.UTC)
for ix := 0; ix <= int(lastDayOfEndMonth.Sub(firstDayOfStartMonth).Hours()/hoursInADay); ix++ {
dates = append(dates, lastDayOfEndMonth.Add(-1*stdlibtime.Duration(ix)*hoursInADay*stdlibtime.Hour))
}
Expand Down Expand Up @@ -221,7 +231,8 @@ func (r *repository) processBalanceHistory(
parents[pKey].children[cKey].setBalanceDiffBonus(prevChild.Balance.amount)
}
parents[pKey].Balance.amount += parents[pKey].children[cKey].Balance.amount
if time.New(parents[pKey].children[cKey].Time).UnixNano() >= notBeforeTime.UnixNano() && time.New(parents[pKey].children[cKey].Time).UnixNano() <= notAfterTime.UnixNano() {
if time.New(parents[pKey].children[cKey].Time).UnixNano() >= notBeforeTime.Add(-stdlibtime.Duration(utcOffset.Seconds())*stdlibtime.Second).In(location).UnixNano() &&
time.New(parents[pKey].children[cKey].Time).UnixNano() <= notAfterTime.Add(-stdlibtime.Duration(utcOffset.Seconds())*stdlibtime.Second).In(location).UnixNano() {
parents[pKey].BalanceHistoryEntry.TimeSeries = append(parents[pKey].BalanceHistoryEntry.TimeSeries, parents[pKey].children[cKey])
prevChild = parents[pKey].children[cKey]
}
Expand Down
Loading

0 comments on commit af4c5fe

Please sign in to comment.