@@ -112,36 +112,46 @@ func isActivityAndActivityOnly(updatedFields []string) bool {
112112 return hasActivity
113113}
114114
115+ // deviceDataIdentifiers are common to DosingDecision and Glucose.
116+ //
117+ // They facilitate queries of the latest data for alerts evaluation.
118+ type deviceDataIdentifiers struct {
119+ UploadID string `json:"uploadId,omitempty" bson:"uploadId,omitempty"`
120+ UserID string `json:"-" bson:"_userId,omitempty"`
121+ }
122+
115123func (c * Consumer ) consumeDeviceData (ctx context.Context ,
116124 session sarama.ConsumerGroupSession , msg * sarama.ConsumerMessage ) error {
117125
118- datum := & Glucose {}
119- if _ , err := unmarshalMessageValue (msg .Value , datum ); err != nil {
120- return err
121- }
122126 lgr := c .logger (ctx )
123- lgr .WithField ("data" , datum ).Info ("consuming a device data message" )
124127
125- if datum .UserID == nil {
126- return errors .New ("Unable to retrieve alerts configs: userID is nil" )
128+ id := & deviceDataIdentifiers {}
129+ if _ , err := unmarshalMessageValue (msg .Value , id ); err != nil {
130+ return errors .Wrap (err , "Unable to unmarshal device data message" )
131+ }
132+
133+ lgr .WithField ("identifiers" , id ).Info ("consuming a device data message" )
134+
135+ if id .UserID == "" {
136+ return errors .New ("Unable to retrieve alerts configs: userID is empty" )
127137 }
128- if datum .UploadID == nil {
129- return errors .New ("Unable to retrieve alerts configs: DataSetID is nil " )
138+ if id .UploadID == "" {
139+ return errors .New ("Unable to retrieve alerts configs: DataSetID is empty " )
130140 }
131- ctx = log .NewContextWithLogger (ctx , lgr .WithField ("followedUserID" , * datum .UserID ))
141+ ctx = log .NewContextWithLogger (ctx , lgr .WithField ("followedUserID" , id .UserID ))
132142 lastComm := alerts.LastCommunication {
133- UserID : * datum .UserID ,
143+ UserID : id .UserID ,
134144 LastReceivedDeviceData : time .Now (),
135- DataSetID : * datum .UploadID ,
145+ DataSetID : id .UploadID ,
136146 }
137147 err := c .LastCommunications .RecordReceivedDeviceData (ctx , lastComm )
138148 if err != nil {
139149 lgr .WithError (err ).Info ("Unable to record device data received" )
140150 }
141- notes , err := c .Evaluator .EvaluateData (ctx , * datum .UserID , * datum .UploadID )
151+ notes , err := c .Evaluator .EvaluateData (ctx , id .UserID , id .UploadID )
142152 if err != nil {
143153 format := "Unable to evalaute device data triggered event for user %s"
144- return errors .Wrapf (err , format , * datum .UserID )
154+ return errors .Wrapf (err , format , id .UserID )
145155 }
146156 for idx , note := range notes {
147157 lgr .WithField ("idx" , idx ).WithField ("note" , note ).Debug ("notes" )
0 commit comments