Fixed heatmap not working in mssql (#5248)

This commit is contained in:
kolaente 2018-11-01 19:12:17 +01:00 committed by techknowlogick
parent fa165621ed
commit 225c48982b

View File

@ -19,6 +19,7 @@ type UserHeatmapData struct {
func GetUserHeatmapDataByUser(user *User) ([]*UserHeatmapData, error) { func GetUserHeatmapDataByUser(user *User) ([]*UserHeatmapData, error) {
hdata := make([]*UserHeatmapData, 0) hdata := make([]*UserHeatmapData, 0)
var groupBy string var groupBy string
var groupByName = "timestamp" // We need this extra case because mssql doesn't allow grouping by alias
switch { switch {
case setting.UseSQLite3: case setting.UseSQLite3:
groupBy = "strftime('%s', strftime('%Y-%m-%d', created_unix, 'unixepoch'))" groupBy = "strftime('%s', strftime('%Y-%m-%d', created_unix, 'unixepoch'))"
@ -28,13 +29,14 @@ func GetUserHeatmapDataByUser(user *User) ([]*UserHeatmapData, error) {
groupBy = "extract(epoch from date_trunc('day', to_timestamp(created_unix)))" groupBy = "extract(epoch from date_trunc('day', to_timestamp(created_unix)))"
case setting.UseMSSQL: case setting.UseMSSQL:
groupBy = "dateadd(DAY,0, datediff(day,0, dateadd(s, created_unix, '19700101')))" groupBy = "dateadd(DAY,0, datediff(day,0, dateadd(s, created_unix, '19700101')))"
groupByName = groupBy
} }
err := x.Select(groupBy+" as timestamp, count(user_id) as contributions"). err := x.Select(groupBy+" AS timestamp, count(user_id) as contributions").
Table("action"). Table("action").
Where("user_id = ?", user.ID). Where("user_id = ?", user.ID).
And("created_unix > ?", (util.TimeStampNow() - 31536000)). And("created_unix > ?", (util.TimeStampNow() - 31536000)).
GroupBy("timestamp"). GroupBy(groupByName).
OrderBy("timestamp"). OrderBy("timestamp").
Find(&hdata) Find(&hdata)
return hdata, err return hdata, err