forked from statping/statping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
421 lines (379 loc) · 12.3 KB
/
database.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// Statping
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
// https://github.com/hunterlong/statping
//
// The licenses for most software and other practical works are designed
// to take away your freedom to share and change the works. By contrast,
// the GNU General Public License is intended to guarantee your freedom to
// share and change all versions of a program--to make sure it remains free
// software for all its users.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package core
import (
"fmt"
"github.com/go-yaml/yaml"
"github.com/hunterlong/statping/core/notifier"
"github.com/hunterlong/statping/types"
"github.com/hunterlong/statping/utils"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"os"
"time"
)
var (
// DbSession stores the Statping database session
DbSession *gorm.DB
DbModels []interface{}
)
func init() {
DbModels = []interface{}{&types.Service{}, &types.User{}, &types.Hit{}, &types.Failure{}, &types.Message{}, &types.Group{}, &types.Checkin{}, &types.CheckinHit{}, ¬ifier.Notification{}}
}
// DbConfig stores the config.yml file for the statup configuration
type DbConfig types.DbConfig
// failuresDB returns the 'failures' database column
func failuresDB() *gorm.DB {
return DbSession.Model(&types.Failure{})
}
// hitsDB returns the 'hits' database column
func hitsDB() *gorm.DB {
return DbSession.Model(&types.Hit{})
}
// servicesDB returns the 'services' database column
func servicesDB() *gorm.DB {
return DbSession.Model(&types.Service{})
}
// coreDB returns the single column 'core'
func coreDB() *gorm.DB {
return DbSession.Table("core").Model(&CoreApp)
}
// usersDB returns the 'users' database column
func usersDB() *gorm.DB {
return DbSession.Model(&types.User{})
}
// checkinDB returns the Checkin records for a service
func checkinDB() *gorm.DB {
return DbSession.Model(&types.Checkin{})
}
// checkinHitsDB returns the Checkin Hits records for a service
func checkinHitsDB() *gorm.DB {
return DbSession.Model(&types.CheckinHit{})
}
// messagesDb returns the Checkin records for a service
func messagesDb() *gorm.DB {
return DbSession.Model(&types.Message{})
}
// messagesDb returns the Checkin records for a service
func groupsDb() *gorm.DB {
return DbSession.Model(&types.Group{})
}
// HitsBetween returns the gorm database query for a collection of service hits between a time range
func (s *Service) HitsBetween(t1, t2 time.Time, group string, column string) *gorm.DB {
selector := Dbtimestamp(group, column)
if CoreApp.DbConnection == "postgres" {
timeQuery := fmt.Sprintf("service = %v AND created_at BETWEEN '%v.000000' AND '%v.000000'", s.Id, t1.UTC().Format(types.POSTGRES_TIME), t2.UTC().Format(types.POSTGRES_TIME))
return DbSession.Model(&types.Hit{}).Select(selector).Where(timeQuery)
} else {
return DbSession.Model(&types.Hit{}).Select(selector).Where("service = ? AND created_at BETWEEN ? AND ?", s.Id, t1.UTC().Format(types.TIME_DAY), t2.UTC().Format(types.TIME_DAY))
}
}
// CloseDB will close the database connection if available
func CloseDB() {
if DbSession != nil {
DbSession.DB().Close()
}
}
// AfterFind for Core will set the timezone
func (c *Core) AfterFind() (err error) {
c.CreatedAt = utils.Timezoner(c.CreatedAt, CoreApp.Timezone)
c.UpdatedAt = utils.Timezoner(c.UpdatedAt, CoreApp.Timezone)
return
}
// AfterFind for Service will set the timezone
func (s *Service) AfterFind() (err error) {
s.CreatedAt = utils.Timezoner(s.CreatedAt, CoreApp.Timezone)
s.UpdatedAt = utils.Timezoner(s.UpdatedAt, CoreApp.Timezone)
return
}
// AfterFind for Hit will set the timezone
func (h *Hit) AfterFind() (err error) {
h.CreatedAt = utils.Timezoner(h.CreatedAt, CoreApp.Timezone)
return
}
// AfterFind for Failure will set the timezone
func (f *Failure) AfterFind() (err error) {
f.CreatedAt = utils.Timezoner(f.CreatedAt, CoreApp.Timezone)
return
}
// AfterFind for USer will set the timezone
func (u *User) AfterFind() (err error) {
u.CreatedAt = utils.Timezoner(u.CreatedAt, CoreApp.Timezone)
u.UpdatedAt = utils.Timezoner(u.UpdatedAt, CoreApp.Timezone)
return
}
// AfterFind for Checkin will set the timezone
func (c *Checkin) AfterFind() (err error) {
c.CreatedAt = utils.Timezoner(c.CreatedAt, CoreApp.Timezone)
c.UpdatedAt = utils.Timezoner(c.UpdatedAt, CoreApp.Timezone)
return
}
// AfterFind for checkinHit will set the timezone
func (c *CheckinHit) AfterFind() (err error) {
c.CreatedAt = utils.Timezoner(c.CreatedAt, CoreApp.Timezone)
return
}
// AfterFind for Message will set the timezone
func (u *Message) AfterFind() (err error) {
u.CreatedAt = utils.Timezoner(u.CreatedAt, CoreApp.Timezone)
u.UpdatedAt = utils.Timezoner(u.UpdatedAt, CoreApp.Timezone)
u.StartOn = utils.Timezoner(u.StartOn.UTC(), CoreApp.Timezone)
u.EndOn = utils.Timezoner(u.EndOn.UTC(), CoreApp.Timezone)
return
}
// BeforeCreate for Hit will set CreatedAt to UTC
func (h *Hit) BeforeCreate() (err error) {
if h.CreatedAt.IsZero() {
h.CreatedAt = time.Now().UTC()
}
return
}
// BeforeCreate for Failure will set CreatedAt to UTC
func (f *Failure) BeforeCreate() (err error) {
if f.CreatedAt.IsZero() {
f.CreatedAt = time.Now().UTC()
}
return
}
// BeforeCreate for User will set CreatedAt to UTC
func (u *User) BeforeCreate() (err error) {
if u.CreatedAt.IsZero() {
u.CreatedAt = time.Now().UTC()
u.UpdatedAt = time.Now().UTC()
}
return
}
// BeforeCreate for Message will set CreatedAt to UTC
func (u *Message) BeforeCreate() (err error) {
if u.CreatedAt.IsZero() {
u.CreatedAt = time.Now().UTC()
u.UpdatedAt = time.Now().UTC()
}
return
}
// BeforeCreate for Service will set CreatedAt to UTC
func (s *Service) BeforeCreate() (err error) {
if s.CreatedAt.IsZero() {
s.CreatedAt = time.Now().UTC()
s.UpdatedAt = time.Now().UTC()
}
return
}
// BeforeCreate for Checkin will set CreatedAt to UTC
func (c *Checkin) BeforeCreate() (err error) {
if c.CreatedAt.IsZero() {
c.CreatedAt = time.Now().UTC()
c.UpdatedAt = time.Now().UTC()
}
return
}
// BeforeCreate for checkinHit will set CreatedAt to UTC
func (c *CheckinHit) BeforeCreate() (err error) {
if c.CreatedAt.IsZero() {
c.CreatedAt = time.Now().UTC()
}
return
}
// InsertCore create the single row for the Core settings in Statping
func (db *DbConfig) InsertCore() (*Core, error) {
CoreApp = &Core{Core: &types.Core{
Name: db.Project,
Description: db.Description,
Config: "config.yml",
ApiKey: utils.NewSHA1Hash(9),
ApiSecret: utils.NewSHA1Hash(16),
Domain: db.Domain,
MigrationId: time.Now().Unix(),
}}
CoreApp.DbConnection = db.DbConn
query := coreDB().Create(&CoreApp)
return CoreApp, query.Error
}
// Connect will attempt to connect to the sqlite, postgres, or mysql database
func (db *DbConfig) Connect(retry bool, location string) error {
if DbSession != nil {
return nil
}
var conn, dbType string
var err error
dbType = Configs.DbConn
if Configs.DbPort == 0 {
Configs.DbPort = DefaultPort(dbType)
}
switch dbType {
case "sqlite":
conn = location + "/statup.db"
dbType = "sqlite3"
case "mysql":
host := fmt.Sprintf("%v:%v", Configs.DbHost, Configs.DbPort)
conn = fmt.Sprintf("%v:%v@tcp(%v)/%v?charset=utf8&parseTime=True&loc=UTC", Configs.DbUser, Configs.DbPass, host, Configs.DbData)
case "postgres":
conn = fmt.Sprintf("host=%v port=%v user=%v dbname=%v password=%v timezone=UTC sslmode=disable", Configs.DbHost, Configs.DbPort, Configs.DbUser, Configs.DbData, Configs.DbPass)
case "mssql":
host := fmt.Sprintf("%v:%v", Configs.DbHost, Configs.DbPort)
conn = fmt.Sprintf("sqlserver://%v:%v@%v?database=%v", Configs.DbUser, Configs.DbPass, host, Configs.DbData)
}
dbSession, err := gorm.Open(dbType, conn)
if err != nil {
if retry {
utils.Log(1, fmt.Sprintf("Database connection to '%v' is not available, trying again in 5 seconds...", conn))
return db.waitForDb()
} else {
return err
}
}
err = dbSession.DB().Ping()
if err == nil {
DbSession = dbSession
utils.Log(1, fmt.Sprintf("Database %v connection '%v@%v:%v' at %v was successful.", dbType, Configs.DbUser, Configs.DbHost, Configs.DbPort, Configs.DbData))
}
return err
}
// waitForDb will sleep for 5 seconds and try to connect to the database again
func (db *DbConfig) waitForDb() error {
time.Sleep(5 * time.Second)
return db.Connect(true, utils.Directory)
}
// DatabaseMaintence will automatically delete old records from 'failures' and 'hits'
// this function is currently set to delete records 7+ days old every 60 minutes
func DatabaseMaintence() {
for range time.Tick(60 * time.Minute) {
utils.Log(1, "Checking for database records older than 3 months...")
since := time.Now().AddDate(0, -3, 0).UTC()
DeleteAllSince("failures", since)
DeleteAllSince("hits", since)
}
}
// DeleteAllSince will delete a specific table's records based on a time.
func DeleteAllSince(table string, date time.Time) {
sql := fmt.Sprintf("DELETE FROM %v WHERE created_at < '%v';", table, date.Format("2006-01-02"))
db := DbSession.Exec(sql)
if db.Error != nil {
utils.Log(2, db.Error)
}
}
// Update will save the config.yml file
func (db *DbConfig) Update() error {
var err error
config, err := os.Create(utils.Directory + "/config.yml")
if err != nil {
utils.Log(4, err)
return err
}
data, err := yaml.Marshal(db)
if err != nil {
utils.Log(3, err)
return err
}
config.WriteString(string(data))
config.Close()
return err
}
// Save will initially create the config.yml file
func (db *DbConfig) Save() (*DbConfig, error) {
var err error
config, err := os.Create(utils.Directory + "/config.yml")
if err != nil {
utils.Log(4, err)
return nil, err
}
db.ApiKey = utils.NewSHA1Hash(16)
db.ApiSecret = utils.NewSHA1Hash(16)
data, err := yaml.Marshal(db)
if err != nil {
utils.Log(3, err)
return nil, err
}
config.WriteString(string(data))
defer config.Close()
return db, err
}
// CreateCore will initialize the global variable 'CoreApp". This global variable contains most of Statping app.
func (c *DbConfig) CreateCore() *Core {
newCore := &types.Core{
Name: c.Project,
Description: c.Description,
Config: "config.yml",
ApiKey: c.ApiKey,
ApiSecret: c.ApiSecret,
Domain: c.Domain,
MigrationId: time.Now().Unix(),
}
db := coreDB().Create(&newCore)
if db.Error == nil {
CoreApp = &Core{Core: newCore}
}
CoreApp, err := SelectCore()
if err != nil {
utils.Log(4, err)
}
return CoreApp
}
// DropDatabase will DROP each table Statping created
func (db *DbConfig) DropDatabase() error {
utils.Log(1, "Dropping Database Tables...")
err := DbSession.DropTableIfExists("checkins")
err = DbSession.DropTableIfExists("checkin_hits")
err = DbSession.DropTableIfExists("notifications")
err = DbSession.DropTableIfExists("core")
err = DbSession.DropTableIfExists("failures")
err = DbSession.DropTableIfExists("hits")
err = DbSession.DropTableIfExists("services")
err = DbSession.DropTableIfExists("users")
err = DbSession.DropTableIfExists("messages")
return err.Error
}
// CreateDatabase will CREATE TABLES for each of the Statping elements
func (db *DbConfig) CreateDatabase() error {
var err error
utils.Log(1, "Creating Database Tables...")
for _, table := range DbModels {
if err := DbSession.CreateTable(table); err.Error != nil {
return err.Error
}
}
if err := DbSession.Table("core").CreateTable(&types.Core{}); err.Error != nil {
return err.Error
}
utils.Log(1, "Statping Database Created")
return err
}
// MigrateDatabase will migrate the database structure to current version.
// This function will NOT remove previous records, tables or columns from the database.
// If this function has an issue, it will ROLLBACK to the previous state.
func (db *DbConfig) MigrateDatabase() error {
utils.Log(1, "Migrating Database Tables...")
tx := DbSession.Begin()
defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()
if tx.Error != nil {
return tx.Error
}
for _, table := range DbModels {
tx = tx.AutoMigrate(table)
}
if err := tx.Table("core").AutoMigrate(&types.Core{}); err.Error != nil {
tx.Rollback()
utils.Log(3, fmt.Sprintf("Statping Database could not be migrated: %v", tx.Error))
return tx.Error
}
utils.Log(1, "Statping Database Migrated")
return tx.Commit().Error
}