-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate.go
134 lines (116 loc) · 3.96 KB
/
generate.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
package attendeesrv
import (
"context"
"fmt"
aulogging "github.com/StephanHCB/go-autumn-logging"
"github.com/eurofurence/reg-attendee-service/internal/entity"
"github.com/eurofurence/reg-attendee-service/internal/repository/config"
"github.com/eurofurence/reg-attendee-service/internal/repository/database"
"math/rand"
"strings"
"time"
)
func (s *AttendeeServiceImplData) GenerateFakeRegistrations(ctx context.Context, count uint) error {
for regNo := uint(0); regNo < count; regNo++ {
attendee := fakeRegistration()
id, err := database.GetRepository().AddAttendee(ctx, attendee)
if err != nil {
aulogging.Logger.Ctx(ctx).Warn().Printf("failed to save attendee #%d with nickname %s - BAILING OUT", regNo+1, attendee.Nickname)
return err
}
aulogging.Logger.Ctx(ctx).Info().Printf("successfully saved attendee #%d with badgeNo %d nickname %s", regNo+1, id, attendee.Nickname)
}
return nil
}
func fakeRegistration() *entity.Attendee {
return &entity.Attendee{
Nickname: randomString(3, 40, 2),
FirstName: randomString(3, 30, 0),
LastName: randomString(3, 30, 0),
Street: randomString(3, 120, 0),
Zip: randomString(3, 15, 0),
City: randomString(3, 80, 0),
Country: "",
Email: "jsquirrel_github_9a6d@packetloss.de",
Phone: "12345",
Birthday: randomValidBirthday(),
Pronouns: "he/him",
TshirtSize: oneOf(config.AllowedTshirtSizes()),
SpokenLanguages: "," + randomSelection(config.AllowedSpokenLanguages(), 1, 5) + ",",
RegistrationLanguage: oneOf(config.AllowedRegistrationLanguages()),
Flags: ",terms-accepted," + randomSelection([]string{"hc", "anon", "digi-book"}, 1, 3) + ",",
Packages: ",room-none,attendance," + oneOf([]string{"sponsor", "sponsor2", "tshirt"}) + ",",
Options: "," + randomSelection(config.AllowedOptions(), 1, 4) + ",",
UserComments: "",
Identity: randomString(10, 12, 0) + "_gen",
}
}
func randomString(minlen int, maxlen int, maxNumSpecials int) string {
text := ""
normal := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
special := []rune("äöüÄÖÜß 松鼠วั")
length := minlen + safeRandIntn(maxlen-minlen)
specialcount := 0
// always start and end in normal
idx := rand.Intn(len(normal))
text += fmt.Sprintf("%c", normal[idx])
for i := 1; i < length-1; i++ {
idx := rand.Intn(len(normal) + len(special))
if idx >= len(normal) {
specialcount++
if specialcount > maxNumSpecials {
idx := rand.Intn(len(normal))
text += fmt.Sprintf("%c", normal[idx])
} else {
text += fmt.Sprintf("%c", special[idx-len(normal)])
}
} else {
text += fmt.Sprintf("%c", normal[idx])
}
}
idx = rand.Intn(len(normal))
text += fmt.Sprintf("%c", normal[idx])
return text
}
const isoDateFormat = "2006-01-02"
func randomValidBirthday() string {
earliest, _ := time.Parse(isoDateFormat, config.EarliestBirthday())
latest, _ := time.Parse(isoDateFormat, config.LatestBirthday())
randUnix := earliest.Unix() + rand.Int63n(latest.Unix()-earliest.Unix())
return time.Unix(randUnix, 0).Format(isoDateFormat)
}
func randomSelection(allowed []string, min int, max int) string {
length := min + safeRandIntn(max-min)
if length >= len(allowed) {
return strings.Join(allowed, ",")
}
picked := make(map[int]bool)
for i := range allowed {
picked[i] = false
}
for i := 0; i < length; i++ {
choice := safeRandIntn(len(allowed))
for picked[choice] {
choice = safeRandIntn(len(allowed))
}
picked[choice] = true
}
taken := make([]string, 0)
for k, v := range picked {
if v {
taken = append(taken, allowed[k])
}
}
return strings.Join(taken, ",")
}
func oneOf(allowed []string) string {
picked := safeRandIntn(len(allowed))
return allowed[picked]
}
func safeRandIntn(len int) int {
if len > 0 {
return rand.Intn(len)
} else {
return 0
}
}