-
Notifications
You must be signed in to change notification settings - Fork 19
/
stats.go
250 lines (205 loc) · 6.37 KB
/
stats.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
248
249
250
package main
import (
"database/sql"
"fmt"
"github.com/jmoiron/sqlx"
log "github.com/sirupsen/logrus"
"golbat/config"
"golbat/decoder"
"time"
)
func StartDbUsageStatsLogger(db *sqlx.DB) {
ticker := time.NewTicker(30 * time.Second)
go func() {
for {
<-ticker.C
stats := db.Stats()
log.Infof("DB - InUse: %d Idle %d WaitDuration %s", stats.InUse, stats.Idle, stats.WaitDuration)
}
}()
}
type PokemonIdToDelete struct {
Id string `db:"id"`
}
const databaseDeleteChunkSize = 500
func StartDatabaseArchiver(db *sqlx.DB) {
ticker := time.NewTicker(time.Minute)
go func() {
for {
<-ticker.C
log.Infof("DB - Archive of pokemon table - starting")
start := time.Now()
var resultCounter int64
var result sql.Result
var err error
start = time.Now()
for {
pokemonId := []PokemonIdToDelete{}
err = db.Select(&pokemonId,
fmt.Sprintf("SELECT id FROM pokemon WHERE expire_timestamp < UNIX_TIMESTAMP() AND expire_timestamp_verified = 1 LIMIT %d;", databaseDeleteChunkSize))
if err != nil {
log.Errorf("DB - Archive of pokemon table (expire time verified) select error [after %d rows] %s", resultCounter, err)
break
}
if len(pokemonId) == 0 {
break
}
var ids []string
for i := 0; i < len(pokemonId); i++ {
ids = append(ids, pokemonId[i].Id)
}
query, args, _ := sqlx.In("DELETE FROM pokemon WHERE id IN (?);", ids)
query = db.Rebind(query)
result, err = db.Exec(query, args...)
if err != nil {
log.Errorf("DB - Archive of pokemon table (expire time verified) error [after %d rows] %s", resultCounter, err)
break
} else {
rows, _ := result.RowsAffected()
resultCounter += rows
if rows < databaseDeleteChunkSize {
elapsed := time.Since(start)
log.Infof("DB - Archive of pokemon table (verified timestamps) took %s (%d rows)", elapsed, resultCounter)
break
}
}
}
log.Infof("DB - Archive of pokemon table - starting second phase (unverified timestamps)")
resultCounter = 0
start = time.Now()
for {
pokemonId := []PokemonIdToDelete{}
err = db.Select(&pokemonId,
fmt.Sprintf("SELECT id FROM pokemon WHERE expire_timestamp < (UNIX_TIMESTAMP() - 2400) AND expire_timestamp_verified = 0 LIMIT %d;", databaseDeleteChunkSize))
if err != nil {
log.Errorf("DB - Archive of pokemon table (unverified timestamps) select error [after %d rows] %s", resultCounter, err)
break
}
if len(pokemonId) == 0 {
break
}
var ids []string
for i := 0; i < len(pokemonId); i++ {
ids = append(ids, pokemonId[i].Id)
}
query, args, _ := sqlx.In("DELETE FROM pokemon WHERE id IN (?);", ids)
query = db.Rebind(query)
result, err = db.Exec(query, args...)
if err != nil {
log.Errorf("DB - Archive of pokemon table (unverified timestamps) error [after %d rows] %s", resultCounter, err)
break
} else {
rows, _ := result.RowsAffected()
resultCounter += rows
if rows < databaseDeleteChunkSize {
elapsed := time.Since(start)
log.Infof("DB - Archive of pokemon table (unverified timestamps) took %s (%d rows)", elapsed, resultCounter)
break
}
}
}
}
}()
}
func StartStatsExpiry(db *sqlx.DB) {
ticker := time.NewTicker(3*time.Hour + 7*time.Minute)
go func() {
for {
<-ticker.C
start := time.Now()
var result sql.Result
var err error
result, err = db.Exec("DELETE FROM pokemon_area_stats WHERE `datetime` < UNIX_TIMESTAMP() - 10080;")
elapsed := time.Since(start)
if err != nil {
log.Errorf("DB - Cleanup of pokemon_area_stats table error %s", err)
} else {
rows, _ := result.RowsAffected()
log.Infof("DB - Cleanup of pokemon_area_stats table took %s (%d rows)", elapsed, rows)
}
tables := []string{"pokemon_stats", "pokemon_shiny_stats", "pokemon_iv_stats", "pokemon_hundo_stats", "pokemon_nundo_stats", "invasion_stats", "quest_stats", "raid_stats"}
for _, table := range tables {
start = time.Now()
result, err = db.Exec(fmt.Sprintf("DELETE FROM %s WHERE `date` < DATE(NOW() - INTERVAL %d DAY);", table, config.Config.Cleanup.StatsDays))
elapsed = time.Since(start)
if err != nil {
log.Errorf("DB - Cleanup of %s table error %s", table, err)
} else {
rows, _ := result.RowsAffected()
log.Infof("DB - Cleanup of %s table took %s (%d rows)", table, elapsed, rows)
}
}
}
}()
}
func StartIncidentExpiry(db *sqlx.DB) {
ticker := time.NewTicker(time.Hour + 11*time.Minute)
go func() {
for {
<-ticker.C
start := time.Now()
var result sql.Result
var err error
result, err = db.Exec("DELETE FROM incident WHERE expiration < UNIX_TIMESTAMP();")
elapsed := time.Since(start)
if err != nil {
log.Errorf("DB - Cleanup of incident table error %s", err)
} else {
rows, _ := result.RowsAffected()
log.Infof("DB - Cleanup of incident table took %s (%d rows)", elapsed, rows)
}
}
}()
}
func StartQuestExpiry(db *sqlx.DB) {
ticker := time.NewTicker(time.Hour + 1*time.Minute)
go func() {
for {
<-ticker.C
start := time.Now()
var totalRows int64 = 0
var result sql.Result
var err error
result, err = db.Exec("UPDATE pokestop " +
"SET " +
"quest_type = NULL," +
"quest_timestamp = NULL," +
"quest_target = NULL," +
"quest_conditions = NULL," +
"quest_rewards = NULL," +
"quest_template = NULL," +
"quest_title = NULL " +
"WHERE quest_expiry < UNIX_TIMESTAMP();")
if err != nil {
log.Errorf("DB - Cleanup of quest table error %s", err)
} else {
rows, _ := result.RowsAffected()
totalRows += rows
if rows > 0 {
decoder.ClearPokestopCache()
}
}
result, err = db.Exec("UPDATE pokestop " +
"SET " +
"alternative_quest_type = NULL," +
"alternative_quest_timestamp = NULL," +
"alternative_quest_target = NULL," +
"alternative_quest_conditions = NULL," +
"alternative_quest_rewards = NULL," +
"alternative_quest_template = NULL," +
"alternative_quest_title = NULL " +
"WHERE alternative_quest_expiry < UNIX_TIMESTAMP();")
if err != nil {
log.Errorf("DB - Cleanup of quest table error %s", err)
} else {
rows, _ := result.RowsAffected()
totalRows += rows
if rows > 0 {
decoder.ClearPokestopCache()
}
}
elapsed := time.Since(start)
log.Infof("DB - Cleanup of quest table took %s (%d quests)", elapsed, totalRows)
}
}()
}