我使用的是 gem 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个查询,并且都返回正确的结果。
即,当查询涵盖具有2个不同时区的记录时,
groupdate
无法计算最后一个记录的记录。我想确保这是一个groupdate gem bug。还是我错过了什么?
最佳答案
我相信这是因为您正在将两个convert_tz之间的HH:mm:ss清零
请为“day”变量更新您的SELECT:
然后将GROUP BY值替换为:day-或者您可以将以上更改复制粘贴到group by中