-
Notifications
You must be signed in to change notification settings - Fork 568
/
stats_db.go
61 lines (49 loc) · 1.54 KB
/
stats_db.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
package stats
import (
"database/sql"
"embed"
"fmt"
_ "github.com/mattn/go-sqlite3"
"github.com/cosmos/cosmos-sdk/types/simulation"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)
//go:embed schema.sql
var embedFs embed.FS
// TODO: Setup an sql schema file instead of doing it in-line here
type StatsDb struct {
enabled bool
db *sql.DB
}
func SetupStatsDb(config ExportConfig) (StatsDb, error) {
if !config.WriteStatsToDB {
return StatsDb{enabled: false}, nil
}
setupSqlCmd, err := embedFs.ReadFile("schema.sql")
if err != nil {
return StatsDb{}, fmt.Errorf("error in reading schema.sql: %w", err)
}
db, err := sql.Open("sqlite3", "./blocks.db")
if err != nil {
return StatsDb{}, err
}
if _, err := db.Exec(string(setupSqlCmd)); err != nil {
db.Close()
return StatsDb{}, fmt.Errorf("error in init from schema.sql init: %w", err)
}
return StatsDb{enabled: true, db: db}, nil
}
func (stats StatsDb) Cleanup() {
if stats.db != nil {
stats.db.Close()
}
}
func (stats StatsDb) LogActionResult(header tmproto.Header, opMsg simulation.OperationMsg, resultData []byte) error {
if !stats.enabled {
return nil
}
appHash := fmt.Sprintf("%X", header.AppHash)
resData := fmt.Sprintf("%X", resultData)
sts := "INSERT INTO blocks(height,module,name,comment,passed, gasWanted, gasUsed, msg, resData, appHash) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10);"
_, err := stats.db.Exec(sts, header.Height, opMsg.Route, opMsg.Name, opMsg.Comment, opMsg.OK, opMsg.GasWanted, opMsg.GasUsed, opMsg.Msg, resData, appHash)
return err
}