From 747deb5e8f9c2d0218b0bcc4f780237018d89d8c Mon Sep 17 00:00:00 2001 From: LUCCA DUKIC <109136188+LuccaBitfly@users.noreply.github.com> Date: Mon, 6 Nov 2023 09:46:11 +0100 Subject: [PATCH 1/3] (BIDS-2629) optimize withdrawal queries --- db/db.go | 48 ++++++++++++++++++++++++++++++++++++++---------- go.mod | 4 ---- go.sum | 14 ++------------ handlers/api.go | 13 +++++++------ 4 files changed, 47 insertions(+), 32 deletions(-) diff --git a/db/db.go b/db/db.go index c63245ff10..804c82e164 100644 --- a/db/db.go +++ b/db/db.go @@ -2120,12 +2120,40 @@ func GetTotalAmountWithdrawn() (sum uint64, count uint64, err error) { Sum uint64 `db:"sum"` Count uint64 `db:"count"` }{} + lastExportedDay, err := GetLastExportedStatisticDay() + if err != nil { + return 0, 0, fmt.Errorf("error getting latest exported statistic day for withdrawals count: %w", err) + } + _, lastEpochOfDay := utils.GetFirstAndLastEpochForDay(lastExportedDay) + cutoffSlot := (lastEpochOfDay * utils.Config.Chain.ClConfig.SlotsPerEpoch) + 1 + err = ReaderDb.Get(&res, ` - SELECT - COALESCE(sum(w.amount), 0) as sum, - COALESCE(count(*), 0) as count - FROM blocks_withdrawals w - INNER JOIN blocks b ON b.blockroot = w.block_root AND b.status = '1'`) + WITH today AS ( + SELECT + COALESCE(SUM(w.amount), 0) as sum, + COUNT(*) as count + FROM blocks_withdrawals w + INNER JOIN blocks b ON b.blockroot = w.block_root AND b.status = '1' + WHERE w.block_slot >= $1 + ), + stats AS ( + SELECT + COALESCE(SUM(withdrawals_amount_total), 0) as sum, + COALESCE(SUM(withdrawals_total), 0) as count + FROM validator_stats + WHERE day = $2 + ) + SELECT + today.sum + stats.sum as sum, + today.count + stats.count as count + FROM today, stats;`, cutoffSlot, lastExportedDay) + if err != nil { + if err == sql.ErrNoRows { + return 0, 0, nil + } + return 0, 0, fmt.Errorf("error fetching total withdrawal count and amount: %w", err) + } + return res.Sum, res.Count, err } @@ -2391,18 +2419,18 @@ func GetTotalWithdrawalsCount(validators []uint64) (uint64, error) { err = ReaderDb.Get(&count, ` WITH today AS ( - SELECT COUNT(*) as count_today + SELECT COUNT(*) as count FROM blocks_withdrawals w INNER JOIN blocks b ON b.blockroot = w.block_root AND b.status = '1' WHERE w.validatorindex = ANY($1) AND w.block_slot >= $2 ), stats AS ( - SELECT COALESCE(SUM(withdrawals), 0) as total_count + SELECT COALESCE(SUM(withdrawals_total), 0) as count FROM validator_stats - WHERE validatorindex = ANY($1) + WHERE validatorindex = ANY($1) AND day = $3 ) - SELECT today.count_today + stats.total_count - FROM today, stats;`, validatorFilter, cutoffSlot) + SELECT today.count + stats.count + FROM today, stats;`, validatorFilter, cutoffSlot, lastExportedDay) if err != nil { if err == sql.ErrNoRows { return 0, nil diff --git a/go.mod b/go.mod index e54cc60c1d..8c9cb73e82 100644 --- a/go.mod +++ b/go.mod @@ -31,7 +31,6 @@ require ( github.com/gorilla/websocket v1.5.0 github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d github.com/jackc/pgx-shopspring-decimal v0.0.0-20220624020537-1d36b5a1853e - github.com/jackc/pgx/v4 v4.18.1 github.com/jackc/pgx/v5 v5.4.3 github.com/jmoiron/sqlx v1.2.0 github.com/juliangruber/go-intersect v1.1.0 @@ -153,9 +152,6 @@ require ( github.com/ipfs/go-verifcid v0.0.2 // indirect github.com/ipld/go-codec-dagpb v1.5.0 // indirect github.com/ipld/go-ipld-prime v0.19.0 // indirect - github.com/jackc/chunkreader/v2 v2.0.1 // indirect - github.com/jackc/pgconn v1.14.0 // indirect - github.com/jackc/pgproto3/v2 v2.3.2 // indirect github.com/jackc/puddle/v2 v2.2.1 // indirect github.com/jbenet/goprocess v0.1.4 // indirect github.com/kr/pretty v0.3.1 // indirect diff --git a/go.sum b/go.sum index 5a9fb4f215..929bcdac92 100644 --- a/go.sum +++ b/go.sum @@ -88,7 +88,6 @@ github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go github.com/Kubuxu/go-os-helper v0.0.1/go.mod h1:N8B+I7vPCT80IcP58r50u4+gEEcsZETFUpAzWW2ep1Y= github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc= github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE= -github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc= github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398/go.mod h1:a1uqRtAwp2Xwc6WNPJEufxJ7fx3npB4UV/JOLmbu5I0= @@ -257,7 +256,6 @@ github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWH github.com/cncf/xds/go v0.0.0-20220314180256-7f1daf1720fc/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/cncf/xds/go v0.0.0-20230105202645-06c439db220b h1:ACGZRIr7HsgBKHsueQ1yM4WaVaXh21ynwqsF8M8tXhA= github.com/cncf/xds/go v0.0.0-20230105202645-06c439db220b/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= -github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I= github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ= github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8= github.com/cockroachdb/datadriven v1.0.2/go.mod h1:a9RdTaap04u637JoCzcUoIcDmvwSUtcUFtT/C3kJlTU= @@ -498,7 +496,6 @@ github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5x github.com/gofrs/flock v0.8.0/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU= github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw= github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU= -github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw= github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/gogo/googleapis v0.0.0-20180223154316-0cd9801be74a/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFGgqEef3s= github.com/gogo/googleapis v1.1.0/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFGgqEef3s= @@ -932,6 +929,7 @@ github.com/iris-contrib/go.uuid v2.0.0+incompatible/go.mod h1:iz2lgM/1UnEf1kP0L/ github.com/iris-contrib/jade v1.1.3/go.mod h1:H/geBymxJhShH5kecoiOCSssPX7QWYH7UaeZTSWddIk= github.com/iris-contrib/pongo2 v0.0.1/go.mod h1:Ssh+00+3GAZqSQb30AvBRNxBx7rf0GqwkjqxNd0u65g= github.com/iris-contrib/schema v0.0.1/go.mod h1:urYA3uvUNG1TIIjOSCzHr9/LmbQo8LrOcOqfqxa4hXw= +github.com/jackc/chunkreader v1.0.0 h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0= github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo= github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk= github.com/jackc/chunkreader/v2 v2.0.1 h1:i+RDz65UE+mmpjTfyz0MoVTnzeYxroil2G82ki7MGG8= @@ -943,15 +941,14 @@ github.com/jackc/pgconn v1.8.0/go.mod h1:1C2Pb36bGIP9QHGBYCjnyhqu7Rv3sGshaQUvmfG github.com/jackc/pgconn v1.9.0/go.mod h1:YctiPyvzfU11JFxoXokUOOKQXQmDMoJL9vJzHH8/2JY= github.com/jackc/pgconn v1.9.1-0.20210724152538-d89c8390a530/go.mod h1:4z2w8XhRbP1hYxkpTuBjTS3ne3J48K83+u0zoyvg2pI= github.com/jackc/pgconn v1.14.0 h1:vrbA9Ud87g6JdFWkHTJXppVce58qPIdP7N8y0Ml/A7Q= -github.com/jackc/pgconn v1.14.0/go.mod h1:9mBNlny0UvkgJdCDvdVHYSjI+8tD2rnKK69Wz8ti++E= github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE= github.com/jackc/pgio v1.0.0/go.mod h1:oP+2QK2wFfUWgr+gxjoBH9KGBb31Eio69xUb0w5bYf8= github.com/jackc/pgmock v0.0.0-20190831213851-13a1b77aafa2/go.mod h1:fGZlG77KXmcq05nJLRkk0+p82V8B8Dw8KN2/V9c/OAE= github.com/jackc/pgmock v0.0.0-20201204152224-4fe30f7445fd/go.mod h1:hrBW0Enj2AZTNpt/7Y5rr2xe/9Mn757Wtb2xeBzPv2c= -github.com/jackc/pgmock v0.0.0-20210724152146-4ad1a8207f65 h1:DadwsjnMwFjfWc9y5Wi/+Zz7xoE5ALHsRQlOctkOiHc= github.com/jackc/pgmock v0.0.0-20210724152146-4ad1a8207f65/go.mod h1:5R2h2EEX+qri8jOWMbJCtaPWkrrNc7OHwsp2TCqp7ak= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgproto3 v1.1.0 h1:FYYE4yRw+AgI8wXIinMlNjBbp/UitDJwfj5LqqewP1A= github.com/jackc/pgproto3 v1.1.0/go.mod h1:eR5FA3leWg7p9aeAqi37XOTgTIbkABlvcPB3E5rlc78= github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190420180111-c116219b62db/go.mod h1:bhq50y+xrl9n5mRYyCBFKkpRVTLYJVWeCc+mEAI3yXA= github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190609003834-432c2951c711/go.mod h1:uH0AWtUmuShn0bcesswc4aBTWGvw0cAxIJp+6OB//Wg= @@ -960,7 +957,6 @@ github.com/jackc/pgproto3/v2 v2.0.0-rc3.0.20190831210041-4c03ce451f29/go.mod h1: github.com/jackc/pgproto3/v2 v2.0.6/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= github.com/jackc/pgproto3/v2 v2.1.1/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= github.com/jackc/pgproto3/v2 v2.3.2 h1:7eY55bdBeCz1F2fTzSz69QC+pG46jYq9/jtSPiJ5nn0= -github.com/jackc/pgproto3/v2 v2.3.2/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b/go.mod h1:vsD4gTJCa9TptPL8sPkXrLZ+hDuNrZCnj29CQpr4X1E= github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= @@ -977,13 +973,11 @@ github.com/jackc/pgx/v4 v4.0.0-20190421002000-1b8f0016e912/go.mod h1:no/Y67Jkk/9 github.com/jackc/pgx/v4 v4.0.0-pre1.0.20190824185557-6972a5742186/go.mod h1:X+GQnOEnf1dqHGpw7JmHqHc1NxDoalibchSk9/RWuDc= github.com/jackc/pgx/v4 v4.12.1-0.20210724153913-640aa07df17c/go.mod h1:1QD0+tgSXP7iUjYm9C1NxKhny7lq6ee99u/z+IHFcgs= github.com/jackc/pgx/v4 v4.18.1 h1:YP7G1KABtKpB5IHrO9vYwSrCOhs7p3uqhvhhQBptya0= -github.com/jackc/pgx/v4 v4.18.1/go.mod h1:FydWkUyadDmdNH/mHnGob881GawxeEm7TcMCzkb+qQE= github.com/jackc/pgx/v5 v5.4.3 h1:cxFyXhxlvAifxnkKKdlxv8XqUf59tDlYjnV5YYfsJJY= github.com/jackc/pgx/v5 v5.4.3/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA= github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= -github.com/jackc/puddle v1.3.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= github.com/jackpal/gateway v1.0.5/go.mod h1:lTpwd4ACLXmpyiCTRtfiNyVnUmqT9RivzCDQetPfnjA= @@ -2098,7 +2092,6 @@ golang.org/x/crypto v0.0.0-20211215165025-cf75a172585e/go.mod h1:P+XmwS30IXTQdn5 golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.0.0-20220919173607-35f4265a4bc0/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58= golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= @@ -2224,7 +2217,6 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug golang.org/x/net v0.0.0-20220812174116-3211cb980234/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= golang.org/x/net v0.0.0-20220920183852-bf014ff85ad5/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= golang.org/x/net v0.0.0-20220921203646-d300de134e69/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= -golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8= golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= @@ -2391,7 +2383,6 @@ golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -2402,7 +2393,6 @@ golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= diff --git a/handlers/api.go b/handlers/api.go index dc4f9fc910..86cc9961ac 100644 --- a/handlers/api.go +++ b/handlers/api.go @@ -1523,7 +1523,7 @@ func getEpoch(epoch int64) ([]interface{}, error) { // @Failure 400 {object} types.ApiResponse // @Router /api/v1/validator/{indexOrPubkey} [get] func ApiValidatorGet(w http.ResponseWriter, r *http.Request) { - apiValidator(w, r) + getApiValidator(w, r) } // ApiValidator godoc @@ -1536,11 +1536,11 @@ func ApiValidatorGet(w http.ResponseWriter, r *http.Request) { // @Failure 400 {object} types.ApiResponse // @Router /api/v1/validator [post] func ApiValidatorPost(w http.ResponseWriter, r *http.Request) { - apiValidator(w, r) + getApiValidator(w, r) } // This endpoint supports both GET and POST but requires different swagger descriptions based on the type -func apiValidator(w http.ResponseWriter, r *http.Request) { +func getApiValidator(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") vars := mux.Vars(r) @@ -1585,7 +1585,7 @@ func apiValidator(w http.ResponseWriter, r *http.Request) { WITH today AS ( SELECT w.validatorindex, - COALESCE(SUM(w.amount), 0) as amount_today + COALESCE(SUM(w.amount), 0) as amount FROM blocks_withdrawals w INNER JOIN blocks b ON b.blockroot = w.block_root AND b.status = '1' WHERE w.validatorindex = ANY($1) AND w.block_slot >= $2 @@ -1594,14 +1594,15 @@ func apiValidator(w http.ResponseWriter, r *http.Request) { stats AS ( SELECT vs.validatorindex, - COALESCE(vs.withdrawals_amount_total, 0) as total_amount + COALESCE(vs.withdrawals_amount, 0) as amount FROM validator_stats vs WHERE vs.validatorindex = ANY($1) AND vs.day = $3 + GROUP BY vs.validatorindex ), withdrawals_summary AS ( SELECT COALESCE(t.validatorindex, s.validatorindex) as validatorindex, - COALESCE(t.amount_today, 0) + COALESCE(s.total_amount, 0) as total + COALESCE(t.amount, 0) + COALESCE(s.amount, 0) as total FROM today t FULL JOIN stats s ON t.validatorindex = s.validatorindex ) From 77023951f0d1f2bd09ffd2b63e9baa40d52755e6 Mon Sep 17 00:00:00 2001 From: LUCCA DUKIC <109136188+LuccaBitfly@users.noreply.github.com> Date: Mon, 6 Nov 2023 10:25:49 +0100 Subject: [PATCH 2/3] (BIDS-2629) minor query fix --- handlers/api.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/handlers/api.go b/handlers/api.go index 86cc9961ac..e297405ef3 100644 --- a/handlers/api.go +++ b/handlers/api.go @@ -1594,10 +1594,9 @@ func getApiValidator(w http.ResponseWriter, r *http.Request) { stats AS ( SELECT vs.validatorindex, - COALESCE(vs.withdrawals_amount, 0) as amount + COALESCE(vs.withdrawals_amount_total, 0) as amount FROM validator_stats vs WHERE vs.validatorindex = ANY($1) AND vs.day = $3 - GROUP BY vs.validatorindex ), withdrawals_summary AS ( SELECT From 234343ca40906bafb025109696299679ab306d10 Mon Sep 17 00:00:00 2001 From: LUCCA DUKIC <109136188+LuccaBitfly@users.noreply.github.com> Date: Tue, 7 Nov 2023 09:12:33 +0100 Subject: [PATCH 3/3] (BIDS-2629) remove obsolete semicolons --- db/db.go | 4 ++-- handlers/api.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/db/db.go b/db/db.go index 804c82e164..33928490ce 100644 --- a/db/db.go +++ b/db/db.go @@ -2146,7 +2146,7 @@ func GetTotalAmountWithdrawn() (sum uint64, count uint64, err error) { SELECT today.sum + stats.sum as sum, today.count + stats.count as count - FROM today, stats;`, cutoffSlot, lastExportedDay) + FROM today, stats`, cutoffSlot, lastExportedDay) if err != nil { if err == sql.ErrNoRows { return 0, 0, nil @@ -2430,7 +2430,7 @@ func GetTotalWithdrawalsCount(validators []uint64) (uint64, error) { WHERE validatorindex = ANY($1) AND day = $3 ) SELECT today.count + stats.count - FROM today, stats;`, validatorFilter, cutoffSlot, lastExportedDay) + FROM today, stats`, validatorFilter, cutoffSlot, lastExportedDay) if err != nil { if err == sql.ErrNoRows { return 0, nil diff --git a/handlers/api.go b/handlers/api.go index e297405ef3..1b51248f1b 100644 --- a/handlers/api.go +++ b/handlers/api.go @@ -1619,7 +1619,7 @@ func getApiValidator(w http.ResponseWriter, r *http.Request) { LEFT JOIN validator_names n ON n.publickey = v.pubkey LEFT JOIN withdrawals_summary ws ON ws.validatorindex = v.validatorindex WHERE v.validatorindex = ANY($1) - ORDER BY v.validatorindex; + ORDER BY v.validatorindex `, pq.Array(queryIndices), cutoffSlot, lastExportedDay) if err != nil { logger.Warnf("error retrieving validator data from db: %v", err)