2017-01-25 20:01:02 +01:00
|
|
|
package datastore
|
|
|
|
|
|
|
|
import (
|
2018-05-04 12:20:37 +02:00
|
|
|
"database/sql"
|
2018-05-07 19:45:42 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2018-05-07 16:05:53 +02:00
|
|
|
"time"
|
2018-05-07 19:45:42 +02:00
|
|
|
|
|
|
|
"github.com/usefathom/fathom/pkg/models"
|
2017-01-25 20:01:02 +01:00
|
|
|
)
|
|
|
|
|
2018-04-25 14:29:32 +02:00
|
|
|
// SavePageview inserts a single pageview model into the connected database
|
2018-05-07 16:05:53 +02:00
|
|
|
func SavePageview(p *models.Pageview) error {
|
2018-05-08 13:08:16 +02:00
|
|
|
query := dbx.Rebind(`INSERT INTO pageviews(hostname, pathname, session_id, is_new_visitor, is_new_session, is_unique, is_bounce, referrer, duration, timestamp) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
|
|
|
|
result, err := dbx.Exec(query, p.Hostname, p.Pathname, p.SessionID, p.IsNewVisitor, p.IsNewSession, p.IsUnique, p.IsBounce, p.Referrer, p.Duration, p.Timestamp)
|
2017-01-25 20:01:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-07 16:05:53 +02:00
|
|
|
p.ID, _ = result.LastInsertId()
|
2018-04-25 14:29:32 +02:00
|
|
|
return nil
|
|
|
|
}
|
2017-01-25 20:01:02 +01:00
|
|
|
|
2018-05-04 12:20:37 +02:00
|
|
|
func UpdatePageview(p *models.Pageview) error {
|
2018-05-07 17:01:20 +02:00
|
|
|
query := dbx.Rebind(`UPDATE pageviews SET is_bounce = ?, duration = ? WHERE id = ?`)
|
2018-05-07 16:05:53 +02:00
|
|
|
_, err := dbx.Exec(query, p.IsBounce, p.Duration, p.ID)
|
2018-05-04 12:20:37 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-07 16:05:53 +02:00
|
|
|
func GetMostRecentPageviewBySessionID(sessionID string) (*models.Pageview, error) {
|
|
|
|
result := &models.Pageview{}
|
2018-05-07 17:01:20 +02:00
|
|
|
query := dbx.Rebind(`SELECT * FROM pageviews WHERE session_id = ? ORDER BY id DESC LIMIT 1`)
|
2018-05-07 16:05:53 +02:00
|
|
|
err := dbx.Get(result, query, sessionID)
|
2018-05-04 12:20:37 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, ErrNoResults
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-07 16:05:53 +02:00
|
|
|
return result, nil
|
2018-05-04 12:20:37 +02:00
|
|
|
}
|
|
|
|
|
2018-05-07 16:05:53 +02:00
|
|
|
func GetProcessablePageviews() ([]*models.Pageview, error) {
|
|
|
|
var results []*models.Pageview
|
|
|
|
thirtyMinsAgo := time.Now().Add(-30 * time.Minute)
|
|
|
|
fiveMinsAgo := time.Now().Add(-5 * time.Minute)
|
2018-05-07 17:01:20 +02:00
|
|
|
query := dbx.Rebind(`SELECT * FROM pageviews WHERE ( duration > 0 OR timestamp < ? ) AND timestamp < ? LIMIT 500`)
|
2018-05-07 16:05:53 +02:00
|
|
|
err := dbx.Select(&results, query, thirtyMinsAgo, fiveMinsAgo)
|
2018-04-26 20:40:00 +02:00
|
|
|
return results, err
|
|
|
|
}
|
2018-05-07 19:45:42 +02:00
|
|
|
|
|
|
|
func DeletePageviews(pageviews []*models.Pageview) error {
|
|
|
|
ids := []string{}
|
|
|
|
for _, p := range pageviews {
|
|
|
|
ids = append(ids, strconv.FormatInt(p.ID, 10))
|
|
|
|
}
|
|
|
|
query := dbx.Rebind(`DELETE FROM pageviews WHERE id IN(` + strings.Join(ids, ",") + `)`)
|
|
|
|
_, err := dbx.Exec(query)
|
|
|
|
return err
|
|
|
|
}
|