Skip to content

Commit

Permalink
sql: fix unused duration formatting for index recommendations
Browse files Browse the repository at this point in the history
Fixes: #85222 for DB Console

Previously, the index details page would not display unused index
durations correctly for short durations (<1 hour).  This change corrects
the formatting logic to display these durations correctly, and in a more
readable format.

Release justification: low risk, high benefit changes to existing
functionality

Release note: None
  • Loading branch information
Thomas Hardy committed Sep 8, 2022
1 parent 3ff179c commit fa5929a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
34 changes: 29 additions & 5 deletions pkg/sql/idxusage/index_usage_stats_rec.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package idxusage

import (
"fmt"
"math"
"time"

"github.com/cockroachdb/cockroach/pkg/server/serverpb"
Expand Down Expand Up @@ -42,7 +43,7 @@ var DropUnusedIndexDuration = settings.RegisterDurationSetting(
settings.NonNegativeDuration,
)

const indexExceedUsageDurationReasonPlaceholder = "This index has not been used in over %s and can be removed for better write performance."
const indexExceedUsageDurationReasonPlaceholder = "This index has not been used in over %sand can be removed for better write performance."
const indexNeverUsedReason = "This index has not been used and can be removed for better write performance."

// UnusedIndexRecommendationTestingKnobs provides hooks and knobs for unit tests.
Expand Down Expand Up @@ -124,9 +125,32 @@ func (i IndexStatsRow) recommendDropUnusedIndex(
}

func formatDuration(d time.Duration) string {
days := d / (24 * time.Hour)
hours := d % (24 * time.Hour)
minutes := hours % time.Hour
const numHoursInDay = 24
const numMinutesInHour = 60
const numSecondsInMinute = 60

return fmt.Sprintf("%dd%dh%dm", days, hours/time.Hour, minutes)
days := int64(d.Hours()) / (numHoursInDay)
hours := int64(math.Floor(d.Hours())) % numHoursInDay
minutes := int64(math.Floor(d.Minutes())) % numMinutesInHour
seconds := int64(math.Floor(d.Seconds())) % numSecondsInMinute

var daysSubstring string
var hoursSubstring string
var minutesSubstring string
var secondsSubstring string

if days > 0 {
daysSubstring = fmt.Sprintf("%d days, ", days)
}
if hours > 0 {
hoursSubstring = fmt.Sprintf("%d hours, ", hours)
}
if minutes > 0 {
minutesSubstring = fmt.Sprintf("%d minutes, ", minutes)
}
if seconds > 0 {
secondsSubstring = fmt.Sprintf("%d seconds, ", seconds)
}

return fmt.Sprintf("%s%s%s%s", daysSubstring, hoursSubstring, minutesSubstring, secondsSubstring)
}
2 changes: 1 addition & 1 deletion pkg/sql/idxusage/index_usage_stats_rec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestGetRecommendationsFromIndexStats(t *testing.T) {
TableID: 1,
IndexID: 2,
Type: serverpb.IndexRecommendation_DROP_UNUSED,
Reason: "This index has not been used in over 0d1h0m and can be removed for better write performance.",
Reason: "This index has not been used in over 1 hours, and can be removed for better write performance.",
},
},
},
Expand Down

0 comments on commit fa5929a

Please sign in to comment.