-
Notifications
You must be signed in to change notification settings - Fork 8
/
statistics.go
211 lines (176 loc) · 5.22 KB
/
statistics.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
package main
import (
"database/sql"
"errors"
"fmt"
"log"
"time"
)
// 统计交易量
// 以太坊和合约
// 可能同时存在合约和转账行为
// tokenvaue 为代币数量
// 所以统计因该 group by addr_token && type
// sum(value)
// sum(tokenvaue)
func statMoniter(db *sql.DB, interval int64, tblname, stat_tblname string, quit chan bool, in <-chan *Transaction) {
now := time.Now().Unix()
start := queryLastTime(db, stat_tblname)
if start == 0 {
for {
start = queryMinTime(db, tblname)
if start > 0 {
start = start - start%interval
break
}
time.Sleep(30 * time.Second)
}
}
// 统计中间漏掉的数据
passStart := start
if now-start >= interval {
go func(s int64) {
for now-s > interval {
statSQL(db, tblname, stat_tblname, s, s+interval)
s += interval
time.Sleep(1 * time.Second)
// if count > 0 {
// log.Printf("pass: %+v - %+v : %+v", s, s+interval, count)
// }
}
}(start)
// 重新开始
start = now - now%interval
}
passEnd := start
// 距离下一次统计还有多长时间
du := int64(0)
if now < start {
du = interval
} else {
du = interval - (now - start)
}
// 多等待10秒 eg:00:01:10 这一刻统计 00:00:00-00:01:00 时间段的数据
time.Sleep(10 * time.Second)
for {
select {
case <-quit:
log.Println("quit")
return
case tran := <-in:
// 新的交易数据错过了上一个统计间隔
tranTime := tran.Timestamp.Int64()
if tranTime < start && tran.To != "" {
if tranTime < passStart || tranTime >= passEnd {
updateSQL(db, stat_tblname, tran, interval)
// log.Printf("miss: %+v : %+v", start, tranTime)
}
}
case <-time.After(time.Second * time.Duration(du)):
statSQL(db, tblname, stat_tblname, start, start+interval)
du = interval
start += interval
// if count > 0 {
// log.Printf("stat: %+v - %+v : %+v", start, start+interval, count)
// }
}
}
}
// 纠正数据
func updateSQL(db *sql.DB, stat_tblname string, tran *Transaction, interval int64) error {
if tran == nil {
return nil
}
// 查找该时间段内是否已经有统计记录
sqlStr := `select e.id from ` + stat_tblname + ` e
where e.start_time <= $1 and e.end_time > $2 and e.type = $3 and e.interval = $4
and e.addr_token = '` + tran.AddrToken + `' and e.addr_to = '` + tran.To + `'`
var id int64
err := db.QueryRow(sqlStr, tran.Timestamp.Int64(), tran.Timestamp.Int64(), tran.Type, interval).Scan(&id)
if err != nil {
log.Println("select ", err)
}
// 没有对应统计间隔的记录,则新加
if err == sql.ErrNoRows {
sqlStr = `insert into ` + stat_tblname + `("start_time","end_time","sum_value","sum_token_value","type","addr_token","addr_to","sum_gas","interval")
VALUES `
start := tran.Timestamp.Int64() - tran.Timestamp.Int64()%interval
end := start + interval
sqlStr += fmt.Sprintf("(%+v,%+v,%+v,%+v,%+v,'%+v','%+v',%+v,%+v);",
start, end, tran.Value.String(), tran.TokenValue.String(), tran.Type, tran.AddrToken, tran.To, tran.Gas.String(), interval)
r, err := db.Exec(sqlStr)
if err != nil {
log.Println("insert ", err)
} else {
count, err := r.RowsAffected()
if err != nil {
log.Println("insert ", err)
} else {
if count > 0 {
log.Println("new insert count:", count)
}
}
}
}
// 如果已经有数据,则直接累加
if id != 0 {
sqlStr = `update ` + stat_tblname + ` set sum_value = sum_value + ` + tran.Value.String() + `,
sum_token_value = sum_token_value + ` + tran.TokenValue.String() + `,
sum_gas = sum_gas + ` + tran.Gas.String() + `
where id = $1`
r, err := db.Exec(sqlStr, id)
if err != nil {
log.Println("update ", err)
return err
}
_, err = r.RowsAffected()
if err != nil {
log.Println("update ", err)
return err
}
// if count > 0 {
// log.Println("update count:", count)
// }
}
return nil
}
// 分组统计
func statSQL(db *sql.DB, tblname, stat_tblname string, start, end int64) (int64, error) {
if end <= start {
return 0, errors.New("end less than start")
}
interval := end - start
sql := `INSERT INTO ` + stat_tblname + `("start_time","end_time","sum_value","sum_token_value","type","addr_token","addr_to","sum_gas","interval")
select $1,$2 ,sum(e.value),sum(e.token_value),e.type,e.addr_token,e.addr_to,sum(e.gas),$3 from ` + tblname + ` e
where e.ttimestamp >= $4 and e.ttimestamp < $5 and e.addr_to != '' group by e.type, e.addr_to,e.addr_token`
r, err := db.Exec(sql, start, end, interval, start, end)
if err != nil {
log.Println("stat ", sql, err)
return 0, err
}
count, err := r.RowsAffected()
if err != nil {
log.Println("stat ", sql, err)
return 0, err
}
return count, nil
}
func queryMinTime(db *sql.DB, tblname string) int64 {
var tmp int64
err := db.QueryRow(fmt.Sprintf(`select min(ttimestamp) from %s;`, tblname)).Scan(&tmp)
if err != nil {
log.Println("queryMinTime:", err)
return 0
}
return tmp
}
// 最后一次更新时间
func queryLastTime(db *sql.DB, tblname string) int64 {
var tmp int64
err := db.QueryRow(fmt.Sprintf(`select max(end_time) from %s;`, tblname)).Scan(&tmp)
if err != nil {
log.Println("queryLastTime:", err)
return 0
}
return tmp
}