Skip to content

Commit

Permalink
feat: Add Tags field to V1 Event model and V2 Event DTO and model
Browse files Browse the repository at this point in the history
closes #270

Signed-off-by: lenny <[email protected]>
  • Loading branch information
lenny committed Aug 21, 2020
1 parent 06f35a9 commit bcbd875
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 22 deletions.
17 changes: 9 additions & 8 deletions models/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ import (

// Event represents a single measurable event read from a device
type Event struct {
ID string `json:"id,omitempty" codec:"id,omitempty"` // ID uniquely identifies an event, for example a UUID
Pushed int64 `json:"pushed,omitempty" codec:"pushed,omitempty"` // Pushed is a timestamp indicating when the event was exported. If unexported, the value is zero.
Device string `json:"device,omitempty" codec:"device,omitempty"` // Device identifies the source of the event, can be a device name or id. Usually the device name.
Created int64 `json:"created,omitempty" codec:"created,omitempty"` // Created is a timestamp indicating when the event was created.
Modified int64 `json:"modified,omitempty" codec:"modified,omitempty"` // Modified is a timestamp indicating when the event was last modified.
Origin int64 `json:"origin,omitempty" codec:"origin,omitempty"` // Origin is a timestamp that can communicate the time of the original reading, prior to event creation
Readings []Reading `json:"readings,omitempty" codec:"readings,omitempty"` // Readings will contain zero to many entries for the associated readings of a given event.
isValidated bool // internal member used for validation check
ID string `json:"id,omitempty" codec:"id,omitempty"` // ID uniquely identifies an event, for example a UUID
Pushed int64 `json:"pushed,omitempty" codec:"pushed,omitempty"` // Pushed is a timestamp indicating when the event was exported. If unexported, the value is zero.
Device string `json:"device,omitempty" codec:"device,omitempty"` // Device identifies the source of the event, can be a device name or id. Usually the device name.
Created int64 `json:"created,omitempty" codec:"created,omitempty"` // Created is a timestamp indicating when the event was created.
Modified int64 `json:"modified,omitempty" codec:"modified,omitempty"` // Modified is a timestamp indicating when the event was last modified.
Origin int64 `json:"origin,omitempty" codec:"origin,omitempty"` // Origin is a timestamp that can communicate the time of the original reading, prior to event creation
Readings []Reading `json:"readings,omitempty" codec:"readings,omitempty"` // Readings will contain zero to many entries for the associated readings of a given event.
Tags map[string]string `json:"tags,omitempty" codec:"tags,omitempty"` // Tags is an optional collection of key/value pairs that all the event to be tagged with custom information.
isValidated bool // internal member used for validation check
}

func encodeAsCBOR(e Event) ([]byte, error) {
Expand Down
20 changes: 14 additions & 6 deletions v2/dtos/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,26 @@ import (
// https://app.swaggerhub.com/apis-docs/EdgeXFoundry1/core-data/2.x#/Event
type Event struct {
common.Versionable `json:",inline"`
ID string `json:"id"`
Pushed int64 `json:"pushed,omitempty"`
DeviceName string `json:"deviceName" validate:"required"`
Created int64 `json:"created"`
Origin int64 `json:"origin" validate:"required"`
Readings []BaseReading `json:"readings" validate:"gt=0,dive,required"`
ID string `json:"id"`
Pushed int64 `json:"pushed,omitempty"`
DeviceName string `json:"deviceName" validate:"required"`
Created int64 `json:"created"`
Origin int64 `json:"origin" validate:"required"`
Readings []BaseReading `json:"readings" validate:"gt=0,dive,required"`
Tags map[string]string `json:"tags,omitempty"`
}

func FromEventModelToDTO(event models.Event) Event {
var readings []BaseReading
for _, reading := range event.Readings {
readings = append(readings, FromReadingModelToDTO(reading))
}

tags := make(map[string]string)
for tag, value := range event.Tags {
tags[tag] = value
}

return Event{
Versionable: common.Versionable{ApiVersion: v2.ApiVersion},
ID: event.Id,
Expand All @@ -37,5 +44,6 @@ func FromEventModelToDTO(event models.Event) Event {
Created: event.Created,
Origin: event.Origin,
Readings: readings,
Tags: tags,
}
}
6 changes: 6 additions & 0 deletions v2/dtos/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ func TestFromEventModelToDTO(t *testing.T) {
DeviceName: TestDeviceName,
Created: TestTimestamp,
Origin: TestTimestamp,
Tags: map[string]string{
"GatewayID": "Intel123",
},
}
expectedDTO := Event{
Versionable: common.Versionable{ApiVersion: v2.ApiVersion},
Expand All @@ -30,6 +33,9 @@ func TestFromEventModelToDTO(t *testing.T) {
DeviceName: TestDeviceName,
Created: TestTimestamp,
Origin: TestTimestamp,
Tags: map[string]string{
"GatewayID": "Intel123",
},
}

tests := []struct {
Expand Down
7 changes: 7 additions & 0 deletions v2/dtos/requests/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,16 @@ func AddEventReqToEventModels(addRequests []AddEventRequest) (events []models.Ev
for i, r := range a.Event.Readings {
readings[i] = dtos.ToReadingModel(r, a.Event.DeviceName)
}

tags := make(map[string]string)
for tag, value := range a.Event.Tags {
tags[tag] = value
}

e.DeviceName = a.Event.DeviceName
e.Origin = a.Event.Origin
e.Readings = readings
e.Tags = tags
events = append(events, e)
}
return events
Expand Down
10 changes: 8 additions & 2 deletions v2/dtos/requests/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ var testAddEvent = AddEventRequest{
Value: TestReadingValue,
},
}},
Tags: map[string]string{
"GatewayId": "Houston-0001",
},
},
}

Expand Down Expand Up @@ -223,6 +226,9 @@ func Test_AddEventReqToEventModels(t *testing.T) {
DeviceName: TestDeviceName,
Origin: TestOriginTime,
Readings: []models.Reading{s},
Tags: map[string]string{
"GatewayId": "Houston-0001",
},
}}

tests := []struct {
Expand All @@ -233,8 +239,8 @@ func Test_AddEventReqToEventModels(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
readingModel := AddEventReqToEventModels(tt.addEvents)
assert.Equal(t, expectedEventModel, readingModel, "AddEventReqToEventModels did not result in expected Event model.")
eventModel := AddEventReqToEventModels(tt.addEvents)
assert.Equal(t, expectedEventModel, eventModel, "AddEventReqToEventModels did not result in expected Event model.")
})
}
}
13 changes: 7 additions & 6 deletions v2/models/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ package models
type Event struct {
CorrelationId string
Checksum string
Id string // Id uniquely identifies an event, for example a UUID
Pushed int64 // Pushed is a timestamp indicating when the event was exported. If unexported, the value is zero.
DeviceName string // DeviceName identifies the source of the event
Created int64 // Created is a timestamp indicating when the event was created.
Origin int64 // Origin is a timestamp that can communicate the time of the original reading, prior to event creation
Readings []Reading // Readings will contain zero to many entries for the associated readings of a given event.
Id string // Id uniquely identifies an event, for example a UUID
Pushed int64 // Pushed is a timestamp indicating when the event was exported. If unexported, the value is zero.
DeviceName string // DeviceName identifies the source of the event
Created int64 // Created is a timestamp indicating when the event was created.
Origin int64 // Origin is a timestamp that can communicate the time of the original reading, prior to event creation
Readings []Reading // Readings will contain zero to many entries for the associated readings of a given event.
Tags map[string]string // Tags is an optional collection of key/value pairs that all the event to be tagged with custom information.
}

0 comments on commit bcbd875

Please sign in to comment.