/
activity_tracker_plugin.go
193 lines (177 loc) · 5.2 KB
/
activity_tracker_plugin.go
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
package chromebus
import (
"log"
"net/http"
"strconv"
"sync"
"time"
"github.com/robfig/cron"
)
const ActivityTracker PluginSpec = "ActivityTracker"
// HTTP response codes
const shouldBlockHttpStatus = 202
const shouldNotBlockHttpStatus = 203
const leewayIsExpired = 204
const leewayIsNotExpired = 205
var goofing bool = false
var ticker *time.Ticker
var lastStartedGoofing *time.Time
// total time tracking
var timesSpentGoofing *[1024]time.Duration
var index int
var totalDuration time.Duration
var wasWarned = false
var leewayExpired = false
var leewayMutex = new(sync.Mutex)
var suspendCount = 0
var suspendEnabled = false
var manualBlocked = false
var autoCloseCountdown = 0
var plugin *Plugin = &Plugin{
Init: func(input chan ChromebusRecord, aggregator Aggregator) {
log.Printf("Started Activity plugin")
cron := cron.New()
cron.AddFunc(resetCron, resetLeeway)
//cron.AddFunc("0 */5 * * * *", resetLeeway) // 3pm every day
cron.Start()
log.Printf("cron initialized with [%s]", resetCron)
initLeeway()
go monitor()
},
Handle: func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/" + string(ActivityTracker) + "/suspend":
if leewayExpired {
log.Printf("Suspending...")
leewayMutex.Lock()
suspendEnabled = true
suspendCount++
leewayMutex.Unlock()
}
case "/" + string(ActivityTracker) + "/isleewayexpired":
if leewayExpired {
w.WriteHeader(leewayIsExpired)
} else {
w.WriteHeader(leewayIsNotExpired)
}
case "/" + string(ActivityTracker) + "/open":
leewayMutex.Lock()
c := r.URL.Query().Get("count")
log.Printf("opening for " + c + " seconds")
var err error
autoCloseCountdown, err = strconv.Atoi(c)
if err != nil {
log.Printf("ERROR: " + c + " is not an integer")
}
leewayMutex.Unlock()
case "/" + string(ActivityTracker) + "/block":
leewayMutex.Lock()
manualBlocked = true
leewayMutex.Unlock()
case "/" + string(ActivityTracker) + "/unblock":
leewayMutex.Lock()
manualBlocked = false
leewayMutex.Unlock()
case "/" + string(ActivityTracker) + "/goofing":
leewayMutex.Lock()
goofing = true
if shouldBlockSites() {
w.WriteHeader(shouldBlockHttpStatus)
} else {
w.WriteHeader(shouldNotBlockHttpStatus)
}
leewayMutex.Unlock()
case "/" + string(ActivityTracker) + "/notgoofing":
leewayMutex.Lock()
goofing = false
w.WriteHeader(202)
leewayMutex.Unlock()
case "/" + string(ActivityTracker) + "/logstatus":
leewayMutex.Lock()
if autoCloseCountdown == 0 {
log.Printf("Site blocker engaged because autoCloseCountdown has reached 0")
}
if leewayExpired {
if suspendEnabled {
log.Printf("Leeway has expired (%dm/%dm) but suspend is enabled", totalDuration/time.Minute, minutesBeforeBlock)
} else {
log.Printf("Leeway has expired (%dm/%dm)", totalDuration/time.Minute, minutesBeforeBlock)
}
}
leewayMutex.Unlock()
default:
log.Printf("not recognized... %s" + r.URL.Path)
}
},
Cleanup: func() {
if leewayExpired {
// TODO: pushbullet. this cant work on the server otherwise
//notifier.SendMessage("You failed to stay under your max time.")
}
},
}
func shouldBlockSites() bool {
// TODO: we are now ignoreing manualBlock to reduce complexity. Either re-add if desired, or remove all logic that surrounds it.
// using autoCloseCountdown for everything instead
return autoCloseCountdown == 0 || (leewayExpired && !suspendEnabled)
}
func initLeeway() {
leewayMutex.Lock()
defer leewayMutex.Unlock()
timesSpentGoofing = new([1024]time.Duration)
totalDuration = time.Duration(0)
lastStartedGoofing = nil
index = 0
leewayExpired = false
wasWarned = false
}
func resetLeeway() {
log.Print("Resetting...")
initLeeway()
}
func init() {
PluginRegistry[ActivityTracker] = plugin
}
func monitor() {
ticker = time.NewTicker(1 * time.Second)
for t := range ticker.C {
leewayMutex.Lock()
if autoCloseCountdown > 0 {
autoCloseCountdown--
if autoCloseCountdown == 0 {
goofing = false
}
}
if goofing && lastStartedGoofing == nil {
lastStartedGoofing = new(time.Time)
*lastStartedGoofing = t
log.Printf("Started goofing: time %d", t)
} else if lastStartedGoofing != nil {
latestDuration := time.Since(*lastStartedGoofing)
currentTime := uint64(totalDuration) + uint64(latestDuration)
if currentTime >= targetDuration() {
leewayExpired = true
suspendEnabled = false
} else if currentTime > uint64(time.Duration(minutesBeforeWarn*time.Minute)) && !wasWarned {
// TODO: pushbullet. this cant work on the server otherwise
//notifier.SendMessage(strconv.Itoa(minutesBeforeBlock-minutesBeforeWarn) + " minutes till block")
wasWarned = true
}
//log.Printf("Still goofing %d", latestDuration.String())
if !goofing {
lastStartedGoofing = nil
pushDuration(latestDuration)
log.Printf("Stopped goofing: goofing duration %fs", latestDuration.Seconds())
}
}
leewayMutex.Unlock()
}
}
func targetDuration() uint64 {
return uint64(time.Duration(uint64(minutesBeforeBlock+suspendMinutes*suspendCount) * uint64(time.Minute)))
}
func pushDuration(duration time.Duration) {
totalDuration = time.Duration(uint64(totalDuration) + uint64(duration))
timesSpentGoofing[index] = duration
index++
}