-
Notifications
You must be signed in to change notification settings - Fork 0
/
pairing.go
406 lines (338 loc) · 9.42 KB
/
pairing.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package main
import (
"encoding/json"
"errors"
"fmt"
"math/rand"
"os"
"strings"
"github.com/johnamadeo/server"
mailgun "github.com/mailgun/mailgun-go"
)
const (
// MaxTries is the max. number of times to attempt re-matching if a non-optimal matching is initially made
MaxTries = 50
// RecentRoundRange : max. no. of rounds between 2 pairings that is considered recent
RecentRoundRange = 5
// EmailIntro : Text to put in the beginning of the email body
EmailIntro = "Your Mealbot group this week is:"
// EmailFooter : Text to put at the end of the email body
EmailFooter = "Feel free to reply all in this thread for scheduling. I'm a robot, so I can only read 1's and 0's.\n\n Sent by your friendly neighborhood Mealbot! Learn more about me at https://mealbot-web.herokuapp.com"
// EmailSubject : Subject of the email
EmailSubject = "Your new Mealbot group"
)
// Pair :
type Pair struct {
ID1 string
ID2 string
ExtraID string
}
// NewPair :
func NewPair(name1 string, name2 string) Pair {
if name1 < name2 {
return Pair{
ID1: name1,
ID2: name2,
ExtraID: "",
}
}
return Pair{
ID1: name2,
ID2: name1,
ExtraID: "",
}
}
func (p Pair) String() string {
return fmt.Sprintf("%-40s %-40s %-40s", p.ID1, p.ID2, p.ExtraID)
}
// Round : Data structure for storing info on one round of pairings between members
type Round struct {
Number int
Pairs map[Pair]bool
Paired map[string]bool
DrawOrderedPairs []Pair // for logging; keeps order in which pairs were made
}
// NewRound :
func NewRound(roundNumber int) Round {
return Round{
Number: roundNumber,
Pairs: map[Pair]bool{},
Paired: map[string]bool{},
DrawOrderedPairs: []Pair{},
}
}
// AddPair : Record a new pairing made during the round
func (r *Round) AddPair(member1 string, member2 string) {
r.Paired[member1] = true
r.Paired[member2] = true
pair := NewPair(member1, member2)
r.Pairs[pair] = true
r.DrawOrderedPairs = append(r.DrawOrderedPairs, pair)
}
// IsPaired : Check if member has already been paired this round
func (r Round) IsPaired(member string) bool {
_, ok := r.Paired[member]
return ok
}
func (r Round) String() string {
header := fmt.Sprintf("---------\nRound %d\n---------\n", r.Number)
pairs := []string{}
for _, pair := range r.DrawOrderedPairs {
pairs = append(pairs, pair.String())
}
return header + strings.Join(pairs, "\n") + "\n"
}
// MinimalMember : A pared down version of the Member struct (see members.go) for the pairing process
type MinimalMember struct {
ID string
Name string
Trait string
LastRoundWith map[string]int
}
// MembersMap :
type MembersMap map[string]MinimalMember
// UpdateLastRoundWith : Updates the last round 2 members were matched with each other
func (mm *MembersMap) UpdateLastRoundWith(id1 string, id2 string, roundNum int) {
(*mm)[id1].LastRoundWith[id2] = roundNum
(*mm)[id2].LastRoundWith[id1] = roundNum
}
func (mm MembersMap) String() string {
str := "X's last round with Y\n"
for memberID := range mm {
str += fmt.Sprintf("%s\n", memberID)
for partnerID, lastRound := range mm[memberID].LastRoundWith {
str += fmt.Sprintf("\t%-30s : %d\n", partnerID, lastRound)
}
str += "\n"
}
return str
}
type RandomIntGenerator func(int) int
func runPairingAlgorithm(members MembersMap, roundNum int, genRandomInt RandomIntGenerator) (MembersMap, Round) {
memberIds := []string{}
for id := range members {
memberIds = append(memberIds, id)
}
tries := 0
var round Round
// retry until a) a round w/o repeats is found, or b) MaxTries is reached
for {
// Make a deep copy of the members map
bytes, _ := json.Marshal(members)
var tempMembers MembersMap
json.Unmarshal(bytes, &tempMembers)
round = NewRound(roundNum)
numRecentMatches := 0
// hold out odd member out and add it back in at the end of round
var extraMemberID string
if len(tempMembers)%2 == 1 {
extraMemberID = memberIds[genRandomInt(len(memberIds))]
}
for _, memberID := range memberIds {
if round.IsPaired(memberID) || memberID == extraMemberID {
continue
}
member := tempMembers[memberID]
var goodCandidates, okCandidates, badCandidates []MinimalMember
for candidateID, lastRound := range member.LastRoundWith {
if round.IsPaired(candidateID) || candidateID == extraMemberID {
continue
}
notRecentlyMatched := lastRound == -1 || round.Number-lastRound > RecentRoundRange
hasDifferentTraits := member.Trait == members[candidateID].Trait
switch {
case notRecentlyMatched && hasDifferentTraits:
goodCandidates = append(goodCandidates, members[candidateID])
case notRecentlyMatched && !hasDifferentTraits:
okCandidates = append(okCandidates, members[candidateID])
default:
badCandidates = append(badCandidates, members[candidateID])
}
}
var partner MinimalMember
switch {
case len(goodCandidates) > 0:
partner = selectRandomPartner(genRandomInt, goodCandidates)
case len(okCandidates) > 0:
partner = selectRandomPartner(genRandomInt, okCandidates)
default:
partner = selectRandomPartner(genRandomInt, badCandidates)
numRecentMatches++
}
round.AddPair(memberID, partner.ID)
tempMembers.UpdateLastRoundWith(memberID, partner.ID, round.Number)
}
tries++
if numRecentMatches == 0 || tries == MaxTries {
members = tempMembers
fmt.Println(round)
fmt.Printf("%d recent matches\n", numRecentMatches)
break
}
}
return members, round
}
func runPairingRound(orgname string, roundNum int, testMode bool) error {
members, err := getMinimalMembersFromDB(orgname)
if err != nil {
return err
}
members, round := runPairingAlgorithm(members, roundNum, rand.Intn)
if !testMode {
err = sendEmails(orgname, round, members)
if err != nil {
return err
}
}
err = saveRoundInDB(round, members, orgname)
if err != nil {
return err
}
return nil
}
func sendEmails(orgname string, round Round, members MembersMap) error {
for pair := range round.Pairs {
toEmails := []string{pair.ID1, pair.ID2}
toNames := []string{members[pair.ID1].Name, members[pair.ID2].Name}
err := sendEmail(orgname, toEmails, toNames)
if err != nil {
return err
}
}
return nil
}
func selectRandomPartner(genRandomInt RandomIntGenerator, members []MinimalMember) MinimalMember {
return members[genRandomInt(len(members))]
}
func getMinimalMembersFromDB(orgname string) (MembersMap, error) {
crossMatchTrait, err := GetCrossMatchTrait(orgname)
if err != nil {
return MembersMap{}, err
}
db, err := server.CreateDBConnection(LocalDBConnection)
defer db.Close()
if err != nil {
return MembersMap{}, err
}
minimalMembers := MembersMap{}
members, err := GetMembersFromDB(orgname, true)
if err != nil {
return MembersMap{}, err
}
for _, member := range members {
minimalMembers[member.Email] = MinimalMember{
ID: member.Email,
Name: member.Name,
Trait: member.Metadata[crossMatchTrait],
LastRoundWith: member.LastRoundWith,
}
}
return minimalMembers, nil
}
func saveRoundInDB(round Round, members MembersMap, orgname string) error {
db, err := server.CreateDBConnection(LocalDBConnection)
defer db.Close()
if err != nil {
return err
}
for pair := range round.Pairs {
columns := "(organization, id1, id2, extraId, round)"
placeholder := "($1, $2, $3, $4, $5)"
_, err := db.Exec(
fmt.Sprintf("INSERT INTO pairs %s VALUES %s", columns, placeholder),
orgname,
pair.ID1,
pair.ID2,
pair.ExtraID,
round.Number,
)
if err != nil {
return err
}
}
for _, member := range members {
bytes, err := json.Marshal(member.LastRoundWith)
if err != nil {
return err
}
_, err = db.Exec(
"UPDATE members SET last_round_with = $1 WHERE organization = $2 AND email = $3",
server.JSONB(bytes),
orgname,
member.ID,
)
if err != nil {
return err
}
}
_, err = db.Exec(
"UPDATE rounds SET done = $1 WHERE organization = $2 AND id = $3",
true,
orgname,
round.Number,
)
if err != nil {
return err
}
return nil
}
func sendEmail(orgname string, toEmails []string, toNames []string) error {
smtpAddress, ok := os.LookupEnv("MAILGUN_SMTP_LOGIN")
if !ok {
return errors.New("environment variable MAILGUN_SMTP_LOGIN not set")
}
domain, ok := os.LookupEnv("MAILGUN_DOMAIN")
if !ok {
return errors.New("environment variable MAILGUN_DOMAIN not set")
}
apiKey, ok := os.LookupEnv("MAILGUN_API_KEY")
if !ok {
return errors.New("environment variable MAILGUN_API_KEY not set")
}
from := fmt.Sprintf("%s Mealbot <%s>", orgname, smtpAddress)
text := fmt.Sprintf(
"%s \n%s\n\n %s",
EmailIntro,
strings.Join(toNames, "\r\n"),
EmailFooter,
)
mg := mailgun.NewMailgun(domain, apiKey)
message := mg.NewMessage(
from,
EmailSubject,
text,
toEmails...,
)
_, _, err := mg.Send(message)
if err != nil {
return err
}
fmt.Println("Emails queued on Mailgun!")
return nil
}
func runPairingScheduler(testMode bool) error {
db, err := server.CreateDBConnection(LocalDBConnection)
defer db.Close()
if err != nil {
return err
}
rows, err := db.Query(
"SELECT organization, id FROM rounds WHERE scheduled_date < now() AT TIME ZONE 'utc' AND done = false",
)
if err != nil {
return err
}
for rows.Next() {
var orgname string
var roundNum int
err := rows.Scan(&orgname, &roundNum)
if err != nil {
return err
}
err = runPairingRound(orgname, roundNum, testMode)
if err != nil {
return err
}
}
return nil
}