-
Notifications
You must be signed in to change notification settings - Fork 0
/
vote.go
181 lines (150 loc) · 3.65 KB
/
vote.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package db
import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/goverland-labs/goverland-platform-events/events/aggregator"
"github.com/rs/zerolog/log"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type Vote struct {
ID string `gorm:"primarykey"`
Ipfs string
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
Voter string
SpaceID string
ProposalID string
Choice json.RawMessage `gorm:"serializer:json"`
Reason string
App string
Vp float64
VpByStrategy []float64 `gorm:"serializer:json"`
VpState string
Published bool
}
type VoteRepo struct {
conn *gorm.DB
}
func NewVoteRepo(conn *gorm.DB) *VoteRepo {
return &VoteRepo{
conn: conn,
}
}
func (r *VoteRepo) Upsert(v *Vote) (isNew bool, err error) {
result := r.conn.
Where(Vote{ID: v.ID}).
FirstOrCreate(&v)
if result.Error != nil {
return false, result.Error
}
return result.RowsAffected > 0, nil
}
// BatchCreate creates votes in batch
func (r *VoteRepo) BatchCreate(data []Vote) error {
return r.conn.Model(&Vote{}).Clauses(clause.OnConflict{
DoNothing: true,
}).CreateInBatches(data, 500).Error
}
func (r *VoteRepo) GetLatestVote(id string) (*Vote, error) {
var (
dummy = Vote{}
_ = dummy.CreatedAt
_ = dummy.ProposalID
)
var v Vote
err := r.conn.
Where("proposal_id = ?", id).
Order("created_at DESC").
First(&v).
Error
return &v, err
}
func (r *VoteRepo) SelectForPublish(limit int) ([]Vote, error) {
var list []Vote
err := r.conn.
Where("published is false").
Limit(limit).
Find(&list).
Error
return list, err
}
func (r *VoteRepo) MarkAsPublished(votes []Vote) error {
ids := make([]string, len(votes))
for i := range votes {
ids[i] = votes[i].ID
}
return r.conn.
Model(Vote{}).
Where("id in ?", ids).
Update("published", true).
Error
}
type VoteService struct {
repo *VoteRepo
publisher Publisher
}
func NewVoteService(repo *VoteRepo, publisher Publisher) *VoteService {
return &VoteService{
repo: repo,
publisher: publisher,
}
}
func (s *VoteService) Upsert(vote *Vote) error {
_, err := s.repo.Upsert(vote)
return err
}
func (s *VoteService) BatchCreate(votes []Vote) error {
return s.repo.BatchCreate(votes)
}
func (s *VoteService) GetLatestVote(id string) (*Vote, error) {
p, err := s.repo.GetLatestVote(id)
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
return p, nil
}
func (s *VoteService) Publish(votes []Vote, batchSize int) error {
if len(votes) == 0 {
return nil
}
pl := make(aggregator.VotesPayload, len(votes))
for i := range votes {
pl[i] = aggregator.VotePayload{
ID: votes[i].ID,
Ipfs: votes[i].Ipfs,
Voter: votes[i].Voter,
Created: int(votes[i].CreatedAt.Unix()),
OriginalDaoID: votes[i].SpaceID,
ProposalID: votes[i].ProposalID,
Choice: votes[i].Choice,
Reason: votes[i].Reason,
App: votes[i].App,
Vp: votes[i].Vp,
VpByStrategy: votes[i].VpByStrategy,
VpState: votes[i].VpState,
}
}
now := time.Now()
for _, chunk := range chunkBy(pl, batchSize) {
err := s.publisher.PublishJSON(context.Background(), aggregator.SubjectVoteCreated, chunk)
if err != nil {
return fmt.Errorf("publish: %w", err)
}
}
log.Info().Msgf("votes are published in %f seconds", time.Since(now).Seconds())
return nil
}
func chunkBy[T any](items []T, chunkSize int) (chunks [][]T) {
for chunkSize < len(items) {
items, chunks = items[chunkSize:], append(chunks, items[0:chunkSize:chunkSize])
}
return append(chunks, items)
}