-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.go
178 lines (149 loc) · 4.02 KB
/
random.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
package main
import (
"math/rand"
"net/url"
"strings"
"github.com/brianvoe/gofakeit/v6"
uuid "github.com/satori/go.uuid"
)
type PropertyType string
const (
PropertyTypeString PropertyType = "string"
PropertyTypeNumber PropertyType = "number"
PropertyTypeDate PropertyType = "date"
PropertyTypeBoolean PropertyType = "boolean"
PropertyTypeObject PropertyType = "object"
PropertyTypeArray PropertyType = "array"
PropertyTypeNil PropertyType = "nil"
)
var propertyTypes = []PropertyType{
PropertyTypeString,
PropertyTypeString,
PropertyTypeString,
PropertyTypeNumber,
PropertyTypeNumber,
PropertyTypeDate,
PropertyTypeBoolean,
PropertyTypeObject,
PropertyTypeArray,
PropertyTypeNil,
}
const (
IDENTITY_COUNT = 42000
PROPERTY_COUNT = 50
EVENT_NAME_COUNT = 200
SCREEN_NAME_COUNT = 100
)
var identities []struct {
UserID string
AnonymousID []string
}
var properties []struct {
Name string
Type PropertyType
}
var eventNames []string
var screenNames []string
func init() {
// init identities userId and anonymousIds
for i := 0; i < IDENTITY_COUNT; i++ {
identity := struct {
UserID string
AnonymousID []string
}{
UserID: "u-" + uuid.NewV4().String(),
}
for i := 0; i < 1+rand.Intn(4); i++ { // 1 to 5 AnonymousID per identity
identity.AnonymousID = append(identity.AnonymousID, "a-"+uuid.NewV4().String())
}
identities = append(identities, identity)
}
// init properties name and type
for i := 0; i < PROPERTY_COUNT; i++ {
name := strings.ReplaceAll(pureString(gofakeit.HipsterSentence(1+rand.Intn(5))), " ", "_")
properties = append(properties, struct {
Name string
Type PropertyType
}{
Name: name,
Type: propertyTypes[rand.Intn(len(propertyTypes))],
})
}
// init events name
for i := 0; i < EVENT_NAME_COUNT; i++ {
eventNames = append(eventNames, pureString(gofakeit.HipsterSentence(1+rand.Intn(5))))
}
// init screens name
for i := 0; i < SCREEN_NAME_COUNT; i++ {
screenNames = append(screenNames, pureString(gofakeit.HipsterSentence(1+rand.Intn(5))))
}
}
func pureString(str string) string {
str = strings.ToLower(str)
str = strings.ReplaceAll(str, ",", "")
str = strings.ReplaceAll(str, "'", "")
str = strings.ReplaceAll(str, ".", "")
str = strings.ReplaceAll(str, "_", "")
str = strings.ReplaceAll(str, "-", "")
return str
}
// var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
// func RandStringRunes(n int) string {
// b := make([]rune, n)
// for i := range b {
// b[i] = letterRunes[rand.Intn(len(letterRunes))]
// }
// return string(b)
// }
func RandPageTitle() string {
return gofakeit.Dessert() + " | " + gofakeit.Company()
}
func RandURL() (string, string, string, error) {
u, err := url.Parse(gofakeit.URL())
if err != nil {
return "", "", "", err
}
search := u.RawQuery
if len(search) > 0 {
search = "?" + search
}
return u.String(), u.Path, search, nil
}
func RandIdentity() (string, []string) {
identity := identities[rand.Intn(IDENTITY_COUNT)]
return identity.UserID, identity.AnonymousID
}
func RandEventName() string {
return eventNames[rand.Intn(EVENT_NAME_COUNT)]
}
func RandScreenName() string {
return strings.ReplaceAll(eventNames[rand.Intn(SCREEN_NAME_COUNT)], " ", "")
}
func RandProperty() (string, interface{}) {
prop := properties[rand.Intn(PROPERTY_COUNT)]
switch prop.Type {
case PropertyTypeString:
return prop.Name, gofakeit.HipsterSentence(1 + rand.Intn(9))
case PropertyTypeNumber:
return prop.Name, gofakeit.Int16()
case PropertyTypeDate:
return prop.Name, gofakeit.Date()
case PropertyTypeBoolean:
return prop.Name, gofakeit.Bool()
case PropertyTypeObject:
return prop.Name, gofakeit.Address()
case PropertyTypeArray:
return prop.Name, []string{gofakeit.Fruit(), gofakeit.FarmAnimal()}
case PropertyTypeNil:
return prop.Name, nil
}
return prop.Name, nil
}
func RandProperties() map[string]interface{} {
output := map[string]interface{}{}
for i := 0; i < 3+rand.Intn(25); i++ {
name, value := RandProperty()
output[name] = value
}
return output
}