Skip to content

Commit

Permalink
order stats counts descending.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonhadfield committed Dec 15, 2023
1 parent b80f717 commit e16a223
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 84 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ require (

require (
github.com/alexeyco/simpletable v1.0.0
github.com/dustin/go-humanize v1.0.1
github.com/gookit/color v1.5.4
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/divan/num2words v0.0.0-20170904212200-57dba452f942 h1:fJ8/Lid8fF4i7Bwl7vWKvG2KeZzr3yU4qG6h/DPdXLU=
github.com/divan/num2words v0.0.0-20170904212200-57dba452f942/go.mod h1:K88GQWK1aAiPMo9q2LZwyKBfEGnge7kmVVTUcZ61HSc=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
Expand Down
116 changes: 32 additions & 84 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"sort"
"time"

"github.com/dustin/go-humanize"

"github.com/alexeyco/simpletable"

// "github.com/fatih/color"
Expand Down Expand Up @@ -198,25 +200,27 @@ func showNoteHistory(data StatsData) {
Cells: []*simpletable.Cell{
{Align: simpletable.AlignLeft, Text: color.Bold.Sprintf("")},
{Align: simpletable.AlignLeft, Text: color.Bold.Sprintf("Title")},
{Align: simpletable.AlignLeft, Text: color.Bold.Sprintf("Timestamp")},
{Align: simpletable.AlignLeft, Text: color.Bold.Sprintf("Time")},
},
}
table.Body.Cells = append(table.Body.Cells, []*simpletable.Cell{
{Text: "Oldest"},
{Text: fmt.Sprintf("%s", data.OldestNote.Content.Title)},
{Text: fmt.Sprintf("%s", data.OldestNote.CreatedAt)},
{Text: fmt.Sprintf("%s", humanize.Time(time.UnixMicro(data.OldestNote.CreatedAtTimestamp)))},
})
table.Body.Cells = append(table.Body.Cells, []*simpletable.Cell{
{Text: "Newest"},
{Text: fmt.Sprintf("%s", data.NewestNote.Content.Title)},
{Text: fmt.Sprintf("%s", data.NewestNote.CreatedAt)},
{Text: fmt.Sprintf("%s", humanize.Time(time.UnixMicro(data.NewestNote.CreatedAtTimestamp)))},
})
table.Body.Cells = append(table.Body.Cells, []*simpletable.Cell{
{Text: "Most Recently Updated"},
{Text: fmt.Sprintf("%s", data.LastUpdatedNote.Content.Title)},
{Text: fmt.Sprintf("%s", data.LastUpdatedNote.UpdatedAt)},
{Text: fmt.Sprintf("%s", humanize.Time(time.UnixMicro(data.LastUpdatedNote.UpdatedAtTimestamp)))},
})
fmt.Printf("Note History\n")

color.Bold.Println("Note History")

table.Println()
}

Expand All @@ -230,32 +234,43 @@ func showItemCounts(data StatsData) {
}
table.Body.Cells = append(table.Body.Cells, []*simpletable.Cell{
{Text: "Notes"},
{Text: fmt.Sprintf("%d", data.CoreTypeCounter.counts["Note"])},
{Text: fmt.Sprintf("%s", humanize.Comma(data.CoreTypeCounter.counts["Note"]))},
})
table.Body.Cells = append(table.Body.Cells, []*simpletable.Cell{
{Text: "Tags"},
{Text: fmt.Sprintf("%d", data.CoreTypeCounter.counts["Tag"])},
{Text: fmt.Sprintf("%s", humanize.Comma(data.CoreTypeCounter.counts["Tag"]))},
})
table.Body.Cells = append(table.Body.Cells, []*simpletable.Cell{
{Text: "----------------"},
{Text: "------"},
})
table.Body.Cells = append(table.Body.Cells, []*simpletable.Cell{
{Text: "Notes (In Trash)"},
{Text: fmt.Sprintf("%d", data.CoreTypeCounter.counts["Notes (In Trash)"])},
{Text: fmt.Sprintf("%s", humanize.Comma(data.CoreTypeCounter.counts["Notes (In Trash)"]))},
})
table.Body.Cells = append(table.Body.Cells, []*simpletable.Cell{
{Text: "----------------"},
{Text: "------"},
})
for name, count := range data.OtherTypeCounter.counts {

var keys []string

for key := range data.OtherTypeCounter.counts {
keys = append(keys, key)
}

sort.SliceStable(keys, func(i, j int) bool {
return data.OtherTypeCounter.counts[keys[i]] > data.OtherTypeCounter.counts[keys[j]]
})

for _, k := range keys {
table.Body.Cells = append(table.Body.Cells, []*simpletable.Cell{
{Text: name},
{Text: fmt.Sprintf("%d", count)},
{Text: k},
{Text: fmt.Sprintf("%s", humanize.Comma(data.OtherTypeCounter.counts[k]))},
})
}

fmt.Printf("Item Counts\n")
color.Bold.Println("Item Counts")
table.Println()
}

Expand All @@ -267,13 +282,16 @@ func showLargestNotes(data StatsData) {
{Align: simpletable.AlignLeft, Text: color.Bold.Sprintf("Title")},
},
}

for _, note := range data.LargestNotes {
table.Body.Cells = append(table.Body.Cells, []*simpletable.Cell{
{Text: fmt.Sprintf("%d", note.GetContentSize())},
{Text: fmt.Sprintf("%s", humanize.Bytes(uint64(note.GetContentSize())))},
{Text: fmt.Sprintf("%s", note.Content.Title)},
})
}
fmt.Printf("Largest Notes\n")

color.Bold.Println("Largest Notes")

table.Println()
}

Expand Down Expand Up @@ -324,73 +342,3 @@ func (in *typeCounter) present() {
config.Delim = "^"
fmt.Println(columnize.Format(lines, config))
}

func timeSince(inTime time.Time) string {
now := time.Now()
if inTime.Location() != now.Location() {
now = now.In(inTime.Location())
}

if inTime.After(now) {
inTime, now = now, inTime
}

y1, M1, d1 := inTime.Date()
y2, M2, d2 := now.Date()

h1, m1, s1 := inTime.Clock()
h2, m2, s2 := now.Clock()

year := y2 - y1
month := M2 - M1
day := d2 - d1
hour := h2 - h1
min := m2 - m1
sec := s2 - s1

// Normalize negative values
if sec < 0 {
sec += 60
min--
}

if min < 0 {
min += 60
hour--
}

if hour < 0 {
hour += 24
day--
}

if day < 0 {
// days in month:
t := time.Date(y1, M1, 32, 0, 0, 0, 0, time.UTC)
day += 32 - t.Day()
month--
}

if month < 0 {
month += 12
year--
}

// determine output
switch {
case year > 0:
return fmt.Sprintf("%2d years %2d months %2d days", year, month, day)
case month > 0:
return fmt.Sprintf("%2d months %2d days %2d hours", month, day, hour)
case day > 0:
return fmt.Sprintf("%2d days %2d hours %2d minutes", day, hour, min)
case hour > 0:
return fmt.Sprintf("%2d hours %2d minutes", hour, min)
case min > 0:
return fmt.Sprintf("%2d minutes %2d seconds", min, sec)
case sec > 0:
return fmt.Sprintf("%2d seconds", sec)
default:
return "0"
}
}

0 comments on commit e16a223

Please sign in to comment.