fathom/datastore/seed.go

170 lines
3.5 KiB
Go
Raw Normal View History

package datastore
import (
2016-12-11 13:50:01 +00:00
"fmt"
"math/rand"
"time"
randomdata "github.com/Pallinder/go-randomdata"
"github.com/dannyvankooten/ana/models"
)
2016-12-11 13:50:01 +00:00
var browserNames = []string{
"Chrome",
"Chrome",
"Firefox",
"Safari",
"Safari",
"Internet Explorer",
}
2016-12-11 13:50:01 +00:00
var months = []time.Month{
time.January,
time.February,
time.March,
time.April,
time.May,
time.June,
time.July,
time.August,
time.September,
time.October,
time.November,
time.December,
2016-12-05 16:16:14 +00:00
}
2016-12-11 13:50:01 +00:00
var browserLanguages = []string{
"en-US",
"en-US",
"nl-NL",
"fr-FR",
"de-DE",
"es-ES",
}
2016-12-11 13:50:01 +00:00
var screenResolutions = []string{
"2560x1440",
"1920x1080",
"1920x1080",
"360x640",
}
func seedPages() []models.Page {
2016-12-11 13:50:01 +00:00
var pages = make([]models.Page, 0)
homepage := models.Page{
Hostname: "wordpress.dev",
Path: "/",
Title: "Homepage",
}
homepage.Save(DB)
2016-12-11 13:50:01 +00:00
contactPage := models.Page{
Hostname: "wordpress.dev",
Path: "/contact/",
Title: "Contact",
}
contactPage.Save(DB)
2016-12-11 13:50:01 +00:00
aboutPage := models.Page{
Hostname: "wordpress.dev",
Path: "/about/",
Title: "About Me",
}
aboutPage.Save(DB)
2016-12-11 13:50:01 +00:00
portfolioPage := models.Page{
Hostname: "wordpress.dev",
Path: "/portfolio/",
Title: "Portfolio",
}
portfolioPage.Save(DB)
2016-12-11 13:50:01 +00:00
pages = append(pages, homepage)
pages = append(pages, homepage)
pages = append(pages, contactPage)
pages = append(pages, aboutPage)
pages = append(pages, portfolioPage)
return pages
}
// Seed inserts n random pageviews in the database.
func Seed(n int) {
2016-12-11 13:50:01 +00:00
pages := seedPages()
stmtVisitor, _ := DB.Prepare("SELECT v.id FROM visitors v WHERE v.visitor_key = ? LIMIT 1")
2016-12-11 13:50:01 +00:00
defer stmtVisitor.Close()
// insert X random hits
for i := 0; i < n; i++ {
// print a dot as progress indicator
fmt.Print(".")
date := randomDateBeforeNow()
2016-12-11 13:50:01 +00:00
// create or find visitor
visitor := models.Visitor{
IpAddress: randomdata.IpV4Address(),
DeviceOS: "Linux",
BrowserName: randSliceElement(browserNames),
BrowserVersion: "54.0",
BrowserLanguage: randSliceElement(browserLanguages),
ScreenResolution: randSliceElement(screenResolutions),
Country: randomdata.Country(randomdata.TwoCharCountry),
}
dummyUserAgent := visitor.BrowserName + visitor.BrowserVersion + visitor.DeviceOS
visitor.Key = visitor.GenerateKey(date.Format("2006-01-02"), visitor.IpAddress, dummyUserAgent)
2016-12-11 13:50:01 +00:00
err := stmtVisitor.QueryRow(visitor.Key).Scan(&visitor.ID)
if err != nil {
visitor.Save(DB)
2016-12-11 13:50:01 +00:00
}
// generate random timestamp
timestamp := fmt.Sprintf("%s %d:%d:%d", date.Format("2006-01-02"), randInt(10, 24), randInt(10, 60), randInt(10, 60))
pv := models.Pageview{
VisitorID: visitor.ID,
ReferrerUrl: "",
ReferrerKeyword: "",
Timestamp: timestamp,
}
DB.Exec("START TRANSACTION")
2016-12-11 13:50:01 +00:00
// insert between 1-4 pageviews for this visitor
for j := 0; j <= randInt(1, 4); j++ {
page := pages[randInt(0, len(pages))]
pv.PageID = page.ID
pv.Save(DB)
2016-12-11 13:50:01 +00:00
}
DB.Exec("COMMIT")
2016-12-11 13:50:01 +00:00
}
}
2016-11-26 15:17:52 +00:00
func randomDate() time.Time {
2016-12-11 13:50:01 +00:00
now := time.Now()
month := months[randInt(0, len(months))]
t := time.Date(randInt(now.Year()-1, now.Year()), month, randInt(1, 31), randInt(0, 23), randInt(0, 59), randInt(0, 59), 0, time.UTC)
2016-12-11 13:50:01 +00:00
return t
2016-11-26 15:17:52 +00:00
}
func randomDateBeforeNow() time.Time {
2016-12-11 13:50:01 +00:00
now := time.Now()
date := randomDate()
for date.After(now) {
date = randomDate()
}
2016-11-26 15:17:52 +00:00
2016-12-11 13:50:01 +00:00
return date
2016-11-26 15:17:52 +00:00
}
func randSliceElement(slice []string) string {
2016-12-11 13:50:01 +00:00
return slice[randInt(0, len(slice))]
}
func randInt(min int, max int) int {
2016-12-11 13:50:01 +00:00
return min + rand.Intn(max-min)
}