Skip to content

Commit

Permalink
refactor: separate count and query functions in DB interface
Browse files Browse the repository at this point in the history
Signed-off-by: Ginny Guan <[email protected]>
  • Loading branch information
jinlinGuan committed Oct 21, 2024
1 parent 6ace714 commit 803138e
Show file tree
Hide file tree
Showing 14 changed files with 179 additions and 135 deletions.
9 changes: 8 additions & 1 deletion internal/core/data/application/reading.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,14 @@ func ReadingsByDeviceNameAndResourceNamesAndTimeRange(deviceName string, resourc
dbClient := container.DBClientFrom(dic.Get)
var readingModels []models.Reading
if len(resourceNames) > 0 {
readingModels, totalCount, err = dbClient.ReadingsByDeviceNameAndResourceNamesAndTimeRange(deviceName, resourceNames, start, end, offset, limit)
totalCount, err = dbClient.ReadingCountByDeviceNameAndResourceNamesAndTimeRange(deviceName, resourceNames, start, end)
if err != nil {
return readings, totalCount, errors.NewCommonEdgeXWrapper(err)
}
if cont, err := utils.CheckCountRange(totalCount, offset, limit); !cont {
return []dtos.BaseReading{}, totalCount, err
}
readingModels, err = dbClient.ReadingsByDeviceNameAndResourceNamesAndTimeRange(deviceName, resourceNames, start, end, offset, limit)
} else {
totalCount, err = dbClient.ReadingCountByDeviceNameAndTimeRange(deviceName, start, end)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/core/data/controller/http/reading_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,8 @@ func TestReadingsByDeviceNameAndResourceNamesAndTimeRange(t *testing.T) {
dbClientMock := &dbMock.DBClient{}
dbClientMock.On("ReadingCountByDeviceNameAndTimeRange", TestDeviceName, int64(0), int64(100)).Return(totalCount, nil)
dbClientMock.On("ReadingsByDeviceNameAndTimeRange", TestDeviceName, int64(0), int64(100), 0, 10).Return([]models.Reading{}, nil)
dbClientMock.On("ReadingsByDeviceNameAndResourceNamesAndTimeRange", TestDeviceName, testResourceNames, int64(0), int64(100), 0, 10).Return([]models.Reading{}, totalCount, nil)
dbClientMock.On("ReadingCountByDeviceNameAndResourceNamesAndTimeRange", TestDeviceName, testResourceNames, int64(0), int64(100)).Return(totalCount, nil)
dbClientMock.On("ReadingsByDeviceNameAndResourceNamesAndTimeRange", TestDeviceName, testResourceNames, int64(0), int64(100), 0, 10).Return([]models.Reading{}, nil)
dic.Update(di.ServiceConstructorMap{
container.DBClientInterfaceName: func(get di.Get) interface{} {
return dbClientMock
Expand Down
3 changes: 2 additions & 1 deletion internal/core/data/infrastructure/interfaces/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ type DBClient interface {
ReadingCountByDeviceNameAndResourceNameAndTimeRange(deviceName string, resourceName string, start int64, end int64) (uint32, errors.EdgeX)
ReadingCountByTimeRange(start int64, end int64) (uint32, errors.EdgeX)
ReadingsByResourceNameAndTimeRange(resourceName string, start int64, end int64, offset int, limit int) ([]model.Reading, errors.EdgeX)
ReadingsByDeviceNameAndResourceNamesAndTimeRange(deviceName string, resourceNames []string, start int64, end int64, offset, limit int) ([]model.Reading, uint32, errors.EdgeX)
ReadingsByDeviceNameAndResourceNamesAndTimeRange(deviceName string, resourceNames []string, start int64, end int64, offset, limit int) ([]model.Reading, errors.EdgeX)
ReadingCountByDeviceNameAndResourceNamesAndTimeRange(deviceName string, resourceName []string, start int64, end int64) (uint32, errors.EdgeX)
ReadingsByDeviceNameAndTimeRange(deviceName string, start int64, end int64, offset int, limit int) ([]model.Reading, errors.EdgeX)
ReadingCountByDeviceNameAndTimeRange(deviceName string, start int64, end int64) (uint32, errors.EdgeX)
LatestReadingByOffset(offset uint32) (model.Reading, errors.EdgeX)
Expand Down
53 changes: 38 additions & 15 deletions internal/core/data/infrastructure/interfaces/mocks/DBClient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion internal/core/metadata/application/deviceprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,16 @@ func DeviceProfilesByManufacturerAndModel(offset int, limit int, manufacturer st
return deviceProfiles, totalCount, errors.NewCommonEdgeX(errors.KindContractInvalid, "model is empty", nil)
}
dbClient := container.DBClientFrom(dic.Get)
dps, totalCount, err := dbClient.DeviceProfilesByManufacturerAndModel(offset, limit, manufacturer, model)
totalCount, err = dbClient.DeviceProfileCountByManufacturerAndModel(manufacturer, model)
if err != nil {
return deviceProfiles, totalCount, errors.NewCommonEdgeXWrapper(err)
}
cont, err := utils.CheckCountRange(totalCount, offset, limit)
if !cont {
return []dtos.DeviceProfile{}, totalCount, err
}

dps, err := dbClient.DeviceProfilesByManufacturerAndModel(offset, limit, manufacturer, model)
if err != nil {
return deviceProfiles, totalCount, errors.NewCommonEdgeXWrapper(err)
}
Expand Down
5 changes: 3 additions & 2 deletions internal/core/metadata/controller/http/deviceprofile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1454,8 +1454,9 @@ func TestDeviceProfilesByManufacturerAndModel(t *testing.T) {

dic := mockDic()
dbClientMock := &mocks.DBClient{}
dbClientMock.On("DeviceProfilesByManufacturerAndModel", 0, 10, TestManufacturer, TestModel).Return(deviceProfiles, expectedTotalProfileCount, nil)
dbClientMock.On("DeviceProfilesByManufacturerAndModel", 1, 2, TestManufacturer, TestModel).Return([]models.DeviceProfile{deviceProfiles[1], deviceProfiles[2]}, expectedTotalProfileCount, nil)
dbClientMock.On("DeviceProfileCountByManufacturerAndModel", TestManufacturer, TestModel).Return(expectedTotalProfileCount, nil)
dbClientMock.On("DeviceProfilesByManufacturerAndModel", 0, 10, TestManufacturer, TestModel).Return(deviceProfiles, nil)
dbClientMock.On("DeviceProfilesByManufacturerAndModel", 1, 2, TestManufacturer, TestModel).Return([]models.DeviceProfile{deviceProfiles[1], deviceProfiles[2]}, nil)
dbClientMock.On("DeviceProfilesByManufacturerAndModel", 4, 1, TestManufacturer, TestModel).Return([]models.DeviceProfile{}, expectedTotalProfileCount, errors.NewCommonEdgeX(errors.KindRangeNotSatisfiable, "query objects bounds out of range.", nil))
dic.Update(di.ServiceConstructorMap{
container.DBClientInterfaceName: func(get di.Get) interface{} {
Expand Down
3 changes: 2 additions & 1 deletion internal/core/metadata/infrastructure/interfaces/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ type DBClient interface {
AllDeviceProfiles(offset int, limit int, labels []string) ([]model.DeviceProfile, errors.EdgeX)
DeviceProfilesByModel(offset int, limit int, model string) ([]model.DeviceProfile, errors.EdgeX)
DeviceProfilesByManufacturer(offset int, limit int, manufacturer string) ([]model.DeviceProfile, errors.EdgeX)
DeviceProfilesByManufacturerAndModel(offset int, limit int, manufacturer string, model string) ([]model.DeviceProfile, uint32, errors.EdgeX)
DeviceProfilesByManufacturerAndModel(offset int, limit int, manufacturer string, model string) ([]model.DeviceProfile, errors.EdgeX)
DeviceProfileCountByLabels(labels []string) (uint32, errors.EdgeX)
DeviceProfileCountByManufacturer(manufacturer string) (uint32, errors.EdgeX)
DeviceProfileCountByModel(model string) (uint32, errors.EdgeX)
DeviceProfileCountByManufacturerAndModel(manufacturer string, model string) (uint32, errors.EdgeX)

AddDeviceService(ds model.DeviceService) (model.DeviceService, errors.EdgeX)
DeviceServiceById(id string) (model.DeviceService, errors.EdgeX)
Expand Down
51 changes: 37 additions & 14 deletions internal/core/metadata/infrastructure/interfaces/mocks/DBClient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 9 additions & 13 deletions internal/pkg/infrastructure/postgres/device_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (

pkgCommon "github.com/edgexfoundry/edgex-go/internal/pkg/common"
pgClient "github.com/edgexfoundry/edgex-go/internal/pkg/db/postgres"
"github.com/edgexfoundry/edgex-go/internal/pkg/utils"
"github.com/edgexfoundry/go-mod-core-contracts/v3/errors"
model "github.com/edgexfoundry/go-mod-core-contracts/v3/models"
)
Expand Down Expand Up @@ -180,21 +179,11 @@ func (c *Client) DeviceProfilesByManufacturer(offset int, limit int, manufacture
}

// DeviceProfilesByManufacturerAndModel query device profiles with offset, limit, manufacturer and model
func (c *Client) DeviceProfilesByManufacturerAndModel(offset int, limit int, manufacturer string, model string) (profiles []model.DeviceProfile, totalCount uint32, err errors.EdgeX) {
func (c *Client) DeviceProfilesByManufacturerAndModel(offset int, limit int, manufacturer string, model string) (profiles []model.DeviceProfile, err errors.EdgeX) {
ctx := context.Background()
offset, validLimit := getValidOffsetAndLimit(offset, limit)
queryObj := map[string]any{modelField: model, manufacturerField: manufacturer}
totalCount, err = getTotalRowsCount(ctx, c.ConnPool, sqlQueryCountByJSONField(deviceProfileTableName), queryObj)
if err != nil {
return profiles, totalCount, err
}
cont, err := utils.CheckCountRange(totalCount, offset, limit)
if !cont {
return profiles, totalCount, err
}
profiles, err = queryDeviceProfiles(ctx, c.ConnPool, sqlQueryContentByJSONFieldWithPagination(deviceProfileTableName), queryObj, offset, validLimit)

return profiles, totalCount, err
return queryDeviceProfiles(ctx, c.ConnPool, sqlQueryContentByJSONFieldWithPagination(deviceProfileTableName), queryObj, offset, validLimit)
}

// DeviceProfileCountByLabels returns the total count of Device Profiles with labels specified. If no label is specified, the total count of all device profiles will be returned.
Expand Down Expand Up @@ -222,6 +211,13 @@ func (c *Client) DeviceProfileCountByModel(model string) (uint32, errors.EdgeX)
return getTotalRowsCount(ctx, c.ConnPool, sqlQueryCountByJSONField(deviceProfileTableName), queryObj)
}

// DeviceProfileCountByManufacturerAndModel returns the count of Device Profiles associated with specified manufacturer and model
func (c *Client) DeviceProfileCountByManufacturerAndModel(manufacturer, model string) (uint32, errors.EdgeX) {
ctx := context.Background()
queryObj := map[string]any{manufacturerField: manufacturer, modelField: model}
return getTotalRowsCount(ctx, c.ConnPool, sqlQueryCountByJSONField(deviceProfileTableName), queryObj)
}

func deviceProfileNameExists(ctx context.Context, connPool *pgxpool.Pool, name string) (bool, errors.EdgeX) {
var exists bool
queryObj := map[string]any{nameField: name}
Expand Down
39 changes: 14 additions & 25 deletions internal/pkg/infrastructure/postgres/reading.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (

pgClient "github.com/edgexfoundry/edgex-go/internal/pkg/db/postgres"
dbModels "github.com/edgexfoundry/edgex-go/internal/pkg/infrastructure/postgres/models"
"github.com/edgexfoundry/edgex-go/internal/pkg/utils"

"github.com/edgexfoundry/go-mod-core-contracts/v3/errors"
model "github.com/edgexfoundry/go-mod-core-contracts/v3/models"

Expand Down Expand Up @@ -130,39 +128,21 @@ func (c *Client) ReadingsByDeviceNameAndResourceNameAndTimeRange(deviceName stri
}

// ReadingsByDeviceNameAndResourceNamesAndTimeRange query readings by the specified device and resourceName slice, origin within the time range, offset and limit
func (c *Client) ReadingsByDeviceNameAndResourceNamesAndTimeRange(deviceName string, resourceNames []string, start int64, end int64, offset, limit int) ([]model.Reading, uint32, errors.EdgeX) {
func (c *Client) ReadingsByDeviceNameAndResourceNamesAndTimeRange(deviceName string, resourceNames []string, start int64, end int64, offset, limit int) ([]model.Reading, errors.EdgeX) {
ctx := context.Background()

sqlStatement := sqlQueryAllWithPaginationAndTimeRangeDescByCol(readingTableName, originCol, originCol,
[]string{resourceNameCol}, deviceNameCol, resourceNameCol)

// build the query args for the where condition using in querying readings and reading count
queryArgs := []any{start, end, deviceName, resourceNames}
// make a copy for query count args as we don't need offset and limit while querying total count
queryCountArgs := append([]any{}, queryArgs...)
// add offset and limit for query args
queryArgs = append(queryArgs, offset, limit)

// build the query args for the where condition using in querying readings
queryArgs := []any{start, end, deviceName, resourceNames, offset, limit}
// query readings
readings, err := queryReadings(ctx, c.ConnPool, sqlStatement, queryArgs...)
if err != nil {
return nil, 0, errors.NewCommonEdgeXWrapper(err)
}

// get the total count of readings based on the condition column names and query count args
totalCount, err := getTotalRowsCount(context.Background(),
c.ConnPool,
sqlQueryCountByTimeRangeCol(readingTableName, originCol, []string{resourceNameCol}, deviceNameCol, resourceNameCol),
queryCountArgs...)
if err != nil {
return nil, 0, errors.NewCommonEdgeXWrapper(err)
}
cont, err := utils.CheckCountRange(totalCount, offset, limit)
if !cont {
return readings, totalCount, err
return nil, errors.NewCommonEdgeXWrapper(err)
}

return readings, totalCount, nil
return readings, nil
}

// ReadingCountByDeviceName returns the count of Readings associated a specific Device from db
Expand Down Expand Up @@ -213,6 +193,15 @@ func (c *Client) ReadingCountByDeviceNameAndResourceNameAndTimeRange(deviceName
resourceName)
}

// ReadingCountByDeviceNameAndResourceNamesAndTimeRange returns the count of readings by origin within the time range
// associated with the specified device and resourceName slice from db
func (c *Client) ReadingCountByDeviceNameAndResourceNamesAndTimeRange(deviceName string, resourceNames []string, start int64, end int64) (uint32, errors.EdgeX) {
return getTotalRowsCount(context.Background(),
c.ConnPool,
sqlQueryCountByTimeRangeCol(readingTableName, originCol, []string{resourceNameCol}, deviceNameCol, resourceNameCol),
start, end, deviceName, resourceNames)
}

func (c *Client) LatestReadingByOffset(offset uint32) (model.Reading, errors.EdgeX) {
ctx := context.Background()

Expand Down
Loading

0 comments on commit 803138e

Please sign in to comment.