-
Notifications
You must be signed in to change notification settings - Fork 485
/
Copy pathevent.go
275 lines (235 loc) · 9.92 KB
/
event.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//
// Copyright (C) 2020-2024 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0
package application
import (
"context"
"fmt"
"strings"
msgTypes "github.com/edgexfoundry/go-mod-messaging/v4/pkg/types"
bootstrapContainer "github.com/edgexfoundry/go-mod-bootstrap/v4/bootstrap/container"
"github.com/edgexfoundry/go-mod-bootstrap/v4/di"
"github.com/edgexfoundry/go-mod-core-contracts/v4/common"
"github.com/edgexfoundry/go-mod-core-contracts/v4/dtos"
"github.com/edgexfoundry/go-mod-core-contracts/v4/errors"
"github.com/edgexfoundry/go-mod-core-contracts/v4/models"
"github.com/edgexfoundry/edgex-go/internal/core/data/container"
"github.com/edgexfoundry/edgex-go/internal/pkg/correlation"
"github.com/edgexfoundry/edgex-go/internal/pkg/utils"
"github.com/google/uuid"
)
const CoreDataEventTopicPrefix = "core"
// ValidateEvent validates if e is a valid event with corresponding device profile name and device name and source name
// ValidateEvent throws error when profileName or deviceName doesn't match to e
func (a *CoreDataApp) ValidateEvent(e models.Event, profileName string, deviceName string, sourceName string, _ context.Context, _ *di.Container) errors.EdgeX {
if e.ProfileName != profileName {
return errors.NewCommonEdgeX(errors.KindContractInvalid, fmt.Sprintf("event's profileName %s mismatches %s", e.ProfileName, profileName), nil)
}
if e.DeviceName != deviceName {
return errors.NewCommonEdgeX(errors.KindContractInvalid, fmt.Sprintf("event's deviceName %s mismatches %s", e.DeviceName, deviceName), nil)
}
if e.SourceName != sourceName {
return errors.NewCommonEdgeX(errors.KindContractInvalid, fmt.Sprintf("event's sourceName %s mismatches %s", e.SourceName, sourceName), nil)
}
return nil
}
// The AddEvent function accepts the new event model from the controller functions
// and invokes addEvent function in the infrastructure layer
func (a *CoreDataApp) AddEvent(e models.Event, ctx context.Context, dic *di.Container) (err errors.EdgeX) {
configuration := container.ConfigurationFrom(dic.Get)
if !configuration.Writable.PersistData {
return nil
}
dbClient := container.DBClientFrom(dic.Get)
// Add the event and readings to the database
if configuration.Writable.PersistData {
correlationId := correlation.FromContext(ctx)
addedEvent, err := dbClient.AddEvent(e)
if err != nil {
return errors.NewCommonEdgeXWrapper(err)
}
e = addedEvent
a.lc.Debugf(
"Event created on DB successfully. Event-id: %s, Correlation-id: %s ",
e.Id,
correlationId,
)
a.eventsPersistedCounter.Inc(1)
a.readingsPersistedCounter.Inc(int64(len(addedEvent.Readings)))
}
return nil
}
// PublishEvent publishes incoming AddEventRequest in the format of []byte through MessageClient
func (a *CoreDataApp) PublishEvent(data []byte, serviceName string, profileName string, deviceName string, sourceName string, ctx context.Context, dic *di.Container) {
lc := bootstrapContainer.LoggingClientFrom(dic.Get)
msgClient := bootstrapContainer.MessagingClientFrom(dic.Get)
configuration := container.ConfigurationFrom(dic.Get)
correlationId := correlation.FromContext(ctx)
basePrefix := configuration.MessageBus.GetBaseTopicPrefix()
publishTopic := common.NewPathBuilder().EnableNameFieldEscape(configuration.Service.EnableNameFieldEscape).
SetPath(basePrefix).SetPath(common.EventsPublishTopic).SetPath(CoreDataEventTopicPrefix).SetNameFieldPath(serviceName).SetNameFieldPath(profileName).SetNameFieldPath(deviceName).SetNameFieldPath(sourceName).BuildPath()
lc.Debugf("Publishing AddEventRequest to MessageBus. Topic: %s; %s: %s", publishTopic, common.CorrelationHeader, correlationId)
msgEnvelope := msgTypes.NewMessageEnvelope(data, ctx)
err := msgClient.Publish(msgEnvelope, publishTopic)
if err != nil {
lc.Errorf("Unable to send message for API event. Correlation-id: %s, Profile Name: %s, "+
"Device Name: %s, Source Name: %s, Error: %v", correlationId, profileName, deviceName, sourceName, err)
} else {
lc.Debugf("Event Published to MessageBus. Topic: %s, Correlation-id: %s ", publishTopic, correlationId)
}
}
func (a *CoreDataApp) EventById(id string, dic *di.Container) (dtos.Event, errors.EdgeX) {
if id == "" {
return dtos.Event{}, errors.NewCommonEdgeX(errors.KindInvalidId, "id is empty", nil)
}
_, err := uuid.Parse(id)
if err != nil {
return dtos.Event{}, errors.NewCommonEdgeX(errors.KindInvalidId, "fail to parse id as an UUID", err)
}
dbClient := container.DBClientFrom(dic.Get)
event, err := dbClient.EventById(id)
if err != nil {
return dtos.Event{}, errors.NewCommonEdgeXWrapper(err)
}
eventDTO := dtos.FromEventModelToDTO(event)
return eventDTO, nil
}
// The DeleteEventById function accepts event id from the controller functions
// and invokes DeleteEventById function in the infrastructure layer to remove
// event
func (a *CoreDataApp) DeleteEventById(id string, dic *di.Container) errors.EdgeX {
if id == "" {
return errors.NewCommonEdgeX(errors.KindInvalidId, "id is empty", nil)
} else {
_, err := uuid.Parse(id)
if err != nil {
return errors.NewCommonEdgeX(errors.KindInvalidId, "Failed to parse ID as an UUID", err)
}
}
dbClient := container.DBClientFrom(dic.Get)
_, err := dbClient.EventById(id)
if err != nil {
return errors.NewCommonEdgeXWrapper(err)
}
err = dbClient.DeleteEventById(id)
if err != nil {
return errors.NewCommonEdgeXWrapper(err)
}
return nil
}
// EventTotalCount return the count of all events currently stored in the database and error if any
func (a *CoreDataApp) EventTotalCount(dic *di.Container) (uint32, errors.EdgeX) {
dbClient := container.DBClientFrom(dic.Get)
count, err := dbClient.EventTotalCount()
if err != nil {
return 0, errors.NewCommonEdgeXWrapper(err)
}
return count, nil
}
// EventCountByDeviceName return the count of all events associated with given device and error if any
func (a *CoreDataApp) EventCountByDeviceName(deviceName string, dic *di.Container) (uint32, errors.EdgeX) {
dbClient := container.DBClientFrom(dic.Get)
count, err := dbClient.EventCountByDeviceName(deviceName)
if err != nil {
return 0, errors.NewCommonEdgeXWrapper(err)
}
return count, nil
}
// The DeleteEventsByDeviceName function will be invoked by controller functions
// and then invokes DeleteEventsByDeviceName function in the infrastructure layer to remove
// all events/readings that are associated with the given deviceName
func (a *CoreDataApp) DeleteEventsByDeviceName(deviceName string, dic *di.Container) errors.EdgeX {
if len(strings.TrimSpace(deviceName)) <= 0 {
return errors.NewCommonEdgeX(errors.KindInvalidId, "blank device name is not allowed", nil)
}
dbClient := container.DBClientFrom(dic.Get)
lc := bootstrapContainer.LoggingClientFrom(dic.Get)
go func() {
err := dbClient.DeleteEventsByDeviceName(deviceName)
if err != nil {
lc.Errorf("Delete events by device name failed: %v", err)
}
}()
return nil
}
// AllEvents query events by offset and limit
func (a *CoreDataApp) AllEvents(offset int, limit int, dic *di.Container) (events []dtos.Event, totalCount uint32, err errors.EdgeX) {
dbClient := container.DBClientFrom(dic.Get)
totalCount, err = dbClient.EventTotalCount()
if err != nil {
return events, totalCount, errors.NewCommonEdgeXWrapper(err)
}
cont, err := utils.CheckCountRange(totalCount, offset, limit)
if !cont {
return []dtos.Event{}, totalCount, err
}
eventModels, err := dbClient.AllEvents(offset, limit)
if err != nil {
return events, totalCount, errors.NewCommonEdgeXWrapper(err)
}
events = make([]dtos.Event, len(eventModels))
for i, e := range eventModels {
events[i] = dtos.FromEventModelToDTO(e)
}
return events, totalCount, nil
}
// EventsByDeviceName query events with offset, limit and name
func (a *CoreDataApp) EventsByDeviceName(offset int, limit int, name string, dic *di.Container) (events []dtos.Event, totalCount uint32, err errors.EdgeX) {
if name == "" {
return events, totalCount, errors.NewCommonEdgeX(errors.KindContractInvalid, "name is empty", nil)
}
dbClient := container.DBClientFrom(dic.Get)
totalCount, err = dbClient.EventCountByDeviceName(name)
if err != nil {
return events, totalCount, errors.NewCommonEdgeXWrapper(err)
}
cont, err := utils.CheckCountRange(totalCount, offset, limit)
if !cont {
return []dtos.Event{}, totalCount, err
}
eventModels, err := dbClient.EventsByDeviceName(offset, limit, name)
if err != nil {
return events, totalCount, errors.NewCommonEdgeXWrapper(err)
}
events = make([]dtos.Event, len(eventModels))
for i, e := range eventModels {
events[i] = dtos.FromEventModelToDTO(e)
}
return events, totalCount, nil
}
// EventsByTimeRange query events with offset, limit and time range
func (a *CoreDataApp) EventsByTimeRange(startTime int64, endTime int64, offset int, limit int, dic *di.Container) (events []dtos.Event, totalCount uint32, err errors.EdgeX) {
dbClient := container.DBClientFrom(dic.Get)
totalCount, err = dbClient.EventCountByTimeRange(startTime, endTime)
if err != nil {
return events, totalCount, errors.NewCommonEdgeXWrapper(err)
}
cont, err := utils.CheckCountRange(totalCount, offset, limit)
if !cont {
return []dtos.Event{}, totalCount, err
}
eventModels, err := dbClient.EventsByTimeRange(startTime, endTime, offset, limit)
if err != nil {
return events, totalCount, errors.NewCommonEdgeXWrapper(err)
}
events = make([]dtos.Event, len(eventModels))
for i, e := range eventModels {
events[i] = dtos.FromEventModelToDTO(e)
}
return events, totalCount, nil
}
// The DeleteEventsByAge function will be invoked by controller functions
// and then invokes DeleteEventsByAge function in the infrastructure layer to remove
// events that are older than age. Age is supposed in milliseconds since created timestamp.
func (a *CoreDataApp) DeleteEventsByAge(age int64, dic *di.Container) errors.EdgeX {
dbClient := container.DBClientFrom(dic.Get)
lc := bootstrapContainer.LoggingClientFrom(dic.Get)
go func() {
err := dbClient.DeleteEventsByAge(age)
if err != nil {
lc.Errorf("Delete events by age failed: %v", err)
}
}()
return nil
}