Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

bugfix: timestamp as epoch #50

Merged
merged 4 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
linters:
enable:
- golint
- revive
issues:
exclude:
- "Error return value of .* is not checked"
11 changes: 11 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,18 @@ repos:
- id: check-merge-conflict
exclude: ^vendor/
- id: detect-private-key
- repo: https://github.com/golangci/golangci-lint
rev: v1.49.0
hooks:
- id: golangci-lint
exclude: ^vendor/
- repo: https://github.com/hadolint/hadolint
rev: v2.10.0
hooks:
- id: hadolint
exclude: ^vendor/
- repo: https://github.com/dnephin/pre-commit-golang
rev: v0.5.0
hooks:
- id: go-fmt
exclude: ^vendor/
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ RUN go build -o /usr/local/bin/tilty main.go
FROM alpine:3.16
LABEL maintainer="3vilpenguin@gmail.com"

# hadolint ignore=DL3018
RUN apk add -U --no-cache bluez

COPY --from=builder /usr/local/bin/tilty /usr/local/bin/tilty
Expand Down
42 changes: 24 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,40 +46,46 @@ logfile = /var/log/foo.log # defaults to stdout

# SQLite example
[sqlite]
enabled = true
file = /etc/tilty/tilt.sqlite

# Generic application/json example
[webhook]
url = http://www.foo.com
headers = {"Content-Type": "application/json"}
template = {"color": "{{ color }}", "gravity": {{ gravity }}, "mac": "{{ mac }}", "temp": {{ temp }}, "timestamp": "{{ timestamp }}"}
method = POST
enabled = true
url = "http://www.foo.com"
headers = "{\"Content-Type\": \"application/json\"}"
template = "{\"color\": \"{{.Color}}\", \"gravity\": {{.Gravity}}, \"mac\": \"{{.Mac}}\", \"temp\": {{.Temp}}, \"timestamp\": \"{{.Timestamp}}\", \"gravity_unit\": \"G\", \"temp_unit\": \"F\"}"
method = "POST"

# Brewstat.us example
[webhook]
url = https://www.brewstat.us/tilt/0yjRbGd2/log
headers = {"Content-Type": "application/x-www-form-urlencoded; charset=utf-8"}
template = {"Color": "{{ color }}", "SG": {{ gravity }}, "Temp": {{ temp }}, "Timepoint": "{{ timestamp }}"}
method = POST
enabled = true
url = "https://www.brewstat.us/tilt/0yjRbGd2/log"
headers = "{\"Content-Type\": \"application/json\"}"
template = "{\"Color\": \"{{.Color}}\", \"SG\": {{.Gravity}}, \"Temp\": {{.Temp}}, \"Timepoint\": \"{{.Timestamp}}\"}"
method = "POST"

# Brewers Friend example
[webhook]
url = https://log.brewersfriend.com/tilt/3009ec67c6d81276185c90824951bd32bg
headers = {"Content-Type": "application/x-www-form-urlencoded"}
template = {"SG": {{ gravity }}, "Temp": {{ temp }}, "Color": "{{ color }}"}
method = POST
enabled = true
url = "https://log.brewersfriend.com/tilt/3009ec67c6d81276185c90824951bd32bg"
headers = "{\"Content-Type\": \"application/json\"}"
template = "{\"SG\": \"{{.Gravity}}\", \"Temp\": {{.Temp}}, \"Color\": {{.Color}}}"
method = "POST"

# Brewfather custom stream example
[webhook]
url = https://log.brewfather.net/stream?id=aTHF9WlXKrAb1C
headers = {"Content-Type": "application/json"}
template = {"name": "Tilt {{ color }}", "gravity": {{ gravity }}, "gravity_unit": "G", "temp": {{ temp }}, "temp_unit": "F"}
method = POST
enabled = true
url = "https://log.brewfather.net/stream?id=aTHF9WlXKrAb1C"
headers = "{\"Content-Type\": \"application/json\"}"
template = "{\"name\": \"Tilt {{.Color}}\", \"gravity\": {{.Gravity}}, \"gravity_unit\": \"G\", \"temp\": {{.Temp}}, \"temp_unit\": \"F\"}"
method = "POST"

