Skip to content

Commit

Permalink
fix order by in query
Browse files Browse the repository at this point in the history
Signed-off-by: Musilah <[email protected]>
  • Loading branch information
Musilah committed Mar 1, 2024
1 parent d984262 commit 6e8269e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions api/openapi/readers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,10 @@ components:
type: string
default: none
enum:
- avg
- sum
- min
- max
- MAX
- SUM
- MIN
- MAX
required: false
Interval:
name: interval
Expand Down
4 changes: 2 additions & 2 deletions readers/timescale/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func New(db *sqlx.DB) readers.MessageRepository {
func (tr timescaleRepository) ReadAll(chanID string, rpm readers.PageMetadata) (readers.MessagesPage, error) {
order := "time"
format := defTable
baseQuery := fmt.Sprintf(`FROM %s WHERE %s GROUP BY time`, format, fmtCondition(rpm))
baseQuery := fmt.Sprintf(`FROM %s WHERE %s GROUP BY 1`, format, fmtCondition(rpm))

if rpm.Format != "" && rpm.Format != defTable {
order = "created"
Expand All @@ -42,7 +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/1000))) 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/1000))) AS time, %s(value) AS value %s ORDER BY 1 DESC LIMIT :limit OFFSET :offset;`, rpm.Interval, rpm.Aggregation, baseQuery)
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(rpm), order)
Expand Down

0 comments on commit 6e8269e

Please sign in to comment.