我正在使用宝石 ahoy 来跟踪访问次数,并使用 groupdate 来显示每日统计信息。
要计算每日访问次数,我在Visit
模型中使用此查询:
Visit.group_by_day(:started_at).order('started_at').count
直到最后一天18,当我的时区从BRT( - 03:00)变为BRST(-02:00)时,它工作正常。
现在,上述查询会在18:
之后的每一天返回0
次访问次数
2.1.3 :026 > puts Visit.group_by_day(:started_at).order('started_at desc').limit(10).count.to_yaml
(0.3ms) SELECT COUNT(*) AS count_all, DATE_ADD(CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(DATE_SUB(started_at, INTERVAL 0 HOUR), '+00:00', 'America/Sao_Paulo'), '%Y-%m-%d 00:00:00'), 'America/Sao_Paulo', '+00:00'), INTERVAL 0 HOUR) AS day FROM `visits` WHERE (started_at IS NOT NULL) GROUP BY DATE_ADD(CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(DATE_SUB(started_at, INTERVAL 0 HOUR), '+00:00', 'America/Sao_Paulo'), '%Y-%m-%d 00:00:00'), 'America/Sao_Paulo', '+00:00'), INTERVAL 0 HOUR) ORDER BY started_at desc LIMIT 10
---
2015-10-14 03:00:00.000000000 Z: 158
2015-10-15 03:00:00.000000000 Z: 127
2015-10-16 03:00:00.000000000 Z: 101
2015-10-17 03:00:00.000000000 Z: 112
2015-10-18 03:00:00.000000000 Z: 111
2015-10-19 03:00:00.000000000 Z: 0
2015-10-20 03:00:00.000000000 Z: 0
2015-10-21 03:00:00.000000000 Z: 0
2015-10-22 03:00:00.000000000 Z: 0
但是,如果我只选择最近5天(全部都在BRST时区),它可以工作:
2.1.3 :027 > puts Visit.group_by_day(:started_at).order('started_at desc').limit(5).count.to_yaml
(0.3ms) SELECT COUNT(*) AS count_all, DATE_ADD(CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(DATE_SUB(started_at, INTERVAL 0 HOUR), '+00:00', 'America/Sao_Paulo'), '%Y-%m-%d 00:00:00'), 'America/Sao_Paulo', '+00:00'), INTERVAL 0 HOUR) AS day FROM `visits` WHERE (started_at IS NOT NULL) GROUP BY DATE_ADD(CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(DATE_SUB(started_at, INTERVAL 0 HOUR), '+00:00', 'America/Sao_Paulo'), '%Y-%m-%d 00:00:00'), 'America/Sao_Paulo', '+00:00'), INTERVAL 0 HOUR) ORDER BY started_at desc LIMIT 5
---
2015-10-19 02:00:00.000000000 Z: 85
2015-10-20 02:00:00.000000000 Z: 72
2015-10-21 02:00:00.000000000 Z: 84
2015-10-22 02:00:00.000000000 Z: 80
2015-10-23 02:00:00.000000000 Z: 21
我直接在mysql服务器上完成了这两个查询,两者都返回了正确的结果。
即,当查询涵盖具有2个不同时区的记录时,groupdate
无法计算最后一个记录。
我想确保这是一个groupdate gem bug。或者我错过了什么?
答案 0 :(得分:1)
我相信这是因为你正在将两个convert_tz之间的HH:mm:ss归零
请为'day'变量更新SELECT:
然后将:GROUP BY值替换为:day - 或者您可以通过
将上述更改复制粘贴到组中