2021-09-12 14:17:31 +00:00
package main
2021-09-13 03:25:47 +00:00
import (
2021-09-14 03:36:48 +00:00
"flag"
2021-09-13 03:25:47 +00:00
"fmt"
"log"
"net/http"
2021-09-15 05:04:32 +00:00
"strconv"
2021-09-14 03:36:48 +00:00
"strings"
2021-09-15 03:34:08 +00:00
"time"
2021-09-13 03:25:47 +00:00
"github.com/helium-blockchain-exporter/heliumapi"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// metricInfo is a metric exported by the helium blockchain exporter
type metricInfo struct {
Desc * prometheus . Desc
Type prometheus . ValueType
}
// Exporter collect metrics from the helium blockchain api and exports them as prometheus metrics.
type Exporter struct {
2021-09-25 01:30:22 +00:00
Accounts [ ] Account
StartTime time . Time
}
// NewExporter returns an initialized Exporter
func NewExporter ( accountAddress [ ] string ) ( * Exporter , error ) {
accounts := make ( [ ] Account , 0 )
for _ , accountAddress := range accountAddress {
if accountAddress != "" {
accounts = append ( accounts , NewAccount ( accountAddress ) )
}
}
return & Exporter {
Accounts : accounts ,
StartTime : time . Now ( ) ,
} , nil
2021-09-15 03:34:08 +00:00
}
// Account represents a helium account
type Account struct {
2021-09-25 01:30:22 +00:00
Address string
Tx AccountTx
}
func NewAccount ( address string ) Account {
return Account {
Address : address ,
Tx : AccountTx {
DepositTotal : 0 ,
WithdrawalTotal : 0 ,
LastUpdate : time . Now ( ) ,
} ,
}
2021-09-15 03:34:08 +00:00
}
2021-09-25 01:30:22 +00:00
type AccountTx struct {
DepositTotal int
WithdrawalTotal int
LastUpdate time . Time
2021-09-13 03:25:47 +00:00
}
const (
namespace = "helium"
)
var (
2021-09-25 01:30:22 +00:00
// labels
2021-09-14 03:36:48 +00:00
commonAccountLabels = [ ] string { "account" }
2021-09-14 03:51:47 +00:00
commonHotspotLabels = append ( commonAccountLabels , "hotspot" , "hotspot_name" )
2021-09-14 03:36:48 +00:00
2021-09-13 03:25:47 +00:00
// exporter metrics
// helium oracle metrics
oraclePrice = metricInfo {
prometheus . NewDesc (
prometheus . BuildFQName ( namespace , "oracle" , "price_usd" ) ,
"The oracle price of an HNT token in USD." ,
nil , nil ,
) ,
prometheus . GaugeValue ,
}
// helium stats metrics
statsValidators = metricInfo {
prometheus . NewDesc (
2021-09-25 01:30:22 +00:00
prometheus . BuildFQName ( namespace , "stats" , "validators" ) ,
2021-09-15 03:34:08 +00:00
"The total number of validators." ,
2021-09-13 03:25:47 +00:00
nil , nil ,
) ,
2021-09-25 01:30:22 +00:00
prometheus . GaugeValue ,
2021-09-13 03:25:47 +00:00
}
statsOuis = metricInfo {
prometheus . NewDesc (
2021-09-25 01:30:22 +00:00
prometheus . BuildFQName ( namespace , "stats" , "ouis" ) ,
2021-09-15 03:34:08 +00:00
"The total number of organization unique identifiers." ,
2021-09-13 03:25:47 +00:00
nil , nil ,
) ,
2021-09-25 01:30:22 +00:00
prometheus . GaugeValue ,
2021-09-13 03:25:47 +00:00
}
statsHotspotsDataOnly = metricInfo {
prometheus . NewDesc (
2021-09-25 01:30:22 +00:00
prometheus . BuildFQName ( namespace , "stats" , "hotspots_dataonly" ) ,
2021-09-15 03:34:08 +00:00
"The total number of data only hotspots." ,
2021-09-13 03:25:47 +00:00
nil , nil ,
) ,
2021-09-25 01:30:22 +00:00
prometheus . GaugeValue ,
2021-09-13 03:25:47 +00:00
}
statsBlocks = metricInfo {
prometheus . NewDesc (
2021-09-25 01:30:22 +00:00
prometheus . BuildFQName ( namespace , "stats" , "blocks" ) ,
2021-09-15 03:34:08 +00:00
"The total height/number of blocks in the blockchain." ,
2021-09-13 03:25:47 +00:00
nil , nil ,
) ,
2021-09-25 01:30:22 +00:00
prometheus . GaugeValue ,
2021-09-13 03:25:47 +00:00
}
statsChallenges = metricInfo {
prometheus . NewDesc (
2021-09-25 01:30:22 +00:00
prometheus . BuildFQName ( namespace , "stats" , "challenges" ) ,
2021-09-15 03:34:08 +00:00
"The total number of challenges." ,
2021-09-13 03:25:47 +00:00
nil , nil ,
) ,
2021-09-25 01:30:22 +00:00
prometheus . GaugeValue ,
2021-09-13 03:25:47 +00:00
}
statsCities = metricInfo {
prometheus . NewDesc (
prometheus . BuildFQName ( namespace , "stats" , "cities" ) ,
"The number of cities with at least one helium hotspot." ,
nil , nil ,
) ,
prometheus . GaugeValue ,
}
statsConsensusGroups = metricInfo {
prometheus . NewDesc (
2021-09-25 01:30:22 +00:00
prometheus . BuildFQName ( namespace , "stats" , "consensus_groups" ) ,
2021-09-15 03:34:08 +00:00
"The total number of consensus groups." ,
2021-09-13 03:25:47 +00:00
nil , nil ,
) ,
prometheus . GaugeValue ,
}
statsCountries = metricInfo {
prometheus . NewDesc (
prometheus . BuildFQName ( namespace , "stats" , "countries" ) ,
"The number of countries with at least on helium hotspot." ,
nil , nil ,
) ,
prometheus . GaugeValue ,
}
statsHotspots = metricInfo {
prometheus . NewDesc (
2021-09-25 01:30:22 +00:00
prometheus . BuildFQName ( namespace , "stats" , "hotspots" ) ,
2021-09-15 03:34:08 +00:00
"The total number of hotspots." ,
2021-09-13 03:25:47 +00:00
nil , nil ,
) ,
2021-09-25 01:30:22 +00:00
prometheus . GaugeValue ,
2021-09-13 03:25:47 +00:00
}
statsTokenSupply = metricInfo {
prometheus . NewDesc (
2021-09-25 01:30:22 +00:00
prometheus . BuildFQName ( namespace , "stats" , "token" ) ,
2021-09-13 03:25:47 +00:00
"The total supply of HNT tokens in circulation." ,
nil , nil ,
) ,
prometheus . GaugeValue ,
}
2021-09-14 03:36:48 +00:00
// helium account metrics
accountBalanceHnt = metricInfo {
prometheus . NewDesc (
prometheus . BuildFQName ( namespace , "account" , "balance_hnt" ) ,
2021-09-15 05:04:32 +00:00
"The number of HNT token owned by an account." ,
2021-09-14 03:36:48 +00:00
commonAccountLabels , nil ,
) ,
prometheus . GaugeValue ,
}
accountActivity = metricInfo {
prometheus . NewDesc (
prometheus . BuildFQName ( namespace , "account" , "activity_total" ) ,
2021-09-14 03:51:47 +00:00
"The total number of time an activity occurred in an account." ,
2021-09-14 03:36:48 +00:00
append ( commonAccountLabels , "type" ) , nil ,
) ,
prometheus . CounterValue ,
}
accountRewardsHnt = metricInfo {
prometheus . NewDesc (
2021-09-17 20:45:01 +00:00
prometheus . BuildFQName ( namespace , "account" , "rewards_hnt_total" ) ,
2021-09-15 05:04:32 +00:00
"The number of HNT token rewarded to an account." ,
2021-09-14 03:36:48 +00:00
commonAccountLabels , nil ,
) ,
2021-09-17 20:45:01 +00:00
prometheus . CounterValue ,
2021-09-14 03:36:48 +00:00
}
2021-09-25 01:30:22 +00:00
accountDepositsHnt = metricInfo {
prometheus . NewDesc (
prometheus . BuildFQName ( namespace , "account" , "deposits_hnt_total" ) ,
"The number of HNT tokens deposited to this account." ,
commonAccountLabels , nil ,
) ,
prometheus . CounterValue ,
}
accountWithdrawalsHnt = metricInfo {
prometheus . NewDesc (
prometheus . BuildFQName ( namespace , "account" , "withdrawals_hnt_total" ) ,
"The number of HNT tokens withdrawn from this account." ,
commonAccountLabels , nil ,
) ,
prometheus . CounterValue ,
}
2021-09-13 03:25:47 +00:00
2021-09-15 05:04:32 +00:00
// helium hotspot metrics
hotspotUp = metricInfo {
prometheus . NewDesc (
prometheus . BuildFQName ( namespace , "hotspot" , "up" ) ,
"Whether a hotspot is online." ,
commonHotspotLabels , nil ,
) ,
prometheus . GaugeValue ,
}
2021-09-17 20:45:01 +00:00
hotspotRelayed = metricInfo {
2021-09-15 05:04:32 +00:00
prometheus . NewDesc (
2021-09-17 20:45:01 +00:00
prometheus . BuildFQName ( namespace , "hotspot" , "relayed" ) ,
"Whether a hotspot is relayed." ,
2021-09-15 05:04:32 +00:00
commonHotspotLabels , nil ,
) ,
prometheus . GaugeValue ,
}
2021-09-17 20:45:01 +00:00
hotspotBlocks = metricInfo {
2021-09-15 05:04:32 +00:00
prometheus . NewDesc (
2021-09-17 20:45:01 +00:00
prometheus . BuildFQName ( namespace , "hotspot" , "blocks_total" ) ,
"The block height of a hotspot. Check on the hotspot itself for the most recent data." ,
2021-09-15 05:04:32 +00:00
commonHotspotLabels , nil ,
) ,
prometheus . CounterValue ,
}
2021-09-17 20:45:01 +00:00
hotspotRewardsScale = metricInfo {
2021-09-15 05:04:32 +00:00
prometheus . NewDesc (
2021-09-17 20:45:01 +00:00
prometheus . BuildFQName ( namespace , "hotspot" , "rewards_scale" ) ,
2021-09-15 05:04:32 +00:00
"The reward scale of a hotspot." ,
commonHotspotLabels , nil ,
) ,
prometheus . GaugeValue ,
}
hotspotGeocodeInfo = metricInfo {
prometheus . NewDesc (
prometheus . BuildFQName ( namespace , "hotspot" , "geocode_info" ) ,
"Information on the location of a hotspot." ,
append ( commonHotspotLabels , "lng" , "lat" , "street" , "state" , "country" , "city" ) , nil ,
) ,
prometheus . GaugeValue ,
}
hotspotAntennaInfo = metricInfo {
prometheus . NewDesc (
prometheus . BuildFQName ( namespace , "hotspot" , "antenna_info" ) ,
2021-09-17 20:45:01 +00:00
"Information on the antenna of a hotspot." ,
2021-09-15 05:04:32 +00:00
append ( commonHotspotLabels , "gain" , "elevation" ) , nil ,
) ,
prometheus . GaugeValue ,
}
hotspotActivity = metricInfo {
prometheus . NewDesc (
prometheus . BuildFQName ( namespace , "hotspot" , "activity_total" ) ,
"The total number of time an activity occurred in a hotspot." ,
append ( commonHotspotLabels , "type" ) , nil ,
) ,
prometheus . CounterValue ,
}
hotspotRewardsHnt = metricInfo {
prometheus . NewDesc (
2021-09-17 20:45:01 +00:00
prometheus . BuildFQName ( namespace , "hotspot" , "rewards_hnt_total" ) ,
2021-09-15 05:04:32 +00:00
"The number of HNT token rewarded to a hotspot." ,
commonHotspotLabels , nil ,
) ,
2021-09-17 20:45:01 +00:00
prometheus . CounterValue ,
2021-09-15 05:04:32 +00:00
}
2021-09-13 03:25:47 +00:00
)
2021-09-15 05:04:32 +00:00
func bool2Float64 ( b bool ) float64 {
if b {
return 1.0
}
return 0.0
}
2021-09-13 03:25:47 +00:00
// Describe describes all the metrics ever exported by the helium blockchain exporter.
// implements prometheus.Collector.
func ( e * Exporter ) Describe ( ch chan <- * prometheus . Desc ) {
ch <- oraclePrice . Desc
ch <- statsValidators . Desc
ch <- statsOuis . Desc
ch <- statsHotspotsDataOnly . Desc
ch <- statsBlocks . Desc
ch <- statsChallenges . Desc
ch <- statsCities . Desc
ch <- statsConsensusGroups . Desc
ch <- statsCountries . Desc
ch <- statsHotspots . Desc
ch <- statsTokenSupply . Desc
2021-09-14 03:36:48 +00:00
ch <- accountBalanceHnt . Desc
ch <- accountActivity . Desc
ch <- accountRewardsHnt . Desc
2021-09-25 01:30:22 +00:00
ch <- accountDepositsHnt . Desc
ch <- accountWithdrawalsHnt . Desc
2021-09-15 05:04:32 +00:00
ch <- hotspotUp . Desc
2021-09-17 20:45:01 +00:00
ch <- hotspotRelayed . Desc
ch <- hotspotBlocks . Desc
ch <- hotspotRewardsScale . Desc
2021-09-15 05:04:32 +00:00
ch <- hotspotGeocodeInfo . Desc
ch <- hotspotAntennaInfo . Desc
ch <- hotspotActivity . Desc
ch <- hotspotRewardsHnt . Desc
2021-09-13 03:25:47 +00:00
}
// Collect fetches the data from the helium blockchain api and delivers them as Prometheus metrics.
// implements prometheus.Collector.
func ( e * Exporter ) Collect ( ch chan <- prometheus . Metric ) {
e . collectOracleMetrics ( ch )
e . collectStatsMetrics ( ch )
2021-09-17 20:45:01 +00:00
for i := range e . Accounts {
e . collectAccountMetrics ( ch , & e . Accounts [ i ] )
e . collectHotspotMetrics ( ch , & e . Accounts [ i ] )
2021-09-14 03:36:48 +00:00
}
2021-09-13 03:25:47 +00:00
}
2021-09-14 03:36:48 +00:00
// collectOracleMetrics collect metrics in the oracle group from the helium api
2021-09-13 03:25:47 +00:00
func ( e * Exporter ) collectOracleMetrics ( ch chan <- prometheus . Metric ) {
currentOraclePrice , err := heliumapi . GetCurrentOraclePrice ( )
if err != nil {
fmt . Println ( err )
return
}
ch <- prometheus . MustNewConstMetric (
oraclePrice . Desc , oraclePrice . Type , float64 ( currentOraclePrice . Data . Price ) / 100000000 ,
)
}
2021-09-14 03:36:48 +00:00
// collectStatsMetrics collect metrics in the stats group from the helium api
2021-09-13 03:25:47 +00:00
func ( e * Exporter ) collectStatsMetrics ( ch chan <- prometheus . Metric ) {
blockchainStats , err := heliumapi . GetBlockchainStats ( )
if err != nil {
fmt . Println ( err )
return
}
ch <- prometheus . MustNewConstMetric (
statsValidators . Desc , statsValidators . Type , float64 ( blockchainStats . Data . Counts . Validators ) ,
)
ch <- prometheus . MustNewConstMetric (
statsOuis . Desc , statsOuis . Type , float64 ( blockchainStats . Data . Counts . Ouis ) ,
)
ch <- prometheus . MustNewConstMetric (
statsHotspotsDataOnly . Desc , statsHotspotsDataOnly . Type , float64 ( blockchainStats . Data . Counts . HotspotsDataonly ) ,
)
ch <- prometheus . MustNewConstMetric (
statsBlocks . Desc , statsBlocks . Type , float64 ( blockchainStats . Data . Counts . Blocks ) ,
)
ch <- prometheus . MustNewConstMetric (
statsChallenges . Desc , statsChallenges . Type , float64 ( blockchainStats . Data . Counts . Challenges ) ,
)
ch <- prometheus . MustNewConstMetric (
statsCities . Desc , statsCities . Type , float64 ( blockchainStats . Data . Counts . Cities ) ,
)
ch <- prometheus . MustNewConstMetric (
statsConsensusGroups . Desc , statsConsensusGroups . Type , float64 ( blockchainStats . Data . Counts . ConsensusGroups ) ,
)
ch <- prometheus . MustNewConstMetric (
statsCountries . Desc , statsCountries . Type , float64 ( blockchainStats . Data . Counts . Countries ) ,
)
ch <- prometheus . MustNewConstMetric (
statsHotspots . Desc , statsHotspots . Type , float64 ( blockchainStats . Data . Counts . Hotspots ) ,
)
ch <- prometheus . MustNewConstMetric (
statsTokenSupply . Desc , statsTokenSupply . Type , blockchainStats . Data . TokenSupply ,
)
}
2021-09-14 03:36:48 +00:00
// collectStatsMetrics collect metrics in the account group from the helium api
2021-09-15 03:34:08 +00:00
func ( e * Exporter ) collectAccountMetrics ( ch chan <- prometheus . Metric , account * Account ) {
2021-09-25 01:30:22 +00:00
accountForAddress , err := heliumapi . GetAccountForAddress ( account . Address )
2021-09-15 03:34:08 +00:00
if err != nil {
fmt . Println ( err )
return
}
2021-09-25 01:30:22 +00:00
accountActivityForAddress , err := heliumapi . GetActivityCountsForAccount ( account . Address )
2021-09-15 03:34:08 +00:00
if err != nil {
fmt . Println ( err )
return
}
2021-09-25 01:30:22 +00:00
accountRewardTotalsForAddress , err := heliumapi . GetRewardTotalsForAccount ( account . Address , & e . StartTime , nil )
if err != nil {
fmt . Println ( err )
return
}
2021-09-25 19:09:40 +00:00
err = account . collectTransactionMetrics ( ch )
2021-09-14 03:51:47 +00:00
if err != nil {
fmt . Println ( err )
return
}
ch <- prometheus . MustNewConstMetric (
accountBalanceHnt . Desc , accountBalanceHnt . Type , float64 ( accountForAddress . Data . Balance ) ,
2021-09-25 01:30:22 +00:00
account . Address ,
2021-09-15 03:34:08 +00:00
)
for accType , count := range accountActivityForAddress . Data {
ch <- prometheus . MustNewConstMetric (
accountActivity . Desc , accountActivity . Type , float64 ( count ) ,
2021-09-25 01:30:22 +00:00
account . Address , accType ,
2021-09-15 03:34:08 +00:00
)
}
ch <- prometheus . MustNewConstMetric (
accountRewardsHnt . Desc , accountRewardsHnt . Type , accountRewardTotalsForAddress . Data . Sum ,
2021-09-25 01:30:22 +00:00
account . Address ,
)
2021-09-14 03:36:48 +00:00
}
2021-09-15 05:04:32 +00:00
// collectStatsMetrics collect metrics in the hotspot group from the helium api
func ( e * Exporter ) collectHotspotMetrics ( ch chan <- prometheus . Metric , account * Account ) {
2021-09-25 01:30:22 +00:00
hotspotsForAddress , err := heliumapi . GetHotspotsForAccount ( account . Address )
2021-09-15 05:04:32 +00:00
if err != nil {
fmt . Println ( err )
return
}
for _ , hotspotData := range hotspotsForAddress . Data {
2021-09-17 20:45:01 +00:00
hotspotActivityForAddress , err := heliumapi . GetHotspotActivityCount ( hotspotData . Address )
if err != nil {
fmt . Println ( err )
return
}
2021-09-25 01:30:22 +00:00
hotspotRewardTotalsForAddress , err := heliumapi . GetRewardsTotalForHotspot ( hotspotData . Address , & e . StartTime , nil )
2021-09-17 20:45:01 +00:00
if err != nil {
fmt . Println ( err )
return
}
2021-09-15 05:04:32 +00:00
ch <- prometheus . MustNewConstMetric (
hotspotUp . Desc , hotspotUp . Type , bool2Float64 ( hotspotData . Status . Online == "online" ) ,
2021-09-25 01:30:22 +00:00
account . Address , hotspotData . Address , hotspotData . Name ,
2021-09-15 05:04:32 +00:00
)
ch <- prometheus . MustNewConstMetric (
2021-09-17 20:45:01 +00:00
hotspotRelayed . Desc , hotspotRelayed . Type , bool2Float64 ( len ( hotspotData . Status . ListenAddrs ) > 0 && strings . HasPrefix ( hotspotData . Status . ListenAddrs [ 0 ] , "/p2p" ) ) ,
2021-09-25 01:30:22 +00:00
account . Address , hotspotData . Address , hotspotData . Name ,
2021-09-15 05:04:32 +00:00
)
ch <- prometheus . MustNewConstMetric (
2021-09-17 20:45:01 +00:00
hotspotBlocks . Desc , hotspotBlocks . Type , float64 ( hotspotData . Status . Height ) ,
2021-09-25 01:30:22 +00:00
account . Address , hotspotData . Address , hotspotData . Name ,
2021-09-15 05:04:32 +00:00
)
ch <- prometheus . MustNewConstMetric (
2021-09-17 20:45:01 +00:00
hotspotRewardsScale . Desc , hotspotRewardsScale . Type , float64 ( hotspotData . RewardScale ) ,
2021-09-25 01:30:22 +00:00
account . Address , hotspotData . Address , hotspotData . Name ,
2021-09-15 05:04:32 +00:00
)
ch <- prometheus . MustNewConstMetric (
hotspotGeocodeInfo . Desc , hotspotGeocodeInfo . Type , 1.0 ,
2021-09-25 01:30:22 +00:00
account . Address , hotspotData . Address , hotspotData . Name , strconv . FormatFloat ( hotspotData . Lng , 'f' , 6 , 64 ) , strconv . FormatFloat ( hotspotData . Lat , 'f' , 6 , 64 ) , hotspotData . Geocode . LongStreet , hotspotData . Geocode . LongState , hotspotData . Geocode . LongCountry , hotspotData . Geocode . LongCity ,
2021-09-15 05:04:32 +00:00
)
ch <- prometheus . MustNewConstMetric (
hotspotAntennaInfo . Desc , hotspotAntennaInfo . Type , 1.0 ,
2021-09-25 01:30:22 +00:00
account . Address , hotspotData . Address , hotspotData . Name , strconv . Itoa ( hotspotData . Gain ) , strconv . Itoa ( hotspotData . Elevation ) ,
2021-09-15 05:04:32 +00:00
)
2021-09-17 20:45:01 +00:00
for accType , count := range hotspotActivityForAddress . Data {
ch <- prometheus . MustNewConstMetric (
hotspotActivity . Desc , hotspotActivity . Type , float64 ( count ) ,
2021-09-25 01:30:22 +00:00
account . Address , hotspotData . Address , hotspotData . Name , accType ,
2021-09-17 20:45:01 +00:00
)
}
ch <- prometheus . MustNewConstMetric (
hotspotRewardsHnt . Desc , hotspotRewardsHnt . Type , hotspotRewardTotalsForAddress . Data . Sum ,
2021-09-25 01:30:22 +00:00
account . Address , hotspotData . Address , hotspotData . Name ,
2021-09-17 20:45:01 +00:00
)
2021-09-15 05:04:32 +00:00
}
}
2021-09-25 19:09:40 +00:00
func ( a * Account ) collectTransactionMetrics ( ch chan <- prometheus . Metric ) error {
2021-09-25 01:30:22 +00:00
now := time . Now ( )
2021-09-25 19:09:40 +00:00
activities , err := heliumapi . GetActivityForAccount ( a . Address , [ ] string { } , & a . Tx . LastUpdate , & now )
2021-09-25 01:30:22 +00:00
if err != nil {
2021-09-25 19:09:40 +00:00
return err
2021-09-15 03:34:08 +00:00
}
2021-09-12 14:17:31 +00:00
2021-09-25 19:09:40 +00:00
// logic based on https://github.com/helium/hotspot-app/blob/918563fba84d1abf4554a43a4d42bb838d017bd3/src/features/wallet/root/useActivityItem.tsx#L336
for _ , activity := range activities . AddGatewayV1 {
a . Tx . WithdrawalTotal += activity . StakingFee
}
for _ , activity := range activities . AssertLocationV1 {
a . Tx . WithdrawalTotal += activity . StakingFee
}
for _ , activity := range activities . AssertLocationV2 {
a . Tx . WithdrawalTotal += activity . StakingFee
}
for _ , activity := range activities . PaymentV1 {
if activity . Payer == a . Address {
a . Tx . WithdrawalTotal += activity . Amount
} else {
a . Tx . DepositTotal += activity . Amount
}
}
for _ , activity := range activities . PaymentV2 {
if activity . Payer == a . Address {
paymentTotal := 0
for _ , payment := range activity . Payments {
paymentTotal += payment . Amount
}
a . Tx . WithdrawalTotal += paymentTotal
} else {
for _ , payment := range activity . Payments {
if payment . Payee == a . Address {
a . Tx . DepositTotal += payment . Amount
}
}
}
}
for _ , activity := range activities . RewardsV1 {
for _ , reward := range activity . Rewards {
a . Tx . DepositTotal += reward . Amount
}
}
for _ , activity := range activities . RewardsV2 {
for _ , reward := range activity . Rewards {
a . Tx . DepositTotal += reward . Amount
}
}
for _ , activity := range activities . StakeValidatorV1 {
a . Tx . WithdrawalTotal += activity . Stake
}
for _ , activity := range activities . TokenBurnV1 {
a . Tx . WithdrawalTotal += activity . Amount
}
for _ , activity := range activities . TransferHotspotV1 {
if activity . Buyer == a . Address {
a . Tx . WithdrawalTotal += activity . AmountToSeller
} else {
a . Tx . DepositTotal += activity . AmountToSeller
}
}
for _ , activity := range activities . UnstakeValidatorV1 {
a . Tx . WithdrawalTotal += activity . StakeAmount
}
2021-09-25 01:30:22 +00:00
a . Tx . LastUpdate = now
2021-09-25 19:09:40 +00:00
ch <- prometheus . MustNewConstMetric (
accountDepositsHnt . Desc , accountDepositsHnt . Type , float64 ( a . Tx . DepositTotal ) ,
a . Address ,
)
ch <- prometheus . MustNewConstMetric (
accountWithdrawalsHnt . Desc , accountWithdrawalsHnt . Type , float64 ( a . Tx . WithdrawalTotal ) ,
a . Address ,
)
return nil
2021-09-15 03:34:08 +00:00
}
2021-09-12 14:17:31 +00:00
func main ( ) {
2021-09-25 19:09:40 +00:00
fHeliumAccounts := flag . String ( "accounts" , "" , "A comma-delimited list of helium accounts to scrape (optional)" )
2021-09-14 03:36:48 +00:00
fMetricsPath := flag . String ( "metricpath" , "/metrics" , "The metrics path" )
fListenAddress := flag . String ( "listenAddress" , "0.0.0.0" , "The http server listen address" )
2021-09-25 19:09:40 +00:00
fListenPort := flag . String ( "listenPort" , "9865" , "The http server listen port" )
2021-09-14 03:36:48 +00:00
flag . Parse ( )
2021-09-15 03:34:08 +00:00
heliumAccounts := strings . Split ( * fHeliumAccounts , "," )
2021-09-14 03:36:48 +00:00
serverAddr := * fListenAddress + ":" + * fListenPort
2021-09-13 03:25:47 +00:00
2021-09-14 03:36:48 +00:00
e , err := NewExporter ( heliumAccounts )
2021-09-13 03:25:47 +00:00
if err != nil {
log . Fatalf ( "failed to start exporter: %s" , err . Error ( ) )
}
r := prometheus . NewRegistry ( )
r . MustRegister ( e )
// setup http route
http . HandleFunc ( "/" , func ( w http . ResponseWriter , r * http . Request ) {
_ , _ = w . Write ( [ ] byte ( ` < html >
< head > < title > Helium blockchain exporter < / title > < / head >
< body >
< h1 > Helium blockchain exporter < / h1 >
2021-09-14 03:36:48 +00:00
< p > < a href = ' ` + *fMetricsPath + ` ' > Metrics < / a > < / p >
2021-09-13 03:25:47 +00:00
< / body >
< / html > ` ) )
} )
2021-09-14 03:36:48 +00:00
http . Handle ( * fMetricsPath , promhttp . HandlerFor ( r , promhttp . HandlerOpts { } ) )
2021-09-15 03:34:08 +00:00
fmt . Printf ( "listening on %v\n" , serverAddr )
2021-09-14 03:36:48 +00:00
http . ListenAndServe ( serverAddr , nil )
2021-09-12 14:17:31 +00:00
}