Skip to content

Commit

Permalink
perf: make pman ls ~4x faster
Browse files Browse the repository at this point in the history
this is done by only checking the lastEdited value once a day and then caching those values in the db
  • Loading branch information
theredditbandit committed May 21, 2024
1 parent 62794de commit 33f6f03
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 8 deletions.
6 changes: 4 additions & 2 deletions cmd/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var lsCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, _ []string) error {
filterFlag, _ := cmd.Flags().GetString("f")
oldUI, _ := cmd.Flags().GetBool("c")
refreshLastEditTime, _ := cmd.Flags().GetBool("r")
data, err := db.GetAllRecords(db.DBName, StatusBucket)
if err != nil {
return err
Expand All @@ -29,14 +30,15 @@ var lsCmd = &cobra.Command{
data = utils.FilterByStatuses(data, strings.Split(filterFlag, ","))
}
if oldUI {
return ui.RenderTable(data)
return ui.RenderTable(data, refreshLastEditTime)
}
return ui.RenderInteractiveTable(data)
return ui.RenderInteractiveTable(data, refreshLastEditTime)
},
}

func init() {
rootCmd.AddCommand(lsCmd)
lsCmd.Flags().String("f", "", "Filter projects by status. Usage : pman ls --f <status1[,status2]>")
lsCmd.Flags().Bool("c", false, "list projects using the colorful table. Usage : pman ls --c")
lsCmd.Flags().Bool("r", false, "Refresh Last Edited time: pman ls --r")
}
45 changes: 42 additions & 3 deletions pkg/ui/interactiveTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,55 @@ func (m tableModel) View() string {
return baseStyle.Render(m.table.View()) + "\n"
}

func RenderInteractiveTable(data map[string]string) error {
func RenderInteractiveTable(data map[string]string, refreshLastEditedTime bool) error {
var rows []table.Row
var lastEdited string

col := []table.Column{
{Title: "Status", Width: 20},
{Title: "Project", Width: 40},
{Title: "Last Edited", Width: 20},
}
var rows []table.Row

if refreshLastEditedTime {
err := utils.UpdateLastEditedTime()
if err != nil {
return err
}
} else {
rec, err := db.GetRecord(db.DBName, "lastRefreshTime", pkg.ConfigBucket)
if err != nil { // lastRefreshTime key does not exist in db
refreshLastEditedTime = true
err := utils.UpdateLastEditedTime()
if err != nil {
return err
}
}
if utils.DayPassed(rec) { // lastEdited values are more than a day old. need to refresh them
refreshLastEditedTime = true
err := utils.UpdateLastEditedTime()
if err != nil {
return err
}
}
}

for proj, status := range data {
alias, err := db.GetRecord(db.DBName, proj, pkg.ProjectAliasBucket)
lastEdited := utils.GetLastModifiedTime(db.DBName, proj)
if refreshLastEditedTime {
lastEdited = utils.GetLastModifiedTime(db.DBName, proj)
rec := map[string]string{proj: lastEdited}
err := db.WriteToDB(db.DBName, rec, pkg.LastUpdatedBucket)
if err != nil {
return err
}
} else {
lE, err := db.GetRecord(db.DBName, proj, pkg.LastUpdatedBucket)
if err != nil {
return err
}
lastEdited = lE
}
if err == nil {
pname := fmt.Sprintf("%s (%s)", proj, alias)
row := []string{utils.TitleCase(status), pname, lastEdited} // Status | projectName (alias) | lastEdited
Expand Down
44 changes: 41 additions & 3 deletions pkg/ui/statusTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,51 @@ import (
)

// RenderTable: renders the given data as a table
func RenderTable(data map[string]string) error {
func RenderTable(data map[string]string, refreshLastEditedTime bool) error {
var tableData [][]string
var lastEdited string

if refreshLastEditedTime {
err := utils.UpdateLastEditedTime()
if err != nil {
return err
}
} else {
rec, err := db.GetRecord(db.DBName, "lastRefreshTime", pkg.ConfigBucket)
if err != nil { // lastRefreshTime key does not exist in db
refreshLastEditedTime = true
err := utils.UpdateLastEditedTime()
if err != nil {
return err
}
}
if utils.DayPassed(rec) { // lastEdited values are more than a day old. need to refresh them
refreshLastEditedTime = true
err := utils.UpdateLastEditedTime()
if err != nil {
return err
}
}
}

for p, status := range data {
alias, err := db.GetRecord(db.DBName, p, pkg.ProjectAliasBucket)
lastEdited := utils.GetLastModifiedTime(db.DBName, p)
if refreshLastEditedTime {
lastEdited = utils.GetLastModifiedTime(db.DBName, p)
rec := map[string]string{p: lastEdited}
err := db.WriteToDB(db.DBName, rec, pkg.LastUpdatedBucket)
if err != nil {
return err
}
} else {
lE, err := db.GetRecord(db.DBName, p, pkg.LastUpdatedBucket)
if err != nil {
return err
}
lastEdited = lE
}
if err == nil {
pname := fmt.Sprintf("%s (%s)", p, alias)
pname := fmt.Sprintf("%s (%s) nil error", p, alias)
row := []string{utils.TitleCase(status), pname, lastEdited} // Status | projectName (alias) | lastEdited
tableData = append(tableData, row)
} else {
Expand Down
21 changes: 21 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"path/filepath"
"strconv"
"time"

"github.com/charmbracelet/glamour"
Expand Down Expand Up @@ -109,3 +110,23 @@ func ReadREADME(dbname, projectName string) ([]byte, error) {
}
return data, nil
}

func UpdateLastEditedTime() error {
r := fmt.Sprint(time.Now().Unix())
rec := map[string]string{"lastRefreshTime": r}
err := db.WriteToDB(db.DBName, rec, pkg.ConfigBucket)
if err != nil {
return err
}
return nil
}

func DayPassed(t string) bool {
oneDay := 86400
now := time.Now().Unix()
recTime, _ := strconv.ParseInt(t, 10, 64)
if now-recTime > int64(oneDay) {

Check failure on line 128 in pkg/utils/utils.go

View workflow job for this annotation

GitHub Actions / test (stable, ubuntu-latest)

S1008: should use 'return now-recTime > int64(oneDay)' instead of 'if now-recTime > int64(oneDay) { return true }; return false' (gosimple)
return true
}
return false
}

0 comments on commit 33f6f03

Please sign in to comment.