-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #364 from green-ecolution/feature/set-watering-pla…
…n-status-on-tree-CRUD Feature/set watering plan status on tree crud
- Loading branch information
Showing
13 changed files
with
661 additions
and
333 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package tree | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
|
||
"github.com/green-ecolution/green-ecolution-backend/internal/entities" | ||
"github.com/green-ecolution/green-ecolution-backend/internal/logger" | ||
"github.com/green-ecolution/green-ecolution-backend/internal/service/domain/utils" | ||
"github.com/green-ecolution/green-ecolution-backend/internal/storage/postgres/tree" | ||
) | ||
|
||
func (s *TreeService) HandleNewSensorData(ctx context.Context, event *entities.EventNewSensorData) error { | ||
log := logger.GetLogger(ctx) | ||
log.Debug("handle event", "event", event.Type(), "service", "TreeService") | ||
t, err := s.treeRepo.GetBySensorID(ctx, event.New.SensorID) | ||
if err != nil { | ||
log.Error("failed to get tree by sensor id", "sensor_id", event.New.SensorID, "err", err) | ||
return nil | ||
} | ||
|
||
status := utils.CalculateWateringStatus(ctx, t.PlantingYear, event.New.Data.Watermarks) | ||
|
||
if status == t.WateringStatus { | ||
log.Debug("sensor status has not changed", "sensor_status", status) | ||
return nil | ||
} | ||
|
||
newTree, err := s.treeRepo.Update(ctx, t.ID, tree.WithWateringStatus(status)) | ||
if err != nil { | ||
log.Error("failed to update tree with new watering status", "tree_id", t.ID, "watering_status", status, "err", err) | ||
return err | ||
} | ||
|
||
slog.Info("updating tree watering status", "prev_status", t.WateringStatus, "new_status", status) | ||
|
||
s.publishUpdateTreeEvent(ctx, t, newTree) | ||
return nil | ||
} |
162 changes: 162 additions & 0 deletions
162
internal/service/domain/tree/handle_new_sensor_data_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package tree | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/green-ecolution/green-ecolution-backend/internal/entities" | ||
"github.com/green-ecolution/green-ecolution-backend/internal/storage" | ||
storageMock "github.com/green-ecolution/green-ecolution-backend/internal/storage/_mock" | ||
"github.com/green-ecolution/green-ecolution-backend/internal/worker" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
func TestTreeService_HandleNewSensorData(t *testing.T) { | ||
t.Run("should update watering status on new sensor data event", func(t *testing.T) { | ||
treeRepo := storageMock.NewMockTreeRepository(t) | ||
sensorRepo := storageMock.NewMockSensorRepository(t) | ||
imageRepo := storageMock.NewMockImageRepository(t) | ||
clusterRepo := storageMock.NewMockTreeClusterRepository(t) | ||
eventManager := worker.NewEventManager(entities.EventTypeUpdateTree) | ||
svc := NewTreeService(treeRepo, sensorRepo, imageRepo, clusterRepo, eventManager) | ||
|
||
_, ch, _ := eventManager.Subscribe(entities.EventTypeUpdateTree) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
go eventManager.Run(ctx) | ||
|
||
sensorDataEvent := entities.SensorData{ | ||
SensorID: "sensor-1", | ||
Data: &entities.MqttPayload{ | ||
Watermarks: []entities.Watermark{ | ||
{Centibar: 30, Depth: 30}, | ||
{Centibar: 40, Depth: 60}, | ||
{Centibar: 50, Depth: 90}, | ||
}, | ||
}, | ||
} | ||
|
||
treeNew := entities.Tree{ | ||
ID: 1, | ||
PlantingYear: int32(time.Now().Year() - 2), | ||
WateringStatus: entities.WateringStatusGood, | ||
} | ||
|
||
tree := entities.Tree{ | ||
ID: 1, | ||
PlantingYear: int32(time.Now().Year() - 2), | ||
WateringStatus: entities.WateringStatusUnknown, | ||
} | ||
|
||
event := entities.NewEventSensorData(&sensorDataEvent) | ||
|
||
treeRepo.EXPECT().GetBySensorID(mock.Anything, "sensor-1").Return(&tree, nil) | ||
treeRepo.EXPECT().Update(mock.Anything, mock.Anything, mock.Anything).Return(&treeNew, nil) | ||
|
||
err := svc.HandleNewSensorData(context.Background(), &event) | ||
|
||
assert.NoError(t, err) | ||
select { | ||
case receivedEvent := <-ch: | ||
e, ok := receivedEvent.(entities.EventUpdateTree) | ||
assert.True(t, ok) | ||
assert.Equal(t, *e.Prev, tree) | ||
assert.Equal(t, *e.New, treeNew) | ||
case <-time.After(100 * time.Millisecond): | ||
t.Fatal("event was not received") | ||
} | ||
}) | ||
|
||
t.Run("should not update and not send event if the sensor has no linked tree", func(t *testing.T) { | ||
treeRepo := storageMock.NewMockTreeRepository(t) | ||
sensorRepo := storageMock.NewMockSensorRepository(t) | ||
imageRepo := storageMock.NewMockImageRepository(t) | ||
clusterRepo := storageMock.NewMockTreeClusterRepository(t) | ||
eventManager := worker.NewEventManager(entities.EventTypeUpdateTree) | ||
svc := NewTreeService(treeRepo, sensorRepo, imageRepo, clusterRepo, eventManager) | ||
|
||
// event | ||
_, ch, _ := eventManager.Subscribe(entities.EventTypeUpdateTree) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
go eventManager.Run(ctx) | ||
|
||
sensorDataEvent := entities.SensorData{ | ||
SensorID: "sensor-1", | ||
Data: &entities.MqttPayload{ | ||
Watermarks: []entities.Watermark{ | ||
{Centibar: 61, Depth: 30}, | ||
{Centibar: 24, Depth: 60}, | ||
{Centibar: 24, Depth: 90}, | ||
}, | ||
}, | ||
} | ||
|
||
event := entities.NewEventSensorData(&sensorDataEvent) | ||
|
||
treeRepo.EXPECT().GetBySensorID(mock.Anything, "sensor-1").Return(nil, storage.ErrTreeNotFound) | ||
|
||
// when | ||
err := svc.HandleNewSensorData(context.Background(), &event) | ||
|
||
// then | ||
assert.NoError(t, err) | ||
select { | ||
case <-ch: | ||
t.Fatal("event was received. It should not have been sent") | ||
case <-time.After(100 * time.Millisecond): | ||
assert.True(t, true) | ||
} | ||
}) | ||
|
||
t.Run("should not update and not send event if tree could not be updated", func(t *testing.T) { | ||
treeRepo := storageMock.NewMockTreeRepository(t) | ||
sensorRepo := storageMock.NewMockSensorRepository(t) | ||
imageRepo := storageMock.NewMockImageRepository(t) | ||
clusterRepo := storageMock.NewMockTreeClusterRepository(t) | ||
eventManager := worker.NewEventManager(entities.EventTypeUpdateTree) | ||
svc := NewTreeService(treeRepo, sensorRepo, imageRepo, clusterRepo, eventManager) | ||
|
||
// event | ||
_, ch, _ := eventManager.Subscribe(entities.EventTypeUpdateTree) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
go eventManager.Run(ctx) | ||
|
||
sensorDataEvent := entities.SensorData{ | ||
SensorID: "sensor-1", | ||
Data: &entities.MqttPayload{ | ||
Watermarks: []entities.Watermark{ | ||
{Centibar: 30, Depth: 30}, | ||
{Centibar: 40, Depth: 60}, | ||
{Centibar: 50, Depth: 90}, | ||
}, | ||
}, | ||
} | ||
|
||
tree := entities.Tree{ | ||
ID: 1, | ||
PlantingYear: int32(time.Now().Year() - 2), | ||
WateringStatus: entities.WateringStatusUnknown, | ||
} | ||
|
||
event := entities.NewEventSensorData(&sensorDataEvent) | ||
|
||
treeRepo.EXPECT().GetBySensorID(mock.Anything, "sensor-1").Return(&tree, nil) | ||
treeRepo.EXPECT().Update(mock.Anything, mock.Anything, mock.Anything).Return(nil, storage.ErrTreeNotFound) | ||
|
||
// when | ||
err := svc.HandleNewSensorData(context.Background(), &event) | ||
|
||
// then | ||
assert.ErrorIs(t, err, storage.ErrTreeNotFound) | ||
select { | ||
case <-ch: | ||
t.Fatal("event was received. It should not have been sent") | ||
case <-time.After(100 * time.Millisecond): | ||
assert.True(t, true) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.