From a31dbbc7cf9ccfcd694fe06bb78cacf3062b7518 Mon Sep 17 00:00:00 2001 From: Till Kuhn Date: Thu, 2 Jan 2025 12:46:06 +0100 Subject: [PATCH] change column order --- .golangci.yml | 18 +++++++++++------- Makefile | 2 +- cmd/root.go | 3 ++- pkg/tracker/ioreg.go | 5 ++++- pkg/tracker/punch.go | 8 ++++---- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 44450e0..24ee1bb 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -49,6 +49,10 @@ issues: - text: "G404:" # Ignore weak random number generator lint. We do not need strong randomness here. linters: - gosec + - text: "G204:" # Ignore Subprocess launched with a potential tainted input or cmd arguments (uses a constant) + path: pkg/tracker/ioreg.go + linters: + - gosec - path: _test\.go linters: - gochecknoglobals @@ -57,16 +61,16 @@ issues: - noctx # this is the exception for commit, data and version variable which with pass in using ldflags - path: internal/version/version.go - linters: [gochecknoglobals] + linters: [ gochecknoglobals ] - path: cmd/punch.go - linters: [gochecknoglobals,mnd] + linters: [ gochecknoglobals,mnd ] - path: cmd/report.go - linters: [gochecknoglobals,mnd] + linters: [ gochecknoglobals,mnd ] - path: cmd/root.go - linters: [gochecknoglobals,mnd] + linters: [ gochecknoglobals,mnd ] - path: cmd/track.go - linters: [gochecknoglobals,mnd] + linters: [ gochecknoglobals,mnd ] - path: pkg/tracker/report.go - linters: [mnd] # magic number detection is annoying here + linters: [ mnd ] # magic number detection is annoying here - path: pkg/tracker/tracker.go - linters: [mnd] \ No newline at end of file + linters: [ mnd ] diff --git a/Makefile b/Makefile index 9bc7c22..3cf1b58 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ OSNAME = $(shell uname -o) PROJECT_PKG = $(shell grep -e ^module go.mod|cut -d' ' -f2|xargs) # git info for ldflags inspired by https://github.com/oras-project/oras/blob/main/Makefile GIT_COMMIT = $(shell git rev-parse --short HEAD) -GIT_TAG = $(shell git describe --tags --abbrev=0 2>/dev/null) +GIT_TAG = $(shell git describe --tags --abbrev=0 2>/dev/null) #GIT_DIRTY = $(shell test -n "`git status --porcelain`" && echo "dirty" || echo "clean") LDFLAGS = -w LDFLAGS += -X main.cmmit=${GIT_COMMIT} diff --git a/cmd/root.go b/cmd/root.go index 79a48c2..2673689 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -23,7 +23,8 @@ var ctxCancel context.CancelFunc var rootCmd = &cobra.Command{ Use: "billy-idle", Short: "Simple busy / idle time tracker inspired by the ancient article 'Inactivity and Idle Time on OS X'.", - Long: `Simple busy / idle time tracker based on the macOS timer called HIDIdleTime that tracks the last time you interacted with the computer, e.g. moved the mouse, typed a key, or interacted with the computer. + Long: `Simple busy / idle time tracker based on the macOS timer called HIDIdleTime that tracks the last time you interacted with the computer, +e.g. moved the mouse, typed a key, or interacted with the computer. billy-idle simply queries this value periodically using the ioreg utility that ships with macOS, and matches it against a pre-defined threshold. If exceeded, it will create a record for the busy time period in database. This data can later be used as input for time tracking tools or statistics.`, diff --git a/pkg/tracker/ioreg.go b/pkg/tracker/ioreg.go index a628f03..74eb912 100644 --- a/pkg/tracker/ioreg.go +++ b/pkg/tracker/ioreg.go @@ -9,12 +9,15 @@ import ( "time" ) +// limitDepth is passed to "-d" arg (limit tree to the given depth) to reduce amount of output +const limitDepth = 4 // 4 should be sufficient to catch relevant HIDIdleTime figures + var idleMatcher = regexp.MustCompile("\"HIDIdleTime\"\\s*=\\s*(\\d+)") // IdleTime gets the current idle time (HIDIdleTime) in milliseconds from the external ioreg command // todo optimize by limit depth, e.g. -d 4 should be sufficient ... -d limit tree to the given depth func IdleTime(ctx context.Context, cmd string) (int64, error) { - cmdExec := exec.CommandContext(ctx, cmd, "-d", "4", "-c", "IOHIDSystem") + cmdExec := exec.CommandContext(ctx, cmd, "-d", strconv.Itoa(limitDepth), "-c", "IOHIDSystem") stdout, err := cmdExec.Output() if err != nil { return 0, err diff --git a/pkg/tracker/punch.go b/pkg/tracker/punch.go index 27cf350..2279b66 100644 --- a/pkg/tracker/punch.go +++ b/pkg/tracker/punch.go @@ -27,7 +27,7 @@ func (t *Tracker) PunchReport(ctx context.Context) error { var spentBusyTotal, plannedBusyTotal time.Duration table := tablewriter.NewWriter(t.opts.Out) bold := tablewriter.Colors{tablewriter.Bold} - table.SetHeader([]string{"🕰 Date", "CW", "Weekday", "🐝 Busy", "⏲️ Planned", "Overtime"}) + table.SetHeader([]string{"CW", "📅 Date", "Weekday", "🐝 Busy", "⏲️ Plan", "🕰 Overtime"}) table.SetHeaderColor(bold, bold, bold, bold, bold, bold) table.SetBorder(false) table.SetAlignment(tablewriter.ALIGN_LEFT) @@ -46,8 +46,8 @@ func (t *Tracker) PunchReport(ctx context.Context) error { curWeek = week table.Append([]string{ - r.Day.Format(" 2006-01-02"), strconv.Itoa(week), + r.Day.Format(" 2006-01-02"), r.Day.Format("Monday"), FDur(spentDay), FDur(plannedDay), @@ -100,7 +100,7 @@ func (t *Tracker) UpsertPunchRecordWithPlannedDuration(ctx context.Context, busy } // No update - let's insert a new row - iQuery := `INSERT INTO ` + tablePunch + ` (day,busy_secs,client,planned_secs) VALUES ($1,$2,$3,$4) RETURNING id` + iQuery := `INSERT ` + `INTO ` + tablePunch + ` (day,busy_secs,client,planned_secs) VALUES ($1,$2,$3,$4) RETURNING id` var id int if err := t.db.QueryRowContext(ctx, iQuery, day, busyDuration.Seconds(), t.opts.ClientID, plannedDuration.Seconds()).Scan(&id); err != nil { return errors.Wrap(err, "unable to insert new record in busy table") @@ -113,7 +113,7 @@ func (t *Tracker) UpsertPunchRecordWithPlannedDuration(ctx context.Context, busy func (t *Tracker) PunchRecords(ctx context.Context) ([]PunchRecord, error) { // select sum(ROUND((JULIANDAY(busy_end) - JULIANDAY(busy_start)) * 86400)) || ' secs' AS total from track // current month: select * from punch where substr(day, 6, 2) = strftime('%m', 'now') - query := `SELECT day,busy_secs,planned_secs FROM ` + tablePunch + ` WHERE substr(day, 6, 2) = strftime('%m', 'now') ` + + query := `SELECT day,busy_secs,planned_secs ` + `FROM ` + tablePunch + ` WHERE substr(day, 6, 2) = strftime('%m', 'now') ` + `ORDER BY DAY` // WHERE busy_start >= DATE('now', '-7 days') ORDER BY busy_start LIMIT 500` // We could use get since we expect a single result, but this would return an error if nothing is found // which is a likely use case