1
0
Fork 0

refactor make api function pass only the data object instead of the whole response

This commit is contained in:
Massaki Archambault 2021-09-25 23:53:20 -04:00
parent 20eddb8fc3
commit 592fbea0e6
7 changed files with 37 additions and 36 deletions

View File

@ -347,7 +347,7 @@ func (e *Exporter) collectOracleMetrics(wg *sync.WaitGroup, ch chan<- prometheus
} }
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
oraclePrice.Desc, oraclePrice.Type, float64(currentOraclePrice.Data.Price)/100000000, oraclePrice.Desc, oraclePrice.Type, float64(currentOraclePrice.Price)/100000000,
) )
} }
@ -362,34 +362,34 @@ func (e *Exporter) collectStatsMetrics(wg *sync.WaitGroup, ch chan<- prometheus.
} }
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
statsValidators.Desc, statsValidators.Type, float64(blockchainStats.Data.Counts.Validators), statsValidators.Desc, statsValidators.Type, float64(blockchainStats.Counts.Validators),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
statsOuis.Desc, statsOuis.Type, float64(blockchainStats.Data.Counts.Ouis), statsOuis.Desc, statsOuis.Type, float64(blockchainStats.Counts.Ouis),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
statsHotspotsDataOnly.Desc, statsHotspotsDataOnly.Type, float64(blockchainStats.Data.Counts.HotspotsDataonly), statsHotspotsDataOnly.Desc, statsHotspotsDataOnly.Type, float64(blockchainStats.Counts.HotspotsDataonly),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
statsBlocks.Desc, statsBlocks.Type, float64(blockchainStats.Data.Counts.Blocks), statsBlocks.Desc, statsBlocks.Type, float64(blockchainStats.Counts.Blocks),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
statsChallenges.Desc, statsChallenges.Type, float64(blockchainStats.Data.Counts.Challenges), statsChallenges.Desc, statsChallenges.Type, float64(blockchainStats.Counts.Challenges),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
statsCities.Desc, statsCities.Type, float64(blockchainStats.Data.Counts.Cities), statsCities.Desc, statsCities.Type, float64(blockchainStats.Counts.Cities),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
statsConsensusGroups.Desc, statsConsensusGroups.Type, float64(blockchainStats.Data.Counts.ConsensusGroups), statsConsensusGroups.Desc, statsConsensusGroups.Type, float64(blockchainStats.Counts.ConsensusGroups),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
statsCountries.Desc, statsCountries.Type, float64(blockchainStats.Data.Counts.Countries), statsCountries.Desc, statsCountries.Type, float64(blockchainStats.Counts.Countries),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
statsHotspots.Desc, statsHotspots.Type, float64(blockchainStats.Data.Counts.Hotspots), statsHotspots.Desc, statsHotspots.Type, float64(blockchainStats.Counts.Hotspots),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
statsTokenSupply.Desc, statsTokenSupply.Type, blockchainStats.Data.TokenSupply, statsTokenSupply.Desc, statsTokenSupply.Type, blockchainStats.TokenSupply,
) )
} }
@ -404,7 +404,7 @@ func (e *Exporter) collectAccountMetrics(wg *sync.WaitGroup, ch chan<- prometheu
} }
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
accountBalanceHnt.Desc, accountBalanceHnt.Type, float64(accountForAddress.Data.Balance), accountBalanceHnt.Desc, accountBalanceHnt.Type, float64(accountForAddress.Balance),
account.Address, account.Address,
) )
} }
@ -419,7 +419,7 @@ func (e *Exporter) collectAccountActivityMetrics(wg *sync.WaitGroup, ch chan<- p
return return
} }
for accType, count := range accountActivityForAddress.Data { for accType, count := range *accountActivityForAddress {
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
accountActivity.Desc, accountActivity.Type, float64(count), accountActivity.Desc, accountActivity.Type, float64(count),
account.Address, accType, account.Address, accType,
@ -438,7 +438,7 @@ func (e *Exporter) collectAccountRewardsTotalMetrics(wg *sync.WaitGroup, ch chan
} }
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
accountRewardsHnt.Desc, accountRewardsHnt.Type, accountRewardTotalsForAddress.Data.Sum, accountRewardsHnt.Desc, accountRewardsHnt.Type, accountRewardTotalsForAddress.Sum,
account.Address, account.Address,
) )
} }
@ -459,7 +459,7 @@ func (e *Exporter) collectAccountTransactionsMetrics(wg *sync.WaitGroup, ch chan
return return
} }
// logic 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 account.Tx.WithdrawalTotal += activity.StakingFee
} }
@ -539,7 +539,7 @@ func (e *Exporter) collectHotspotMetrics(wg *sync.WaitGroup, ch chan<- prometheu
return return
} }
for _, hotspotData := range hotspotsForAddress.Data { for _, hotspotData := range *hotspotsForAddress {
// collect hotspot metric requiring extra queries in a new routine // collect hotspot metric requiring extra queries in a new routine
wg.Add(2) wg.Add(2)
go e.collectHotspotActivityMetrics(wg, ch, account, hotspotData) go e.collectHotspotActivityMetrics(wg, ch, account, hotspotData)
@ -582,7 +582,7 @@ func (e *Exporter) collectHotspotActivityMetrics(wg *sync.WaitGroup, ch chan<- p
return return
} }
for accType, count := range hotspotActivityForAddress.Data { for accType, count := range *hotspotActivityForAddress {
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
hotspotActivity.Desc, hotspotActivity.Type, float64(count), hotspotActivity.Desc, hotspotActivity.Type, float64(count),
account.Address, hotspotData.Address, hotspotData.Name, accType, account.Address, hotspotData.Address, hotspotData.Name, accType,
@ -601,7 +601,7 @@ func (e *Exporter) collectHotspotRewardsMetrics(wg *sync.WaitGroup, ch chan<- pr
} }
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
hotspotRewardsHnt.Desc, hotspotRewardsHnt.Type, hotspotRewardTotalsForAddress.Data.Sum, hotspotRewardsHnt.Desc, hotspotRewardsHnt.Type, hotspotRewardTotalsForAddress.Sum,
account.Address, hotspotData.Address, hotspotData.Name, account.Address, hotspotData.Address, hotspotData.Name,
) )
} }

