This repository has been archived by the owner on Nov 23, 2022. It is now read-only.
/
chart.go
189 lines (151 loc) · 5.3 KB
/
chart.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
package chart
import (
"fmt"
"github.com/everstake/teztracker/models"
"github.com/jinzhu/gorm"
)
type (
// Repository is the block repo implementation.
Repository struct {
db *gorm.DB
}
Repo interface {
BlocksNumber(from, to int64, period string) ([]models.ChartData, error)
TransactionsVolume(from, to int64, period string) (data []models.ChartData, err error)
OperationsNumber(from, to int64, period, destination string, operationKinds, entrypoints []string) (data []models.ChartData, err error)
FeesVolume(from, to int64, period string) (data []models.ChartData, err error)
ActivationsNumber(from, to int64, period string) (data []models.ChartData, err error)
AvgBlockDelay(from, to int64, period string) (data []models.ChartData, err error)
DelegationVolume(from, to int64, period string) (data []models.ChartData, err error)
Bakers(from, to int64, period string) (data []models.ChartData, err error)
InsertWhaleAccounts(dayTime int64) error
WhaleAccounts(from, to int64, period string) (data []models.ChartData, err error)
}
)
// New creates an instance of repository using the provided db.
func New(db *gorm.DB) *Repository {
return &Repository{
db: db,
}
}
func (r *Repository) getDB(from, to int64, period string) *gorm.DB {
db := r.db.
Where("timestamp >= to_timestamp(?)", from).
Where("timestamp <= to_timestamp(?)", to).
Order("timestamp asc").
Group(fmt.Sprintf("date_trunc('%s', timestamp)", period))
return db
}
func (r *Repository) BlocksNumber(from, to int64, period string) (data []models.ChartData, err error) {
db := r.getDB(from, to, period)
err = db.Select(fmt.Sprintf("date_trunc('%s', timestamp) as timestamp, count(1) blocks", period)).
Table("tezos.blocks").
Find(&data).Error
if err != nil {
return nil, err
}
return data, nil
}
func (r *Repository) TransactionsVolume(from, to int64, period string) (data []models.ChartData, err error) {
db := r.getDB(from, to, period)
err = db.Select(fmt.Sprintf("date_trunc('%s', timestamp) as timestamp, sum(amount) transaction_volume", period)).
Table("tezos.operations").
Where("kind = 'transaction'").
Where("status = 'applied'").
Find(&data).Error
if err != nil {
return nil, err
}
return data, nil
}
func (r *Repository) OperationsNumber(from, to int64, period, destination string, operationKinds, entrypoints []string) (data []models.ChartData, err error) {
db := r.getDB(from, to, period)
db = db.Select(fmt.Sprintf("date_trunc('%s', timestamp) as timestamp, count(1) operations", period)).
Table("tezos.operations").
Where("status = 'applied'")
if destination != "" {
db = db.Where("destination = ?", destination)
}
if len(operationKinds) > 0 {
db = db.Where("kind IN (?)", operationKinds)
}
if len(entrypoints) > 0 {
db = db.Where("parameters_entrypoints IN (?)", entrypoints)
}
err = db.Find(&data).Error
if err != nil {
return nil, err
}
return data, nil
}
func (r *Repository) FeesVolume(from, to int64, period string) (data []models.ChartData, err error) {
db := r.getDB(from, to, period)
err = db.Select(fmt.Sprintf("date_trunc('%s', timestamp) as timestamp, sum(fee) fees", period)).
Table("tezos.operations").
Where("kind = 'transaction'").
Find(&data).Error
if err != nil {
return nil, err
}
return data, nil
}
func (r *Repository) ActivationsNumber(from, to int64, period string) (data []models.ChartData, err error) {
db := r.getDB(from, to, period)
err = db.Select(fmt.Sprintf("date_trunc('%s', timestamp) as timestamp, count(1) activations", period)).
Table("tezos.operations").
Where("kind = 'activate_account'").
Find(&data).Error
if err != nil {
return nil, err
}
return data, nil
}
func (r *Repository) AvgBlockDelay(from, to int64, period string) (data []models.ChartData, err error) {
db := r.getDB(from, to, period)
err = db.Select(fmt.Sprintf("date_trunc('%s', timestamp) as timestamp, extract(epoch from avg(block_delay)) average_delay", period)).
Table("tezos.blocks_delay").
Find(&data).Error
if err != nil {
return nil, err
}
return data, nil
}
func (r *Repository) DelegationVolume(from, to int64, period string) (data []models.ChartData, err error) {
db := r.getDB(from, to, period)
err = db.Select(fmt.Sprintf("date_trunc('%s', timestamp) as timestamp, sum(delegation_amount) delegation_volume", period)).
Table("tezos.delegations_view").
Find(&data).Error
if err != nil {
return nil, err
}
return data, nil
}
func (r *Repository) Bakers(from, to int64, period string) (data []models.ChartData, err error) {
db := r.getDB(from, to, period)
err = db.Select(fmt.Sprintf("date_trunc('%s', timestamp) as timestamp, count(distinct (baker)) bakers", period)).
Table("tezos.blocks").
Find(&data).Error
if err != nil {
return nil, err
}
return data, nil
}
func (r *Repository) InsertWhaleAccounts(dayTime int64) (err error) {
err = r.db.Exec("Select tezos.insert_whale_stat(?)", dayTime).Error
if err != nil {
return err
}
return nil
}
func (r *Repository) WhaleAccounts(from, to int64, period string) (data []models.ChartData, err error) {
err = r.db.Select("day as timestamp, whale_accounts").
Table("tezos.whale_accounts_periods").
Where("date_part('epoch',day)::int >= ?", from).
Where("date_part('epoch',day)::int <= ?", to).
Order("day asc").
Find(&data).Error
if err != nil {
return nil, err
}
return data, nil
}