Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): support for querying metadata sql #686

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading