cachet: SQL errors with Metrics rendering in Cachet 2.4-dev and Postgres 10.
Fresh Cachet 2.4-dev install with PostgreSQL 10 (and a table prefix of cachet_). Any attempts to have a Metric item’s graph displayed have an image loading placeholder, and the Laravel log fills up with this:
LINE 1: SELECT to_char(cachet_metric_points.created_at, 'YYYY-MM-DD ...
^ (SQL: SELECT to_char(cachet_metric_points.created_at, 'YYYY-MM-DD HH24:00') AS key, sum(cachet_metric_points.value * cachet_metric_points.counter) AS value FROM cachet_metrics INNER JOIN cachet_metric_points ON cachet_metrics.id = cachet_metric_points.metric_id WHERE cachet_metrics.id = :metricId AND cachet_metric_points.created_at >= (NOW() - INTERVAL '12' HOUR) AND cachet_metric_points.created_at <= NOW() GROUP BY to_char(cachet_metric_points.created_at, 'HH24:00') ORDER BY to_char(cachet_metric_points.created_at, 'HH24:00')) in /var/www/Cachet/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
There is a full stack trace, but the problem is in the SQL, so I’m omitting it.
There’s two separate problems here.
The first is that with recent PostgreSQL versions, it’s not quite smart enough to identify the to_char() call as a key, so you have to explicitly state it in the GROUP BY clause, like this:
SELECT to_char(cachet_metric_points.created_at, 'YYYY-MM-DD HH24:00') AS key, sum(cachet_metric_points.value * cachet_metric_points.counter) AS value FROM cachet_metrics INNER JOIN cachet_metric_points ON cachet_metrics.id = cachet_metric_points.metric_id WHERE cachet_metrics.id = <INSERT_ID_HERE> AND cachet_metric_points.created_at >= (NOW() - INTERVAL '12' HOUR) AND cachet_metric_points.created_at <= NOW() GROUP BY to_char(cachet_metric_points.created_at, 'HH24:00'), cachet_metric_points.created_at ORDER BY to_char(cachet_metric_points.created_at, 'HH24:00');
That’s an easy fix.
But there’s another problem which is preventing it from returning data. This system that I’m running it on is freshly installed, and I only just created data this morning, so the created_at values are very recent:
SELECT * FROM cachet_metric_points;
id | metric_id | value | created_at | updated_at | counter
----+-----------+--------+---------------------+---------------------+---------
1 | 1 | 42.000 | 2018-09-24 18:15:30 | 2018-09-24 12:15:57 | 3
2 | 1 | 38.000 | 2018-09-24 18:50:30 | 2018-09-24 12:50:43 | 1
3 | 1 | 37.000 | 2018-09-24 18:50:30 | 2018-09-24 12:50:46 | 2
4 | 1 | 40.000 | 2018-09-24 18:51:00 | 2018-09-24 12:50:47 | 1
5 | 1 | 35.000 | 2018-09-24 18:51:00 | 2018-09-24 12:50:48 | 1
(5 rows)
created_at appears stored in UTC and updated_at is stored at local time, which makes the <= NOW() part of the WHERE clause clobber those data points. This may be because I’m passing in data points with a local epoch time (that’s Central, while the server is Mountain)… but I don’t think that would (should?) cause issues like that. Either way, the results of my query that I added the GROUP BY fix to are blank, and that doesn’t seem right.
Eventually, of course, time will move past that created_at value, and it should start returning results, but this seems a bit off to me.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 2
- Comments: 20 (3 by maintainers)
We’re using the fix from @viraja1 https://github.com/CachetHQ/Cachet/pull/3830 and it seems to work great, would it be possible to merge this so we don’t have to actually run a fork?
@si458 If you are asking for a fix in the official docker image (which isn’t unreasonable), that I don’t know. I’ll defer to the Cachet repo members for that one.
If you would like something now that somewhat works, the fix I posted above does seem to work for me (see the diff I posted). You can fork Cachet and add it yourself. Then change your Dockerfile to point to your forked repo.
One caveat with this is I only got everything working with uploading in my local timezone. I could not get upload in UTC working with displaying in my local timezone. I also ran into a snag with daylight savings during the beginning of November, but that was my fault. I also had to setup my local timezone to work in all my containers.
So if you use the fix I posted above you should at least be able to display in whatever timezone you upload. This is currently sufficient for my needs, but does involve a bit of work to get working (namely the fix in my diff and passing through my local timezone) and I understand if you would like to just have it “work”.
Yes I tried to upload my metrics in my current time zone, but that had adverse affects. My Last hour graph displayed nothing, and my 12 hours graph only displayed the most recent 7 hours (though they were correct). This probably has something to do with UTC and my timezone being in PDT (7 hours behind).
My current “fix”, which isn’t really a fix, is to just upload everything in UTC and display in UTC. That seems to work just fine. I will look more into getting the timezone to display correctly and see if I can figure it out.
Just wanted to post that I got this working with Cachet 2.4.0 dev and Postgres 12 all in docker containers. See my diff here.
Please note I am not a php developer, like at all. I did these changes through trial and error based off @Puyodead1’s comment above. I will lastly mention my “Last Hour” graph is delayed by 10 minutes, but that’s a separate issue (expected? Idk…).
-Joe