View File

@ -8,7 +8,7 @@ import (
"github.com/helium-blockchain-exporter/heliumapi/activity" "github.com/helium-blockchain-exporter/heliumapi/activity"
) )
func GetAccountForAddress(account string) (*AccountResp, error) { func GetAccountForAddress(account string) (*Account, error) {
path := "/v1/accounts/" + account path := "/v1/accounts/" + account
// query the api // query the api
@ -24,7 +24,7 @@ func GetAccountForAddress(account string) (*AccountResp, error) {
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err) return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
} }
return &respobject, nil return &respobject.Data, nil
} }
func GetActivityForAccount(account string, filterTypes []string, minTime *time.Time, maxTime *time.Time) (*activity.Activities, error) { func GetActivityForAccount(account string, filterTypes []string, minTime *time.Time, maxTime *time.Time) (*activity.Activities, error) {
@ -57,7 +57,7 @@ func GetActivityForAccount(account string, filterTypes []string, minTime *time.T
return activity.NewActivities(combinedResp) return activity.NewActivities(combinedResp)
} }
func GetActivityCountsForAccount(account string) (*ActivityCountsResp, error) { func GetActivityCountsForAccount(account string) (*ActivityCounts, error) {
path := "/v1/accounts/" + account + "/activity/count" path := "/v1/accounts/" + account + "/activity/count"
// query the api // query the api
@ -73,10 +73,10 @@ func GetActivityCountsForAccount(account string) (*ActivityCountsResp, error) {
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err) return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
} }
return &respobject, nil return &respobject.Data, nil
} }
func GetRewardTotalsForAccount(account string, minTime *time.Time, maxTime *time.Time) (*RewardTotalResp, error) { func GetRewardTotalsForAccount(account string, minTime *time.Time, maxTime *time.Time) (*RewardTotal, error) {
path := "/v1/accounts/" + account + "/rewards/sum" path := "/v1/accounts/" + account + "/rewards/sum"
params := map[string]string{} params := map[string]string{}
if minTime != nil { if minTime != nil {
@ -99,10 +99,10 @@ func GetRewardTotalsForAccount(account string, minTime *time.Time, maxTime *time
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err) return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
} }
return &respobject, nil return &respobject.Data, nil
} }
func GetHotspotsForAccount(account string) (*AccountHotspotsResp, error) { func GetHotspotsForAccount(account string) (*[]AccountHotspot, error) {
path := "/v1/accounts/" + account + "/hotspots" path := "/v1/accounts/" + account + "/hotspots"
// query the api // query the api
@ -118,5 +118,5 @@ func GetHotspotsForAccount(account string) (*AccountHotspotsResp, error) {
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err) return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
} }
return &respobject, nil return &respobject.Data, nil
} }

