package database import ( "context" "fmt" "log" "os" "strconv" "strings" "time" _ "github.com/jackc/pgx/v5/stdlib" _ "github.com/joho/godotenv/autoload" "gorm.io/driver/postgres" "gorm.io/gorm" ) type Services interface { Health() map[string]string GetDB(string) *gorm.DB } type service struct { simrsDB *gorm.DB satuDataDB *gorm.DB } var ( hostSimrs = os.Getenv("SIMRS_STAG_HOST") userNameSimrs = os.Getenv("SIMRS_STAG_USER") passwordSimrs = os.Getenv("SIMRS_STAG_PASS") dbNameSimrs = os.Getenv("SIMRS_STAG_NAME") portSimrs = os.Getenv("SIMRS_STAG_PORT") hostSatuData = os.Getenv("SATUDATA_HOST") userNameSatuData = os.Getenv("SATUDATA_USERNAME") passwordSatuData = os.Getenv("SATUDATA_PASSWORD") dbNameSatuData = os.Getenv("SATUDATA_NAME") portSatuData = os.Getenv("SATUDATA_PORT") dbInstance *service ) func New() Services { // Reuse Connection if dbInstance != nil { return dbInstance } simrs := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Jakarta", hostSimrs, userNameSimrs, passwordSimrs, dbNameSimrs, portSimrs) SimrsDB, err := gorm.Open(postgres.Open(simrs), &gorm.Config{}) if err != nil { log.Fatal("Failed to connect to database SIMRS: ", err) } else { log.Println("Successfully connected to the database SIMRS") } satuData := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Jakarta", hostSatuData, userNameSatuData, passwordSatuData, dbNameSatuData, portSatuData) satuDataDB, err := gorm.Open(postgres.Open(satuData), &gorm.Config{}) if err != nil { log.Fatal("Failed to connect to database SatuData: ", err) } else { log.Println("Successfully connected to the database SatuData") } mongoURI := fmt.Sprintf("mongodb://%s:%s@%s:%s/%s?authSource=admin", user, pass, host, port, database) dbInstance = &service{ simrsDB: SimrsDB, satuDataDB: satuDataDB, } return dbInstance } // Health checks the health of the database connection by pinging the database. // It returns a map with keys indicating various health statistics. func (s *service) Health() map[string]string { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() stats := make(map[string]string) // Ping the database using GORM db, err := s.simrsDB.DB() // Retrieve the underlying sql.DB instance from GORM if err != nil { stats["status"] = "down" stats["error"] = fmt.Sprintf("failed to get sql.DB from GORM: %v", err) log.Fatalf("failed to get sql.DB from GORM: %v", err) // Log the error and terminate the program return stats } err = db.PingContext(ctx) if err != nil { stats["status"] = "down" stats["error"] = fmt.Sprintf("db down: %v", err) log.Fatalf("db down: %v", err) // Log the error and terminate the program return stats } // Database is up, add more statistics stats["status"] = "up" stats["message"] = "It's healthy" // Get database stats dbStats := db.Stats() stats["open_connections"] = strconv.Itoa(dbStats.OpenConnections) stats["in_use"] = strconv.Itoa(dbStats.InUse) stats["idle"] = strconv.Itoa(dbStats.Idle) stats["wait_count"] = strconv.FormatInt(dbStats.WaitCount, 10) stats["wait_duration"] = dbStats.WaitDuration.String() stats["max_idle_closed"] = strconv.FormatInt(dbStats.MaxIdleClosed, 10) stats["max_lifetime_closed"] = strconv.FormatInt(dbStats.MaxLifetimeClosed, 10) // Evaluate stats to provide a health message if dbStats.OpenConnections > 40 { // Assuming 50 is the max for this example stats["message"] = "The database is experiencing heavy load." } if dbStats.WaitCount > 1000 { stats["message"] = "The database has a high number of wait events, indicating potential bottlenecks." } if dbStats.MaxIdleClosed > int64(dbStats.OpenConnections)/2 { stats["message"] = "Many idle connections are being closed, consider revising the connection pool settings." } if dbStats.MaxLifetimeClosed > int64(dbStats.OpenConnections)/2 { stats["message"] = "Many connections are being closed due to max lifetime, consider increasing max lifetime or revising the connection usage pattern." } return stats } // Close closes the database connection. // It logs a message indicating the disconnection from the specific database. // If the connection is successfully closed, it returns nil. // If an error occurs while closing the connection, it returns the error. func (s *service) GetDB(database string) *gorm.DB { if strings.ToLower(database) == "simrs" { return s.simrsDB } else if strings.ToLower(database) == "satudata" { return s.satuDataDB } log.Println("Database tidak ditemukan") return nil }