Skip to content

Commit

Permalink
dr: Add the end device provisioning info to the bleve index
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlad Vitan committed Aug 7, 2024
1 parent 1ee0931 commit e2dd892
Showing 1 changed file with 54 additions and 4 deletions.
58 changes: 54 additions & 4 deletions pkg/devicerepository/store/bleve/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ import (
)

const (
indexPath = "index.bleve"
brandDocumentType = "brand"
modelDocumentType = "model"
profileDocumentType = "profile"
indexPath = "index.bleve"
brandDocumentType = "brand"
modelDocumentType = "model"
profileDocumentType = "profile"
indexableEndDeviceProvisioningInfoDocumentType = "endDeviceProvisioningInfo"
)

type indexableBrand struct {
Expand Down Expand Up @@ -71,6 +72,13 @@ type indexableProfile struct {
Type string // Index document type, always profileDocumentType
}

type indexableEndDeviceProvisioningInfo struct {
EndDeviceProvisioningInfo *ttnpb.EndDeviceProvisioningInfo

VendorID, VendorProfileID string
Type string // endDeviceProvisioningInfoType
}

func newIndex(path string, overwrite bool, keywords ...string) (bleve.Index, error) {
mapping := bleve.NewIndexMapping()
if st, err := os.Stat(path); err == nil && st.IsDir() && overwrite {
Expand Down Expand Up @@ -203,11 +211,53 @@ func (c Config) Initialize(ctx context.Context, lorawanDevicesPath string, overw
}
}

// Add the end device provisioning info to the index.
for _, model := range models.Models {
profiles, err := s.GetEndDeviceProfiles(store.GetEndDeviceProfilesRequest{
ModelID: model.ModelId,
})
if err != nil && !errors.IsNotFound(err) {
return err
}

for _, profile := range profiles.Profiles {
vendorID := strconv.Itoa(int(brand.LoraAllianceVendorId))
vendorProfileID := strconv.Itoa(int(profile.VendorProfileID))

template, err := s.GetTemplate(&ttnpb.GetTemplateRequest{
EndDeviceProfileIds: &ttnpb.GetTemplateRequest_EndDeviceProfileIdentifiers{
VendorId: brand.LoraAllianceVendorId,
VendorProfileId: profile.VendorProfileID,
},
}, profile)
if err != nil {
return err
}

edpi := indexableEndDeviceProvisioningInfo{
VendorID: vendorID,
VendorProfileID: vendorProfileID,

Type: indexableEndDeviceProvisioningInfoDocumentType,
EndDeviceProvisioningInfo: &ttnpb.EndDeviceProvisioningInfo{
EndDeviceModel: model,
EndDeviceTemplate: template,
},
}

if err := batch.Index(fmt.Sprintf("%s:%s", vendorID, vendorProfileID), edpi); err != nil {
return err
}
}
}

log.FromContext(ctx).WithField("brand_id", brand.BrandId).Debug("Adding brand to index")
if err := index.Batch(batch); err != nil {
return err
}

}

return nil
}

Expand Down

0 comments on commit e2dd892

Please sign in to comment.