1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
package commands
import (
"context"
"database/sql"
"errors"
"fmt"
"strconv"
"time"
punchctx "punchcard/internal/context"
"punchcard/internal/queries"
"github.com/spf13/cobra"
)
func NewStatusCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "status",
Aliases: []string{"st"},
Short: "Show current status and summaries",
Long: `Show the current status including:
- Current week work summary by project and client
- Current month work summary by project and client
- Active timer status (if any)
- Clients and projects list (use --clients/-c or --projects/-p to show only one type)`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
q := punchctx.GetDB(cmd.Context())
if q == nil {
return fmt.Errorf("database not available in context")
}
ctx := cmd.Context()
// Get active timer status first
activeEntry, err := q.GetActiveTimeEntry(ctx)
var hasActiveTimer bool
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return fmt.Errorf("failed to check for active timer: %w", err)
}
hasActiveTimer = (err == nil)
// Display active timer status
if hasActiveTimer {
duration := time.Since(activeEntry.StartTime)
cmd.Printf("š“ Active Timer (running for %v)\n", duration.Round(time.Second))
// Get client info
client, err := findClient(ctx, q, strconv.FormatInt(activeEntry.ClientID, 10))
if err != nil {
cmd.Printf(" Client: ID %d (error getting name: %v)\n", activeEntry.ClientID, err)
} else {
clientInfo := client.Name
if client.BillableRate.Valid {
rateInDollars := float64(client.BillableRate.Int64) / 100.0
clientInfo += fmt.Sprintf(" ($%.2f/hr)", rateInDollars)
}
cmd.Printf(" Client: %s\n", clientInfo)
}
// Get project info if exists
if activeEntry.ProjectID.Valid {
project, err := findProject(ctx, q, strconv.FormatInt(activeEntry.ProjectID.Int64, 10))
if err != nil {
cmd.Printf(" Project: ID %d (error getting name: %v)\n", activeEntry.ProjectID.Int64, err)
} else {
projectInfo := project.Name
if project.BillableRate.Valid {
rateInDollars := float64(project.BillableRate.Int64) / 100.0
projectInfo += fmt.Sprintf(" ($%.2f/hr)", rateInDollars)
}
cmd.Printf(" Project: %s\n", projectInfo)
}
} else {
cmd.Printf(" Project: (none)\n")
}
// Show description if exists
if activeEntry.Description.Valid {
cmd.Printf(" Description: %s\n", activeEntry.Description.String)
} else {
cmd.Printf(" Description: (none)\n")
}
// Show billable rate if it exists on the time entry
if activeEntry.BillableRate.Valid {
rateInDollars := float64(activeEntry.BillableRate.Int64) / 100.0
cmd.Printf(" Billable Rate: $%.2f/hr\n", rateInDollars)
}
cmd.Printf("\n")
} else {
cmd.Printf("āŖ No active timer\n")
// Try to show the most recent time entry (will be completed since no active timer)
recentEntry, err := q.GetMostRecentTimeEntry(ctx)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return fmt.Errorf("failed to get most recent time entry: %w", err)
}
if err == nil {
// Display the most recent entry
duration := recentEntry.EndTime.Time.Sub(recentEntry.StartTime)
cmd.Printf("\nš Most Recent Entry\n")
// Get client info
client, err := findClient(ctx, q, strconv.FormatInt(recentEntry.ClientID, 10))
if err != nil {
cmd.Printf(" Client: ID %d (error getting name: %v)\n", recentEntry.ClientID, err)
} else {
clientInfo := client.Name
if client.BillableRate.Valid {
rateInDollars := float64(client.BillableRate.Int64) / 100.0
clientInfo += fmt.Sprintf(" ($%.2f/hr)", rateInDollars)
}
cmd.Printf(" Client: %s\n", clientInfo)
}
// Get project info if exists
if recentEntry.ProjectID.Valid {
project, err := findProject(ctx, q, strconv.FormatInt(recentEntry.ProjectID.Int64, 10))
if err != nil {
cmd.Printf(" Project: ID %d (error getting name: %v)\n", recentEntry.ProjectID.Int64, err)
} else {
projectInfo := project.Name
if project.BillableRate.Valid {
rateInDollars := float64(project.BillableRate.Int64) / 100.0
projectInfo += fmt.Sprintf(" ($%.2f/hr)", rateInDollars)
}
cmd.Printf(" Project: %s\n", projectInfo)
}
} else {
cmd.Printf(" Project: (none)\n")
}
// Show description if exists
if recentEntry.Description.Valid {
cmd.Printf(" Description: %s\n", recentEntry.Description.String)
} else {
cmd.Printf(" Description: (none)\n")
}
// Show billable rate if it exists on the time entry
if recentEntry.BillableRate.Valid {
rateInDollars := float64(recentEntry.BillableRate.Int64) / 100.0
cmd.Printf(" Billable Rate: $%.2f/hr\n", rateInDollars)
}
// Show time information
cmd.Printf(" Started: %s\n", recentEntry.StartTime.Format("Jan 2, 2006 at 3:04 PM"))
cmd.Printf(" Ended: %s\n", recentEntry.EndTime.Time.Format("Jan 2, 2006 at 3:04 PM"))
cmd.Printf(" Duration: %v\n", duration.Round(time.Minute))
}
cmd.Printf("\n")
}
// Display clients and projects
showClients, _ := cmd.Flags().GetBool("clients")
showProjects, _ := cmd.Flags().GetBool("projects")
if err := displayClientsAndProjects(ctx, cmd, q, showClients, showProjects); err != nil {
return fmt.Errorf("failed to display clients and projects: %w", err)
}
// Display current week summary
if err := displayWeekSummary(ctx, cmd, q); err != nil {
return fmt.Errorf("failed to display week summary: %w", err)
}
// Display current month summary
if err := displayMonthSummary(ctx, cmd, q); err != nil {
return fmt.Errorf("failed to display month summary: %w", err)
}
return nil
},
}
cmd.Flags().BoolP("clients", "c", false, "Show clients list")
cmd.Flags().BoolP("projects", "p", false, "Show projects list")
return cmd
}
func displayClientsAndProjects(ctx context.Context, cmd *cobra.Command, q *queries.Queries, showClients, showProjects bool) error {
if showClients && showProjects {
cmd.Printf("š Clients & Projects\n")
} else if showClients {
cmd.Printf("š„ Clients\n")
} else if showProjects {
cmd.Printf("š Projects\n")
}
clients, err := q.ListAllClients(ctx)
if err != nil {
return fmt.Errorf("failed to get clients: %w", err)
}
projects, err := q.ListAllProjects(ctx)
if err != nil {
return fmt.Errorf("failed to get projects: %w", err)
}
if len(clients) == 0 {
cmd.Printf(" No clients found\n\n")
return nil
}
// Group projects by client
projectsByClient := make(map[int64][]queries.ListAllProjectsRow)
for _, project := range projects {
projectsByClient[project.ClientID] = append(projectsByClient[project.ClientID], project)
}
if showClients && showProjects {
// Show clients with their projects nested
for _, client := range clients {
email := ""
if client.Email.Valid {
email = fmt.Sprintf(" <%s>", client.Email.String)
}
rate := ""
if client.BillableRate.Valid {
rateInDollars := float64(client.BillableRate.Int64) / 100.0
rate = fmt.Sprintf(" - $%.2f/hr", rateInDollars)
}
cmd.Printf(" ⢠%s%s (ID: %d)%s\n", client.Name, email, client.ID, rate)
clientProjects := projectsByClient[client.ID]
if len(clientProjects) == 0 {
cmd.Printf(" āāā (no projects)\n")
} else {
for i, project := range clientProjects {
prefix := "āāā"
if i == len(clientProjects)-1 {
prefix = "āāā"
}
rate := ""
if project.BillableRate.Valid {
rateInDollars := float64(project.BillableRate.Int64) / 100.0
rate = fmt.Sprintf(" - $%.2f/hr", rateInDollars)
}
cmd.Printf(" %s %s (ID: %d)%s\n", prefix, project.Name, project.ID, rate)
}
}
}
} else if showClients {
// Show only clients
for _, client := range clients {
email := ""
if client.Email.Valid {
email = fmt.Sprintf(" <%s>", client.Email.String)
}
rate := ""
if client.BillableRate.Valid {
rateInDollars := float64(client.BillableRate.Int64) / 100.0
rate = fmt.Sprintf(" - $%.2f/hr", rateInDollars)
}
cmd.Printf(" ⢠%s%s (ID: %d)%s\n", client.Name, email, client.ID, rate)
}
} else if showProjects {
// Show only projects with their client names
for _, project := range projects {
rate := ""
if project.BillableRate.Valid {
rateInDollars := float64(project.BillableRate.Int64) / 100.0
rate = fmt.Sprintf(" - $%.2f/hr", rateInDollars)
}
cmd.Printf(" ⢠%s (Client: %s, ID: %d)%s\n", project.Name, project.ClientName, project.ID, rate)
}
}
cmd.Printf("\n")
return nil
}
func displayWeekSummary(ctx context.Context, cmd *cobra.Command, q *queries.Queries) error {
cmd.Printf("š
This Week\n")
weekSummary, err := q.GetWeekSummaryByProject(ctx)
if err != nil {
return fmt.Errorf("failed to get week summary: %w", err)
}
if len(weekSummary) == 0 {
cmd.Printf(" No time entries this week\n\n")
return nil
}
// Group by client and calculate totals
clientTotals := make(map[int64]time.Duration)
currentClientID := int64(-1)
for _, row := range weekSummary {
duration := time.Duration(row.TotalSeconds) * time.Second
clientTotals[row.ClientID] += duration
if row.ClientID != currentClientID {
if currentClientID != -1 {
// Print client total
cmd.Printf(" Total: %v\n", clientTotals[currentClientID].Round(time.Minute))
}
cmd.Printf(" ⢠%s:\n", row.ClientName)
currentClientID = row.ClientID
}
projectName := "(no project)"
if row.ProjectName.Valid {
projectName = row.ProjectName.String
}
cmd.Printf(" - %s: %v\n", projectName, duration.Round(time.Minute))
}
// Print final client total
if currentClientID != -1 {
cmd.Printf(" Total: %v\n", clientTotals[currentClientID].Round(time.Minute))
}
// Print grand total
var grandTotal time.Duration
for _, total := range clientTotals {
grandTotal += total
}
cmd.Printf(" WEEK TOTAL: %v\n\n", grandTotal.Round(time.Minute))
return nil
}
func displayMonthSummary(ctx context.Context, cmd *cobra.Command, q *queries.Queries) error {
cmd.Printf("š This Month\n")
monthSummary, err := q.GetMonthSummaryByProject(ctx)
if err != nil {
return fmt.Errorf("failed to get month summary: %w", err)
}
if len(monthSummary) == 0 {
cmd.Printf(" No time entries this month\n\n")
return nil
}
// Group by client and calculate totals
clientTotals := make(map[int64]time.Duration)
currentClientID := int64(-1)
for _, row := range monthSummary {
duration := time.Duration(row.TotalSeconds) * time.Second
clientTotals[row.ClientID] += duration
if row.ClientID != currentClientID {
if currentClientID != -1 {
// Print client total
cmd.Printf(" Total: %v\n", clientTotals[currentClientID].Round(time.Minute))
}
cmd.Printf(" ⢠%s:\n", row.ClientName)
currentClientID = row.ClientID
}
projectName := "(no project)"
if row.ProjectName.Valid {
projectName = row.ProjectName.String
}
cmd.Printf(" - %s: %v\n", projectName, duration.Round(time.Minute))
}
// Print final client total
if currentClientID != -1 {
cmd.Printf(" Total: %v\n", clientTotals[currentClientID].Round(time.Minute))
}
// Print grand total
var grandTotal time.Duration
for _, total := range clientTotals {
grandTotal += total
}
cmd.Printf(" MONTH TOTAL: %v\n\n", grandTotal.Round(time.Minute))
return nil
}
|