-
Notifications
You must be signed in to change notification settings - Fork 19
/
positions.go
139 lines (118 loc) · 4.33 KB
/
positions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE.DATANODE file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
package sqlstore
import (
"context"
"errors"
"fmt"
"code.vegaprotocol.io/vega/datanode/entities"
"code.vegaprotocol.io/vega/datanode/metrics"
v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
"github.com/georgysavva/scany/pgxscan"
)
var (
ErrPositionNotFound = errors.New("party not found")
positionsOrdering = TableOrdering{
ColumnOrdering{Name: "vega_time", Sorting: ASC},
ColumnOrdering{Name: "party_id", Sorting: ASC},
ColumnOrdering{Name: "market_id", Sorting: ASC},
}
)
type Positions struct {
*ConnectionSource
batcher MapBatcher[entities.PositionKey, entities.Position]
}
func NewPositions(connectionSource *ConnectionSource) *Positions {
a := &Positions{
ConnectionSource: connectionSource,
batcher: NewMapBatcher[entities.PositionKey, entities.Position](
"positions",
entities.PositionColumns),
}
return a
}
func (ps *Positions) Flush(ctx context.Context) ([]entities.Position, error) {
defer metrics.StartSQLQuery("Positions", "Flush")()
return ps.batcher.Flush(ctx, ps.Connection)
}
func (ps *Positions) Add(ctx context.Context, p entities.Position) error {
ps.batcher.Add(p)
return nil
}
func (ps *Positions) GetByMarketAndParty(ctx context.Context,
marketIDRaw string,
partyIDRaw string,
) (entities.Position, error) {
var (
position = entities.Position{}
marketID = entities.MarketID(marketIDRaw)
partyID = entities.PartyID(partyIDRaw)
)
defer metrics.StartSQLQuery("Positions", "GetByMarketAndParty")()
err := pgxscan.Get(ctx, ps.Connection, &position,
`SELECT * FROM positions_current WHERE market_id=$1 AND party_id=$2`,
marketID, partyID)
if pgxscan.NotFound(err) {
return position, fmt.Errorf("'%v/%v': %w", marketID, partyID, ErrPositionNotFound)
}
return position, err
}
func (ps *Positions) GetByMarket(ctx context.Context, marketID string) ([]entities.Position, error) {
defer metrics.StartSQLQuery("Positions", "GetByMarket")()
positions := []entities.Position{}
err := pgxscan.Select(ctx, ps.Connection, &positions,
`SELECT * FROM positions_current WHERE market_id=$1`,
entities.MarketID(marketID))
return positions, err
}
func (ps *Positions) GetByParty(ctx context.Context, partyID string) ([]entities.Position, error) {
defer metrics.StartSQLQuery("Positions", "GetByParty")()
positions := []entities.Position{}
err := pgxscan.Select(ctx, ps.Connection, &positions,
`SELECT * FROM positions_current WHERE party_id=$1`,
entities.PartyID(partyID))
return positions, err
}
func (ps *Positions) GetByPartyConnection(ctx context.Context, partyIDRaw string, marketIDRaw string, pagination entities.CursorPagination) ([]entities.Position, entities.PageInfo, error) {
var (
args []interface{}
pageInfo entities.PageInfo
query = `select * from positions_current`
where string
partyID = entities.PartyID(partyIDRaw)
marketID = entities.MarketID(marketIDRaw)
err error
)
if marketID == "" {
where = fmt.Sprintf(" where party_id=%s", nextBindVar(&args, partyID))
} else {
where = fmt.Sprintf(" where party_id=%s and market_id=%s", nextBindVar(&args, partyID), nextBindVar(&args, marketID))
}
query = fmt.Sprintf("%s %s", query, where)
query, args, err = PaginateQuery[entities.PositionCursor](query, args, positionsOrdering, pagination)
if err != nil {
return nil, pageInfo, err
}
var positions []entities.Position
if err = pgxscan.Select(ctx, ps.Connection, &positions, query, args...); err != nil {
return nil, pageInfo, err
}
positions, pageInfo = entities.PageEntities[*v2.PositionEdge](positions, pagination)
return positions, pageInfo, nil
}
func (ps *Positions) GetAll(ctx context.Context) ([]entities.Position, error) {
defer metrics.StartSQLQuery("Positions", "GetAll")()
positions := []entities.Position{}
err := pgxscan.Select(ctx, ps.Connection, &positions,
`SELECT * FROM positions_current`)
return positions, err
}