Skip to content

Commit

Permalink
fxi: fix events and event_delivery queries
Browse files Browse the repository at this point in the history
  • Loading branch information
jirevwe committed Dec 20, 2024
1 parent b6c7b92 commit cd1338d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 28 deletions.
29 changes: 16 additions & 13 deletions database/postgres/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,21 @@ const (
searchFilter = ` AND search_token @@ websearch_to_tsquery('simple',:query) `

baseCountPrevEvents = `
SELECT COUNT(DISTINCT(ev.id)) AS COUNT
FROM convoy.events ev
LEFT JOIN convoy.events_endpoints ee ON ev.id = ee.event_id
WHERE ev.deleted_at IS NULL
select exists(
SELECT 1
FROM convoy.events ev
LEFT JOIN convoy.events_endpoints ee ON ev.id = ee.event_id
WHERE ev.deleted_at IS NULL
`

baseCountPrevEventSearch = `
SELECT COUNT(DISTINCT(ev.id)) AS COUNT
FROM convoy.events_search ev
LEFT JOIN convoy.events_endpoints ee ON ev.id = ee.event_id
WHERE ev.deleted_at IS NULL
select exists(
SELECT 1
FROM convoy.events_search ev
LEFT JOIN convoy.events_endpoints ee ON ev.id = ee.event_id
WHERE ev.deleted_at IS NULL
`

countPrevEvents = ` AND ev.id > :cursor GROUP BY ev.id ORDER BY ev.id %s LIMIT 1`

softDeleteProjectEvents = `
Expand Down Expand Up @@ -551,7 +554,7 @@ func (e *eventRepo) LoadEventsPaged(ctx context.Context, projectID string, filte
events = append(events, data)
}

var count datastore.PrevRowCount
var rowCount datastore.PrevRowCount
if len(events) > 0 {
first := events[0]
qarg := arg
Expand All @@ -565,7 +568,7 @@ func (e *eventRepo) LoadEventsPaged(ctx context.Context, projectID string, filte
tmp := getCountDeliveriesPrevRowQuery(filter.Pageable.SortOrder())
tmp = fmt.Sprintf(tmp, filter.Pageable.SortOrder())

cq := baseCountEvents + filterQuery + tmp
cq := baseCountEvents + filterQuery + tmp + ");"
countQuery, qargs, err = sqlx.Named(cq, qarg)
if err != nil {
return nil, datastore.PaginationData{}, err
Expand All @@ -578,14 +581,14 @@ func (e *eventRepo) LoadEventsPaged(ctx context.Context, projectID string, filte
countQuery = e.db.GetReadDB().Rebind(countQuery)

// count the row number before the first row
rows, err := e.db.GetReadDB().QueryxContext(ctx, countQuery, qargs...)
rows, err = e.db.GetReadDB().QueryxContext(ctx, countQuery, qargs...)
if err != nil {
return nil, datastore.PaginationData{}, err
}
defer closeWithError(rows)

if rows.Next() {
err = rows.StructScan(&count)
err = rows.StructScan(&rowCount)
if err != nil {
return nil, datastore.PaginationData{}, err
}
Expand All @@ -601,7 +604,7 @@ func (e *eventRepo) LoadEventsPaged(ctx context.Context, projectID string, filte
events = events[:len(events)-1]
}

pagination := &datastore.PaginationData{PrevRowCount: count}
pagination := &datastore.PaginationData{PrevRowCount: rowCount}
pagination = pagination.Build(filter.Pageable, ids)

return events, *pagination, nil
Expand Down
27 changes: 13 additions & 14 deletions database/postgres/event_delivery.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ const (
%s
%s
AND ed.id <= :cursor
GROUP BY ed.id, ep.id, ev.id, d.id, s.id
ORDER BY ed.id %s
LIMIT :limit
)
Expand All @@ -97,7 +96,6 @@ const (
%s
%s
AND ed.id >= :cursor
GROUP BY ed.id, ep.id, ev.id, d.id, s.id
ORDER BY ed.id %s
LIMIT :limit
)
Expand Down Expand Up @@ -130,14 +128,15 @@ const (
AND ed.deleted_at IS NULL`

countPrevEventDeliveries = `
SELECT COUNT(DISTINCT(ed.id))
FROM convoy.event_deliveries ed
LEFT JOIN convoy.events ev ON ed.event_id = ev.id
WHERE ed.deleted_at IS NULL
%s
AND ed.id > :cursor
GROUP BY ed.id, ev.id
ORDER BY ed.id %s
select exists(
SELECT 1
FROM convoy.event_deliveries ed
LEFT JOIN convoy.events ev ON ed.event_id = ev.id
WHERE ed.deleted_at IS NULL
%s
AND ed.id > :cursor
ORDER BY ed.id %s
);
`

loadEventDeliveriesIntervals = `
Expand Down Expand Up @@ -759,7 +758,7 @@ func (e *eventDeliveryRepo) LoadEventDeliveriesPaged(ctx context.Context, projec
})
}

var count datastore.PrevRowCount
var rowCount datastore.PrevRowCount
if len(eventDeliveries) > 0 {
var countQuery string
var qargs []interface{}
Expand All @@ -783,14 +782,14 @@ func (e *eventDeliveryRepo) LoadEventDeliveriesPaged(ctx context.Context, projec
countQuery = e.db.GetReadDB().Rebind(countQuery)

// count the row number before the first row
rows, err := e.db.GetReadDB().QueryxContext(ctx, countQuery, qargs...)
rows, err = e.db.GetReadDB().QueryxContext(ctx, countQuery, qargs...)
if err != nil {
return nil, datastore.PaginationData{}, err
}
defer closeWithError(rows)

if rows.Next() {
err = rows.StructScan(&count)
err = rows.StructScan(&rowCount)
if err != nil {
return nil, datastore.PaginationData{}, err
}
Expand All @@ -806,7 +805,7 @@ func (e *eventDeliveryRepo) LoadEventDeliveriesPaged(ctx context.Context, projec
eventDeliveries = eventDeliveries[:len(eventDeliveries)-1]
}

pagination := &datastore.PaginationData{PrevRowCount: count}
pagination := &datastore.PaginationData{PrevRowCount: rowCount}
pagination = pagination.Build(pageable, ids)

return eventDeliveries, *pagination, nil
Expand Down
3 changes: 2 additions & 1 deletion datastore/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ type PaginationData struct {
}

type PrevRowCount struct {
Count int
Count int
Exists bool
}

func (p *PaginationData) Build(pageable Pageable, items []string) *PaginationData {
Expand Down

0 comments on commit cd1338d

Please sign in to comment.