[datadog]
enabled = true
# Note: make sure that the dd agent has DD_DOGSTATSD_NON_LOCAL_TRAFFIC=true
host = statsdhost.corp.com
port = 8125
statsd_host = "statsdhost.corp.com"
statsd_port = 8125

```

Expand Down
7 changes: 3 additions & 4 deletions emitters/datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import (
"encoding/json"
"fmt"
"github.com/DataDog/datadog-go/v5/statsd"
"github.com/go-kit/kit/log/level"
_ "github.com/mattn/go-sqlite3"
"github.com/go-kit/log/level"
"github.com/myoung34/tilty/tilt"
)

Expand All @@ -15,7 +14,7 @@ type Datadog struct {
StatsdPort int `json:"statsd_port"`
}

func DatadogEmitWithClient(payload tilt.TiltPayload, emitterConfig interface{}, client statsd.ClientInterface) (string, error) {
func DatadogEmitWithClient(payload tilt.Payload, emitterConfig interface{}, client statsd.ClientInterface) (string, error) {

defer client.Close()

Expand All @@ -39,7 +38,7 @@ func DatadogEmitWithClient(payload tilt.TiltPayload, emitterConfig interface{},
return "", nil
}

func DatadogEmit(payload tilt.TiltPayload, emitterConfig interface{}) (string, error) {
func DatadogEmit(payload tilt.Payload, emitterConfig interface{}) (string, error) {
datadog := Datadog{}
jsonString, _ := json.Marshal(emitterConfig)
json.Unmarshal(jsonString, &datadog)
Expand Down
6 changes: 3 additions & 3 deletions emitters/datadog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ func TestDatadogEmit(t *testing.T) {
sampleConfig.ConfigData.Set("datadog.statsd_host", "testing")
sampleConfig.ConfigData.Set("datadog.statsd_port", "8125")

payload := tilt.TiltPayload{
Id: "0987654321",
payload := tilt.Payload{
ID: "0987654321",
Mac: "66:77:88:99:00",
Color: "BLACK",
Major: 65,
Minor: 1098,
Rssi: -7,
Timestamp: "2019-11-10 23:59:00 +0000 UTC",
Timestamp: 1661445284,
}
resp, err := DatadogEmitWithClient(payload, sampleConfig.ConfigData.Get("datadog"), &statsd.NoOpClient{})
assert.Equal(t, nil, err)
Expand Down
2 changes: 1 addition & 1 deletion emitters/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ type Template struct {
Gravity string
Mac string
Temp string
Timestamp string
Timestamp int64
}
6 changes: 3 additions & 3 deletions emitters/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"database/sql"
"encoding/json"
"fmt"
"github.com/go-kit/kit/log/level"
_ "github.com/mattn/go-sqlite3"
"github.com/go-kit/log/level"
_ "github.com/mattn/go-sqlite3" // Per docs
"github.com/myoung34/tilty/tilt"
"log"
)
Expand All @@ -15,7 +15,7 @@ type SQLite struct {
File string
}

func SQLiteEmit(payload tilt.TiltPayload, emitterConfig interface{}) (string, error) {
func SQLiteEmit(payload tilt.Payload, emitterConfig interface{}) (string, error) {
sqlite := SQLite{}
jsonString, _ := json.Marshal(emitterConfig)
json.Unmarshal(jsonString, &sqlite)
Expand Down
6 changes: 3 additions & 3 deletions emitters/sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ func TestSQLite(t *testing.T) {
sampleConfig.ConfigData.Set("sqlite.enabled", true)
sampleConfig.ConfigData.Set("sqlite.file", "foo.db")

payload := tilt.TiltPayload{
Id: "0987654321",
payload := tilt.Payload{
ID: "0987654321",
Mac: "66:77:88:99:00",
Color: "BLACK",
Major: 65,
Minor: 1098,
Rssi: -7,
Timestamp: "2019-11-10 23:59:00 +0000 UTC",
Timestamp: 1661445284,
}
resp, err := SQLiteEmit(payload, sampleConfig.ConfigData.Get("sqlite"))
assert.Equal(t, nil, err)
Expand Down
15 changes: 9 additions & 6 deletions emitters/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/go-kit/kit/log/level"
"github.com/go-kit/log/level"
"github.com/myoung34/tilty/tilt"
"io"
"net/http"
Expand All @@ -14,18 +14,18 @@ import (

type Webhook struct {
Enabled bool
Url string
URL string
Headers string
Template string
Method string
}

func WebhookEmit(payload tilt.TiltPayload, emitterConfig interface{}) (string, error) {
func WebhookEmit(payload tilt.Payload, emitterConfig interface{}) (string, error) {
webhook := Webhook{}
jsonString, _ := json.Marshal(emitterConfig)
json.Unmarshal(jsonString, &webhook)

level.Info(tilt.Logger).Log("emitters.webhook", fmt.Sprintf("%s", webhook.Url))
level.Info(tilt.Logger).Log("emitters.webhook", webhook.URL)
level.Info(tilt.Logger).Log("emitters.webhook", fmt.Sprintf("%v", webhook.Enabled))
level.Info(tilt.Logger).Log("emitters.webhook", fmt.Sprintf("%+v", webhook.Headers))
level.Info(tilt.Logger).Log("emitters.webhook", fmt.Sprintf("%+v", webhook.Template))
Expand All @@ -43,7 +43,10 @@ func WebhookEmit(payload tilt.TiltPayload, emitterConfig interface{}) (string, e
}
level.Info(tilt.Logger).Log("emitters.webhook", fmt.Sprintf("%+v", payload))

tmpl, err := template.New("test").Parse(`{"name": "Tilt {{.Color}}", "gravity": {{.Gravity}}, "gravity_unit": "G", "temp": {{.Temp}}, "temp_unit": "F"}`)
tmpl, err := template.New("webhook").Parse(`{"name": "Tilt {{.Color}}", "gravity": {{.Gravity}}, "gravity_unit": "G", "temp": {{.Temp}}, "temp_unit": "F"}`)
if len(webhook.Template) > 0 {
tmpl, err = template.New("webhook").Parse(webhook.Template)
}
if err != nil {
level.Error(tilt.Logger).Log("emitters.webhook", err)
return "", err
Expand All @@ -56,7 +59,7 @@ func WebhookEmit(payload tilt.TiltPayload, emitterConfig interface{}) (string, e
bodyReader := bytes.NewReader(tpl.Bytes())

// Set up the request
req, err := http.NewRequest(webhook.Method, webhook.Url, bodyReader)
req, err := http.NewRequest(webhook.Method, webhook.URL, bodyReader)
if err != nil {
level.Error(tilt.Logger).Log("emitters.webhook", err)
return "", err
Expand Down
32 changes: 16 additions & 16 deletions emitters/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (

type TiltWebhookTest struct {
Type string
Payload tilt.TiltPayload
Payload tilt.Payload
Enabled bool
Url string
URL string
Headers string
Template string
Method string
Expand Down Expand Up @@ -43,23 +43,23 @@ func TestWebhook(t *testing.T) {
name: "POST",
in: TiltWebhookTest{
Type: "webhook",
Payload: tilt.TiltPayload{
Id: "1234567890",
Payload: tilt.Payload{
ID: "1234567890",
Mac: "11:22:33:44:55",
Color: "RED",
Major: 90,
Minor: 1024,
Rssi: -67,
Timestamp: "2009-11-10 23:00:00 +0000 UTC",
Timestamp: 1661445284,
},
Enabled: true,
Url: "http://something.com",
URL: "http://something.com",
Headers: "{\"Content-Type\": \"application/json\", \"Foo\": \"bar\"}",
Template: "{\"color\": \"{{ color }}\", \"gravity\": {{ gravity }}, \"mac\": \"{{ mac }}\", \"temp\": {{ temp }}, \"timestamp\": \"{{ timestamp }}\"}",
Template: "{\"color\": \"{{.Color}}\", \"gravity\": {{.Gravity}}, \"mac\": \"{{.Mac}}\", \"temp\": {{.Temp}}, \"timestamp\": \"{{.Timestamp}}\", \"gravity_unit\": \"G\", \"temp_unit\": \"F\"}",
Method: "POST",
},
out: TiltTest{
Response: "{\"Response\":\"{\\\"name\\\": \\\"Tilt RED\\\", \\\"gravity\\\": 1024, \\\"gravity_unit\\\": \\\"G\\\", \\\"temp\\\": 90, \\\"temp_unit\\\": \\\"F\\\"}\"}",
Response: "{\"Response\":\"{\\\"color\\\": \\\"RED\\\", \\\"gravity\\\": 1024, \\\"mac\\\": \\\"11:22:33:44:55\\\", \\\"temp\\\": 90, \\\"timestamp\\\": \\\"1661445284\\\", \\\"gravity_unit\\\": \\\"G\\\", \\\"temp_unit\\\": \\\"F\\\"}\"}",
CallCount: 1,
CallSignature: "POST http://something.com",
},
Expand All @@ -68,31 +68,31 @@ func TestWebhook(t *testing.T) {
name: "GET",
in: TiltWebhookTest{
Type: "webhook",
Payload: tilt.TiltPayload{
Id: "0987654321",
Payload: tilt.Payload{
ID: "0987654321",
Mac: "66:77:88:99:00",
Color: "BLACK",
Major: 65,
Minor: 1098,
Rssi: -7,
Timestamp: "2019-11-10 23:59:00 +0000 UTC",
Timestamp: 1661445284,
},
Enabled: true,
Url: "http://fake.com",
URL: "http://fake.com",
Headers: "{\"Content-Type\": \"application/json\"}",
Template: "{\"color\": \"{{ color }}\", \"gravity\": {{ gravity }}, \"mac\": \"{{ mac }}\", \"temp\": {{ temp }}, \"timestamp\": \"{{ timestamp }}\"}",
Template: "{\"color\": \"{{.Color}}\", \"gravity\": {{.Gravity}}, \"mac\": \"{{.Mac}}\", \"temp\": {{.Temp}}, \"timestamp\": \"{{.Timestamp}}\", \"gravity_unit\": \"G\", \"temp_unit\": \"F\"}",
Method: "GET",
},
out: TiltTest{
Response: "{\"Response\":\"{\\\"name\\\": \\\"Tilt BLACK\\\", \\\"gravity\\\": 1098, \\\"gravity_unit\\\": \\\"G\\\", \\\"temp\\\": 65, \\\"temp_unit\\\": \\\"F\\\"}\"}",
Response: "{\"Response\":\"{\\\"color\\\": \\\"BLACK\\\", \\\"gravity\\\": 1098, \\\"mac\\\": \\\"66:77:88:99:00\\\", \\\"temp\\\": 65, \\\"timestamp\\\": \\\"1661445284\\\", \\\"gravity_unit\\\": \\\"G\\\", \\\"temp_unit\\\": \\\"F\\\"}\"}",
CallCount: 2,
CallSignature: "GET http://fake.com",
},
},
}
for _, theT := range theTests {

httpmock.RegisterResponder(theT.in.Method, theT.in.Url,
httpmock.RegisterResponder(theT.in.Method, theT.in.URL,
func(req *http.Request) (*http.Response, error) {
buf := new(bytes.Buffer)
buf.ReadFrom(req.Body)
Expand All @@ -104,7 +104,7 @@ func TestWebhook(t *testing.T) {
t.Run(theT.name, func(t *testing.T) {
sampleConfig := tilt.ParseConfig("some/file/somewhere.toml")

sampleConfig.ConfigData.Set("webhook.url", theT.in.Url)
sampleConfig.ConfigData.Set("webhook.url", theT.in.URL)
sampleConfig.ConfigData.Set("webhook.headers", theT.in.Headers)
sampleConfig.ConfigData.Set("webhook.template", theT.in.Template)
sampleConfig.ConfigData.Set("webhook.method", theT.in.Method)
Expand Down
Loading