Swap healthcheck endpoint to count the approximate number of history entries

This commit is contained in:
David Dworken 2023-09-15 17:21:16 -07:00
parent 63741a0d14
commit 6e33e1ee1d
No known key found for this signature in database
2 changed files with 6 additions and 6 deletions

View File

@ -8,11 +8,11 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
func (db *DB) CountHistoryEntries(ctx context.Context) (int64, error) { func (db *DB) CountApproximateHistoryEntries(ctx context.Context) (int64, error) {
var numDbEntries int64 var numDbEntries int64
tx := db.WithContext(ctx).Model(&shared.EncHistoryEntry{}).Count(&numDbEntries) err := db.WithContext(ctx).Raw("SELECT reltuples::bigint FROM pg_class WHERE relname = 'enc_history_entries'").Row().Scan(&numDbEntries)
if tx.Error != nil { if err != nil {
return 0, fmt.Errorf("tx.Error: %w", tx.Error) return 0, fmt.Errorf("DB Error: %w", err)
} }
return numDbEntries, nil return numDbEntries, nil

View File

@ -222,7 +222,7 @@ func (s *Server) feedbackHandler(w http.ResponseWriter, r *http.Request) {
func (s *Server) healthCheckHandler(w http.ResponseWriter, r *http.Request) { func (s *Server) healthCheckHandler(w http.ResponseWriter, r *http.Request) {
if s.isProductionEnvironment { if s.isProductionEnvironment {
encHistoryEntryCount, err := s.db.CountHistoryEntries(r.Context()) encHistoryEntryCount, err := s.db.CountApproximateHistoryEntries(r.Context())
checkGormError(err) checkGormError(err)
if encHistoryEntryCount < 1000 { if encHistoryEntryCount < 1000 {
panic("Suspiciously few enc history entries!") panic("Suspiciously few enc history entries!")
@ -285,7 +285,7 @@ func (s *Server) statsHandler(w http.ResponseWriter, r *http.Request) {
numEntriesProcessed, err := s.db.UsageDataTotal(r.Context()) numEntriesProcessed, err := s.db.UsageDataTotal(r.Context())
checkGormError(err) checkGormError(err)
numDbEntries, err := s.db.CountHistoryEntries(r.Context()) numDbEntries, err := s.db.CountApproximateHistoryEntries(r.Context())
checkGormError(err) checkGormError(err)
oneWeek := time.Hour * 24 * 7 oneWeek := time.Hour * 24 * 7