Skip to content

Commit

Permalink
pretty print top counts
Browse files Browse the repository at this point in the history
  • Loading branch information
facundoolano committed Jul 27, 2024
1 parent 88f8b47 commit 23a1ce1
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"log"
"math"
"os"
"regexp"
"strconv"
Expand Down Expand Up @@ -196,9 +197,34 @@ func loadLogs(dbs *dbSession) error {
func printTopTable(columnNames []string, rowValues [][]string) {
tab := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
fmt.Fprintf(tab, "%s\n", strings.ToUpper(strings.Join(columnNames, "\t")))
for _, value := range rowValues {
// TODO for last item (always the count) do pretty formatting e.g. 1.3K instead of 1301
fmt.Fprintf(tab, "%s\n", strings.Join(value, "\t"))
for _, row := range rowValues {
row[len(row)-1] = prettyPrintCount(row[len(row)-1])
fmt.Fprintf(tab, "%s\n", strings.Join(row, "\t"))
}
tab.Flush()
}

func prettyPrintCount(countStr string) string {
// FIXME some unnecessary work, first db stringifies, then this parses to int, then formats again.
// this suggests the query implementation and/or APIs could be made smarter
n, _ := strconv.Atoi(countStr)
if n == 0 {
return "0"
}

// HAZMAT: authored by chatgpt
// Define suffixes and corresponding values
suffixes := []string{"", "K", "M", "B", "T"}
base := 1000.0
absValue := math.Abs(float64(n))
magnitude := int(math.Floor(math.Log(absValue) / math.Log(base)))
value := absValue / math.Pow(base, float64(magnitude))

if magnitude == 0 {
// No suffix, present as an integer
return fmt.Sprintf("%d", n)
} else {
// Use the suffix and present as a float with 1 decimal place
return fmt.Sprintf("%.1f%s", value, suffixes[magnitude])
}
}

0 comments on commit 23a1ce1

Please sign in to comment.