Skip to content

Instantly share code, notes, and snippets.

@gmarik
Forked from nf/analyze.go
Created November 12, 2015 19:26
Show Gist options
  • Save gmarik/2946ae47a39232eb3543 to your computer and use it in GitHub Desktop.
Save gmarik/2946ae47a39232eb3543 to your computer and use it in GitHub Desktop.
'spent' script to log where time is spent
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"sort"
"strconv"
"strings"
"time"
)
const idleSecs = 120 // seconds to be regarded as "idle"
var (
lineRe = regexp.MustCompile(`^([^ ]+ [^ ]+) ([^ ]+) ([^,]+), (.*)$`)
webPrefixes = []string{
"Google Calendar",
"Google.com - Calendar",
"Twitter",
}
webSuffixes = []string{
"Code Review",
"Gmail",
"Google Docs",
"Google Drive",
"Google+",
"Google.com Mail",
"Memegen",
"YouTube",
"golang/go",
}
)
func main() {
f, err := os.Open("spent.log")
check(err)
defer f.Close()
s := bufio.NewScanner(f)
counts := map[string]int{}
for s.Scan() {
m := lineRe.FindStringSubmatch(s.Text())
if m == nil {
continue
}
ts, d, app, title := m[1], m[2], m[3], m[4]
secs, _ := strconv.ParseFloat(d, 63)
if secs > idleSecs {
// Ignore idle state.
continue
}
classify:
switch app {
case "Google Chrome", "Chrome":
for _, s := range webPrefixes {
if strings.HasPrefix(title, s) {
counts[s]++
break classify
}
}
for _, s := range webSuffixes {
if strings.HasSuffix(title, s) {
counts[s]++
break classify
}
}
counts["Misc. web"]++
default:
counts[app]++
}
_, _ = ts, d
}
var data []datum
var total int
for label, count := range counts {
data = append(data, datum{label, count})
total += count
}
sort.Sort(dataByCount(data))
for _, d := range data {
fmt.Printf("%v %s\n", time.Duration(d.count)*10*time.Second, d.label)
}
fmt.Println(time.Duration(total) * 10 * time.Second)
}
type datum struct {
label string
count int
}
type dataByCount []datum
func (s dataByCount) Len() int { return len(s) }
func (s dataByCount) Less(i, j int) bool { return s[i].count < s[j].count }
func (s dataByCount) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func check(err error) {
if err != nil {
panic(err)
}
}
#!/usr/bin/osascript
global frontApp, frontAppName, windowTitle
set windowTitle to ""
tell application "System Events"
set frontApp to first application process whose frontmost is true
set frontAppName to name of frontApp
tell process frontAppName
tell (1st window whose value of attribute "AXMain" is true)
set windowTitle to value of attribute "AXTitle"
end tell
end tell
end tell
tell application "System Events" to set the saveractivated to (exists process "ScreenSaverEngine")
if saveractivated then
return "Screen saver active"
else
return {frontAppName, windowTitle}
end if
#!/bin/bash
while true; do
idletime=$(/usr/sbin/ioreg -c IOHIDSystem | /usr/bin/awk '/HIDIdleTime/ {print $NF/1000000000; exit}')
echo $(date '+%F %H:%M:%S') $idletime $(front) >> ~/spent.log
sleep 10
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment