github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/sampledataset/dataset.go (about) 1 /* 2 Copyright 2023. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package sampledataset 18 19 import ( 20 "encoding/json" 21 "fmt" 22 "math/rand" 23 "time" 24 25 "github.com/brianvoe/gofakeit/v6" 26 "github.com/siglens/siglens/pkg/config" 27 ) 28 29 type Generator interface { 30 Init() error 31 GetLogLine() ([]byte, error) 32 GetRawLog() (map[string]interface{}, error) 33 } 34 35 type DynamicUserGenerator struct { 36 baseBody map[string]interface{} 37 tNowEpoch uint64 38 tsKey string 39 faker *gofakeit.Faker 40 seed int64 41 } 42 43 func InitDynamicUserGenerator(ts bool, seed int64) *DynamicUserGenerator { 44 return &DynamicUserGenerator{ 45 seed: seed, 46 tsKey: config.GetTimeStampKey(), 47 } 48 } 49 50 func (r *DynamicUserGenerator) Init() error { 51 gofakeit.Seed(r.seed) 52 r.faker = gofakeit.NewUnlocked(r.seed) 53 rand.Seed(r.seed) 54 r.baseBody = make(map[string]interface{}) 55 r.generateRandomBody() 56 _, err := json.Marshal(r.baseBody) 57 if err != nil { 58 return err 59 } 60 r.tNowEpoch = uint64(time.Now().UnixMilli()) - 80*24*3600*1000 61 return nil 62 } 63 64 func (r *DynamicUserGenerator) GetLogLine() ([]byte, error) { 65 r.generateRandomBody() 66 return json.Marshal(r.baseBody) 67 } 68 69 func (r *DynamicUserGenerator) GetRawLog() (map[string]interface{}, error) { 70 r.generateRandomBody() 71 return r.baseBody, nil 72 } 73 74 func (r *DynamicUserGenerator) generateRandomBody() { 75 randomizeBody(r.faker, r.baseBody, r.tsKey) 76 } 77 78 func randomizeBody(f *gofakeit.Faker, m map[string]interface{}, tsKey string) { 79 80 m["batch"] = fmt.Sprintf("batch-%d", f.Number(1, 1000)) 81 p := f.Person() 82 m["first_name"] = p.FirstName 83 m["last_name"] = p.LastName 84 m["gender"] = p.Gender 85 m["ssn"] = p.SSN 86 m["image"] = p.Image 87 m["hobby"] = p.Hobby 88 89 m["job_description"] = p.Job.Descriptor 90 m["job_level"] = p.Job.Level 91 m["job_title"] = p.Job.Title 92 m["job_company"] = p.Job.Company 93 94 m["address"] = p.Address.Address 95 m["street"] = p.Address.Street 96 m["city"] = p.Address.City 97 m["state"] = p.Address.State 98 m["zip"] = p.Address.Zip 99 m["country"] = p.Address.Country 100 m["latitude"] = p.Address.Latitude 101 m["longitude"] = p.Address.Longitude 102 m["user_phone"] = p.Contact.Phone 103 m["user_email"] = p.Contact.Email 104 105 m["user_color"] = f.Color() 106 m["weekday"] = f.WeekDay() 107 m["http_method"] = f.HTTPMethod() 108 m["http_status"] = f.HTTPStatusCodeSimple() 109 m["app_name"] = f.AppName() 110 m["app_version"] = f.AppVersion() 111 m["ident"] = f.UUID() 112 m["user_agent"] = f.UserAgent() 113 m["url"] = f.URL() 114 m["group"] = fmt.Sprintf("group %d", f.Number(0, 2)) 115 m["question"] = f.Question() 116 m["latency"] = f.Number(0, 10_000_000) 117 m[tsKey] = uint64(time.Now().UnixMilli()) 118 }