-
Notifications
You must be signed in to change notification settings - Fork 8
/
director.go
247 lines (216 loc) · 6.71 KB
/
director.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package main
import (
"context"
"errors"
"fmt"
"strings"
"github.com/decred/dcrdata/exchanges/v2"
"github.com/planetdecred/pdanalytics/attackcost"
"github.com/planetdecred/pdanalytics/charts"
"github.com/planetdecred/pdanalytics/commstats"
"github.com/planetdecred/pdanalytics/dcrd"
exchangesModule "github.com/planetdecred/pdanalytics/exchanges"
"github.com/planetdecred/pdanalytics/gov/politeia"
"github.com/planetdecred/pdanalytics/homepage"
"github.com/planetdecred/pdanalytics/mempool"
"github.com/planetdecred/pdanalytics/netsnapshot"
"github.com/planetdecred/pdanalytics/parameters"
"github.com/planetdecred/pdanalytics/postgres"
"github.com/planetdecred/pdanalytics/pow"
"github.com/planetdecred/pdanalytics/propagation"
"github.com/planetdecred/pdanalytics/stakingreward"
"github.com/planetdecred/pdanalytics/stats"
"github.com/planetdecred/pdanalytics/treasury"
"github.com/planetdecred/pdanalytics/vsp"
"github.com/planetdecred/pdanalytics/web"
)
func setupModules(ctx context.Context, cfg *config, client *dcrd.Dcrd, server *web.Server, xcBot *exchanges.ExchangeBot) error {
var err error
var pgDb *postgres.PgDb
var dbInstance = func() (*postgres.PgDb, error) {
if pgDb == nil {
pgDb, err = postgres.NewPgDb(cfg.DBHost, cfg.DBPort, cfg.DBUser, cfg.DBPass, cfg.DBName, cfg.DebugLevel == "debug")
if err != nil {
return nil, err
}
if err = pgDb.CreateTables(ctx); err != nil {
log.Error("Error creating tables: ", err)
return nil, err
}
}
return pgDb, nil
}
if cfg.EnableStats {
db, err := dbInstance()
if err != nil {
return err
}
err = stats.Activate(server, db)
if err != nil {
log.Error(err)
return fmt.Errorf("Failed to create staking reward component, %s", err.Error())
}
log.Info("Staking Reward Calculator Enabled")
}
var stk *stakingreward.Calculator
if cfg.EnableStakingRewardCalculator {
stk, err = stakingreward.New(client, server, xcBot)
if err != nil {
log.Error(err)
return fmt.Errorf("Failed to create staking reward component, %s", err.Error())
}
log.Info("Staking Reward Calculator Enabled")
}
var prms *parameters.Parameters
if cfg.EnableChainParameters {
prms, err = parameters.New(client, server)
if err != nil {
log.Error(err)
return fmt.Errorf("Failed to create parameters component, %s", err.Error())
}
log.Info("Chain Parameters Enabled")
}
var chrts *charts.Charts
if cfg.EnableCharts {
chrts, err = charts.New(client, server)
if err != nil {
log.Error(err)
return fmt.Errorf("Failed to create Charts component, %s", err.Error())
}
log.Info("Charts parameters Enabled")
}
var ac *attackcost.Attackcost
if cfg.EnableAttackCost {
ac, err = attackcost.New(client, server, xcBot)
if err != nil {
log.Error(err)
return fmt.Errorf("Failed to create attackcost component, %s", err.Error())
}
log.Info("Attack Cost Calculator Enabled")
}
var mp *mempool.Collector
if cfg.EnableMempool {
mdb, err := dbInstance()
if err != nil {
return err
}
mp, err = mempool.NewCollector(ctx, client, cfg.MempoolInterval, mdb, server)
if err != nil {
log.Error(err)
return fmt.Errorf("Failed to create new mempool component, %s", err.Error())
}
go mp.StartMonitoring(ctx)
log.Info("Attack Cost Calculator Enabled")
}
if cfg.EnablePropagation {
var syncDbs = map[string]propagation.Store{}
//register instances
for i := 0; i < len(cfg.PropDBName); i++ {
databaseName := cfg.PropDBName[i]
syncDb, err := postgres.NewPgDb(cfg.PropDBHost[i], cfg.PropDBPort[i], cfg.PropDBUser[i], cfg.PropDBPass[i],
databaseName, cfg.DebugLevel == "debug")
if err != nil {
return err
}
if !syncDb.TableExists("block") {
msg := fmt.Sprintf("the database, %s is missing the block table", databaseName)
log.Error(msg)
return errors.New(msg)
}
if !syncDb.TableExists("vote") {
msg := fmt.Sprintf("the database, %s is missing the vote table", databaseName)
log.Error(msg)
return errors.New(msg)
}
syncDbs[databaseName] = syncDb
}
propDb, err := dbInstance()
if err != nil {
return err
}
_, err = propagation.New(ctx, client, propDb, syncDbs, server)
if err != nil {
log.Error(err)
return fmt.Errorf("Failed to create new propagation component, %s", err.Error())
}
}
if cfg.EnableProposals || cfg.EnableProposalsHttp {
err = politeia.Activate(ctx, client, cfg.PoliteiaAPIURL,
cfg.ProposalsFileName, cfg.PiPropRepoOwner, cfg.PiPropRepoName, cfg.DataDir, server,
cfg.EnableProposals, cfg.EnableProposalsHttp)
if err != nil {
log.Error(err)
requestShutdown()
}
log.Info("Proposals module Enabled")
}
if cfg.EnableNetworkSnapshot || cfg.EnableNetworkSnapshotHTTP {
db, err := dbInstance()
if err != nil {
return err
}
err = netsnapshot.Activate(ctx, db, cfg.NetworkSnapshotOptions, server)
if err != nil {
log.Error(err)
return fmt.Errorf("Failed to activate network snapshot component, %s", err.Error())
}
log.Info("Network nodes module Enabled")
}
if cfg.EnableExchange || cfg.EnableExchangeHttp {
db, err := dbInstance()
if err != nil {
return err
}
if err := exchangesModule.Activate(ctx, strings.Split(cfg.DisabledExchanges, ","), db, server,
cfg.EnableExchange, cfg.EnableExchangeHttp); err != nil {
return fmt.Errorf("Failed to ectivate the exchanges modules, %s", err.Error())
}
log.Info("Exchange module enabled")
}
if cfg.CommunityStat || cfg.CommunityStatHttp {
db, err := dbInstance()
if err != nil {
return err
}
if err := commstats.Activate(ctx, db, server, &cfg.CommunityStatOptions); err != nil {
return fmt.Errorf("Failed to ectivate the exchanges modules, %s", err.Error())
}
log.Info("Community stat module enabled")
}
if cfg.EnablePow || cfg.EnablePowHttp {
db, err := dbInstance()
if err != nil {
return err
}
if err := pow.Activate(ctx, cfg.DisabledPows, cfg.PowInterval, db, server, cfg.EnablePow, cfg.EnablePowHttp); err != nil {
return fmt.Errorf("Failed to ectivate the PoW modules, %s", err.Error())
}
log.Info("PoW module enabled")
}
if cfg.EnableVSP || cfg.EnableVSPHttp {
db, err := dbInstance()
if err != nil {
return err
}
if err := vsp.Activate(ctx, cfg.VSPInterval, db, server, cfg.EnableVSP, cfg.EnableVSPHttp); err != nil {
return fmt.Errorf("Failed to ectivate the VSP modules, %s", err.Error())
}
log.Info("VSP module enabled")
}
if cfg.EnableTreasuryChart {
if err := treasury.Activate(server, xcBot, cfg.APIURL); err != nil {
return fmt.Errorf("Failed to activate treasury chart module, %s", err.Error())
}
log.Info("Treasury chart activated")
}
_, err = homepage.New(server, homepage.Mods{
Stk: stk,
Prm: prms,
Ac: ac,
Chrts: chrts,
})
if err != nil {
log.Error(err)
}
return nil
}