2016-11-24 12:10:40 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2016-12-11 13:50:01 +00:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
2016-12-25 15:37:45 +00:00
|
|
|
|
|
|
|
"github.com/dannyvankooten/ana/models"
|
2016-11-24 12:10:40 +00:00
|
|
|
)
|
|
|
|
|
2016-12-11 13:50:01 +00:00
|
|
|
var browserNames = []string{
|
|
|
|
"Chrome",
|
|
|
|
"Chrome",
|
|
|
|
"Firefox",
|
|
|
|
"Safari",
|
|
|
|
"Safari",
|
|
|
|
"Internet Explorer",
|
2016-11-24 12:10:40 +00:00
|
|
|
}
|
|
|
|
|
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-11-24 12:10:40 +00:00
|
|
|
}
|
|
|
|
|
2016-12-11 13:50:01 +00:00
|
|
|
var screenResolutions = []string{
|
|
|
|
"2560x1440",
|
|
|
|
"1920x1080",
|
|
|
|
"1920x1080",
|
|
|
|
"360x640",
|
2016-11-24 12:10:40 +00:00
|
|
|
}
|
|
|
|
|
2016-12-10 13:16:05 +00:00
|
|
|
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(Conn)
|
|
|
|
|
|
|
|
contactPage := models.Page{
|
|
|
|
Hostname: "wordpress.dev",
|
|
|
|
Path: "/contact/",
|
|
|
|
Title: "Contact",
|
|
|
|
}
|
|
|
|
contactPage.Save(Conn)
|
|
|
|
|
|
|
|
aboutPage := models.Page{
|
|
|
|
Hostname: "wordpress.dev",
|
|
|
|
Path: "/about/",
|
|
|
|
Title: "About Me",
|
|
|
|
}
|
|
|
|
aboutPage.Save(Conn)
|
|
|
|
|
|
|
|
portfolioPage := models.Page{
|
|
|
|
Hostname: "wordpress.dev",
|
|
|
|
Path: "/portfolio/",
|
|
|
|
Title: "Portfolio",
|
|
|
|
}
|
|
|
|
portfolioPage.Save(Conn)
|
|
|
|
|
|
|
|
pages = append(pages, homepage)
|
|
|
|
pages = append(pages, homepage)
|
|
|
|
pages = append(pages, contactPage)
|
|
|
|
pages = append(pages, aboutPage)
|
|
|
|
pages = append(pages, portfolioPage)
|
|
|
|
return pages
|
2016-12-08 21:20:40 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 12:10:40 +00:00
|
|
|
func Seed(n int) {
|
2016-12-11 13:50:01 +00:00
|
|
|
pages := seedPages()
|
|
|
|
|
|
|
|
stmtVisitor, _ := Conn.Prepare("SELECT v.id FROM visitors v WHERE v.visitor_key = ? LIMIT 1")
|
|
|
|
defer stmtVisitor.Close()
|
|
|
|
|
|
|
|
// insert X random hits
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
|
|
|
|
// print a dot as progress indicator
|
|
|
|
fmt.Print(".")
|
|
|
|
|
|
|
|
// 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),
|
|
|
|
}
|
|
|
|
visitor.Key = visitor.GenerateKey()
|
|
|
|
|
|
|
|
err := stmtVisitor.QueryRow(visitor.Key).Scan(&visitor.ID)
|
|
|
|
if err != nil {
|
|
|
|
visitor.Save(Conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate random timestamp
|
|
|
|
date := randomDateBeforeNow()
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
Conn.Exec("START TRANSACTION")
|
|
|
|
|
|
|
|
// 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(Conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
Conn.Exec("COMMIT")
|
|
|
|
}
|
2016-11-24 12:10:40 +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(now.Year(), month, randInt(1, 31), randInt(0, 23), randInt(0, 59), randInt(0, 59), 0, time.UTC)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-11-24 12:10:40 +00:00
|
|
|
func randSliceElement(slice []string) string {
|
2016-12-11 13:50:01 +00:00
|
|
|
return slice[randInt(0, len(slice))]
|
2016-11-24 12:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func randInt(min int, max int) int {
|
2016-12-11 13:50:01 +00:00
|
|
|
return min + rand.Intn(max-min)
|
2016-11-24 12:10:40 +00:00
|
|
|
}
|