Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: show time spent on unsynced worklog entries #17

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions internal/ui/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

func toggleTracking(db *sql.DB, selectedIssue string, beginTs, endTs time.Time, comment string) tea.Cmd {
return func() tea.Msg {

row := db.QueryRow(`
SELECT issue_key
from issue_log
Expand Down Expand Up @@ -54,12 +53,10 @@ LIMIT 1

func insertManualEntry(db *sql.DB, issueKey string, beginTS time.Time, endTS time.Time, comment string) tea.Cmd {
return func() tea.Msg {

stmt, err := db.Prepare(`
INSERT INTO issue_log (issue_key, begin_ts, end_ts, comment, active, synced)
VALUES (?, ?, ?, ?, ?, ?);
`)

if err != nil {
return manualEntryInserted{issueKey, err}
}
Expand All @@ -83,15 +80,13 @@ func deleteActiveIssueLog(db *sql.DB) tea.Cmd {

func updateManualEntry(db *sql.DB, rowID int, issueKey string, beginTS time.Time, endTS time.Time, comment string) tea.Cmd {
return func() tea.Msg {

stmt, err := db.Prepare(`
UPDATE issue_log
SET begin_ts = ?,
end_ts = ?,
comment = ?
WHERE ID = ?;
`)

if err != nil {
return manualEntryUpdated{rowID, issueKey, err}
}
Expand Down Expand Up @@ -186,10 +181,10 @@ func fetchJIRAIssues(cl *jira.Client, jql string) tea.Cmd {

if issue.Fields.Status != nil {
status = issue.Fields.Status.Name

}
}
issues = append(issues, Issue{issueKey: issue.Key,
issues = append(issues, Issue{
issueKey: issue.Key,
issueType: issue.Fields.Type.Name,
summary: issue.Fields.Summary,
assignee: assignee,
Expand Down
3 changes: 2 additions & 1 deletion internal/ui/initial.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/charmbracelet/lipgloss"
)

func InitialModel(db *sql.DB, jiraClient *jira.Client, jql string, jiraTimeDeltaMins int) model {
func InitialModel(db *sql.DB, jiraClient *jira.Client, jql string, jiraTimeDeltaMins int, debug bool) model {
var stackItems []list.Item
var worklogListItems []list.Item
var syncedWorklogListItems []list.Item
Expand Down Expand Up @@ -45,6 +45,7 @@ func InitialModel(db *sql.DB, jiraClient *jira.Client, jql string, jiraTimeDelta
jiraTimeDeltaMins: jiraTimeDeltaMins,
showHelpIndicator: true,
trackingInputs: trackingInputs,
debug: debug,
}
m.issueList.Title = "fetching..."
m.issueList.SetStatusBarItemName("issue", "issues")
Expand Down
2 changes: 2 additions & 0 deletions internal/ui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type model struct {
issuesFetched bool
worklogList list.Model
unsyncedWLCount uint
unsyncedWLSecsSpent int
syncedWorklogList list.Model
activeIssueBeginTS time.Time
activeIssueEndTS time.Time
Expand All @@ -87,6 +88,7 @@ type model struct {
showHelpIndicator bool
terminalHeight int
trackingActive bool
debug bool
}

func (m model) Init() tea.Cmd {
Expand Down
6 changes: 6 additions & 0 deletions internal/ui/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@ type syncedWorklogEntry struct {
Comment string
}

func (entry worklogEntry) SecsSpent() int {
return int(entry.EndTS.Sub(entry.BeginTS).Seconds())
}

func (entry worklogEntry) Title() string {
return entry.Comment
}

func (entry worklogEntry) Description() string {
if entry.Error != nil {
return "error: " + entry.Error.Error()
Expand Down Expand Up @@ -99,6 +104,7 @@ func (entry worklogEntry) FilterValue() string { return entry.IssueKey }
func (entry syncedWorklogEntry) Title() string {
return entry.Comment
}

func (entry syncedWorklogEntry) Description() string {
durationMsg := humanize.Time(entry.EndTS)
timeSpentStr := humanizeDuration(int(entry.EndTS.Sub(entry.BeginTS).Seconds()))
Expand Down
5 changes: 3 additions & 2 deletions internal/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func RenderUI(db *sql.DB, jiraClient *jira.Client, jql string, jiraTimeDeltaMins int) {
if len(os.Getenv("DEBUG")) > 0 {
if len(os.Getenv("DEBUG_LOG")) > 0 {
f, err := tea.LogToFile("debug.log", "debug")
if err != nil {
fmt.Println("fatal:", err)
Expand All @@ -19,7 +19,8 @@ func RenderUI(db *sql.DB, jiraClient *jira.Client, jql string, jiraTimeDeltaMins
defer f.Close()
}

p := tea.NewProgram(InitialModel(db, jiraClient, jql, jiraTimeDeltaMins), tea.WithAltScreen())
debug := os.Getenv("DEBUG") == "true"
p := tea.NewProgram(InitialModel(db, jiraClient, jql, jiraTimeDeltaMins, debug), tea.WithAltScreen())
if _, err := p.Run(); err != nil {
fmt.Printf("Alas, there has been an error: %v", err)
os.Exit(1)
Expand Down
14 changes: 9 additions & 5 deletions internal/ui/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.activeIssueBeginTS = beginTS.Local()

endTS, err := time.ParseInLocation(string(timeFormat), m.trackingInputs[entryEndTS].Value(), time.Local)

if err != nil {
m.message = err.Error()
return m, tea.Batch(cmds...)
Expand Down Expand Up @@ -79,7 +78,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
beginTS = beginTS.Local()

endTS, err := time.ParseInLocation(string(timeFormat), m.trackingInputs[entryEndTS].Value(), time.Local)

if err != nil {
m.message = err.Error()
return m, tea.Batch(cmds...)
Expand Down Expand Up @@ -468,7 +466,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
for i := range m.trackingInputs {
m.trackingInputs[i].SetValue("")
}
m.unsyncedWLCount++
cmds = append(cmds, fetchLogEntries(m.db))
}
case manualEntryUpdated:
if msg.err != nil {
Expand All @@ -489,11 +487,17 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.messages = append(m.messages, message)
} else {
var items []list.Item
var secsSpent int
for _, e := range msg.entries {
secsSpent += e.SecsSpent()
items = append(items, list.Item(e))
}
m.worklogList.SetItems(items)
m.unsyncedWLSecsSpent = secsSpent
m.unsyncedWLCount = uint(len(msg.entries))
if m.debug {
m.message = "[io: log entries]"
}
}
case syncedLogEntriesFetchedMsg:
if msg.err != nil {
Expand All @@ -514,6 +518,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.worklogList.SetItem(msg.index, msg.entry)
} else {
m.unsyncedWLCount--
m.unsyncedWLSecsSpent -= msg.entry.SecsSpent()
}
case fetchActiveMsg:
if msg.err != nil {
Expand Down Expand Up @@ -547,7 +552,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.messages = append(m.messages, message)
} else {
cmds = append(cmds, fetchLogEntries(m.db))
m.unsyncedWLCount--
}
case activeTaskLogDeletedMsg:
if msg.err != nil {
Expand Down Expand Up @@ -593,7 +597,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
activeIssue.trackingActive = false
}
m.trackingActive = false
m.unsyncedWLCount++
cmds = append(cmds, fetchLogEntries(m.db))
} else {
m.lastChange = insertChange
if activeIssue != nil {
Expand Down
7 changes: 3 additions & 4 deletions internal/ui/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"github.com/charmbracelet/lipgloss"
)

var (
listWidth = 140
)
var listWidth = 140

func (m model) View() string {
var content string
Expand Down Expand Up @@ -162,7 +160,8 @@ func (m model) View() string {
if m.unsyncedWLCount == 1 {
entryWord = "entry"
}
unsyncedMsg = unsyncedCountStyle.Render(fmt.Sprintf("(%d unsynced %s)", m.unsyncedWLCount, entryWord))
unsyncedTimeMsg := humanizeDuration(m.unsyncedWLSecsSpent)
unsyncedMsg = unsyncedCountStyle.Render(fmt.Sprintf("%d unsynced %s (%s)", m.unsyncedWLCount, entryWord, unsyncedTimeMsg))
}
footerStr := fmt.Sprintf("%s%s%s%s",
modeStyle.Render("punchout"),
Expand Down