package workers import ( "fmt" "time" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/postgres" "github.com/sirupsen/logrus" "github.com/wahyd4/next-dashboard/models" ) type DBWorker struct { DB *gorm.DB } func (worker *DBWorker) SetupDBWorker() { dbConfig := models.GlobalConfig.DB db := setupConnection(dbConfig) DoRequest: for _, check := range dbConfig.Checks { sendQuery(db, dbConfig, check) } time.Sleep(time.Second * time.Duration(models.GlobalConfig.Frequency)) goto DoRequest } func setupConnection(dbConfig *models.DBConfig) *gorm.DB { db, err := gorm.Open(dbConfig.Type, dbConfig.URL) if err != nil { logrus.Fatalf("Can't connect to target monitoring DB: %v", err) } return db } func sendQuery(db *gorm.DB, dbConfig *models.DBConfig, check *models.DBCheck) { logrus.Info("Log db summary") var count int64 if check.Field != "" { db.Table(check.Table).Where(fmt.Sprintf("%s = ?", check.Field), check.Value).Count(&count) db.Create(&models.DBSummary{ Database: dbConfig.Database, Table: check.Table, Field: check.Field, Value: check.Value, Name: check.Name, Count: count, }) } else { db.Table(check.Table).Count(&count) db.Create(&models.DBSummary{ Database: dbConfig.Database, Table: check.Table, Name: check.Name, Count: count, }) } }