Skip to content
Permalink
Browse files

fix aggregate queries for postgresql

  • Loading branch information...
dannyvankooten committed Nov 14, 2018
1 parent dfa56cc commit da87d2a173aa95d90632808cbf600a73265c8191
Showing with 11 additions and 11 deletions.
  1. +5 −5 pkg/datastore/sqlstore/page_stats.go
  2. +6 −6 pkg/datastore/sqlstore/referrer_stats.go
@@ -43,11 +43,11 @@ func (db *sqlstore) SelectAggregatedPageStats(siteID int64, startDate time.Time,
query := db.Rebind(`SELECT
h.name AS hostname,
p.name AS pathname,
MAX(SUM(pageviews), 1) AS pageviews,
MAX(SUM(visitors), 1) AS visitors,
SUM(pageviews) AS pageviews,
SUM(visitors) AS visitors,
SUM(entries) AS entries,
COALESCE(SUM(entries*bounce_rate) / SUM(entries), 0.00) AS bounce_rate,
SUM(pageviews*avg_duration) / SUM(pageviews) AS avg_duration
COALESCE(SUM(entries*bounce_rate) / NULLIF(SUM(entries), 0), 0.00) AS bounce_rate,
COALESCE(SUM(pageviews*avg_duration) / SUM(pageviews), 0.00) AS avg_duration
FROM page_stats s
LEFT JOIN hostnames h ON h.id = s.hostname_id
LEFT JOIN pathnames p ON p.id = s.pathname_id
@@ -60,7 +60,7 @@ func (db *sqlstore) SelectAggregatedPageStats(siteID int64, startDate time.Time,

func (db *sqlstore) GetAggregatedPageStatsPageviews(siteID int64, startDate time.Time, endDate time.Time) (int64, error) {
var result int64
query := db.Rebind(`SELECT SUM(pageviews) FROM page_stats WHERE site_id = ? AND ts >= ? AND ts <= ?`)
query := db.Rebind(`SELECT COALESCE(SUM(pageviews), 0) FROM page_stats WHERE site_id = ? AND ts >= ? AND ts <= ?`)
err := db.Get(&result, query, siteID, startDate.Format(DATE_FORMAT), endDate.Format(DATE_FORMAT))
return result, err
}
@@ -42,9 +42,9 @@ func (db *sqlstore) SelectAggregatedReferrerStats(siteID int64, startDate time.T
var result []*models.ReferrerStats

sql := `SELECT
h.name AS hostname,
p.name AS pathname,
COALESCE(groupname, '') AS groupname,
MIN(h.name) AS hostname,
MIN(p.name) AS pathname,
COALESCE(MIN(groupname), '') AS groupname,
SUM(visitors) AS visitors,
SUM(pageviews) AS pageviews,
SUM(pageviews*bounce_rate) / SUM(pageviews) AS bounce_rate,
@@ -55,9 +55,9 @@ func (db *sqlstore) SelectAggregatedReferrerStats(siteID int64, startDate time.T
WHERE site_id = ? AND ts >= ? AND ts <= ? `

if db.Config.Driver == "sqlite3" {
sql = sql + `GROUP BY COALESCE(NULLIF(groupname, ''), hostname || pathname ) `
sql = sql + `GROUP BY COALESCE(NULLIF(groupname, ''), hostname_id || pathname_id ) `
} else {
sql = sql + `GROUP BY COALESCE(NULLIF(groupname, ''), CONCAT(hostname, pathname) ) `
sql = sql + `GROUP BY COALESCE(NULLIF(groupname, ''), CONCAT(hostname_id, pathname_id) ) `
}
sql = sql + ` ORDER BY pageviews DESC LIMIT ?`

@@ -69,7 +69,7 @@ func (db *sqlstore) SelectAggregatedReferrerStats(siteID int64, startDate time.T

func (db *sqlstore) GetAggregatedReferrerStatsPageviews(siteID int64, startDate time.Time, endDate time.Time) (int64, error) {
var result int64
query := db.Rebind(`SELECT SUM(pageviews) FROM referrer_stats WHERE site_id = ? AND ts >= ? AND ts <= ?`)
query := db.Rebind(`SELECT COALESCE(SUM(pageviews), 0) FROM referrer_stats WHERE site_id = ? AND ts >= ? AND ts <= ?`)
err := db.Get(&result, query, siteID, startDate.Format(DATE_FORMAT), endDate.Format(DATE_FORMAT))
return result, mapError(err)
}

0 comments on commit da87d2a

Please sign in to comment.
You can’t perform that action at this time.