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.Add(time.Second * 1)
minTime := account.LastUpdate
// Explicitly set max_time, to avoid issues with server-side caching
maxTime := time.Now()
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
for _, activity := range activities.AddGatewayV1 {
account.Tx.WithdrawalTotal += activity.StakingFee
updateLastActivityTime(activity.Time)
if activity.Time > lastActivityTime {
account.Tx.WithdrawalTotal += activity.StakingFee
updateLastActivityTime(activity.Time)
}
}
for _, activity := range activities.AssertLocationV1 {
account.Tx.WithdrawalTotal += activity.StakingFee
updateLastActivityTime(activity.Time)
if activity.Time > lastActivityTime {
account.Tx.WithdrawalTotal += activity.StakingFee
updateLastActivityTime(activity.Time)
}
}
for _, activity := range activities.AssertLocationV2 {
account.Tx.WithdrawalTotal += activity.StakingFee
updateLastActivityTime(activity.Time)
if activity.Time > lastActivityTime {
account.Tx.WithdrawalTotal += activity.StakingFee
updateLastActivityTime(activity.Time)
}
}
for _, activity := range activities.PaymentV1 {
if activity.Payer == account.Address {
account.Tx.WithdrawalTotal += activity.Amount
} else {
account.Tx.DepositTotal += activity.Amount
if activity.Time > lastActivityTime {
if activity.Payer == account.Address {
account.Tx.WithdrawalTotal += activity.Amount
} else {
account.Tx.DepositTotal += activity.Amount
}
updateLastActivityTime(activity.Time)
}
updateLastActivityTime(activity.Time)
}
for _, activity := range activities.PaymentV2 {
if activity.Payer == account.Address {
paymentTotal := 0
for _, payment := range activity.Payments {
paymentTotal += payment.Amount
}
account.Tx.WithdrawalTotal += paymentTotal
} else {
for _, payment := range activity.Payments {
if payment.Payee == account.Address {
account.Tx.DepositTotal += payment.Amount
if activity.Time > lastActivityTime {
if activity.Payer == account.Address {
paymentTotal := 0
for _, payment := range activity.Payments {
paymentTotal += payment.Amount
}
account.Tx.WithdrawalTotal += paymentTotal
} else {
for _, payment := range activity.Payments {
if payment.Payee == account.Address {
account.Tx.DepositTotal += payment.Amount
}
}
}
updateLastActivityTime(activity.Time)
}
updateLastActivityTime(activity.Time)
}
for _, activity := range activities.RewardsV1 {
for _, reward := range activity.Rewards {
account.RewardsTotal[reward.Type] += reward.Amount
account.Tx.DepositTotal += reward.Amount
if activity.Time > lastActivityTime {
for _, reward := range activity.Rewards {
account.RewardsTotal[reward.Type] += reward.Amount
account.Tx.DepositTotal += reward.Amount
}
updateLastActivityTime(activity.Time)
}
updateLastActivityTime(activity.Time)
}
for _, activity := range activities.RewardsV2 {
for _, reward := range activity.Rewards {
account.RewardsTotal[reward.Type] += reward.Amount
account.Tx.DepositTotal += reward.Amount
if activity.Time > lastActivityTime {
for _, reward := range activity.Rewards {
account.RewardsTotal[reward.Type] += reward.Amount
account.Tx.DepositTotal += reward.Amount
}
updateLastActivityTime(activity.Time)
}
updateLastActivityTime(activity.Time)
}
for _, activity := range activities.StakeValidatorV1 {
account.Tx.WithdrawalTotal += activity.Stake
updateLastActivityTime(activity.Time)
if activity.Time > lastActivityTime {
account.Tx.WithdrawalTotal += activity.Stake
updateLastActivityTime(activity.Time)
}
}
for _, activity := range activities.TokenBurnV1 {
account.Tx.WithdrawalTotal += activity.Amount
updateLastActivityTime(activity.Time)
if activity.Time > lastActivityTime {
account.Tx.WithdrawalTotal += activity.Amount
updateLastActivityTime(activity.Time)
}
}
for _, activity := range activities.TransferHotspotV1 {
if activity.Buyer == account.Address {
account.Tx.WithdrawalTotal += activity.AmountToSeller
} else {
account.Tx.DepositTotal += activity.AmountToSeller
if activity.Time > lastActivityTime {
if activity.Buyer == account.Address {
account.Tx.WithdrawalTotal += activity.AmountToSeller
} else {
account.Tx.DepositTotal += activity.AmountToSeller
}
updateLastActivityTime(activity.Time)
}
updateLastActivityTime(activity.Time)
}
for _, activity := range activities.UnstakeValidatorV1 {
account.Tx.WithdrawalTotal += activity.StakeAmount
updateLastActivityTime(activity.Time)
if activity.Time > lastActivityTime {
account.Tx.WithdrawalTotal += activity.StakeAmount
updateLastActivityTime(activity.Time)
}
}
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.Add(time.Second * 1)
minTime := hotspot.LastUpdate
// Explicitly set max_time, to avoid issues with server-side caching
maxTime := time.Now()
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 _, reward := range activity.Rewards {
hotspot.RewardsTotal[reward.Type] += reward.Amount
if activity.Time > lastActivityTime {
for _, reward := range activity.Rewards {
hotspot.RewardsTotal[reward.Type] += reward.Amount
}
updateLastActivityTime(activity.Time)
}
updateLastActivityTime(activity.Time)
}
for _, activity := range activities.RewardsV2 {
for _, reward := range activity.Rewards {
hotspot.RewardsTotal[reward.Type] += reward.Amount
if activity.Time > lastActivityTime {
for _, reward := range activity.Rewards {
hotspot.RewardsTotal[reward.Type] += reward.Amount
}
updateLastActivityTime(activity.Time)
}
updateLastActivityTime(activity.Time)
}
hotspot.LastUpdate = time.Unix(lastActivityTime, 0)

View File

@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
@ -124,7 +125,7 @@ func getHeliumApiWithCursor(path string, params *map[string]string) ([][]byte, e
defer resp.Body.Close()
// 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)
if err != nil {
return nil, fmt.Errorf("failed to read response body of %v: %v", req.URL.String(), err)