-
Notifications
You must be signed in to change notification settings - Fork 64
/
resolver_proplog.go
116 lines (97 loc) · 2.71 KB
/
resolver_proplog.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
package gql
import (
"context"
"time"
"github.com/filecoin-project/boost/db"
gqltypes "github.com/filecoin-project/boost/gql/types"
"github.com/graph-gophers/graphql-go"
)
type proposalLogResolver struct {
db.ProposalLog
}
func (pl *proposalLogResolver) DealUUID() graphql.ID {
return graphql.ID(pl.ProposalLog.DealUUID.String())
}
func (pl *proposalLogResolver) CreatedAt() graphql.Time {
return graphql.Time{Time: pl.ProposalLog.CreatedAt}
}
func (pl *proposalLogResolver) ClientAddress() string {
return pl.ProposalLog.ClientAddress.String()
}
func (pl *proposalLogResolver) PieceSize() gqltypes.Uint64 {
return gqltypes.Uint64(pl.ProposalLog.PieceSize)
}
type proposalLogListResolver struct {
TotalCount int32
Logs []*proposalLogResolver
More bool
}
type proposalLogsArgs struct {
Accepted graphql.NullBool
Cursor *gqltypes.BigInt // CreatedAt in milli-seconds
Offset graphql.NullInt
Limit graphql.NullInt
}
// query: proposalLogs(accepted, cursor, offset, limit) ProposalLogList
func (r *resolver) ProposalLogs(ctx context.Context, args proposalLogsArgs) (*proposalLogListResolver, error) {
offset := 0
if args.Offset.Set && args.Offset.Value != nil && *args.Offset.Value > 0 {
offset = int(*args.Offset.Value)
}
limit := 10
if args.Limit.Set && args.Limit.Value != nil && *args.Limit.Value > 0 {
limit = int(*args.Limit.Value)
}
// Fetch one extra deal so that we can check if there are more deals
// beyond the limit
var cursor *time.Time
if args.Cursor != nil {
val := (*args.Cursor).Int64()
asTime := time.Unix(val/1000, (val%1000)*1e6)
cursor = &asTime
}
logs, err := r.plDB.List(ctx, args.Accepted.Value, cursor, offset, limit+1)
if err != nil {
return nil, err
}
more := len(logs) > limit
if more {
// Truncate deal list to limit
logs = logs[:limit]
}
// Get the total log count
count, err := r.plDB.Count(ctx, args.Accepted.Value)
if err != nil {
return nil, err
}
resolvers := make([]*proposalLogResolver, 0, len(logs))
for _, l := range logs {
resolvers = append(resolvers, &proposalLogResolver{ProposalLog: l})
}
return &proposalLogListResolver{
TotalCount: int32(count),
Logs: resolvers,
More: more,
}, nil
}
type propLogCount struct {
Accepted int32
Rejected int32
Period gqltypes.Uint64
}
func (r *resolver) ProposalLogsCount(ctx context.Context) (*propLogCount, error) {
total, err := r.plDB.Count(ctx, nil)
if err != nil {
return nil, err
}
accepted := true
acceptCount, err := r.plDB.Count(ctx, &accepted)
if err != nil {
return nil, err
}
return &propLogCount{
Accepted: int32(acceptCount),
Rejected: int32(total - acceptCount),
Period: gqltypes.Uint64(r.cfg.Dealmaking.DealProposalLogDuration),
}, nil
}