package metrics

import (
	"database/sql"
	"time"
)

// StartDBCollector begins a background goroutine that reads sql.DBStats
// every interval and updates the Prometheus DB pool gauges.
func StartDBCollector(db *sql.DB, interval time.Duration) {
	if db == nil {
		return
	}
	go func() {
		ticker := time.NewTicker(interval)
		defer ticker.Stop()
		for range ticker.C {
			stats := db.Stats()
			DBOpenConnections.Set(float64(stats.OpenConnections))
			DBInUseConnections.Set(float64(stats.InUse))
			DBIdleConnections.Set(float64(stats.Idle))
			DBWaitCount.Set(float64(stats.WaitCount))
			DBWaitDuration.Set(stats.WaitDuration.Seconds())
		}
	}()
}
