Fixed Go formatting
This commit is contained in:
parent
82d87846ff
commit
ba5b46217c
|
@ -25,7 +25,7 @@ func (pwh *ParticleWebhook) Register(router *mux.Router, acc telegraf.Accumulato
|
||||||
func (pwh *ParticleWebhook) eventHandler(w http.ResponseWriter, r *http.Request) {
|
func (pwh *ParticleWebhook) eventHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
|
|
||||||
if err := r.ParseForm(); err != nil {
|
if err := r.ParseForm(); err != nil {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,31 +1,31 @@
|
||||||
package particle
|
package particle
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/url"
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const hexBytes = "0123456789abcdef"
|
const hexBytes = "0123456789abcdef"
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
}
|
}
|
||||||
|
|
||||||
func RandStringBytes(n int) string {
|
func RandStringBytes(n int) string {
|
||||||
b := make([]byte, n)
|
b := make([]byte, n)
|
||||||
for i := range b {
|
for i := range b {
|
||||||
b[i] = hexBytes[rand.Intn(len(hexBytes))]
|
b[i] = hexBytes[rand.Intn(len(hexBytes))]
|
||||||
}
|
}
|
||||||
return string(b)
|
return string(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEventURLEncoded() string {
|
func NewEventURLEncoded() string {
|
||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
return fmt.Sprintf("event=%v&data=%v&published_at=%v&coreid=%v",
|
return fmt.Sprintf("event=%v&data=%v&published_at=%v&coreid=%v",
|
||||||
"event",
|
"event",
|
||||||
rand.Intn(1000),
|
rand.Intn(1000),
|
||||||
url.QueryEscape(time.Now().Format(time.RFC3339)),
|
url.QueryEscape(time.Now().Format(time.RFC3339)),
|
||||||
RandStringBytes(24))
|
RandStringBytes(24))
|
||||||
}
|
}
|
|
@ -15,37 +15,37 @@ type Event interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ParticleEvent struct {
|
type ParticleEvent struct {
|
||||||
Event string `schema:"event"`
|
Event string `schema:"event"`
|
||||||
Data int `schema:"data"`
|
Data int `schema:"data"`
|
||||||
PublishedAt time.Time `schema:"published_at"`
|
PublishedAt time.Time `schema:"published_at"`
|
||||||
CoreID string `schema:"coreid"`
|
CoreID string `schema:"coreid"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pe ParticleEvent) String() string {
|
func (pe ParticleEvent) String() string {
|
||||||
return fmt.Sprintf(`
|
return fmt.Sprintf(`
|
||||||
Event == {
|
Event == {
|
||||||
event: %v,
|
event: %v,
|
||||||
data: %v,
|
data: %v,
|
||||||
published: %v,
|
published: %v,
|
||||||
coreid: %v
|
coreid: %v
|
||||||
}`,
|
}`,
|
||||||
pe.Event,
|
pe.Event,
|
||||||
pe.Data,
|
pe.Data,
|
||||||
pe.PublishedAt,
|
pe.PublishedAt,
|
||||||
pe.CoreID)
|
pe.CoreID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pe ParticleEvent) NewMetric() telegraf.Metric {
|
func (pe ParticleEvent) NewMetric() telegraf.Metric {
|
||||||
t := map[string]string{
|
t := map[string]string{
|
||||||
"event": pe.Event,
|
"event": pe.Event,
|
||||||
"coreid": pe.CoreID,
|
"coreid": pe.CoreID,
|
||||||
}
|
}
|
||||||
f := map[string]interface{}{
|
f := map[string]interface{}{
|
||||||
"data": pe.Data,
|
"data": pe.Data,
|
||||||
}
|
}
|
||||||
m, err := telegraf.NewMetric(pe.Event, t, f, pe.PublishedAt)
|
m, err := telegraf.NewMetric(pe.Event, t, f, pe.PublishedAt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to create %v event", meas)
|
log.Fatalf("Failed to create %v event", meas)
|
||||||
}
|
}
|
||||||
return m
|
return m
|
||||||
}
|
}
|
|
@ -1,16 +1,16 @@
|
||||||
package particle
|
package particle
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf/testutil"
|
"github.com/influxdata/telegraf/testutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ParticleWebhookRequest(urlEncodedString string, t *testing.T) {
|
func ParticleWebhookRequest(urlEncodedString string, t *testing.T) {
|
||||||
var acc testutil.Accumulator
|
var acc testutil.Accumulator
|
||||||
pwh:= &ParticleWebhook{Path: "/particle", acc: &acc}
|
pwh := &ParticleWebhook{Path: "/particle", acc: &acc}
|
||||||
req, _ := http.NewRequest("POST", "/particle", urlEncodedString)
|
req, _ := http.NewRequest("POST", "/particle", urlEncodedString)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
pwh.eventHandler(w, req)
|
pwh.eventHandler(w, req)
|
||||||
|
@ -20,5 +20,5 @@ func ParticleWebhookRequest(urlEncodedString string, t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewEvent(t *testing.T) {
|
func TestNewEvent(t *testing.T) {
|
||||||
ParticleWebhookRequest(NewEventURLEncoded())
|
ParticleWebhookRequest(NewEventURLEncoded())
|
||||||
}
|
}
|
Loading…
Reference in New Issue