Skip to content

Commit 16cf8b4

Browse files
authored
Merge pull request #3 from jms-guy/active_sessions
Active sessions
2 parents 560d79f + 6c41347 commit 16cf8b4

File tree

13 files changed

+118
-22
lines changed

13 files changed

+118
-22
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ go.work.sum
3131
# .idea/
3232
# .vscode/
3333

34-
timekeep.db
34+
testbuild.sh

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ sudo systemctl daemon-reload
176176
- Windows - Check for running processes on service start
177177
- CLI - commands
178178
- sorting for session history
179-
- show active sessions
179+
- show active sessions (✓)
180180
- enhance ping for more service info
181181

182182
## Contributing & Issues

cmd/cli/commands.go

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -225,19 +225,6 @@ func (s *CLIService) ResetStats(args []string, all bool) error {
225225
return nil
226226
}
227227

228-
// Formats a time.Duration value to display hours, minutes or seconds
229-
func (s *CLIService) formatDuration(prefix string, duration time.Duration) {
230-
if duration < time.Minute {
231-
fmt.Printf("%s%d seconds\n", prefix, int(duration.Seconds()))
232-
} else if duration < time.Hour {
233-
fmt.Printf("%s%d minutes\n", prefix, int(duration.Minutes()))
234-
} else {
235-
hours := int(duration.Hours())
236-
minutes := int(duration.Minutes()) % 60
237-
fmt.Printf("%s%dh %dm\n", prefix, hours, minutes)
238-
}
239-
}
240-
241228
// Removes active session and session records for all programs
242229
func (s *CLIService) ResetAllDatabase() error {
243230
err := s.AsRepo.RemoveAllSessions(context.Background())
@@ -275,3 +262,38 @@ func (s *CLIService) ResetDatabaseForProgram(program string) error {
275262

276263
return nil
277264
}
265+
266+
// Prints a list of currently active sessions being tracked by service
267+
func (s *CLIService) GetActiveSessions() error {
268+
activeSessions, err := s.AsRepo.GetAllActiveSessions(context.Background())
269+
if err != nil {
270+
return fmt.Errorf("error getting active sessions: %w", err)
271+
}
272+
if len(activeSessions) == 0 {
273+
fmt.Println("No active sessions.")
274+
return nil
275+
}
276+
277+
fmt.Println("Active sessions: ")
278+
for _, session := range activeSessions {
279+
duration := time.Since(session.StartTime)
280+
sessionDetails := fmt.Sprintf(" • %s - ", session.ProgramName)
281+
282+
s.formatDuration(sessionDetails, duration)
283+
}
284+
285+
return nil
286+
}
287+
288+
// Formats a time.Duration value to display hours, minutes or seconds
289+
func (s *CLIService) formatDuration(prefix string, duration time.Duration) {
290+
if duration < time.Minute {
291+
fmt.Printf("%s%d seconds\n", prefix, int(duration.Seconds()))
292+
} else if duration < time.Hour {
293+
fmt.Printf("%s%d minutes\n", prefix, int(duration.Minutes()))
294+
} else {
295+
hours := int(duration.Hours())
296+
minutes := int(duration.Minutes()) % 60
297+
fmt.Printf("%s%dh %dm\n", prefix, hours, minutes)
298+
}
299+
}

cmd/cli/commands_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,5 +222,16 @@ func TestPingService(t *testing.T) {
222222
}
223223

224224
err = s.PingService()
225-
assert.Nil(t, err, "PingService should not err")
225+
226+
assert.Contains(t, err.Error(), "service not running")
227+
}
228+
229+
func TestGetActiveSessions(t *testing.T) {
230+
s, err := setupTestServiceWithPrograms(t, "notepad.exe", "code.exe")
231+
if err != nil {
232+
t.Fatalf("Failed to setup test service: %v", err)
233+
}
234+
235+
err = s.GetActiveSessions()
236+
assert.Nil(t, err, "GetActiveSessions should not err")
226237
}

cmd/cli/ping_unsupported.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//go:build !windows && !linux
2+
3+
package main
4+
5+
func (s *CLIService) PingService() error {
6+
return nil
7+
}

cmd/cli/ping_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build !linux
1+
//go:build windows
22

33
package main
44

cmd/cli/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func (s *CLIService) RootCmd() *cobra.Command {
2424
rootCmd.AddCommand(s.refreshCmd())
2525
rootCmd.AddCommand(s.resetStatsCmd())
2626
rootCmd.AddCommand(s.pingServiceCmd())
27+
rootCmd.AddCommand(s.getActiveSessionsCmd())
2728

2829
return rootCmd
2930
}

cmd/cli/timekeep.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,15 @@ func (s *CLIService) pingServiceCmd() *cobra.Command {
126126
},
127127
}
128128
}
129+
130+
func (s *CLIService) getActiveSessionsCmd() *cobra.Command {
131+
return &cobra.Command{
132+
Use: "active",
133+
Aliases: []string{"Active", "ACTIVE"},
134+
Short: "Get list of current active sessions being tracked",
135+
Args: cobra.ExactArgs(0),
136+
RunE: func(cmd *cobra.Command, args []string) error {
137+
return s.GetActiveSessions()
138+
},
139+
}
140+
}

cmd/service/internal/events/events_unsupported.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ import (
99
"github.com/jms-guy/timekeep/internal/repository"
1010
)
1111

12-
func (e *EventController) MonitorProcesses(logger *log.Logger, s *sessions.SessionManager, pr repository.ProgramRepository, a repository.ActiveRepository, h repository.ActiveRepository, programs []string) {
13-
return
14-
}
15-
16-
func (e *EventController) startProcessMonitor(logger *log.Logger, programs []string) {
12+
func (e *EventController) MonitorProcesses(logger *log.Logger, s *sessions.SessionManager, pr repository.ProgramRepository, a repository.ActiveRepository, h repository.HistoryRepository, programs []string) {
1713
return
1814
}
1915

docs/commands.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,35 @@
33
- `add`
44
- Add a program to begin tracking. Add name of program's executable file name. May specify any number of programs to track in a single command, seperated by spaces in between
55
- `timekeep add notepad.exe`, `timekeep add notepad.exe code.exe chrome.exe`
6+
67
- `rm`
78
- Remove a program from tracking list. May specify any number of programs to remove in a single command, seperated by spaces in between. Takes `--all` flag to clear program list completely
89
- `timekeep rm notepad.exe`, `timekeep rm --all`
10+
911
- `ls`
1012
- Lists programs being tracked by service
1113
- `timekeep ls`
14+
1215
- `stats`
1316
- Shows stats for currently tracked programs. Accepts program name as argument to show in-depth stats for that program, else shows basic stats for all programs
1417
- `timekeep stats`, `timekeep stats notepad.exe`
18+
19+
- `active`
20+
- Display list of current active sessions being tracked by service
21+
- `timekeep active`
22+
1523
- `history`
1624
- Shows session history for a given program
1725
- `timekeep history notepad.exe`
26+
1827
- `refresh`
1928
- Sends a manual refresh command to the service
2029
- `timekeep refresh`
30+
2131
- `reset`
2232
- Reset tracking stats for given programs. Accepts multiple arguments seperated by space. Takes `--all` flag to reset all stats
2333
- `timekeep reset notepad.exe`, `timekeep reset --all`
34+
2435
- `ping`
2536
- Gets current state of Timekeep service
2637
- `timekeep ping`

0 commit comments

Comments
 (0)