-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
gone.go
165 lines (142 loc) · 2.92 KB
/
gone.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
// Gone Time Tracker -or- Where has my time gone?
package main
import (
"encoding/gob"
"flag"
"fmt"
"log"
"os"
"path"
"time"
)
type Tracks map[Window]Track
type Track struct {
Seen time.Time
Spent time.Duration
Idle time.Duration
}
var (
cacheDir, _ = os.UserCacheDir()
dumpFileName = path.Join(cacheDir, "gone.gob")
logFileName = path.Join(cacheDir, "gone.log")
)
var (
tracks Tracks
current Window
logger *log.Logger
zzz bool
)
func (t Track) String() string {
return fmt.Sprintf("%s %s", t.Seen.Format("2006/01/02 15:04:05"), t.Spent)
}
func (w Window) String() string {
return fmt.Sprintf("%s %s", w.Class, w.Name)
}
func (t Tracks) Snooze(idle time.Duration) {
if !zzz {
logger.Println("away from keyboard, idle for", idle)
if c, ok := t[current]; ok {
c.Idle += idle
t[current] = c
}
zzz = true
}
}
func (t Tracks) Wakeup() {
if zzz {
logger.Println("back to keyboard")
if c, ok := t[current]; ok {
c.Seen = time.Now()
t[current] = c
}
zzz = false
}
}
func (t Tracks) Update(w Window) {
if !zzz {
if c, ok := t[current]; ok {
c.Spent += time.Since(c.Seen)
t[current] = c
}
}
if _, ok := t[w]; !ok {
t[w] = Track{}
}
s := t[w]
s.Seen = time.Now()
t[w] = s
current = w
}
func (t Tracks) RemoveSince(d time.Duration) {
for k, v := range t {
if time.Since(v.Seen) > d || v.Idle > d {
logger.Println(v, k)
delete(t, k)
}
}
}
func Load(fname string) Tracks {
t := make(Tracks)
dump, err := os.Open(fname)
if err != nil {
log.Println(err)
return t
}
defer dump.Close()
dec := gob.NewDecoder(dump)
err = dec.Decode(&t)
if err != nil {
log.Println(err)
}
return t
}
func (t Tracks) Store(fname string) {
tmp := fname + ".tmp"
dump, err := os.Create(tmp)
if err != nil {
log.Println(err)
return
}
defer dump.Close()
enc := gob.NewEncoder(dump)
err = enc.Encode(t)
if err != nil {
log.Println(err)
os.Remove(tmp)
return
}
os.Rename(tmp, fname)
}
func (t Tracks) Cleanup(every, since time.Duration) {
tick := time.NewTicker(every)
defer tick.Stop()
for range tick.C {
t.RemoveSince(since)
t.Store(dumpFileName)
}
}
func main() {
var (
display = flag.String("display", os.Getenv("DISPLAY"), "X11 display")
listen = flag.String("listen", "127.0.0.1:8001", "web reporter")
timeout = flag.Duration("timeout", time.Minute*5, "idle timeout")
expire = flag.Duration("expire", time.Hour*8, "expire timeout")
refresh = flag.Duration("refresh", time.Minute, "refresh interval")
)
flag.Parse()
X := Connect(*display)
defer X.Close()
logfile, err := os.OpenFile(logFileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Fatal(err)
}
defer logfile.Close()
logger = log.New(logfile, "", log.LstdFlags)
tracks = Load(dumpFileName)
defer tracks.Store(dumpFileName)
go X.Collect(tracks, *timeout)
go tracks.Cleanup(*refresh, *expire)
if err := webReporter(*listen); err != nil {
log.Fatal(err)
}
}