Skip to content

Commit

Permalink
Update bootstrap service
Browse files Browse the repository at this point in the history
  • Loading branch information
th0th committed Feb 10, 2025
1 parent 66b4a02 commit c6fdbad
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 27 deletions.
27 changes: 14 additions & 13 deletions backend/pkg/poeticmetric/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,21 +159,18 @@ func EventKinds() []EventKind {
}
}

func (e *Event) Fill(params *CreateEventParams, organizationSalt string, safeQueryParameters []string) error {
e.URL = *params.URL
e.UserAgent = params.UserAgent

parsedURL, err := url.Parse(*params.URL)
func (e *Event) Fill(ipAddress string, organizationSalt string, safeQueryParameters []string) error {
parsedURL, err := url.Parse(e.URL)
if err != nil {
return errors.Wrap(err, 0)
}

// visitor ID
checksum := sha256.Sum256([]byte(organizationSalt + params.IPAddress + params.UserAgent))
checksum := sha256.Sum256([]byte(organizationSalt + ipAddress + e.UserAgent))
e.VisitorID = fmt.Sprintf("%x", checksum)

// user agent
userAgent := useragent.Parse(params.UserAgent)
userAgent := useragent.Parse(e.UserAgent)

e.IsBot = userAgent.Bot

Expand Down Expand Up @@ -202,15 +199,19 @@ func (e *Event) Fill(params *CreateEventParams, organizationSalt string, safeQue
}

// locale
language := LocaleLanguageMap()[*params.Locale]
if language != "" {
e.Language = &language
if e.Locale != nil {
language := LocaleLanguageMap()[*e.Locale]
if language != "" {
e.Language = &language
}
}

// time zone
countryISOCode := TimeZoneCountryISOCodeMap()[*params.TimeZone]
if countryISOCode != "" {
e.CountryISOCode = &countryISOCode
if e.TimeZone != nil {
countryISOCode := TimeZoneCountryISOCodeMap()[*e.TimeZone]
if countryISOCode != "" {
e.CountryISOCode = &countryISOCode
}
}

// utm
Expand Down
24 changes: 11 additions & 13 deletions backend/pkg/service/bootstrap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,14 @@ func (s *service) Run(ctx context.Context, params *poeticmetric.BootstrapService
return fmt.Sprintf("%s://%s", protocol, gofakeit.DomainName())
})
referrerPaths := generateSlice(100, randomUrlPath)
//urls := generateSlice(35, func() string { return fmt.Sprintf("https://%s%s", site.Domain, randomUrlPath()) })
urls := generateSlice(35, func() string { return fmt.Sprintf("https://%s%s", site.Domain, randomUrlPath()) })
utmSources := generateSlice(35, gofakeit.Word)
utmCampaigns := generateSlice(35, gofakeit.Word)
utmMediums := generateSlice(35, gofakeit.Word)
utmContents := generateSlice(35, gofakeit.Word)
utmTerms := generateSlice(35, gofakeit.Word)
visitorIds := generateSlice(4000, gofakeit.UUID)
//userAgents := generateSlice(100, gofakeit.UserAgent)
userAgents := generateSlice(100, gofakeit.UserAgent)

for i := 0; i < batches; i += 1 {
events := []*poeticmetric.Event{}
Expand All @@ -217,27 +217,25 @@ func (s *service) Run(ctx context.Context, params *poeticmetric.BootstrapService
timeZone := gofakeit.TimeZoneRegion()

event := poeticmetric.Event{
//CountryISOCode: country.GetIsoCodeFromTimeZoneName(timeZone),
DateTime: gofakeit.DateRange(now.Add(-31*24*time.Hour), now),
DurationSeconds: uint32(gofakeit.IntRange(1, 1200)), //nolint:gosec
ID: uuid.NewString(),
Kind: poeticmetric.EventKindPageView,
//Language: locale.GetLanguage(languageBcp),
Locale: &languageBcp,
SiteID: site.ID,
TimeZone: &timeZone,
Locale: &languageBcp,
SiteID: site.ID,
TimeZone: &timeZone,
URL: gofakeit.RandomString(urls),
UserAgent: gofakeit.RandomString(userAgents),
}

if gofakeit.Bool() {
event.Referrer = poeticmetric.Pointer(fmt.Sprintf("%s%s", gofakeit.RandomString(referrerSites), gofakeit.RandomString(referrerPaths)))
}

//err = event.FillFromUrl(gofakeit.RandomString(urls), nil)
//if err != nil {
// return err
//}

//event.FillFromUserAgent(gofakeit.RandomString(userAgents))
err = event.Fill(gofakeit.IPv4Address(), event.DateTime.String(), nil)
if err != nil {
return err
}

event.VisitorID = gofakeit.RandomString(visitorIds)

Expand Down
2 changes: 1 addition & 1 deletion backend/pkg/service/event/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (s *service) Create(ctx context.Context, params *poeticmetric.CreateEventPa
return errors.Wrap(err, 0)
}

err = event.Fill(params, organizationSalt, site.SafeQueryParameters)
err = event.Fill(params.IPAddress, organizationSalt, site.SafeQueryParameters)
if err != nil {
return errors.Wrap(err, 0)
}
Expand Down

0 comments on commit c6fdbad

Please sign in to comment.