This repository has been archived by the owner on Jan 22, 2020. It is now read-only.
/
actions_effects.go
147 lines (124 loc) · 3.46 KB
/
actions_effects.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
140
141
142
143
144
145
146
147
package horizon
import (
"errors"
"regexp"
"github.com/stellar/horizon/db2"
"github.com/stellar/horizon/db2/history"
"github.com/stellar/horizon/render/hal"
"github.com/stellar/horizon/render/sse"
"github.com/stellar/horizon/resource"
)
// This file contains the actions:
//
// EffectIndexAction: pages of operations
// EffectIndexAction renders a page of effect resources, identified by
// a normal page query and optionally filtered by an account, ledger,
// transaction, or operation.
type EffectIndexAction struct {
Action
AccountFilter string
LedgerFilter int32
TransactionFilter string
OperationFilter int64
PagingParams db2.PageQuery
Records []history.Effect
Page hal.Page
}
// JSON is a method for actions.JSON
func (action *EffectIndexAction) JSON() {
action.Do(
action.EnsureHistoryFreshness,
action.loadParams,
action.ValidateCursorWithinHistory,
action.loadRecords,
action.loadPage,
)
action.Do(func() {
hal.Render(action.W, action.Page)
})
}
// SSE is a method for actions.SSE
func (action *EffectIndexAction) SSE(stream sse.Stream) {
action.Setup(
action.EnsureHistoryFreshness,
action.loadParams,
action.ValidateCursorWithinHistory,
)
action.Do(
action.loadRecords,
func() {
stream.SetLimit(int(action.PagingParams.Limit))
records := action.Records[stream.SentCount():]
for _, record := range records {
res, err := resource.NewEffect(action.Ctx, record)
if err != nil {
stream.Err(action.Err)
return
}
stream.Send(sse.Event{
ID: res.PagingToken(),
Data: res,
})
}
},
)
}
func (action *EffectIndexAction) loadParams() {
action.ValidateCursor()
action.PagingParams = action.GetPageQuery()
action.AccountFilter = action.GetString("account_id")
action.LedgerFilter = action.GetInt32("ledger_id")
action.TransactionFilter = action.GetString("tx_id")
action.OperationFilter = action.GetInt64("op_id")
}
// loadRecords populates action.Records
func (action *EffectIndexAction) loadRecords() {
effects := action.HistoryQ().Effects()
switch {
case action.AccountFilter != "":
effects.ForAccount(action.AccountFilter)
case action.LedgerFilter > 0:
effects.ForLedger(action.LedgerFilter)
case action.OperationFilter > 0:
effects.ForOperation(action.OperationFilter)
case action.TransactionFilter != "":
effects.ForTransaction(action.TransactionFilter)
}
action.Err = effects.Page(action.PagingParams).Select(&action.Records)
}
// loadPage populates action.Page
func (action *EffectIndexAction) loadPage() {
for _, record := range action.Records {
var res hal.Pageable
res, action.Err = resource.NewEffect(action.Ctx, record)
if action.Err != nil {
return
}
action.Page.Add(res)
}
action.Page.BaseURL = action.BaseURL()
action.Page.BasePath = action.Path()
action.Page.Limit = action.PagingParams.Limit
action.Page.Cursor = action.PagingParams.Cursor
action.Page.Order = action.PagingParams.Order
action.Page.PopulateLinks()
}
// ValidateCursor ensures that the provided cursor parameter is of the form
// OPERATIONID-INDEX (such as 1234-56) or is the special value "now" that
// represents the the cursor directly after the last closed ledger
func (action *EffectIndexAction) ValidateCursor() {
c := action.GetString("cursor")
if c == "" {
return
}
ok, err := regexp.MatchString("now|\\d+(-\\d+)?", c)
if err != nil {
action.Err = err
return
}
if !ok {
action.SetInvalidField("cursor", errors.New("invalid format"))
return
}
return
}