Skip to content

Commit

Permalink
It writes to the influxdb now
Browse files Browse the repository at this point in the history
  • Loading branch information
lucapette committed Jan 31, 2017
1 parent 15fd9d2 commit d6d7e63
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -23,3 +23,4 @@ _testmain.go
*.test
*.prof
t
influxdb.conf
1 change: 1 addition & 0 deletions Procfile
@@ -0,0 +1 @@
influx: influxd -config influxdb.conf
1 change: 1 addition & 0 deletions README.md
@@ -1,2 +1,3 @@
# t

My personal tracker
80 changes: 75 additions & 5 deletions main.go
Expand Up @@ -2,10 +2,14 @@ package main

import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"os/signal"
"strings"
"time"
)

Expand All @@ -28,21 +32,87 @@ end tell
frontApp & "," & (activeURL) as String
`

func run() string {
type category struct {
name string
score int
}

type activity struct {
name string
category
}

func (a activity) store() error {
b := bytes.NewBufferString(fmt.Sprintf("activity,category=%s,score=%d value=\"%s\"", a.category.name, a.category.score, a.name))
req, err := http.NewRequest("POST", "http://localhost:8086/write", b)
if err != nil {
return err
}
req.Header.Set("Content-Type", "")
req.Header.Set("User-Agent", "lucapette/t")

params := req.URL.Query()
params.Set("db", "me")
req.URL.RawQuery = params.Encode()

c := &http.Client{Timeout: 100 * time.Millisecond}
resp, err := c.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}

if resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusOK {
var err = fmt.Errorf(string(body))
return err
}

return nil
}

func findCategory(activity string) category {
return category{name: "Stuff", score: 42}
}

func currentActivity() (ac activity) {
cmd := exec.Command("osascript", "-")
cmd.Stdin = bytes.NewBufferString(script)
output, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
return string(output)

values := strings.Split(string(output), ",")

for i := range values {
values[i] = strings.Replace(values[i], "\n", "", -1)
}

if len(values[1]) == 0 {
ac.name = values[0]
} else {
ac.name = values[1]
}

ac.category = findCategory(ac.name)

return ac
}

func main() {
go func() {
c := time.Tick(1 * time.Second)
for range c {
log.Printf(run())
tick := time.Tick(1 * time.Second)
for range tick {
a := currentActivity()
err := a.store()
if err != nil {
log.Fatal(err)
}
}
}()

Expand Down

0 comments on commit d6d7e63

Please sign in to comment.