Skip to content

Commit 427926a

Browse files
authored
Move db operation to store module. (#10)
1 parent af79bcf commit 427926a

File tree

9 files changed

+229
-131
lines changed

9 files changed

+229
-131
lines changed

api/stat_api.go

Lines changed: 6 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
package api
22

33
import (
4-
"encoding/json"
5-
64
commonApi "github.com/Conflux-Chain/go-conflux-util/api"
75
"github.com/gin-gonic/gin"
8-
"github.com/sirupsen/logrus"
9-
"github.com/zero-gravity-labs/zerog-storage-scan/stat"
106
"github.com/zero-gravity-labs/zerog-storage-scan/store"
11-
"gorm.io/gorm"
127
)
138

149
type Type int
@@ -20,7 +15,7 @@ const (
2015
)
2116

2217
func dashboard(_ *gin.Context) (interface{}, error) {
23-
submitStat, err := db.SubmitStatStore.LastByType(stat.Day)
18+
submitStat, err := db.SubmitStatStore.LastByType(store.Day)
2419
if err != nil {
2520
return nil, commonApi.ErrInternal(err)
2621
}
@@ -57,30 +52,8 @@ func getSubmitStatByType(c *gin.Context, t Type) (interface{}, error) {
5752
return nil, err
5853
}
5954

60-
r, _ := json.Marshal(statP)
61-
logrus.WithFields(logrus.Fields{
62-
"skip": statP.Skip,
63-
"limit": statP.Limit,
64-
"minTimestamp": statP.MinTimestamp,
65-
"maxTimestamp": statP.MaxTimestamp,
66-
"intervalType": statP.IntervalType,
67-
"sort": statP.Sort,
68-
}).Infof("queryStat incoming %v", string(r))
69-
70-
var conds []func(db *gorm.DB) *gorm.DB
71-
intervalType := stat.IntervalTypes[statP.IntervalType]
72-
conds = append(conds, StatType(intervalType))
73-
if statP.MinTimestamp != 0 {
74-
conds = append(conds, MinTimestamp(statP.MinTimestamp))
75-
}
76-
if statP.MaxTimestamp != 0 {
77-
conds = append(conds, MaxTimestamp(statP.MaxTimestamp))
78-
}
79-
dbRaw := db.DB.Model(&store.SubmitStat{})
80-
dbRaw.Scopes(conds...)
81-
82-
records := new([]store.SubmitStat)
83-
total, err := db.List(dbRaw, statP.isDesc(), statP.Skip, statP.Limit, records)
55+
total, records, err := db.SubmitStatStore.List(&statP.IntervalType, statP.MinTimestamp, statP.MaxTimestamp,
56+
statP.isDesc(), statP.Skip, statP.Limit)
8457
if err != nil {
8558
return nil, err
8659
}
@@ -91,7 +64,7 @@ func getSubmitStatByType(c *gin.Context, t Type) (interface{}, error) {
9164
switch t {
9265
case StorageStatType:
9366
list := make([]DataStat, 0)
94-
for _, r := range *records {
67+
for _, r := range records {
9568
list = append(list, DataStat{
9669
StatTime: r.StatTime,
9770
FileCount: r.FileCount,
@@ -103,7 +76,7 @@ func getSubmitStatByType(c *gin.Context, t Type) (interface{}, error) {
10376
result["list"] = list
10477
case TxStatType:
10578
list := make([]TxStat, 0)
106-
for _, r := range *records {
79+
for _, r := range records {
10780
list = append(list, TxStat{
10881
StatTime: r.StatTime,
10982
TxCount: r.FileCount,
@@ -113,7 +86,7 @@ func getSubmitStatByType(c *gin.Context, t Type) (interface{}, error) {
11386
result["list"] = list
11487
case FeeStatType:
11588
list := make([]FeeStat, 0)
116-
for _, r := range *records {
89+
for _, r := range records {
11790
list = append(list, FeeStat{
11891
StatTime: r.StatTime,
11992
BaseFee: r.BaseFee,
@@ -127,21 +100,3 @@ func getSubmitStatByType(c *gin.Context, t Type) (interface{}, error) {
127100

128101
return result, nil
129102
}
130-
131-
func StatType(t string) func(db *gorm.DB) *gorm.DB {
132-
return func(db *gorm.DB) *gorm.DB {
133-
return db.Where("stat_type = ?", t)
134-
}
135-
}
136-
137-
func MinTimestamp(minTimestamp int) func(db *gorm.DB) *gorm.DB {
138-
return func(db *gorm.DB) *gorm.DB {
139-
return db.Where("stat_time >= ?", minTimestamp)
140-
}
141-
}
142-
143-
func MaxTimestamp(maxTimestamp int) func(db *gorm.DB) *gorm.DB {
144-
return func(db *gorm.DB) *gorm.DB {
145-
return db.Where("stat_time <= ?", maxTimestamp)
146-
}
147-
}

api/tx_api.go

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ import (
44
"encoding/hex"
55
"encoding/json"
66
"strconv"
7-
"strings"
87

98
commonApi "github.com/Conflux-Chain/go-conflux-util/api"
109
"github.com/ethereum/go-ethereum/common"
1110
"github.com/gin-gonic/gin"
1211
"github.com/pkg/errors"
1312
"github.com/sirupsen/logrus"
1413
"github.com/zero-gravity-labs/zerog-storage-scan/store"
15-
"gorm.io/gorm"
1614
)
1715

1816
func listTx(c *gin.Context) (interface{}, error) {
@@ -21,40 +19,34 @@ func listTx(c *gin.Context) (interface{}, error) {
2119
return nil, err
2220
}
2321

24-
dbRaw := db.DB.Model(&store.Submit{})
25-
var conds []func(db *gorm.DB) *gorm.DB
26-
if param.Address != "" {
27-
addr, exist, err := db.AddressStore.Get(param.Address)
22+
var addrIDPtr *uint64
23+
if param.Address != nil {
24+
addr, exist, err := db.AddressStore.Get(*param.Address)
2825
if err != nil {
2926
return nil, commonApi.ErrInternal(err)
3027
}
3128
if !exist {
3229
return TxList{}, nil
3330
}
34-
conds = append(conds, SenderID(addr.Id))
31+
addrIDPtr = &addr.Id
3532
}
36-
if param.RootHash != "" {
37-
conds = append(conds, RootHash(param.RootHash))
38-
}
39-
dbRaw.Scopes(conds...)
4033

41-
submits := new([]store.Submit)
42-
total, err := db.List(dbRaw, true, param.Skip, param.Limit, submits)
34+
total, submits, err := listSubmits(addrIDPtr, param.RootHash, param.isDesc(), param.Skip, param.Limit)
4335
if err != nil {
4436
return nil, err
4537
}
4638

47-
addrIds := make([]uint64, 0)
48-
for _, submit := range *submits {
49-
addrIds = append(addrIds, submit.SenderID)
39+
addrIDs := make([]uint64, 0)
40+
for _, submit := range submits {
41+
addrIDs = append(addrIDs, submit.SenderID)
5042
}
51-
addrMap, err := db.BatchGetAddresses(addrIds)
43+
addrMap, err := db.BatchGetAddresses(addrIDs)
5244
if err != nil {
5345
return nil, err
5446
}
5547

5648
storageTxs := make([]StorageTx, 0)
57-
for _, submit := range *submits {
49+
for _, submit := range submits {
5850
storageTx := StorageTx{
5951
TxSeq: submit.SubmissionIndex,
6052
BlockNum: submit.BlockNumber,
@@ -70,11 +62,10 @@ func listTx(c *gin.Context) (interface{}, error) {
7062
storageTxs = append(storageTxs, storageTx)
7163
}
7264

73-
result := TxList{
65+
return TxList{
7466
Total: total,
7567
List: storageTxs,
76-
}
77-
return result, nil
68+
}, nil
7869
}
7970

8071
func getTxBrief(c *gin.Context) (interface{}, error) {
@@ -93,8 +84,8 @@ func getTxBrief(c *gin.Context) (interface{}, error) {
9384
return nil, errors.Errorf("Record not found, txSeq %v", *param.TxSeq)
9485
}
9586

96-
addrIds := []uint64{submit.SenderID}
97-
addrMap, err := db.BatchGetAddresses(addrIds)
87+
addrIDs := []uint64{submit.SenderID}
88+
addrMap, err := db.BatchGetAddresses(addrIDs)
9889
if err != nil {
9990
return nil, err
10091
}
@@ -175,14 +166,30 @@ func getTxDetail(c *gin.Context) (interface{}, error) {
175166
return result, nil
176167
}
177168

178-
func SenderID(si uint64) func(db *gorm.DB) *gorm.DB {
179-
return func(db *gorm.DB) *gorm.DB {
180-
return db.Where("sender_id = ?", si)
169+
func listSubmits(addressID *uint64, rootHash *string, idDesc bool, skip, limit int) (int64, []store.Submit, error) {
170+
if addressID == nil {
171+
return db.SubmitStore.List(rootHash, idDesc, skip, limit)
181172
}
182-
}
183173

184-
func RootHash(rh string) func(db *gorm.DB) *gorm.DB {
185-
return func(db *gorm.DB) *gorm.DB {
186-
return db.Where("root_hash = ?", strings.ToLower(strings.TrimPrefix(rh, "0x")))
174+
total, addrSubmits, err := db.AddressSubmitStore.List(addressID, rootHash, idDesc, skip, limit)
175+
if err != nil {
176+
return 0, nil, err
177+
}
178+
179+
submits := make([]store.Submit, 0)
180+
for _, as := range addrSubmits {
181+
submits = append(submits, store.Submit{
182+
SubmissionIndex: as.SubmissionIndex,
183+
RootHash: as.RootHash,
184+
SenderID: as.SenderID,
185+
Length: as.Length,
186+
BlockNumber: as.BlockNumber,
187+
BlockTime: as.BlockTime,
188+
TxHash: as.TxHash,
189+
Status: as.Status,
190+
Fee: as.Fee,
191+
})
187192
}
193+
194+
return total, submits, nil
188195
}

api/types.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ type PageParam struct {
1515

1616
type statParam struct {
1717
PageParam
18-
MinTimestamp int `form:"minTimestamp,default=0" binding:"omitempty,number"`
19-
MaxTimestamp int `form:"maxTimestamp,default=0" binding:"omitempty,number"`
18+
MinTimestamp *int `form:"minTimestamp" binding:"omitempty,number"`
19+
MaxTimestamp *int `form:"maxTimestamp" binding:"omitempty,number"`
2020
IntervalType string `form:"intervalType,default=day" binding:"omitempty,oneof=hour day"`
2121
Sort string `form:"sort,default=desc" binding:"omitempty,oneof=asc desc"`
2222
}
@@ -27,8 +27,13 @@ func (sp *statParam) isDesc() bool {
2727

2828
type listTxParam struct {
2929
PageParam
30-
Address string `form:"address" binding:"omitempty"`
31-
RootHash string `form:"rootHash" binding:"omitempty"`
30+
Address *string `form:"address" binding:"omitempty"`
31+
RootHash *string `form:"rootHash" binding:"omitempty"`
32+
Sort string `form:"sort,default=desc" binding:"omitempty,oneof=asc desc"`
33+
}
34+
35+
func (sp *listTxParam) isDesc() bool {
36+
return strings.EqualFold(sp.Sort, "desc")
3237
}
3338

3439
type queryTxParam struct {

stat/stat.go

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,21 @@ package stat
22

33
import (
44
"context"
5+
"sync"
6+
"time"
7+
58
"github.com/Conflux-Chain/go-conflux-util/viper"
69
"github.com/openweb3/web3go"
710
"github.com/openweb3/web3go/types"
811
"github.com/pkg/errors"
912
"github.com/sirupsen/logrus"
1013
"github.com/zero-gravity-labs/zerog-storage-scan/store"
11-
"sync"
12-
"time"
13-
)
14-
15-
const (
16-
Min = "1m"
17-
TenMin = "10m"
18-
Hour = "1h"
19-
Day = "1d"
2014
)
2115

2216
var (
2317
ErrTimeNotReach = errors.New("time not reach")
2418
ErrBlockNotSync = errors.New("block not sync")
2519
ErrBlockNotFinalized = errors.New("block not finalized")
26-
27-
Intervals = map[string]time.Duration{
28-
Min: time.Minute,
29-
TenMin: time.Minute * 10,
30-
Hour: time.Hour,
31-
Day: time.Hour * 24,
32-
}
33-
34-
IntervalTypes = map[string]string{
35-
"min": Min,
36-
"10min": TenMin,
37-
"hour": Hour,
38-
"day": Day,
39-
}
4020
)
4121

4222
type StatConfig struct {
@@ -52,7 +32,7 @@ type TimeRange struct {
5232

5333
type BaseStat struct {
5434
Config *StatConfig
55-
Db *store.MysqlStore
35+
DB *store.MysqlStore
5636
Sdk *web3go.Client
5737
StartTime *time.Time
5838
}
@@ -123,7 +103,7 @@ func (bs *BaseStat) calStatRangeStart(t *time.Time, statType string) (*time.Time
123103
}
124104

125105
func (bs *BaseStat) firstBlockAfterRangeEnd(rangeEnd *time.Time) (uint64, bool, error) {
126-
return bs.Db.FirstBlockAfterTime(rangeEnd)
106+
return bs.DB.FirstBlockAfterTime(rangeEnd)
127107
}
128108

129109
type Stat interface {

0 commit comments

Comments
 (0)