code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/party_activity_streak.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package sqlstore 17 18 import ( 19 "context" 20 "fmt" 21 22 "code.vegaprotocol.io/vega/datanode/entities" 23 "code.vegaprotocol.io/vega/datanode/metrics" 24 25 "github.com/georgysavva/scany/pgxscan" 26 ) 27 28 type PartyActivityStreaks struct { 29 *ConnectionSource 30 } 31 32 func NewPartyActivityStreaks(connectionSource *ConnectionSource) *PartyActivityStreaks { 33 return &PartyActivityStreaks{ 34 ConnectionSource: connectionSource, 35 } 36 } 37 38 func (pas *PartyActivityStreaks) Add( 39 ctx context.Context, 40 activityStreak *entities.PartyActivityStreak, 41 ) error { 42 defer metrics.StartSQLQuery("PartyActivityStreaks", "Add")() 43 44 _, err := pas.Exec( 45 ctx, partyActivityStreakAddQuery, activityStreak.Fields()...) 46 47 return err 48 } 49 50 func (pas *PartyActivityStreaks) Get( 51 ctx context.Context, 52 party entities.PartyID, 53 epoch *uint64, 54 ) (*entities.PartyActivityStreak, error) { 55 defer metrics.StartSQLQuery("PartyActivityStreaks", "Get")() 56 57 var ( 58 query string 59 args []interface{} 60 activityStreak []*entities.PartyActivityStreak 61 ) 62 if epoch != nil { 63 query = fmt.Sprintf( 64 "SELECT * FROM party_activity_streaks where party_id = %s AND epoch = %s", 65 nextBindVar(&args, party), nextBindVar(&args, *epoch), 66 ) 67 } else { 68 query = fmt.Sprintf( 69 "SELECT * FROM party_activity_streaks where party_id = %s ORDER BY epoch DESC LIMIT 1", 70 nextBindVar(&args, party), 71 ) 72 } 73 74 err := pgxscan.Select(ctx, pas.ConnectionSource, &activityStreak, query, args...) 75 if err != nil { 76 return nil, err 77 } 78 79 if len(activityStreak) <= 0 { 80 return nil, entities.ErrNotFound 81 } 82 83 return activityStreak[0], nil 84 } 85 86 const ( 87 partyActivityStreakAddQuery = `INSERT INTO party_activity_streaks (party_id, active_for, inactive_for, is_active, reward_distribution_activity_multiplier, reward_vesting_activity_multiplier, epoch, traded_volume, open_volume, vega_time, tx_hash) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) 88 ` 89 )