Skip to content

Commit

Permalink
Merge pull request #998 from gobitfly/BEDS-628/refactor-vdb-blocks-query
Browse files Browse the repository at this point in the history
(BEDS 628) refactor vdb blocks query
  • Loading branch information
remoterami authored Oct 28, 2024
2 parents b39b06c + fcdea36 commit 4ab74db
Show file tree
Hide file tree
Showing 7 changed files with 324 additions and 304 deletions.
18 changes: 12 additions & 6 deletions backend/pkg/api/data_access/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ func applySortAndPagination(defaultColumns []types.SortColumn, primary types.Sor
if cursor.IsReverse() {
column.Desc = !column.Desc
}
colOrder := goqu.C(column.Column).Asc()
colOrder := column.Column.Asc().NullsFirst()
if column.Desc {
colOrder = goqu.C(column.Column).Desc()
colOrder = column.Column.Desc().NullsLast()
}
queryOrder = append(queryOrder, colOrder)
}
Expand All @@ -87,15 +87,21 @@ func applySortAndPagination(defaultColumns []types.SortColumn, primary types.Sor
// reverse order to nest conditions
for i := len(queryOrderColumns) - 1; i >= 0; i-- {
column := queryOrderColumns[i]
colWhere := goqu.C(column.Column).Gt(column.Offset)
if column.Desc {
colWhere = goqu.C(column.Column).Lt(column.Offset)
var colWhere exp.Expression

// current convention is opposite of the psql default (ASC: nulls first, DESC: nulls last)
colWhere = goqu.Or(column.Column.Lt(column.Offset), column.Column.IsNull())
if !column.Desc {
colWhere = column.Column.Gt(column.Offset)
if column.Offset == nil {
colWhere = goqu.Or(colWhere, column.Column.IsNull())
}
}

if queryWhere == nil {
queryWhere = colWhere
} else {
queryWhere = goqu.And(goqu.C(column.Column).Eq(column.Offset), queryWhere)
queryWhere = goqu.And(column.Column.Eq(column.Offset), queryWhere)
queryWhere = goqu.Or(colWhere, queryWhere)
}
}
Expand Down
38 changes: 19 additions & 19 deletions backend/pkg/api/data_access/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,14 +344,14 @@ func (d *DataAccessService) GetDashboardNotifications(ctx context.Context, userI

// sorting
defaultColumns := []t.SortColumn{
{Column: enums.NotificationsDashboardsColumns.Timestamp.ToString(), Desc: true, Offset: currentCursor.Epoch},
{Column: enums.NotificationsDashboardsColumns.DashboardName.ToString(), Desc: false, Offset: currentCursor.DashboardName},
{Column: enums.NotificationsDashboardsColumns.DashboardId.ToString(), Desc: false, Offset: currentCursor.DashboardId},
{Column: enums.NotificationsDashboardsColumns.GroupName.ToString(), Desc: false, Offset: currentCursor.GroupName},
{Column: enums.NotificationsDashboardsColumns.GroupId.ToString(), Desc: false, Offset: currentCursor.GroupId},
{Column: enums.NotificationsDashboardsColumns.ChainId.ToString(), Desc: true, Offset: currentCursor.ChainId},
}
order, directions := applySortAndPagination(defaultColumns, t.SortColumn{Column: colSort.Column.ToString(), Desc: colSort.Desc}, currentCursor.GenericCursor)
{Column: enums.NotificationsDashboardsColumns.Timestamp.ToExpr(), Desc: true, Offset: currentCursor.Epoch},
{Column: enums.NotificationsDashboardsColumns.DashboardName.ToExpr(), Desc: false, Offset: currentCursor.DashboardName},
{Column: enums.NotificationsDashboardsColumns.DashboardId.ToExpr(), Desc: false, Offset: currentCursor.DashboardId},
{Column: enums.NotificationsDashboardsColumns.GroupName.ToExpr(), Desc: false, Offset: currentCursor.GroupName},
{Column: enums.NotificationsDashboardsColumns.GroupId.ToExpr(), Desc: false, Offset: currentCursor.GroupId},
{Column: enums.NotificationsDashboardsColumns.ChainId.ToExpr(), Desc: true, Offset: currentCursor.ChainId},
}
order, directions := applySortAndPagination(defaultColumns, t.SortColumn{Column: colSort.Column.ToExpr(), Desc: colSort.Desc}, currentCursor.GenericCursor)
unionQuery = unionQuery.Order(order...)
if directions != nil {
unionQuery = unionQuery.Where(directions)
Expand Down Expand Up @@ -726,9 +726,9 @@ func (d *DataAccessService) GetMachineNotifications(ctx context.Context, userId

// Sorting and limiting if cursor is present
defaultColumns := []t.SortColumn{
{Column: enums.NotificationsMachinesColumns.Timestamp.ToString(), Desc: true, Offset: currentCursor.Epoch},
{Column: enums.NotificationsMachinesColumns.MachineId.ToString(), Desc: false, Offset: currentCursor.MachineId},
{Column: enums.NotificationsMachinesColumns.EventType.ToString(), Desc: false, Offset: currentCursor.EventType},
{Column: enums.NotificationsMachinesColumns.Timestamp.ToExpr(), Desc: true, Offset: currentCursor.Epoch},
{Column: enums.NotificationsMachinesColumns.MachineId.ToExpr(), Desc: false, Offset: currentCursor.MachineId},
{Column: enums.NotificationsMachinesColumns.EventType.ToExpr(), Desc: false, Offset: currentCursor.EventType},
}
var offset interface{}
switch colSort.Column {
Expand All @@ -738,7 +738,7 @@ func (d *DataAccessService) GetMachineNotifications(ctx context.Context, userId
offset = currentCursor.EventThreshold
}

order, directions := applySortAndPagination(defaultColumns, t.SortColumn{Column: colSort.Column.ToString(), Desc: colSort.Desc, Offset: offset}, currentCursor.GenericCursor)
order, directions := applySortAndPagination(defaultColumns, t.SortColumn{Column: colSort.Column.ToExpr(), Desc: colSort.Desc, Offset: offset}, currentCursor.GenericCursor)
ds = ds.Order(order...)
if directions != nil {
ds = ds.Where(directions)
Expand Down Expand Up @@ -847,10 +847,10 @@ func (d *DataAccessService) GetClientNotifications(ctx context.Context, userId u
// Sorting and limiting if cursor is present
// Rows can be uniquely identified by (epoch, client)
defaultColumns := []t.SortColumn{
{Column: enums.NotificationsClientsColumns.Timestamp.ToString(), Desc: true, Offset: currentCursor.Epoch},
{Column: enums.NotificationsClientsColumns.ClientName.ToString(), Desc: false, Offset: currentCursor.Client},
{Column: enums.NotificationsClientsColumns.Timestamp.ToExpr(), Desc: true, Offset: currentCursor.Epoch},
{Column: enums.NotificationsClientsColumns.ClientName.ToExpr(), Desc: false, Offset: currentCursor.Client},
}
order, directions := applySortAndPagination(defaultColumns, t.SortColumn{Column: colSort.Column.ToString(), Desc: colSort.Desc}, currentCursor.GenericCursor)
order, directions := applySortAndPagination(defaultColumns, t.SortColumn{Column: colSort.Column.ToExpr(), Desc: colSort.Desc}, currentCursor.GenericCursor)
ds = ds.Order(order...)
if directions != nil {
ds = ds.Where(directions)
Expand Down Expand Up @@ -1138,11 +1138,11 @@ func (d *DataAccessService) GetNetworkNotifications(ctx context.Context, userId
// Sorting and limiting if cursor is present
// Rows can be uniquely identified by (epoch, network, event_type)
defaultColumns := []t.SortColumn{
{Column: enums.NotificationNetworksColumns.Timestamp.ToString(), Desc: true, Offset: currentCursor.Epoch},
{Column: enums.NotificationNetworksColumns.Network.ToString(), Desc: false, Offset: currentCursor.Network},
{Column: enums.NotificationNetworksColumns.EventType.ToString(), Desc: false, Offset: currentCursor.EventType},
{Column: enums.NotificationNetworksColumns.Timestamp.ToExpr(), Desc: true, Offset: currentCursor.Epoch},
{Column: enums.NotificationNetworksColumns.Network.ToExpr(), Desc: false, Offset: currentCursor.Network},
{Column: enums.NotificationNetworksColumns.EventType.ToExpr(), Desc: false, Offset: currentCursor.EventType},
}
order, directions := applySortAndPagination(defaultColumns, t.SortColumn{Column: colSort.Column.ToString(), Desc: colSort.Desc}, currentCursor.GenericCursor)
order, directions := applySortAndPagination(defaultColumns, t.SortColumn{Column: colSort.Column.ToExpr(), Desc: colSort.Desc}, currentCursor.GenericCursor)
ds = ds.Order(order...)
if directions != nil {
ds = ds.Where(directions)
Expand Down
Loading

0 comments on commit 4ab74db

Please sign in to comment.