Skip to content

Commit

Permalink
don't COALESCE when it's unnecessary
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyvankooten committed Nov 14, 2018
1 parent 5610a0a commit 7ccdea4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
8 changes: 4 additions & 4 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) GetAggregatedPageStats(siteID int64, startDate time.Time, en
query := db.Rebind(`SELECT
h.name AS hostname,
p.name AS pathname,
SUM(pageviews) AS pageviews,
SUM(visitors) AS visitors,
MAX(SUM(pageviews), 1) AS pageviews,
MAX(SUM(visitors), 1) AS visitors,
SUM(entries) AS entries,
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
COALESCE(SUM(entries*bounce_rate) / SUM(entries), 0.00) AS bounce_rate,
SUM(pageviews*avg_duration) / SUM(pageviews) 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 Down
4 changes: 2 additions & 2 deletions pkg/datastore/sqlstore/referrer_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func (db *sqlstore) GetAggregatedReferrerStats(siteID int64, startDate time.Time
COALESCE(groupname, '') AS groupname,
SUM(visitors) AS visitors,
SUM(pageviews) AS pageviews,
COALESCE(SUM(pageviews*NULLIF(bounce_rate, 0)) / SUM(pageviews), 0.00) AS bounce_rate,
COALESCE(SUM(pageviews*avg_duration) / SUM(pageviews), 0.00) AS avg_duration
SUM(pageviews*bounce_rate) / SUM(pageviews) AS bounce_rate,
SUM(pageviews*avg_duration) / SUM(pageviews) AS avg_duration
FROM referrer_stats s
LEFT JOIN hostnames h ON h.id = s.hostname_id
LEFT JOIN pathnames p ON p.id = s.pathname_id
Expand Down
15 changes: 9 additions & 6 deletions pkg/datastore/sqlstore/site_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,22 @@ func (db *sqlstore) updateSiteStats(s *models.SiteStats) error {

func (db *sqlstore) GetSiteStatsPerDay(siteID int64, startDate time.Time, endDate time.Time) ([]*models.SiteStats, error) {
results := []*models.SiteStats{}
query := db.Rebind(`SELECT * FROM site_stats WHERE site_id = ? AND ts >= ? AND ts <= ? ORDER BY ts DESC`)
query := db.Rebind(`SELECT *
FROM site_stats
WHERE site_id = ? AND ts >= ? AND ts <= ?
ORDER BY ts DESC`)
err := db.Select(&results, query, siteID, startDate.Format(DATE_FORMAT), endDate.Format(DATE_FORMAT))
return results, err
}

func (db *sqlstore) GetAggregatedSiteStats(siteID int64, startDate time.Time, endDate time.Time) (*models.SiteStats, error) {
stats := &models.SiteStats{}
query := db.Rebind(`SELECT
COALESCE(SUM(pageviews), 0) AS pageviews,
COALESCE(SUM(visitors), 0) AS visitors,
COALESCE(SUM(sessions), 0) AS sessions,
COALESCE(SUM(pageviews*avg_duration) / NULLIF(SUM(pageviews), 0), 0.00) AS avg_duration,
COALESCE(SUM(sessions*bounce_rate) / NULLIF(SUM(sessions), 0), 0.00) AS bounce_rate
SUM(pageviews) AS pageviews,
SUM(visitors) AS visitors,
SUM(sessions) AS sessions,
SUM(pageviews*avg_duration) / SUM(pageviews) AS avg_duration,
COALESCE(SUM(sessions*bounce_rate) / SUM(sessions), 0.00) AS bounce_rate
FROM site_stats
WHERE site_id = ? AND ts >= ? AND ts <= ? LIMIT 1`)
err := db.Get(stats, query, siteID, startDate.Format(DATE_FORMAT), endDate.Format(DATE_FORMAT))
Expand Down

0 comments on commit 7ccdea4

Please sign in to comment.