Skip to content

Commit

Permalink
fix(shadow): field "eabled" is always false when get shadow by thingId
Browse files Browse the repository at this point in the history
  • Loading branch information
ogofly committed Mar 26, 2024
1 parent 2bc3372 commit 0656ac6
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 49 deletions.
33 changes: 6 additions & 27 deletions shadow/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ type Repo interface {
Create(ctx context.Context, thingId string, s Shadow) (*Shadow, error)
Delete(ctx context.Context, thingId string) error
Update(ctx context.Context, thingId string, version int64, s Shadow) (*Shadow, error)
Get(ctx context.Context, thingId string) (*Shadow, error)
Query(ctx context.Context, q model.PageQuery, query ParsedQuerySql) (model.PageData[Entity], error)
Get(ctx context.Context, thingId string) (*ShadowWithEnable, error)
Query(ctx context.Context, q model.PageQuery, query ParsedQuerySql) (model.PageData[ShadowWithStatus], error)

UpdateConnStatus(ctx context.Context, s []connector.ClientInfo) error
UpdateAllConnStatusDisconnect(ctx context.Context, updateTimeBefore time.Time) error
Expand Down Expand Up @@ -230,8 +230,7 @@ func (s *shadowSvc) Query(ctx context.Context, pq model.PageQuery, query string)
return Page{}, err
}

ssList := s.toShadowWithStatus(p.Content)
mList, err := entityToMap(ssList)
mList, err := entityToMap(p.Content)
if err != nil {
return Page{}, err
}
Expand Down Expand Up @@ -260,26 +259,6 @@ func entityToMap(list []ShadowWithStatus) ([]map[string]interface{}, error) {
return res, nil
}

func (s *shadowSvc) toShadowWithStatus(list []Entity) []ShadowWithStatus {
res := make([]ShadowWithStatus, len(list))
for i, v := range list {
sd, err := toShadow(v)
if err != nil {
log.Errorf("shadow convert error %v, shadow: %#v", err, sd)
continue
}
ss := ShadowWithStatus{Shadow: sd}
cs := v.ConnStatus
ss.Enabled = v.Enabled
ss.Connected = &cs.Connected
ss.ConnectedAt = cs.ConnectedAt
ss.DisconnectedAt = cs.DisconnectedAt
ss.RemoteAddr = cs.RemoteAddr
res[i] = ss
}
return res
}

