Skip to content

Commit

Permalink
Use the 'measurement' json field from the particle webhook as the mea…
Browse files Browse the repository at this point in the history
…surment name, or if it's blank, use the 'name' field of the event's json. (#8609)

(cherry picked from commit d9f2377)
  • Loading branch information
ivorybilled authored and ssoroka committed Jan 27, 2021
1 parent 98e6bdc commit 76ca45d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
10 changes: 8 additions & 2 deletions plugins/inputs/webhooks/particle/particle_webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type event struct {
Data data `json:"data"`
TTL int `json:"ttl"`
PublishedAt string `json:"published_at"`
Database string `json:"measurement"`
Measurement string `json:"measurement"`
}

type data struct {
Expand Down Expand Up @@ -59,6 +59,12 @@ func (rb *ParticleWebhook) eventHandler(w http.ResponseWriter, r *http.Request)
pTime = time.Now()
}

rb.acc.AddFields(e.Name, e.Data.Fields, e.Data.Tags, pTime)
// Use 'measurement' event field as the measurement, or default to the event name.
measurementName := e.Measurement
if measurementName == "" {
measurementName = e.Name
}

rb.acc.AddFields(measurementName, e.Data.Fields, e.Data.Tags, pTime)
w.WriteHeader(http.StatusOK)
}
46 changes: 45 additions & 1 deletion plugins/inputs/webhooks/particle/particle_webhooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestNewItem(t *testing.T) {
"location": "TravelingWilbury",
}

acc.AssertContainsTaggedFields(t, "temperature", fields, tags)
acc.AssertContainsTaggedFields(t, "mydata", fields, tags)
}

func TestUnknowItem(t *testing.T) {
Expand All @@ -57,6 +57,50 @@ func TestUnknowItem(t *testing.T) {
}
}

func TestDefaultMeasurementName(t *testing.T) {
t.Parallel()
var acc testutil.Accumulator
rb := &ParticleWebhook{Path: "/particle", acc: &acc}
resp := postWebhooks(rb, BlankMeasurementJSON())
if resp.Code != http.StatusOK {
t.Errorf("POST new_item returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK)
}

fields := map[string]interface{}{
"temp_c": 26.680000,
}

tags := map[string]string{
"id": "230035001147343438323536",
}

acc.AssertContainsTaggedFields(t, "eventName", fields, tags)
}

func BlankMeasurementJSON() string {
return `
{
"event": "eventName",
"data": {
"tags": {
"id": "230035001147343438323536"
},
"values": {
"temp_c": 26.680000
}
},
"ttl": 60,
"published_at": "2017-09-28T21:54:10.897Z",
"coreid": "123456789938323536",
"userid": "1234ee123ac8e5ec1231a123d",
"version": 10,
"public": false,
"productID": 1234,
"name": "sensor",
"measurement": ""
}`
}

func NewItemJSON() string {
return `
{
Expand Down

0 comments on commit 76ca45d

Please sign in to comment.