-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmetrics.go
84 lines (69 loc) · 2.25 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package async_treasury
import (
"context"
"time"
"github.com/code-payments/code-server/pkg/kin"
"github.com/code-payments/code-server/pkg/metrics"
code_data "github.com/code-payments/code-server/pkg/code/data"
"github.com/code-payments/code-server/pkg/code/data/treasury"
)
const (
treasuryFundCheckEventName = "TreasuryFundPollingCheck"
recentRootIntentCreatedEventName = "RecentRootIntentCreated"
merkleTreeSyncedEventName = "MerkleTreeSynced"
)
func (p *service) metricsGaugeWorker(ctx context.Context) error {
delay := time.Second
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(delay):
start := time.Now()
treasuryPoolRecords, err := p.data.GetAllTreasuryPoolsByState(ctx, treasury.TreasuryPoolStateAvailable)
if err != nil {
continue
}
for _, treasuryPoolRecord := range treasuryPoolRecords {
total, used, err := estimateTreasuryPoolFundingLevels(ctx, p.data, treasuryPoolRecord)
if err != nil {
continue
}
recordTreasuryFundEvent(ctx, treasuryPoolRecord.Name, total, used)
}
delay = time.Second - time.Since(start)
}
}
}
func estimateTreasuryPoolFundingLevels(ctx context.Context, data code_data.Provider, record *treasury.Record) (total uint64, used uint64, err error) {
total, err = data.GetTotalAvailableTreasuryPoolFunds(ctx, record.Vault)
if err != nil {
return 0, 0, err
}
used, err = data.GetUsedTreasuryPoolDeficitFromCommitments(ctx, record.Address)
if err != nil {
return 0, 0, err
}
return total, used, nil
}
func recordTreasuryFundEvent(ctx context.Context, name string, total, used uint64) {
var available uint64
if used < total {
available = total - used
}
metrics.RecordEvent(ctx, treasuryFundCheckEventName, map[string]interface{}{
"treasury": name,
"available": kin.FromQuarks(available),
"used": kin.FromQuarks(used),
})
}
func recordRecentRootIntentCreatedEvent(ctx context.Context, treasuryPoolName string) {
metrics.RecordEvent(ctx, recentRootIntentCreatedEventName, map[string]interface{}{
"treasury": treasuryPoolName,
})
}
func recordMerkleTreeSyncedEvent(ctx context.Context, treasuryPoolName string) {
metrics.RecordEvent(ctx, merkleTreeSyncedEventName, map[string]interface{}{
"treasury": treasuryPoolName,
})
}