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