2018-05-15 13:30:37 +02:00
|
|
|
package aggregator
|
|
|
|
|
|
|
|
import (
|
2018-10-05 14:19:11 +02:00
|
|
|
"fmt"
|
2018-06-01 11:44:20 +02:00
|
|
|
"strings"
|
2018-05-15 13:30:37 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/usefathom/fathom/pkg/datastore"
|
|
|
|
"github.com/usefathom/fathom/pkg/models"
|
|
|
|
)
|
|
|
|
|
2018-10-10 09:02:32 +02:00
|
|
|
func (agg *Aggregator) getSiteStats(r *results, siteID int64, t time.Time) (*models.SiteStats, error) {
|
2018-11-13 15:23:18 +01:00
|
|
|
cacheKey := fmt.Sprintf("%d-%s", siteID, t.Format("2006-01-02T15"))
|
2018-10-05 14:19:11 +02:00
|
|
|
if stats, ok := r.Sites[cacheKey]; ok {
|
2018-05-15 13:30:37 +02:00
|
|
|
return stats, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// get from db
|
2018-10-05 14:19:11 +02:00
|
|
|
stats, err := agg.database.GetSiteStats(siteID, t)
|
2018-05-15 13:30:37 +02:00
|
|
|
if err != nil && err != datastore.ErrNoResults {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if stats == nil {
|
|
|
|
stats = &models.SiteStats{
|
2018-10-05 14:19:11 +02:00
|
|
|
SiteID: siteID,
|
|
|
|
New: true,
|
2018-10-05 14:34:39 +02:00
|
|
|
Date: t,
|
2018-05-15 13:30:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 14:19:11 +02:00
|
|
|
r.Sites[cacheKey] = stats
|
2018-05-15 13:30:37 +02:00
|
|
|
return stats, nil
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:02:32 +02:00
|
|
|
func (agg *Aggregator) getPageStats(r *results, siteID int64, t time.Time, hostname string, pathname string) (*models.PageStats, error) {
|
2018-11-13 15:23:18 +01:00
|
|
|
cacheKey := fmt.Sprintf("%d-%s-%s-%s", siteID, t.Format("2006-01-02T15"), hostname, pathname)
|
2018-10-05 14:19:11 +02:00
|
|
|
if stats, ok := r.Pages[cacheKey]; ok {
|
2018-05-15 13:30:37 +02:00
|
|
|
return stats, nil
|
|
|
|
}
|
|
|
|
|
2018-11-12 14:45:21 +01:00
|
|
|
hostnameID, err := agg.database.HostnameID(hostname)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pathnameID, err := agg.database.PathnameID(pathname)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
stats, err := agg.database.GetPageStats(siteID, t, hostnameID, pathnameID)
|
2018-05-15 13:30:37 +02:00
|
|
|
if err != nil && err != datastore.ErrNoResults {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if stats == nil {
|
|
|
|
stats = &models.PageStats{
|
2018-11-12 14:45:21 +01:00
|
|
|
SiteID: siteID,
|
|
|
|
New: true,
|
|
|
|
HostnameID: hostnameID,
|
|
|
|
PathnameID: pathnameID,
|
|
|
|
Date: t,
|
2018-05-15 13:30:37 +02:00
|
|
|
}
|
2018-10-05 14:19:11 +02:00
|
|
|
|
2018-05-15 13:30:37 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 14:19:11 +02:00
|
|
|
r.Pages[cacheKey] = stats
|
2018-05-15 13:30:37 +02:00
|
|
|
return stats, nil
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:02:32 +02:00
|
|
|
func (agg *Aggregator) getReferrerStats(r *results, siteID int64, t time.Time, hostname string, pathname string) (*models.ReferrerStats, error) {
|
2018-11-13 15:23:18 +01:00
|
|
|
cacheKey := fmt.Sprintf("%d-%s-%s-%s", siteID, t.Format("2006-01-02T15"), hostname, pathname)
|
2018-10-05 14:19:11 +02:00
|
|
|
if stats, ok := r.Referrers[cacheKey]; ok {
|
2018-05-15 13:30:37 +02:00
|
|
|
return stats, nil
|
|
|
|
}
|
|
|
|
|
2018-11-12 14:45:21 +01:00
|
|
|
hostnameID, err := agg.database.HostnameID(hostname)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pathnameID, err := agg.database.PathnameID(pathname)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-15 13:30:37 +02:00
|
|
|
// get from db
|
2018-11-12 14:45:21 +01:00
|
|
|
stats, err := agg.database.GetReferrerStats(siteID, t, hostnameID, pathnameID)
|
2018-05-15 13:30:37 +02:00
|
|
|
if err != nil && err != datastore.ErrNoResults {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if stats == nil {
|
|
|
|
stats = &models.ReferrerStats{
|
2018-11-12 14:45:21 +01:00
|
|
|
SiteID: siteID,
|
|
|
|
New: true,
|
|
|
|
HostnameID: hostnameID,
|
|
|
|
PathnameID: pathnameID,
|
|
|
|
Date: t,
|
|
|
|
Group: "",
|
2018-05-15 13:30:37 +02:00
|
|
|
}
|
2018-06-01 11:44:20 +02:00
|
|
|
|
2018-11-12 14:45:21 +01:00
|
|
|
// TODO: Abstract this so we can add more groupings
|
|
|
|
if strings.Contains(hostname, "www.google.") {
|
2018-06-01 11:44:20 +02:00
|
|
|
stats.Group = "Google"
|
|
|
|
}
|
2018-05-15 13:30:37 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 14:19:11 +02:00
|
|
|
r.Referrers[cacheKey] = stats
|
2018-05-15 13:30:37 +02:00
|
|
|
return stats, nil
|
|
|
|
}
|