Skip to content

Commit

Permalink
fix aggregate queries for postgresql
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyvankooten committed Nov 14, 2018
1 parent dfa56cc commit da87d2a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions pkg/datastore/sqlstore/page_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
12 changes: 6 additions & 6 deletions pkg/datastore/sqlstore/referrer_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 ?`

Expand All @@ -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.