/
generate.go
174 lines (152 loc) · 5.26 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
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
package attendeesrv
import (
"context"
"fmt"
aulogging "github.com/StephanHCB/go-autumn-logging"
"github.com/eurofurence/reg-attendee-service/internal/api/v1/attendee"
"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"
"github.com/eurofurence/reg-attendee-service/internal/repository/selfclient"
"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 (s *AttendeeServiceImplData) SendFakeRegistrationToAPI(ctx context.Context) (string, error) {
a := fakeRegistration()
dto := &attendee.AttendeeDto{}
mapAttendeeToDto(a, dto)
ctxTimeout, cancel := context.WithTimeout(ctx, 20*time.Second)
defer cancel()
return selfclient.SendRegistration(ctxTimeout, dto)
}
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: oneOf(config.AllowedCountries()),
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,stage," + oneOf([]string{"sponsor", "sponsor2", "tshirt"}) + ",",
Options: "," + randomSelection(config.AllowedOptions(), 1, 4) + ",",
UserComments: "generated by load test",
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
}
}
func mapAttendeeToDto(a *entity.Attendee, dto *attendee.AttendeeDto) {
// this cannot fail
dto.Id = a.ID
dto.Nickname = a.Nickname
dto.FirstName = a.FirstName
dto.LastName = a.LastName
dto.Street = a.Street
dto.Zip = a.Zip
dto.City = a.City
dto.Country = a.Country
dto.State = a.State
dto.Email = a.Email
dto.Phone = a.Phone
dto.Telegram = a.Telegram
dto.Partner = a.Partner
dto.Birthday = a.Birthday
dto.Gender = a.Gender
dto.Pronouns = a.Pronouns
dto.TshirtSize = a.TshirtSize
dto.SpokenLanguages = removeWrappingCommas(a.SpokenLanguages)
dto.RegistrationLanguage = removeWrappingCommas(a.RegistrationLanguage)
dto.Flags = removeWrappingCommas(a.Flags)
dto.Packages = removeWrappingCommas(a.Packages)
dto.Options = removeWrappingCommas(a.Options)
dto.UserComments = a.UserComments
}