Skip to content

Commit

Permalink
Merge pull request #5 from prashantgupta24/dev
Browse files Browse the repository at this point in the history
adding service selection to tracker
  • Loading branch information
prashantgupta24 authored Mar 20, 2019
2 parents 0198d73 + cbb7e39 commit 7330e5e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 30 deletions.
19 changes: 12 additions & 7 deletions example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,36 @@ import (
func main() {
fmt.Println("starting activity tracker")

timeToCheck := 5
frequency := 5 //value always in seconds

activityTracker := &tracker.Instance{
TimeToCheck: time.Duration(timeToCheck),
Frequency: frequency,
}
heartbeatCh, quitActivityTracker := activityTracker.Start()

//This starts the tracker for all services
heartbeatCh := activityTracker.Start()

//if you only want to track certain services, you can use StartWithServices
//heartbeatCh := activityTracker.StartWithServices(service.MouseClickHandler(), service.MouseCursorHandler())

timeToKill := time.NewTicker(time.Second * 30)

for {
select {
case heartbeat := <-heartbeatCh:
if !heartbeat.IsActivity {
fmt.Printf("no activity detected in the last %v seconds\n\n", int(timeToCheck))
fmt.Printf("no activity detected in the last %v seconds\n\n", int(frequency))
} else {
fmt.Printf("activity detected in the last %v seconds. ", int(timeToCheck))
fmt.Printf("activity detected in the last %v seconds. ", int(frequency))
fmt.Printf("Activity type:\n")
for activity, time := range heartbeat.Activity {
fmt.Printf("%v at %v\n", activity.ActivityType, time)
fmt.Printf("%v ---> %v\n", activity.ActivityType, time)
}
fmt.Println()
}
case <-timeToKill.C:
fmt.Println("time to kill app")
quitActivityTracker <- struct{}{}
activityTracker.Quit()
return
}
}
Expand Down
12 changes: 8 additions & 4 deletions internal/pkg/service/mouseClickHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"github.com/prashantgupta24/activity-tracker/pkg/activity"
)

type MouseClickHandler struct {
type mouseClickHandler struct {
tickerCh chan struct{}
}

func (m *MouseClickHandler) Start(activityCh chan *activity.Type) {
func (m *mouseClickHandler) Start(activityCh chan *activity.Type) {
m.tickerCh = make(chan struct{})
registrationFree := make(chan struct{})

Expand All @@ -38,15 +38,19 @@ func (m *MouseClickHandler) Start(activityCh chan *activity.Type) {
}()
}

func (m *MouseClickHandler) Trigger() {
func MouseClickHandler() *mouseClickHandler {
return &mouseClickHandler{}
}

func (m *mouseClickHandler) Trigger() {
//doing it the non-blocking sender way
select {
case m.tickerCh <- struct{}{}:
default:
//service is blocked, handle it somehow?
}
}
func (m *MouseClickHandler) Close() {
func (m *mouseClickHandler) Close() {
close(m.tickerCh)
}

Expand Down
12 changes: 8 additions & 4 deletions internal/pkg/service/mouseCursorHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/prashantgupta24/activity-tracker/pkg/activity"
)

type MouseCursorHandler struct {
type mouseCursorHandler struct {
tickerCh chan struct{}
}

Expand All @@ -17,7 +17,7 @@ type cursorInfo struct {
currentMousePos *mouse.Position
}

func (m *MouseCursorHandler) Start(activityCh chan *activity.Type) {
func (m *mouseCursorHandler) Start(activityCh chan *activity.Type) {

m.tickerCh = make(chan struct{})

Expand Down Expand Up @@ -45,15 +45,19 @@ func (m *MouseCursorHandler) Start(activityCh chan *activity.Type) {
}()
}

func (m *MouseCursorHandler) Trigger() {
func MouseCursorHandler() *mouseCursorHandler {
return &mouseCursorHandler{}
}

func (m *mouseCursorHandler) Trigger() {
//doing it the non-blocking sender way
select {
case m.tickerCh <- struct{}{}:
default:
//service is blocked, handle it somehow?
}
}
func (m *MouseCursorHandler) Close() {
func (m *mouseCursorHandler) Close() {
close(m.tickerCh)
}

Expand Down
12 changes: 8 additions & 4 deletions internal/pkg/service/screenChangeHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/prashantgupta24/activity-tracker/pkg/activity"
)

type ScreenChangeHandler struct {
type screenChangeHandler struct {
tickerCh chan struct{}
}

Expand All @@ -17,7 +17,7 @@ type screenInfo struct {
currentPixelColor string
}

func (s *ScreenChangeHandler) Start(activityCh chan *activity.Type) {
func (s *screenChangeHandler) Start(activityCh chan *activity.Type) {

s.tickerCh = make(chan struct{})

Expand Down Expand Up @@ -48,15 +48,19 @@ func (s *ScreenChangeHandler) Start(activityCh chan *activity.Type) {
}()
}

func (s *ScreenChangeHandler) Trigger() {
func ScreenChangeHandler() *screenChangeHandler {
return &screenChangeHandler{}
}

func (s *screenChangeHandler) Trigger() {
//doing it the non-blocking sender way
select {
case s.tickerCh <- struct{}{}:
default:
//service is blocked, handle it somehow?
}
}
func (s *ScreenChangeHandler) Close() {
func (s *screenChangeHandler) Close() {
close(s.tickerCh)
}

Expand Down
23 changes: 15 additions & 8 deletions pkg/tracker/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,16 @@ const (
preHeartbeatTime = time.Millisecond * 100
)

func (tracker *Instance) Start() (heartbeatCh chan *Heartbeat, quit chan struct{}) {

func (tracker *Instance) StartWithServices(services ...service.Instance) (heartbeatCh chan *Heartbeat) {
//register service handlers
tracker.registerHandlers(&service.MouseClickHandler{}, &service.MouseCursorHandler{},
&service.ScreenChangeHandler{})
tracker.registerHandlers(services...)

//returned channels
heartbeatCh = make(chan *Heartbeat, 1)
quit = make(chan struct{})
tracker.quit = make(chan struct{})

go func(tracker *Instance) {
timeToCheck := tracker.TimeToCheck
timeToCheck := time.Duration(tracker.Frequency)
//tickers
tickerHeartbeat := time.NewTicker(time.Second * timeToCheck)
tickerWorker := time.NewTicker(time.Second*timeToCheck - preHeartbeatTime)
Expand Down Expand Up @@ -62,7 +60,7 @@ func (tracker *Instance) Start() (heartbeatCh chan *Heartbeat, quit chan struct{
case activity := <-tracker.activityCh:
activities[activity] = time.Now()
//log.Printf("activity received: %#v\n", activity)
case <-quit:
case <-tracker.quit:
log.Printf("stopping activity tracker\n")
//close all services for a clean exit
for service := range tracker.services {
Expand All @@ -73,7 +71,16 @@ func (tracker *Instance) Start() (heartbeatCh chan *Heartbeat, quit chan struct{
}
}(tracker)

return heartbeatCh, quit
return heartbeatCh
}

func (tracker *Instance) Quit() {
tracker.quit <- struct{}{}
}

func (tracker *Instance) Start() (heartbeatCh chan *Heartbeat) {
return tracker.StartWithServices(service.MouseClickHandler(), service.MouseCursorHandler(),
service.ScreenChangeHandler())
}

func makeActivityMap() map[*activity.Type]time.Time {
Expand Down
7 changes: 4 additions & 3 deletions pkg/tracker/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
)

type Instance struct {
TimeToCheck time.Duration
activityCh chan *activity.Type
services map[service.Instance]bool
Frequency int
activityCh chan *activity.Type
quit chan struct{}
services map[service.Instance]bool
}

type Heartbeat struct {
Expand Down

0 comments on commit 7330e5e

Please sign in to comment.