|
2 | 2 |
|
3 | 3 | [](https://codecov.io/gh/prashantgupta24/activity-tracker) [](https://goreportcard.com/report/github.com/prashantgupta24/activity-tracker) [![version][version-badge]][RELEASES]
|
4 | 4 |
|
5 |
| -It is a libary that lets you monitor certain activities on your machine, and sends a heartbeat at a periodic (configurable) time detailing all the activity changes during that time. The activities that you want to monitor are pluggable handlers and can be added or removed according to your needs. |
| 5 | +It is a libary that lets you monitor certain activities on your machine, and sends a heartbeat at a periodic (configurable) time detailing all the activity changes during that time. The activities that you want to monitor are **pluggable** handlers for those activities and can be added or removed according to your needs. |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +` go get -u github.com/prashantgupta24/activity-tracker` |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | + |
| 14 | + heartbeatFrequency := 60 //value always in seconds |
| 15 | + workerFrequency := 5 //seconds |
| 16 | + |
| 17 | + activityTracker := &tracker.Instance{ |
| 18 | + HeartbeatFrequency: heartbeatFrequency, |
| 19 | + WorkerFrequency: workerFrequency, |
| 20 | + LogLevel: logging.Debug, |
| 21 | + } |
| 22 | + |
| 23 | + //This starts the tracker for all handlers. It gives you a channel |
| 24 | + //which you can listen to for heartbeat objects |
| 25 | + heartbeatCh := activityTracker.Start() |
| 26 | + |
| 27 | + //if you only want to track certain handlers, you can use StartWithhandlers |
| 28 | + //heartbeatCh := activityTracker.StartWithHanders(handler.MouseClickHandler(), handler.MouseCursorHandler()) |
| 29 | + |
| 30 | + |
| 31 | + select { |
| 32 | + case heartbeat := <-heartbeatCh: |
| 33 | + |
| 34 | + if !heartbeat.WasAnyActivity { |
| 35 | + |
| 36 | + logger.Infof("no activity detected in the last %v seconds", int(heartbeatFrequency)) |
| 37 | + |
| 38 | + } else { |
| 39 | + |
| 40 | + logger.Infof("activity detected in the last %v seconds.", int(heartbeatFrequency)) |
| 41 | + |
| 42 | + logger.Infof("Activity type:\n") |
| 43 | + |
| 44 | + for activityType, times := range heartbeat.ActivityMap { |
| 45 | + logger.Infof("activityType : %v times: %v\n", activityType, len(times)) |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | +## Output |
| 51 | + |
| 52 | +The above code created a tracker with all (`Mouse-click`, `Mouse-movement` and `Screen-Change`) handlers activated. The `heartbeat frequency` is set to 60 seconds, i.e. every 60 seconds I received a `heartbeat` which mentioned all activities that were captured. |
| 53 | + |
| 54 | +``` |
| 55 | +INFO[2019-03-30T15:52:01-07:00] starting activity tracker with 60s heartbeat and 5s worker frequency... |
| 56 | +
|
| 57 | +INFO[2019-03-30T15:53:01-07:00] activity detected in the last 60 seconds. |
| 58 | +
|
| 59 | +INFO[2019-03-30T15:53:01-07:00] Activity type: |
| 60 | +INFO[2019-03-30T15:53:01-07:00] activityType : screen-change times: 7 |
| 61 | +INFO[2019-03-30T15:53:01-07:00] activityType : mouse-click times: 10 |
| 62 | +INFO[2019-03-30T15:53:01-07:00] activityType : cursor-move times: 12 |
| 63 | +``` |
| 64 | + |
| 65 | +## How it works |
| 66 | + |
| 67 | +There are 2 primary configs required for the tracker to work: |
| 68 | + |
| 69 | +- `HeartbeatFrequency ` |
| 70 | + |
| 71 | +> The frequency at which you want the heartbeat (in seconds, default 60s) |
| 72 | +
|
| 73 | + |
| 74 | +- `WorkerFrequency` |
| 75 | + |
| 76 | +> The frequency at which you want the checks to happen within a heartbeat (default 60s). |
| 77 | +
|
| 78 | + |
| 79 | +The activity tracker gives you a `heartbeat` object every 60 seconds, that is based on the `HeartbeatFrequency`. But there is something else to understand here. In order for the tracker to know how many times an activity occured, or how many times you moved the cursor for example, it needs to query the mouse movement library `n` number of times. That's where the `WorkerFrequency` comes into play. |
| 80 | + |
| 81 | +The `WorkerFrequency` tells the tracker how many times to query each of the handlers in the tracker within a heartbeat. Let's say you want to know how many times the mouse cursor was moved. What you want to do is to start the tracker with the usual 60s `HeartbeatFrequency `, configured with a `Mouse-cursor` handler. In this case, you set the `WorkerFrequency` to 5 seconds. It will then keep checking the mouse coordinates every 5 seconds to see if there was a movement, and track each time there was a change. At the end of `HeartbeatFrequency`, it will track all the changes and send it in the `heatbeat` object. |
| 82 | + |
| 83 | +> If you are just concerned whether any activity happened within a heartbeat or not, you can set `WorkerFrequency` the same as `HeartbeatFrequency`. |
| 84 | +
|
| 85 | +>If you want to know how many times an activity occured within a heartbeat, you might want to set the `WorkerFrequency` to a low value, so that it keeps quering the handlers. |
| 86 | +
|
| 87 | + |
| 88 | +##### Note: If the `WorkerFrequency` and the `HeartbeatFrequency` are set the same, then the `WorkerFrequency` always is started a fraction of a second before the `HeartbeatFrequency` kicks in. This is done so that when the `heartbeat` is going to be generated at the end of `HeartbeatFrequency`, the worker should have done its job of querying each of the handlers before that. |
| 89 | + |
| 90 | +## Usecase |
| 91 | + |
| 92 | +Suppose you want to track Activities A, B and C on your machine, and you want a heartbeat every 5 minutes. What you want is for the tracker to send you heartbeats every 5 minutes, and each heartbeat would contain whether any of A, B or C occured within those 5 minutes, and if so, at what times. |
| 93 | + |
| 94 | +As another example, let's say you want to monitor whether there was any mouse click on your machine and you want to be monitor every 5 minutes. What you do is start the `Activity Tracker` with just the `mouse click` handler and `heartbeat` frequency set to 5 minutes. The `Start` function of the library gives you a channel which receives a `heartbeat` every 5 minutes, and it has details on whether there was a `click` in those 5 minutes, and if yes, the times the click happened. |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | +# Components |
| 99 | + |
| 100 | +### Heartbeat struct |
| 101 | + |
| 102 | +It is the data packet sent from the tracker library to the user. |
| 103 | + |
| 104 | + type Heartbeat struct { |
| 105 | + WasAnyActivity bool //whether any activity was detected |
| 106 | + ActivityMap map[activity.Type][]time.Time //activity type with its times |
| 107 | + Time time.Time //heartbeat time |
| 108 | + } |
| 109 | + |
| 110 | +`WasAnyActivity` tells if there was any activity within that time frame |
| 111 | +If there was, then the `ActivityMap` will tell you what type of activity it was and what all times it occured. |
| 112 | + |
| 113 | +The `Time` field is the time of the Heartbeat sent (not to be confused with |
| 114 | +the activity time, which is the time the activity occured within the time frame) |
| 115 | + |
| 116 | +### Tracker |
| 117 | + |
| 118 | +The tracker is the main struct for the library. |
| 119 | + |
| 120 | + //Instance is an instance of the tracker |
| 121 | + HeartbeatFrequency int //the frequency at which you want the heartbeat (in seconds, default 60s) |
| 122 | + WorkerFrequency int //therequency at which you want the checks to happen within a heartbeat (in seconds, default 5s) |
| 123 | + LogLevel string |
| 124 | + LogFormat string |
| 125 | + |
| 126 | + |
| 127 | +#### - `HeartbeatFrequency ` |
| 128 | + |
| 129 | +The frequency at which you want the heartbeat (in seconds, default 60s) |
| 130 | + |
| 131 | +##### Note: The `HeartbeatFrequency ` value can be set anywhere between 60 seconds - 300 seconds. Not setting it or setting it to anything other than the allowed range will revert it to default of 60s. |
| 132 | + |
| 133 | +#### - `WorkerFrequency` |
| 134 | + |
| 135 | +The frequency at which you want the checks to happen within a heartbeat (default 60s). |
| 136 | + |
| 137 | +##### Note: The `WorkerFrequency ` value can be set anywhere between 4 seconds - 60 seconds. It CANNOT be more than `HeartbeatFrequency` for obvious reasons. Not setting it or setting it to anything other than the allowed range will revert it to default of 60s. |
| 138 | + |
| 139 | + |
| 140 | +## Relationship between Activity and Handler |
| 141 | + |
| 142 | +Activity and Handler have a 1-1 mapping, i.e. each handler can only handle one type of activity, and vice-versa, each activity should be handled by one handler only. |
| 143 | + |
| 144 | +The `Type` in the `Handler` interface determines the type of activity the particular handler handles. |
| 145 | + |
| 146 | +## New pluggable handlers for activities |
| 147 | + |
| 148 | + |
| 149 | + //Instance is the main interface for a Handler for the tracker |
| 150 | + type Instance interface { |
| 151 | + Start(*log.Logger, chan *activity.Instance) |
| 152 | + Type() activity.Type |
| 153 | + Trigger() |
| 154 | + Close() |
| 155 | + } |
| 156 | + |
| 157 | +Any new type of handler for an activity can be easily added, it just needs to implement the above `Handler` interface and define what type of activity it is going to track, that's it! It can be plugged in with the tracker and then the tracker will include those activity checks in its heartbeat. |
| 158 | + |
| 159 | + |
| 160 | +## Currently supported list of activities/handlers |
| 161 | + |
| 162 | + |
| 163 | +### Activities |
| 164 | + |
| 165 | + MouseCursorMovement Type = "cursor-move" |
| 166 | + MouseClick Type = "mouse-click" |
| 167 | + ScreenChange Type = "screen-change" |
| 168 | + |
| 169 | +### Corresponding handlers |
| 170 | + |
| 171 | + mouseCursorHandler |
| 172 | + mouseClickHandler |
| 173 | + screenChangeHandler |
| 174 | + |
| 175 | + |
| 176 | +- Mouse click (whether any mouse click happened during the time frame) |
| 177 | +- Mouse cursor movement (whether the mouse cursor was moved during the time frame) |
| 178 | +- Screen change (whether the screen was changed anytime within that time frame) |
| 179 | + |
6 | 180 |
|
7 |
| -For example, let's say you want to monitor whether there was any mouse click on your machine and you want to be notified every 5 minutes if there was one or not. What you do is start the `Activity Tracker` with just the `mouse click` handler and `heartbeat` frequency set to 5 minutes. The `Start` function of the library gives you a channel which receives a `heartbeat` every 5 minutes, and it has details on whether there was a `click` in those 5 minutes. |
8 | 181 |
|
9 | 182 | [version-badge]: https://img.shields.io/github/release/prashantgupta24/activity-tracker.svg
|
10 | 183 | [RELEASES]: https://github.com/prashantgupta24/activity-tracker/releases
|
0 commit comments