View File

@ -21,7 +21,6 @@ func NewActivities(resp ActivityResp) (*Activities, error) {
if err := json.Unmarshal(activityRaw, &activityType); err != nil { if err := json.Unmarshal(activityRaw, &activityType); err != nil {
return nil, fmt.Errorf("failed to unmarshal activity: %v", err) return nil, fmt.Errorf("failed to unmarshal activity: %v", err)
} }
fmt.Println(activityType.Type)
switch activityType.Type { switch activityType.Type {
case "add_gateway_v1": case "add_gateway_v1":
addGatewayV1 := AddGatewayV1{} addGatewayV1 := AddGatewayV1{}

View File

@ -6,7 +6,7 @@ import (
"time" "time"
) )
func GetHotspotActivityCount(hotspot string) (*ActivityCountsResp, error) { func GetHotspotActivityCount(hotspot string) (*ActivityCounts, error) {
path := "/v1/hotspots/" + hotspot + "/activity/count" path := "/v1/hotspots/" + hotspot + "/activity/count"
// query the api // query the api
@ -22,10 +22,10 @@ func GetHotspotActivityCount(hotspot string) (*ActivityCountsResp, error) {
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err) return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
} }
return &respobject, nil return &respobject.Data, nil
} }
func GetRewardsTotalForHotspot(hotspot string, minTime *time.Time, maxTime *time.Time) (*RewardTotalResp, error) { func GetRewardsTotalForHotspot(hotspot string, minTime *time.Time, maxTime *time.Time) (*RewardTotal, error) {
path := "/v1/hotspots/" + hotspot + "/rewards/sum" path := "/v1/hotspots/" + hotspot + "/rewards/sum"
params := map[string]string{} params := map[string]string{}
if minTime != nil { if minTime != nil {
@ -48,5 +48,5 @@ func GetRewardsTotalForHotspot(hotspot string, minTime *time.Time, maxTime *time
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err) return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
} }
return &respobject, nil return &respobject.Data, nil
} }

View File

@ -5,7 +5,7 @@ import (
"fmt" "fmt"
) )
func GetCurrentOraclePrice() (*CurrentOraclePriceResp, error) { func GetCurrentOraclePrice() (*CurrentOraclePrice, error) {
const path = "/v1/oracle/prices/current" const path = "/v1/oracle/prices/current"
// query the api // query the api
@ -21,5 +21,5 @@ func GetCurrentOraclePrice() (*CurrentOraclePriceResp, error) {
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err) return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
} }
return &respobject, nil return &respobject.Data, nil
} }

View File

@ -5,7 +5,7 @@ import (
"fmt" "fmt"
) )
func GetBlockchainStats() (*BlockchainStatsResp, error) { func GetBlockchainStats() (*BlockchainStats, error) {
const path = "/v1/stats" const path = "/v1/stats"
// query the api // query the api
@ -21,5 +21,5 @@ func GetBlockchainStats() (*BlockchainStatsResp, error) {
return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err) return nil, fmt.Errorf("failed to unmarshal response from path %v: %v", path, err)
} }
return &respobject, nil return &respobject.Data, nil
} }

View File

@ -58,9 +58,11 @@ type AccountHotspot struct {
} }
type ActivityCountsResp struct { type ActivityCountsResp struct {
Data map[string]int Data ActivityCounts
} }
type ActivityCounts map[string]int
type RewardTotalResp struct { type RewardTotalResp struct {
Meta struct { Meta struct {
MinTime string `json:"min_time"` MinTime string `json:"min_time"`