func (s *shadowSvc) Get(ctx context.Context, thingId string, opt GetOption) (ShadowWithStatus, error) {
ss, err := s.repo.Get(ctx, thingId)
if err != nil {
Expand All @@ -288,7 +267,7 @@ func (s *shadowSvc) Get(ctx context.Context, thingId string, opt GetOption) (Sha
if ss == nil {
return ShadowWithStatus{}, model.ErrNotFound
}
res := ShadowWithStatus{Shadow: *ss}
res := ShadowWithStatus{Shadow: ss.Shadow, Enabled: ss.Enabled}
if opt.WithStatus {
ci, err := s.connectorChecker.ClientInfo(thingId)
if err == nil {
Expand Down Expand Up @@ -360,7 +339,7 @@ func (s *shadowSvc) setState(
// update

ss.Version++
reS, err := txtRepo.Update(ctx, thingId, version, *ss)
reS, err := txtRepo.Update(ctx, thingId, version, ss.Shadow)
if err != nil {
return err
}
Expand Down Expand Up @@ -468,7 +447,7 @@ func (s *shadowSvc) SetTag(ctx context.Context, thingId string, t TagsReq) error
mergerShadow := MergeTags(cur.Tags, t.Tags)
cur.Version++
cur.Tags = mergerShadow
_, err = txtRepo.Update(ctx, thingId, t.Version, *cur)
_, err = txtRepo.Update(ctx, thingId, t.Version, cur.Shadow)
return err
})
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion shadow/shadow_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ type Entity struct {
CreatedAt time.Time `gorm:"autoCreateTime" json:"createdAt"`

ConnStatus ConnStatusEntity `gorm:"foreignKey:thing_id;constraint:OnDelete:CASCADE" json:"connStatus"`
Enabled bool `gorm:"-" json:"enabled"` // fetch from table "thing"
}

func (t Entity) TableName() string {
Expand Down
63 changes: 42 additions & 21 deletions shadow/shadow_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ type shadowRepo struct {
db *gorm.DB
}

type EntityWithEnable struct {
Entity
Enabled bool
}

func (r shadowRepo) ExecWithTx(f func(txtRepo Repo) error) error {
return r.db.Transaction(func(tx *gorm.DB) error {
txRepo := NewShadowRepo(tx)
Expand Down Expand Up @@ -125,18 +130,22 @@ func (r shadowRepo) UpdateAllConnStatusDisconnect(ctx context.Context, updateTim
return res.Error
}

func (r shadowRepo) Get(ctx context.Context, thingId string) (*Shadow, error) {
en := Entity{ThingId: thingId}
res := r.db.First(&en)
func (r shadowRepo) Get(ctx context.Context, thingId string) (*ShadowWithEnable, error) {
e := EntityWithEnable{}
res := r.db.Model(&Entity{}).
Select("t.enabled", "shadow.*").
Joins("LEFT JOIN thing t ON t.id=shadow.thing_id").
Where("shadow.thing_id=?", thingId).
First(&e)

err := res.Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
s, err := toShadow(en)
if err != nil {
return nil, err
}
return &s, err
s, err := toShadow(e.Entity)
se := ShadowWithEnable{Enabled: e.Enabled, Shadow: s}

return &se, err
}

func (r shadowRepo) GetVersion(ctx context.Context, thingId string) (version int64, err error) {
Expand All @@ -160,10 +169,10 @@ func (r shadowRepo) Delete(ctx context.Context, thingId string) error {
return errors.Wrap(err, "delete shadow "+thingId)
}

func (r shadowRepo) Query(ctx context.Context, pq model.PageQuery, q ParsedQuerySql) (model.PageData[Entity], error) {
func (r shadowRepo) Query(ctx context.Context, pq model.PageQuery, q ParsedQuerySql) (model.PageData[ShadowWithStatus], error) {
offset := pq.Offset()
limit := pq.Limit()
var page model.PageData[Entity]
var page model.PageData[ShadowWithStatus]
var total int64

db := r.db.WithContext(ctx).
Expand All @@ -189,26 +198,38 @@ func (r shadowRepo) Query(ctx context.Context, pq model.PageQuery, q ParsedQuery
db.Order(q.OrderBy)
}

results := make([]struct {
Entity
Enabled bool
}, 0)
results := make([]EntityWithEnable, 0)
res = db.Offset(offset).
Limit(limit).
Find(&results)
if res.Error != nil {
return page, res.Error
}

l := make([]Entity, 0)
for _, tmp := range results {
e := tmp.Entity
e.Enabled = tmp.Enabled
l = append(l, e)
}
l, err := toShadowWithStatus(results)
page.Content = l

return page, nil
return page, err
}

func toShadowWithStatus(list []EntityWithEnable) ([]ShadowWithStatus, error) {
res := make([]ShadowWithStatus, len(list))
for i, v := range list {
ss := ShadowWithStatus{}
if s, err := toShadow(v.Entity); err != nil {
return res, errors.WithMessage(err, "entity toShadow")
} else {
ss.Shadow = s
}
ss.Enabled = v.Enabled
cs := v.ConnStatus
ss.Connected = &cs.Connected
ss.ConnectedAt = cs.ConnectedAt
ss.DisconnectedAt = cs.DisconnectedAt
ss.RemoteAddr = cs.RemoteAddr
res[i] = ss
}
return res, nil
}

var _ Repo = (*shadowRepo)(nil)
Expand Down
5 changes: 5 additions & 0 deletions shadow/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ type Shadow struct {
CreatedAt time.Time `json:"createdAt"`
}

type ShadowWithEnable struct {
Enabled bool `json:"enabled"`
Shadow
}

type ShadowWithStatus struct {
Enabled bool `json:"enabled"`
Connected *bool `json:"connected,omitempty"`
Expand Down

0 comments on commit 0656ac6

Please sign in to comment.