-
Notifications
You must be signed in to change notification settings - Fork 12k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Postgres stacked bar graph panel with $__timeGroup has incorrect graph data #10073
Comments
The problem is you are not resetting the series. For every interval where a series has no data you need to insert a row for that series with NULL or 0. Your query should produce something like the following query SELECT * FROM (
SELECT 1512379200 as time, 'CO1' as metric, 100.0 UNION
SELECT 1512379800 as time, 'CO1' as metric, NULL UNION
SELECT 1512380400 as time, 'CO1' as metric, NULL UNION
SELECT 1512381000 as time, 'CO1' as metric, 93 UNION
SELECT 1512379200 as time, 'CO2' as metric, 20 UNION
SELECT 1512379800 as time, 'CO2' as metric, NULL UNION
SELECT 1512380400 as time, 'CO2' as metric, NULL UNION
SELECT 1512381000 as time, 'CO2' as metric, NULL UNION
SELECT 1512379200 as time, 'CO3' as metric, NULL UNION
SELECT 1512379800 as time, 'CO3' as metric, 47 UNION
SELECT 1512380400 as time, 'CO3' as metric, 62.5 UNION
SELECT 1512381000 as time, 'CO3' as metric, 30.5 UNION
SELECT 1512379200 as time, 'CO4' as metric, NULL UNION
SELECT 1512379800 as time, 'CO4' as metric, NULL UNION
SELECT 1512380400 as time, 'CO4' as metric, NULL UNION
SELECT 1512381000 as time, 'CO4' as metric, 37
) as purchases ORDER BY 1,2 This is certainly not optimal and this will make the query you have to write more complicated cause you have to do multiple joins to get that result. Its probably much better to generate those NULLs in the grafana backend so I'm considering implementing it there. I'm not yet sure though how to best control this behaviour. |
Right. I thought it might be something like that. I was trying to do something using coalesce to make the equivalent happen but haven't stumbled on the right solution yet. I agree that having grafana fill in the missing values would be ideal to keep the query complexity down. The equivalent we use for influxdb with a min time interval options value of ">10m" looks like
Thanks for the quick response |
This should produce the results you want/need: SELECT
base.time,
base.company as metric,
paid_price
FROM
(
SELECT
time,
company
FROM generate_series(($__unixEpochFrom()/600)::int*600,($__unixEpochTo()/600)::int*600,600) as times(time),
(SELECT distinct company from purchases) as companies
) as base
LEFT OUTER JOIN (
SELECT
$__timeGroup(purchase_datetime,'10m') as time,
company,
sum(paid_price) as paid_price
FROM purchases
WHERE
$__timeFilter(purchase_datetime)
GROUP BY 1,2
) as p ON (p.company=base.company and p.time = base.time) ORDER BY 1,2; |
Wow! This was unexpected and awesome. Thank you! I completely forgot generate_series existed. |
Great work @svenklemm Is this something we want to add to the pg data source? Having a fill macro or option would be a good addition I think. The query above would be too complicated for some people. We could add a new macro if we to preserve backwards compatability. |
I was thinking about either adding a new macro $__timeGroupFill(column,'5m',NULL) or adding a $__fill(NULL) macro which would be used in conjunction with the $__timeGroup macro. $__timeGroupFill seems like the cleaner way to implement this as you need the interval length for the filling but I'm open for other ideas. Not sure we need the fill value configurable but might aswell. |
@daniellee Alternatively I could add an optional 3rd parameter to $__timeGroup() which would be the fill value. Any preferences? |
@torkelo as you closed this - should I open a new issue for the fill macro functionality? |
Should probably add the fill function for MySQL at the same time. Ref #9487 |
I closed it as it seemed the problem was more a usage issue and not a bug / feat req |
Grafana version: 4.6.2
OS: Linux (Amazon Linux AMI)
I'm trying to replicate a graph I use from a influx datasource regularly using a postgres datasource which is a stacked bar graph with a 10m grouping.
When I view this graph however I get data outside the 10m grouping referenced in bars they shouldn't be in which results in floating values. I'll attach a screenshot in a follow up to this report.
Also if you hover over the graph data points will show values for all for metrics in each group.
Example of my postgres query/data is below:
My panel JSON is
The text was updated successfully, but these errors were encountered: