Skip to content

Commit

Permalink
Merge pull request #470 from bitcoin-sv/refactor/stats
Browse files Browse the repository at this point in the history
Get total mined and seen on network
  • Loading branch information
boecklim authored Jun 18, 2024
2 parents 5e8e354 + 73ba374 commit a9a22b7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
16 changes: 16 additions & 0 deletions internal/metamorph/stats_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type processorStats struct {
statusSeenInOrphanMempool prometheus.Gauge
statusNotMined prometheus.Gauge
statusNotSeen prometheus.Gauge
statusMinedTotal prometheus.Gauge
statusSeenOnNetworkTotal prometheus.Gauge
statusNotSeenStat int64
}

Expand Down Expand Up @@ -80,6 +82,14 @@ func newProcessorStats(opts ...func(stats *processorStats)) *processorStats {
Name: "arc_status_seen_in_orphan_mempool_count",
Help: "Number of monitored transactions with status SEEN_IN_ORPHAN_MEMPOOL",
}),
statusSeenOnNetworkTotal: prometheus.NewGauge(prometheus.GaugeOpts{
Name: "arc_status_seen_on_network_total_count",
Help: "Total number of monitored transactions with status SEEN_ON_NETWORK",
}),
statusMinedTotal: prometheus.NewGauge(prometheus.GaugeOpts{
Name: "arc_status_mined_total_count",
Help: "Total number of monitored transactions with status MINED",
}),
notSeenLimit: notSeenLimitDefault,
notMinedLimit: notMinedLimitDefault,
}
Expand Down Expand Up @@ -121,6 +131,8 @@ func (p *Processor) StartCollectStats() error {
p.stats.statusSeenInOrphanMempool,
p.stats.statusNotMined,
p.stats.statusNotSeen,
p.stats.statusSeenOnNetworkTotal,
p.stats.statusMinedTotal,
)
if err != nil {
return err
Expand All @@ -147,6 +159,8 @@ func (p *Processor) StartCollectStats() error {
p.stats.statusSeenInOrphanMempool,
p.stats.statusNotMined,
p.stats.statusNotSeen,
p.stats.statusSeenOnNetworkTotal,
p.stats.statusMinedTotal,
)

for {
Expand Down Expand Up @@ -176,6 +190,8 @@ func (p *Processor) StartCollectStats() error {
p.stats.statusNotMined.Set(float64(collectedStats.StatusNotMined))
p.stats.statusNotSeen.Set(float64(collectedStats.StatusNotSeen))
p.stats.statusNotSeenStat = collectedStats.StatusNotSeen
p.stats.statusSeenOnNetworkTotal.Set(float64(collectedStats.StatusSeenOnNetworkTotal))
p.stats.statusMinedTotal.Set(float64(collectedStats.StatusMinedTotal))
p.stats.mu.Unlock()
}
}
Expand Down
33 changes: 33 additions & 0 deletions internal/metamorph/store/postgresql/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,39 @@ func (p *PostgreSQL) GetStats(ctx context.Context, since time.Time, notSeenLimit
return nil, err
}

q = `
SELECT
max(status_counts.status_count) FILTER (where status_counts.status = $2 )
,max(status_counts.status_count) FILTER (where status_counts.status = $3 )
FROM
(SELECT all_statuses.status, COALESCE (found_statuses.status_count, 0) AS status_count FROM
(SELECT unnest(ARRAY[
$2::integer,
$3::integer]) AS status) AS all_statuses
LEFT JOIN
(
SELECT
t.status,
count(*) AS status_count
FROM
metamorph.transactions t WHERE t.last_submitted_at > $1
GROUP BY
t.status
) AS found_statuses ON found_statuses.status = all_statuses.status) AS status_counts
;
`

err = p.db.QueryRowContext(ctx, q, since,
metamorph_api.Status_SEEN_ON_NETWORK,
metamorph_api.Status_MINED,
).Scan(
&stats.StatusSeenOnNetworkTotal,
&stats.StatusMinedTotal,
)
if err != nil {
return nil, err
}

qNotSeen := `
SELECT
count(*)
Expand Down
2 changes: 2 additions & 0 deletions internal/metamorph/store/postgresql/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,5 +513,7 @@ func TestPostgresDB(t *testing.T) {
require.Equal(t, int64(0), res.StatusSeenInOrphanMempool)
require.Equal(t, int64(1), res.StatusNotMined)
require.Equal(t, int64(2), res.StatusNotSeen)
require.Equal(t, int64(6), res.StatusMinedTotal)
require.Equal(t, int64(2), res.StatusSeenOnNetworkTotal)
})
}
2 changes: 2 additions & 0 deletions internal/metamorph/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type Stats struct {
StatusSeenInOrphanMempool int64
StatusNotSeen int64
StatusNotMined int64
StatusSeenOnNetworkTotal int64
StatusMinedTotal int64
}

type MetamorphStore interface {
Expand Down

0 comments on commit a9a22b7

Please sign in to comment.