Skip to content

Commit

Permalink
update validation file
Browse files Browse the repository at this point in the history
Signed-off-by: Musilah <[email protected]>
  • Loading branch information
Musilah committed Feb 28, 2024
1 parent bd59b13 commit f7d9899
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 34 deletions.
3 changes: 3 additions & 0 deletions internal/apiutil/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,7 @@ var (

// ErrRollbackTx indicates failed to rollback transaction.
ErrRollbackTx = errors.New("failed to rollback transaction")

// ErrAggregation indicates missing aggregation and interval.
ErrAggregation = errors.New("both aggregation and interval are required")
)
4 changes: 2 additions & 2 deletions pkg/sdk/go/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ func (sdk mgSDK) SendMessage(chanName, msg, key string) errors.SDKError {
subtopicPart = fmt.Sprintf("/%s", strings.ReplaceAll(chanNameParts[1], ".", "/"))
}

reqUrl := fmt.Sprintf("%s/channels/%s/messages%s", sdk.httpAdapterURL, chanID, subtopicPart)
reqURL := fmt.Sprintf("%s/channels/%s/messages%s", sdk.httpAdapterURL, chanID, subtopicPart)

_, _, err := sdk.processRequest(http.MethodPost, reqUrl, ThingPrefix+key, []byte(msg), nil, http.StatusAccepted)
_, _, err := sdk.processRequest(http.MethodPost, reqURL, ThingPrefix+key, []byte(msg), nil, http.StatusAccepted)

return err
}
Expand Down
4 changes: 4 additions & 0 deletions readers/api/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,9 @@ func (req listMessagesReq) validate() error {
return apiutil.ErrInvalidComparator
}

if req.pageMeta.Aggregation == "" || req.pageMeta.Interval == "" {
return apiutil.ErrAggregation
}

return nil
}
4 changes: 0 additions & 4 deletions readers/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,6 @@ func decodeList(_ context.Context, r *http.Request) (interface{}, error) {
return nil, errors.Wrap(apiutil.ErrValidation, err)
}

if aggregation == "" || interval == "" {
return nil, errors.Wrap(apiutil.ErrValidation, errors.New("both aggregation and interval are required"))
}

req := listMessagesReq{
chanID: chi.URLParam(r, "chanID"),
token: apiutil.ExtractBearerToken(r),
Expand Down
30 changes: 2 additions & 28 deletions readers/timescale/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,7 @@ func (tr timescaleRepository) ReadAll(chanID string, rpm readers.PageMetadata) (
// If aggregation is provided, add time_bucket and aggregation to the query
switch {
case rpm.Aggregation != "":
q = fmt.Sprintf(`
SELECT
EXTRACT(epoch FROM time_bucket('%s', to_timestamp(time))) AS time,
%s(value) AS value
%s
ORDER BY
%s DESC
LIMIT
:limit
OFFSET
:offset;`,
rpm.Interval,
rpm.Aggregation,
baseQuery,
order,
)
q = fmt.Sprintf(`SELECT EXTRACT(epoch FROM time_bucket('%s', to_timestamp(time))) AS time, %s(value) AS value %s ORDER BY %s DESC LIMIT :limit OFFSET :offset;`, rpm.Interval, rpm.Aggregation, baseQuery, order)
default:
// Construct the base query without time_bucket and aggregation
q = fmt.Sprintf(`SELECT * FROM %s WHERE %s ORDER BY %s DESC LIMIT :limit OFFSET :offset;`, format, fmtCondition(chanID, rpm), order)
Expand Down Expand Up @@ -121,18 +106,7 @@ func (tr timescaleRepository) ReadAll(chanID string, rpm readers.PageMetadata) (
var countQuery string
switch {
case rpm.Aggregation != "":
countQuery = fmt.Sprintf(`
SELECT COUNT(*)
FROM (
SELECT
EXTRACT(epoch FROM time_bucket('%s', to_timestamp(time))) AS time,
%s(value) AS value
%s
) AS subquery;`,
rpm.Interval,
rpm.Aggregation,
baseQuery,
)
countQuery = fmt.Sprintf(`SELECT COUNT(*) FROM (SELECT EXTRACT(epoch FROM time_bucket('%s', to_timestamp(time))) AS time, %s(value) AS value %s) AS subquery;`, rpm.Interval, rpm.Aggregation, baseQuery)
default:
countQuery = fmt.Sprintf(`SELECT COUNT(*) FROM %s WHERE %s;`, format, fmtCondition(chanID, rpm))
}
Expand Down

0 comments on commit f7d9899

Please sign in to comment.