From fbd91eac53970213447b4fd48633f147ee54bbcb Mon Sep 17 00:00:00 2001 From: ogous Date: Wed, 22 Jan 2025 18:39:20 +0300 Subject: [PATCH] get total volume and winner from campaigns query --- cmd/graphql.ethereum/graph/generated.go | 61 +++++++++++++++++++ cmd/graphql.ethereum/graph/schema.graphqls | 5 ++ .../graph/schema.resolvers.go | 29 +++++---- lib/types/types.go | 9 +-- 4 files changed, 89 insertions(+), 15 deletions(-) diff --git a/cmd/graphql.ethereum/graph/generated.go b/cmd/graphql.ethereum/graph/generated.go index 2b1a8a6..88f49f6 100644 --- a/cmd/graphql.ethereum/graph/generated.go +++ b/cmd/graphql.ethereum/graph/generated.go @@ -65,6 +65,7 @@ type ComplexityRoot struct { Settlement func(childComplexity int) int Starting func(childComplexity int) int Telegram func(childComplexity int) int + TotalVolume func(childComplexity int) int Web func(childComplexity int) int Winner func(childComplexity int) int X func(childComplexity int) int @@ -253,6 +254,13 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.Campaign.Telegram(childComplexity), true + case "Campaign.totalVolume": + if e.complexity.Campaign.TotalVolume == nil { + break + } + + return e.complexity.Campaign.TotalVolume(childComplexity), true + case "Campaign.web": if e.complexity.Campaign.Web == nil { break @@ -1578,6 +1586,50 @@ func (ec *executionContext) fieldContext_Campaign_winner(_ context.Context, fiel return fc, nil } +func (ec *executionContext) _Campaign_totalVolume(ctx context.Context, field graphql.CollectedField, obj *types.Campaign) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Campaign_totalVolume(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.TotalVolume, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(int) + fc.Result = res + return ec.marshalNInt2int(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_Campaign_totalVolume(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Campaign", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return nil, errors.New("field of type Int does not have child fields") + }, + } + return fc, nil +} + func (ec *executionContext) _Changelog_id(ctx context.Context, field graphql.CollectedField, obj *changelog.Changelog) (ret graphql.Marshaler) { fc, err := ec.fieldContext_Changelog_id(ctx, field) if err != nil { @@ -2205,6 +2257,8 @@ func (ec *executionContext) fieldContext_Query_campaigns(ctx context.Context, fi return ec.fieldContext_Campaign_web(ctx, field) case "winner": return ec.fieldContext_Campaign_winner(ctx, field) + case "totalVolume": + return ec.fieldContext_Campaign_totalVolume(ctx, field) } return nil, fmt.Errorf("no field named %q was found under type Campaign", field.Name) }, @@ -2291,6 +2345,8 @@ func (ec *executionContext) fieldContext_Query_campaignById(ctx context.Context, return ec.fieldContext_Campaign_web(ctx, field) case "winner": return ec.fieldContext_Campaign_winner(ctx, field) + case "totalVolume": + return ec.fieldContext_Campaign_totalVolume(ctx, field) } return nil, fmt.Errorf("no field named %q was found under type Campaign", field.Name) }, @@ -5022,6 +5078,11 @@ func (ec *executionContext) _Campaign(ctx context.Context, sel ast.SelectionSet, } out.Concurrently(i, func(ctx context.Context) graphql.Marshaler { return innerFunc(ctx, out) }) + case "totalVolume": + out.Values[i] = ec._Campaign_totalVolume(ctx, field, obj) + if out.Values[i] == graphql.Null { + atomic.AddUint32(&out.Invalids, 1) + } default: panic("unknown field " + strconv.Quote(field.Name)) } diff --git a/cmd/graphql.ethereum/graph/schema.graphqls b/cmd/graphql.ethereum/graph/schema.graphqls index d582e7f..71a5653 100644 --- a/cmd/graphql.ethereum/graph/schema.graphqls +++ b/cmd/graphql.ethereum/graph/schema.graphqls @@ -304,6 +304,11 @@ type Campaign { If any outcome declared as winner, it returns bytes8 id """ winner: String + + """ + It returns total invested amount as usd + """ + totalVolume: Int! } type Outcome { diff --git a/cmd/graphql.ethereum/graph/schema.resolvers.go b/cmd/graphql.ethereum/graph/schema.resolvers.go index e585b82..a651049 100644 --- a/cmd/graphql.ethereum/graph/schema.resolvers.go +++ b/cmd/graphql.ethereum/graph/schema.resolvers.go @@ -412,17 +412,24 @@ func (r *queryResolver) Campaigns(ctx context.Context, category []string) ([]typ return campaigns, nil } err := r.DB.Raw( - `SELECT * -FROM ( - SELECT DISTINCT ON (campaign_id) - campaign_id AS id, - created_by AS created_at, - total_volume, - campaign_content AS content - FROM ninelives_buys_and_sells_1 - ORDER BY campaign_id, total_volume DESC -) sub -ORDER BY total_volume DESC;`, + `SELECT + nc.*, + COALESCE(nbas.total_volume, 0) AS total_volume +FROM + ninelives_campaigns_1 nc +LEFT JOIN ( + SELECT + campaign_id, + MAX(total_volume) AS total_volume + FROM + ninelives_buys_and_sells_1 + GROUP BY + campaign_id +) nbas +ON + nc.id = nbas.campaign_id +ORDER BY + total_volume DESC;`, ). Scan(&campaigns). Error diff --git a/lib/types/types.go b/lib/types/types.go index b18e214..e9ad1c7 100644 --- a/lib/types/types.go +++ b/lib/types/types.go @@ -9,10 +9,11 @@ import ( type ( Campaign struct { - ID string `gorm:"primaryKey"` - CreatedAt time.Time `gorm:"autoCreateTime"` - UpdatedAt time.Time `gorm:"autoUpdateTime"` - Content CampaignContent `json:"content"` + ID string `gorm:"primaryKey"` + CreatedAt time.Time `gorm:"autoCreateTime"` + UpdatedAt time.Time `gorm:"autoUpdateTime"` + Content CampaignContent `json:"content"` + TotalVolume int `json:"totalVolume"` } Outcome struct {