1
0
Fork 0

fix reward value being added to counter multiple times
continuous-integration/drone/push Build was killed Details

An update to the heliumapi made minTime fuzzy, causing
an activity to be returned even if it occured before the
specified minTime.
This commit is contained in:
Massaki Archambault 2021-12-24 12:45:17 -05:00
parent dec12e1780
commit 9501dc9dc0
2 changed files with 77 additions and 52 deletions

View File

@ -529,8 +529,7 @@ func (e *Exporter) collectAccountTransactionsMetrics(wg *sync.WaitGroup, ch chan
} }
} }
// add 1 second to the last update time to avoid querying the same activity twice minTime := account.LastUpdate
minTime := account.LastUpdate.Add(time.Second * 1)
// Explicitly set max_time, to avoid issues with server-side caching // Explicitly set max_time, to avoid issues with server-side caching
maxTime := time.Now() maxTime := time.Now()
activities, err := heliumapi.GetActivityForAccount(account.Address, activityTypes, &minTime, &maxTime) activities, err := heliumapi.GetActivityForAccount(account.Address, activityTypes, &minTime, &maxTime)
@ -541,74 +540,96 @@ func (e *Exporter) collectAccountTransactionsMetrics(wg *sync.WaitGroup, ch chan
// impl based on https://github.com/helium/hotspot-app/blob/918563fba84d1abf4554a43a4d42bb838d017bd3/src/features/wallet/root/useActivityItem.tsx#L336 // impl based on https://github.com/helium/hotspot-app/blob/918563fba84d1abf4554a43a4d42bb838d017bd3/src/features/wallet/root/useActivityItem.tsx#L336
for _, activity := range activities.AddGatewayV1 { for _, activity := range activities.AddGatewayV1 {
account.Tx.WithdrawalTotal += activity.StakingFee if activity.Time > lastActivityTime {
updateLastActivityTime(activity.Time) account.Tx.WithdrawalTotal += activity.StakingFee
updateLastActivityTime(activity.Time)
}
} }
for _, activity := range activities.AssertLocationV1 { for _, activity := range activities.AssertLocationV1 {
account.Tx.WithdrawalTotal += activity.StakingFee if activity.Time > lastActivityTime {
updateLastActivityTime(activity.Time) account.Tx.WithdrawalTotal += activity.StakingFee
updateLastActivityTime(activity.Time)
}
} }
for _, activity := range activities.AssertLocationV2 { for _, activity := range activities.AssertLocationV2 {
account.Tx.WithdrawalTotal += activity.StakingFee if activity.Time > lastActivityTime {
updateLastActivityTime(activity.Time) account.Tx.WithdrawalTotal += activity.StakingFee
updateLastActivityTime(activity.Time)
}
} }
for _, activity := range activities.PaymentV1 { for _, activity := range activities.PaymentV1 {
if activity.Payer == account.Address { if activity.Time > lastActivityTime {
account.Tx.WithdrawalTotal += activity.Amount if activity.Payer == account.Address {
} else { account.Tx.WithdrawalTotal += activity.Amount
account.Tx.DepositTotal += activity.Amount } else {
account.Tx.DepositTotal += activity.Amount
}
updateLastActivityTime(activity.Time)
} }
updateLastActivityTime(activity.Time)
} }
for _, activity := range activities.PaymentV2 { for _, activity := range activities.PaymentV2 {
if activity.Payer == account.Address { if activity.Time > lastActivityTime {
paymentTotal := 0 if activity.Payer == account.Address {
for _, payment := range activity.Payments { paymentTotal := 0
paymentTotal += payment.Amount for _, payment := range activity.Payments {
} paymentTotal += payment.Amount
account.Tx.WithdrawalTotal += paymentTotal }
} else { account.Tx.WithdrawalTotal += paymentTotal
for _, payment := range activity.Payments { } else {
if payment.Payee == account.Address { for _, payment := range activity.Payments {
account.Tx.DepositTotal += payment.Amount if payment.Payee == account.Address {
account.Tx.DepositTotal += payment.Amount
}
} }
} }
updateLastActivityTime(activity.Time)
} }
updateLastActivityTime(activity.Time)
} }
for _, activity := range activities.RewardsV1 { for _, activity := range activities.RewardsV1 {
for _, reward := range activity.Rewards { if activity.Time > lastActivityTime {
account.RewardsTotal[reward.Type] += reward.Amount for _, reward := range activity.Rewards {
account.Tx.DepositTotal += reward.Amount account.RewardsTotal[reward.Type] += reward.Amount
account.Tx.DepositTotal += reward.Amount
}
updateLastActivityTime(activity.Time)
} }
updateLastActivityTime(activity.Time)
} }
for _, activity := range activities.RewardsV2 { for _, activity := range activities.RewardsV2 {
for _, reward := range activity.Rewards { if activity.Time > lastActivityTime {
account.RewardsTotal[reward.Type] += reward.Amount for _, reward := range activity.Rewards {
account.Tx.DepositTotal += reward.Amount account.RewardsTotal[reward.Type] += reward.Amount
account.Tx.DepositTotal += reward.Amount
}
updateLastActivityTime(activity.Time)
} }
updateLastActivityTime(activity.Time)
} }
for _, activity := range activities.StakeValidatorV1 { for _, activity := range activities.StakeValidatorV1 {
account.Tx.WithdrawalTotal += activity.Stake if activity.Time > lastActivityTime {
updateLastActivityTime(activity.Time) account.Tx.WithdrawalTotal += activity.Stake
updateLastActivityTime(activity.Time)
}
} }
for _, activity := range activities.TokenBurnV1 { for _, activity := range activities.TokenBurnV1 {
account.Tx.WithdrawalTotal += activity.Amount if activity.Time > lastActivityTime {
updateLastActivityTime(activity.Time) account.Tx.WithdrawalTotal += activity.Amount
updateLastActivityTime(activity.Time)
}
} }
for _, activity := range activities.TransferHotspotV1 { for _, activity := range activities.TransferHotspotV1 {
if activity.Buyer == account.Address { if activity.Time > lastActivityTime {
account.Tx.WithdrawalTotal += activity.AmountToSeller if activity.Buyer == account.Address {
} else { account.Tx.WithdrawalTotal += activity.AmountToSeller
account.Tx.DepositTotal += activity.AmountToSeller } else {
account.Tx.DepositTotal += activity.AmountToSeller
}
updateLastActivityTime(activity.Time)
} }
updateLastActivityTime(activity.Time)
} }
for _, activity := range activities.UnstakeValidatorV1 { for _, activity := range activities.UnstakeValidatorV1 {
account.Tx.WithdrawalTotal += activity.StakeAmount if activity.Time > lastActivityTime {
updateLastActivityTime(activity.Time) account.Tx.WithdrawalTotal += activity.StakeAmount
updateLastActivityTime(activity.Time)
}
} }
account.LastUpdate = time.Unix(lastActivityTime, 0) account.LastUpdate = time.Unix(lastActivityTime, 0)
@ -791,8 +812,7 @@ func (e *Exporter) collectHotspotRewardsMetrics(wg *sync.WaitGroup, ch chan<- pr
} }
} }
// add 1 second to the last update time to avoid querying the same activity twice minTime := hotspot.LastUpdate
minTime := hotspot.LastUpdate.Add(time.Second * 1)
// Explicitly set max_time, to avoid issues with server-side caching // Explicitly set max_time, to avoid issues with server-side caching
maxTime := time.Now() maxTime := time.Now()
activities, err := heliumapi.GetHotspotActivity(hotspot.Address, activityTypes, &minTime, &maxTime) activities, err := heliumapi.GetHotspotActivity(hotspot.Address, activityTypes, &minTime, &maxTime)
@ -802,16 +822,20 @@ func (e *Exporter) collectHotspotRewardsMetrics(wg *sync.WaitGroup, ch chan<- pr
} }
for _, activity := range activities.RewardsV1 { for _, activity := range activities.RewardsV1 {
for _, reward := range activity.Rewards { if activity.Time > lastActivityTime {
hotspot.RewardsTotal[reward.Type] += reward.Amount for _, reward := range activity.Rewards {
hotspot.RewardsTotal[reward.Type] += reward.Amount
}
updateLastActivityTime(activity.Time)
} }
updateLastActivityTime(activity.Time)
} }
for _, activity := range activities.RewardsV2 { for _, activity := range activities.RewardsV2 {
for _, reward := range activity.Rewards { if activity.Time > lastActivityTime {
hotspot.RewardsTotal[reward.Type] += reward.Amount for _, reward := range activity.Rewards {
hotspot.RewardsTotal[reward.Type] += reward.Amount
}
updateLastActivityTime(activity.Time)
} }
updateLastActivityTime(activity.Time)
} }
hotspot.LastUpdate = time.Unix(lastActivityTime, 0) hotspot.LastUpdate = time.Unix(lastActivityTime, 0)

View File

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"time" "time"
@ -124,7 +125,7 @@ func getHeliumApiWithCursor(path string, params *map[string]string) ([][]byte, e
defer resp.Body.Close() defer resp.Body.Close()
// read the response body and add it to the result array // read the response body and add it to the result array
// log.Printf("querying %v", req.URL.String()) log.Printf("querying %v", req.URL.String())
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to read response body of %v: %v", req.URL.String(), err) return nil, fmt.Errorf("failed to read response body of %v: %v", req.URL.String(), err)