Skip to content

Commit

Permalink
feat(core): support for querying metadata sql (#686)
Browse files Browse the repository at this point in the history
  • Loading branch information
polebug authored Dec 30, 2024
1 parent e942821 commit 719fcbb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 30 deletions.
35 changes: 11 additions & 24 deletions internal/database/dialer/postgres/client_partitioned.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package postgres

import (
"context"
"encoding/json"
"errors"
"fmt"
"math"
Expand All @@ -12,7 +13,6 @@ import (
"github.com/rss3-network/node/internal/database/dialer/postgres/table"
"github.com/rss3-network/node/internal/database/model"
activityx "github.com/rss3-network/protocol-go/schema/activity"
"github.com/rss3-network/protocol-go/schema/metadata"
"github.com/rss3-network/protocol-go/schema/network"
"github.com/samber/lo"
"github.com/sourcegraph/conc/pool"
Expand Down Expand Up @@ -1043,32 +1043,19 @@ func (c *client) buildFindActivitiesStatement(ctx context.Context, partitionedNa
}

if query.Metadata != nil {
databaseStatement = c.buildFindActivitiesMetadataStatement(ctx, databaseStatement, lo.FromPtr(query.Metadata))
}

return databaseStatement.Order("timestamp DESC, index DESC").Limit(query.Limit)
}

// buildFindActivitiesMetadataStatement builds the query metadata statement.
func (c *client) buildFindActivitiesMetadataStatement(_ context.Context, databaseStatement *gorm.DB, meta metadata.Metadata) *gorm.DB {
statement := databaseStatement.Session(&gorm.Session{})

statement = statement.Joins("CROSS JOIN LATERAL jsonb_array_elements(actions::jsonb) AS action_element")

switch data := meta.(type) {
case *metadata.ExchangeSwap:
if data.From.Address != nil {
statement = statement.Where("action_element -> 'metadata' -> 'from' ->> 'address' = ?", data.From.Address)
}

if data.To.Address != nil {
statement = statement.Where("action_element -> 'metadata' -> 'to' ->> 'address' = ?", data.To.Address)
metadataSQL, err := json.Marshal([]map[string]interface{}{
{
"metadata": query.Metadata,
},
})
if err != nil {
zap.L().Error("failed to marshal metadata", zap.Error(err), zap.Any("metadata", query.Metadata))
} else {
databaseStatement = databaseStatement.Where("actions::jsonb @> ?", string(metadataSQL))
}
default:
return databaseStatement
}

return statement
return databaseStatement.Order("timestamp DESC, index DESC").Limit(query.Limit)
}

// buildActivitiesTableNames builds the activities table names.
Expand Down
17 changes: 11 additions & 6 deletions internal/node/component/decentralized/handler_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,24 @@ func (c *Component) BatchGetMetadataActivities(ctx echo.Context) (err error) {

request.Type = lo.ToPtr(typex)

for i := range request.Accounts {
if common.IsHexAddress(request.Accounts[i]) {
request.Accounts[i] = common.HexToAddress(request.Accounts[i]).String()
}
}

// Parse the metadata
if request.RawMetadata == nil || len(request.RawMetadata) == 0 || string(request.RawMetadata) == "{}" {
return response.BadRequestError(ctx, fmt.Errorf("empty metadata"))
}

meta, err := metadata.Unmarshal(typex, request.RawMetadata)
if err != nil {
return response.BadRequestError(ctx, fmt.Errorf("failed to unmarshal metadata: %w", err))
}

request.Metadata = lo.ToPtr(meta)

for i := range request.Accounts {
if common.IsHexAddress(request.Accounts[i]) {
request.Accounts[i] = common.HexToAddress(request.Accounts[i]).String()
}
}

if err = defaults.Set(&request); err != nil {
return response.BadRequestError(ctx, err)
}
Expand Down

0 comments on commit 719fcbb

Please sign in to comment.