Skip to content

Commit

Permalink
Store network name along with ASN
Browse files Browse the repository at this point in the history
  • Loading branch information
cmmarslender committed Mar 6, 2024
1 parent d1dcd7f commit 71ed421
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/chia-network/chia-exporter
go 1.21

require (
github.com/chia-network/go-chia-libs v0.5.3
github.com/chia-network/go-chia-libs v0.6.0
github.com/chia-network/go-modules v0.0.4
github.com/go-sql-driver/mysql v1.7.1
github.com/oschwald/maxminddb-golang v1.12.0
Expand Down
7 changes: 4 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chia-network/go-chia-libs v0.5.3 h1:+7/Myjp1pi7bJ+tFM7zRgww2YmbAvx8SbUkeR7vgLSM=
github.com/chia-network/go-chia-libs v0.5.3/go.mod h1:zQ2cAB+soETmCQBBfcd352FWJRTzXrSqifWuQ2/Gx9Q=
github.com/chia-network/go-chia-libs v0.6.0 h1:HF9OHb2oLRvsVkM5umH11m085UrMp5Pg/xMO43EHmBE=
github.com/chia-network/go-chia-libs v0.6.0/go.mod h1:uqlNmpIlkRd4vtE+cXoUIAEdbDgCKK0Gp9XzrPgpkEM=
github.com/chia-network/go-modules v0.0.4 h1:XlCcuT4j1krLvsFT1Y49Un5xORwcTc8jjE4SHih7OTI=
github.com/chia-network/go-modules v0.0.4/go.mod h1:JP8mG/9ieE76VcGIbzD5G3/4YDmvNhRryiQwp8GQr1U=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
Expand Down Expand Up @@ -83,8 +83,9 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
Expand Down
24 changes: 20 additions & 4 deletions internal/metrics/crawler.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ type CrawlerServiceMetrics struct {

// Debug Metric
debug *prometheus.GaugeVec

// Current network
network *string
}

// InitMetrics sets all the metrics properties
Expand Down Expand Up @@ -248,6 +251,19 @@ func (s *CrawlerServiceMetrics) ProcessIPASNMapping(ips *rpc.GetIPsAfterTimestam
if s.metrics.mysqlClient == nil {
return
}
if s.network == nil {
netInfo, _, err := s.metrics.httpClient.CrawlerService.GetNetworkInfo(&rpc.GetNetworkInfoOptions{})
if err != nil {
log.Errorf("Could not get network information to store with ASN data: %s\n", err.Error())
return
}
if netInfo.NetworkName.IsAbsent() {
log.Error("network name was absent in get_network_info request. Can't store ASNs without network name")
return
}
netName := netInfo.NetworkName.MustGet()
s.network = &netName
}
type countStruct struct {
ASN uint32
Organization string
Expand All @@ -274,7 +290,7 @@ func (s *CrawlerServiceMetrics) ProcessIPASNMapping(ips *rpc.GetIPsAfterTimestam
}
}

err := s.metrics.DeleteASNRecords()
err := s.metrics.DeleteASNRecords(*s.network)
if err != nil {
log.Errorf("unable to delete old ASN records from the database: %s\n", err.Error())
return
Expand All @@ -289,13 +305,13 @@ func (s *CrawlerServiceMetrics) ProcessIPASNMapping(ips *rpc.GetIPsAfterTimestam
continue
}

valueStrings = append(valueStrings, "(?, ?, ?)")
valueArgs = append(valueArgs, asnData.ASN, asnData.Organization, asnData.Count)
valueStrings = append(valueStrings, "(?, ?, ?, ?)")
valueArgs = append(valueArgs, asnData.ASN, asnData.Organization, asnData.Count, *s.network)

// Execute the batch insert when reaching the batch size or the end of the slice
if (i+1)%batchSize == 0 || i+1 == uint32(len(asnCounts)) {
_, err := s.metrics.mysqlClient.Exec(
fmt.Sprintf("INSERT INTO asn(asn, organization, count) VALUES %s", strings.Join(valueStrings, ",")),
fmt.Sprintf("INSERT INTO asn(asn, organization, count, network) VALUES %s", strings.Join(valueStrings, ",")),
valueArgs...)

if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions internal/metrics/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ func (m *Metrics) initTables() error {
" `asn` int unsigned NOT NULL," +
" `organization` VARCHAR(255) NOT NULL," +
" `count` int unsigned NOT NULL," +
" `network` VARCHAR(255) NOT NULL," +
"UNIQUE KEY `asn-unique` (`asn`)" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;"

Expand All @@ -20,12 +21,12 @@ func (m *Metrics) initTables() error {
}

// DeleteASNRecords deletes all records from the asn table in the database
func (m *Metrics) DeleteASNRecords() error {
func (m *Metrics) DeleteASNRecords(networkName string) error {
if m.mysqlClient == nil {
return nil
}
query := "DELETE from asn;"
result, err := m.mysqlClient.Query(query)
query := "DELETE from asn where network=?;"
result, err := m.mysqlClient.Query(query, networkName)
if err != nil {
return err
}
Expand Down

0 comments on commit 71ed421

Please sign in to comment.