HeatMap by Day's and hours (not date)

I almost have a working heatmap that splits hourly occourances by day (not date). I cannot figure out how to smush the graph together so it looks as I need it too, anyone have an idea? Why is this not an option yet, why can we not declare our own x’s and y’s on all the built in charts, only controling x is so frustrating :frowning: .

My time slice is selected for the last 90 days

  • What Grafana version and what operating system are you using?
    9.5.1
    Below is the code I am using with a Status history graph.

SELECT
Created
, SUM(CASE WHEN EXTRACT(HOUR FROM created) = β€˜0’ THEN 1 ELSE 0 END) AS β€˜00:00’
, SUM(CASE WHEN EXTRACT(HOUR FROM created) = β€˜1’ THEN 1 ELSE 0 END) AS β€˜01:00’
, SUM(CASE WHEN EXTRACT(HOUR FROM created) = β€˜2’ THEN 1 ELSE 0 END) AS β€˜02:00’
, SUM(CASE WHEN EXTRACT(HOUR FROM created) = β€˜3’ THEN 1 ELSE 0 END) AS β€˜03:00’
, SUM(CASE WHEN EXTRACT(HOUR FROM created) = β€˜4’ THEN 1 ELSE 0 END) AS β€˜04:00’
, SUM(CASE WHEN EXTRACT(HOUR FROM created) = β€˜5’ THEN 1 ELSE 0 END) AS β€˜05:00’
, SUM(CASE WHEN EXTRACT(HOUR FROM created) = β€˜6’ THEN 1 ELSE 0 END) AS β€˜06:00’
, SUM(CASE WHEN EXTRACT(HOUR FROM created) = β€˜7’ THEN 1 ELSE 0 END) AS β€˜07:00’
, SUM(CASE WHEN EXTRACT(HOUR FROM created) = β€˜8’ THEN 1 ELSE 0 END) AS β€˜08:00’
, SUM(CASE WHEN EXTRACT(HOUR FROM created) = β€˜9’ THEN 1 ELSE 0 END) AS β€˜09:00’
, SUM(CASE WHEN EXTRACT(HOUR FROM created) = β€˜10’ THEN 1 ELSE 0 END) AS β€˜10:00’
, SUM(CASE WHEN EXTRACT(HOUR FROM created) = β€˜11’ THEN 1 ELSE 0 END) AS β€˜11:00’
, SUM(CASE WHEN EXTRACT(HOUR FROM created) = β€˜12’ THEN 1 ELSE 0 END) AS β€˜12:00’
, SUM(CASE WHEN EXTRACT(HOUR FROM created) = β€˜13’ THEN 1 ELSE 0 END) AS β€˜13:00’
, SUM(CASE WHEN EXTRACT(HOUR FROM created) = β€˜14’ THEN 1 ELSE 0 END) AS β€˜14:00’
, SUM(CASE WHEN EXTRACT(HOUR FROM created) = β€˜15’ THEN 1 ELSE 0 END) AS β€˜15:00’
, SUM(CASE WHEN EXTRACT(HOUR FROM created) = β€˜16’ THEN 1 ELSE 0 END) AS β€˜16:00’
, SUM(CASE WHEN EXTRACT(HOUR FROM created) = β€˜17’ THEN 1 ELSE 0 END) AS β€˜17:00’
, SUM(CASE WHEN EXTRACT(HOUR FROM created) = β€˜18’ THEN 1 ELSE 0 END) AS β€˜18:00’
, SUM(CASE WHEN EXTRACT(HOUR FROM created) = β€˜19’ THEN 1 ELSE 0 END) AS β€˜19:00’
, SUM(CASE WHEN EXTRACT(HOUR FROM created) = β€˜20’ THEN 1 ELSE 0 END) AS β€˜20:00’
, SUM(CASE WHEN EXTRACT(HOUR FROM created) = β€˜21’ THEN 1 ELSE 0 END) AS β€˜21:00’
, SUM(CASE WHEN EXTRACT(HOUR FROM created) = β€˜22’ THEN 1 ELSE 0 END) AS β€˜22:00’
, SUM(CASE WHEN EXTRACT(HOUR FROM created) = β€˜23’ THEN 1 ELSE 0 END) AS β€˜23:00’
, CASE DAYOFWEEK(created)
WHEN 1 THEN β€˜Sunday’
WHEN 2 THEN β€˜Monday’
WHEN 3 THEN β€˜Tuesday’
WHEN 4 THEN β€˜Wednesday’
WHEN 5 THEN β€˜Thursday’
WHEN 6 THEN β€˜Friday’
WHEN 7 THEN β€˜Saturday’
END AS day
FROM table
WHERE TenantId = β€˜${TenantId}’
AND $__timeFilter(Created)
Group BY DAYOFWEEK(Created)

From your screenshot it looks like you are using the β€˜Status History’ Panel?
What happens if you use the β€˜State timeline’ instead and settings from below.

Image 73


Its practically the same. And I cannot for the life of me figure out why its reversing Saturday and Sunday in all of the visulazations lol.

That wasn’t the solution i guess. :sweat_smile:
Possible to make a screenshot of the β€˜table view’ ?

As for the Saturday and Sunday. Cant you use Transform β†’ Organize fields for that?
Or Transform β†’ Sort By?


Theres part of the table view, and the other end:
image

As far as organize:
image
Day is not split out like hours are

I think the problem lies in the column β€˜Created’. Which contains dates + times.
As I understood correctly from your post. your main goal was to get insight for the weekdays (of your selection)

I don’t know how it works with SQL but maybe this can still help you. I’m using influx myself. and there is a command drop() function. Which i use at the very end of my query.

Link: drop() function | Flux 0.x Documentation

So you have the values for all the days (of your range) but not the timestamps which triggers the β€˜Status History’ panel. If that makes any sense.

Maybe you want more control by using this plugin?

or this

What plugin is this? Is it installed via JSON? the way our instance is setup, we cannot install plugins that are not just JSON :frowning:

not sure what that means installed via json? Can you show us this method. the 2 plugin I showed you are from Volkov Labs @mikhailvolkov

Dynamic Text

Apache eChart

1 Like

I did some testing and used a regular β€˜table panel’ in the end with some styling for the cells (background color) for this approach. For this I grepped the β€˜time’ part from the full date + time structure and converted this to strings like β€œ01:00:00” β€œ02:00:00” etc… and dropped the β€œ_time” column at the end so there is no official time column present anymore.

The example on the right is based on the β€œHourly Heatmap” panel and uses the β€œ_time” column based on dateTime:RFC3339 as input

For better viewing right click on the image and open in β€˜new tab’


heatmap_vs_heatmap

2 Likes

Thank you for this idea! I have gotten a table setup like the example on the left, I am however having a hard time getting the days of the week to be the X (listed on top of the chart) with the hours on the Y. May I ask how you achieved this? I seem to only be able to get the hours on the X and days on the Y.

1 Like

@tekwhat
I will check this for you tonight

1 Like

I finally got the solution

SELECT
CASE EXTRACT(HOUR FROM (CONVERT_TZ(created, β€˜+00:00’, β€˜-04:00’)))
WHEN 0 THEN β€˜00:00’
WHEN 1 THEN β€˜01:00’
WHEN 2 THEN β€˜02:00’
WHEN 3 THEN β€˜03:00’
WHEN 4 THEN β€˜04:00’
WHEN 5 THEN β€˜05:00’
WHEN 6 THEN β€˜06:00’
WHEN 7 THEN β€˜07:00’
WHEN 8 THEN β€˜08:00’
WHEN 9 THEN β€˜09:00’
WHEN 10 THEN β€˜10:00’
WHEN 11 THEN β€˜11:00’
WHEN 12 THEN β€˜12:00’
WHEN 13 THEN β€˜13:00’
WHEN 14 THEN β€˜14:00’
WHEN 15 THEN β€˜15:00’
WHEN 16 THEN β€˜16:00’
WHEN 17 THEN β€˜17:00’
WHEN 18 THEN β€˜18:00’
WHEN 19 THEN β€˜19:00’
WHEN 20 THEN β€˜20:00’
WHEN 21 THEN β€˜21:00’
WHEN 22 THEN β€˜22:00’
WHEN 23 THEN β€˜23:00’
END AS Hours

, SUM(CASE WHEN DAYOFWEEK(created) = 1 THEN 1 ELSE 0 END) AS β€˜Sunday’
, SUM(CASE WHEN DAYOFWEEK(created) = 2 THEN 1 ELSE 0 END) AS β€˜Monday’
, SUM(CASE WHEN DAYOFWEEK(created) = 3 THEN 1 ELSE 0 END) AS β€˜Tuesday’
, SUM(CASE WHEN DAYOFWEEK(created) = 4 THEN 1 ELSE 0 END) AS β€˜Wednesday’
, SUM(CASE WHEN DAYOFWEEK(created) = 5 THEN 1 ELSE 0 END) AS β€˜Thursday’
, SUM(CASE WHEN DAYOFWEEK(created) = 6 THEN 1 ELSE 0 END) AS β€˜Friday’
, SUM(CASE WHEN DAYOFWEEK(created) = 7 THEN 1 ELSE 0 END) AS β€˜Saturday’

Group by Hours
Order by Hours

1 Like

I just wanted to come back here and share some stuff I will start a new post for another question pertaining to this.

A co-worker and I were able to get a dummy table in grafana.

It must be the first query:

SELECT

CASE

    WHEN hr = 0 THEN '12:00 AM'

    ELSE CAST(hr AS CHAR)

END AS 'Time Bucket',

MAX(CASE WHEN day = 'Monday' THEN 0 ELSE NULL END) AS `Monday`,

MAX(CASE WHEN day = 'Tuesday' THEN 0 ELSE NULL END) AS `Tuesday`,

MAX(CASE WHEN day = 'Wednesday' THEN 0 ELSE NULL END) AS `Wednesday`,

MAX(CASE WHEN day = 'Thursday' THEN 0 ELSE NULL END) AS `Thursday`,

MAX(CASE WHEN day = 'Friday' THEN 0 ELSE NULL END) AS `Friday`,

MAX(CASE WHEN day = 'Saturday' THEN 0 ELSE NULL END) AS `Saturday`,

MAX(CASE WHEN day = 'Sunday' THEN 0 ELSE NULL END) AS `Sunday`

FROM (

SELECT '12:00 AM' AS hr UNION SELECT '1:00 AM' UNION SELECT '2:00 AM' UNION SELECT '3:00 AM' UNION

SELECT '4:00 AM' UNION SELECT '5:00 AM' UNION SELECT '6:00 AM' UNION SELECT '7:00 AM' UNION

SELECT '8:00 AM' UNION SELECT '9:00 AM' UNION SELECT '10:00 AM' UNION SELECT '11:00 AM' UNION

SELECT '12:00 PM' UNION SELECT '1:00 PM' UNION SELECT '2:00 PM' UNION SELECT '3:00 PM' UNION

SELECT '4:00 PM' UNION SELECT '5:00 PM' UNION SELECT '6:00 PM' UNION SELECT '7:00 PM' UNION

SELECT '8:00 PM' UNION SELECT '9:00 PM' UNION SELECT '10:00 PM' UNION SELECT '11:00 PM'

) AS hours

CROSS JOIN (

SELECT 'Monday' AS day UNION SELECT 'Tuesday' UNION SELECT 'Wednesday' UNION

SELECT 'Thursday' UNION SELECT 'Friday' UNION SELECT 'Saturday' UNION SELECT 'Sunday'

) AS days

GROUP BY hr
ORDER BY STR_TO_DATE(hr, β€˜%l:00 %p’);

Then your second query is your data:

SELECT all_hours.est_hour as β€˜Time Bucket’,
COALESCE(SUM(CASE WHEN DAYOFWEEK(CONVERT_TZ(created, β€˜+00:00’, β€˜-04:00’)) = 1 THEN 1 ELSE 0 END), 0) AS Sunday,
COALESCE(SUM(CASE WHEN DAYOFWEEK(CONVERT_TZ(created, β€˜+00:00’, β€˜-04:00’)) = 2 THEN 1 ELSE 0 END), 0) AS Monday,
COALESCE(SUM(CASE WHEN DAYOFWEEK(CONVERT_TZ(created, β€˜+00:00’, β€˜-04:00’)) = 3 THEN 1 ELSE 0 END), 0) AS Tuesday,
COALESCE(SUM(CASE WHEN DAYOFWEEK(CONVERT_TZ(created, β€˜+00:00’, β€˜-04:00’)) = 4 THEN 1 ELSE 0 END), 0) AS Wednesday,
COALESCE(SUM(CASE WHEN DAYOFWEEK(CONVERT_TZ(created, β€˜+00:00’, β€˜-04:00’)) = 5 THEN 1 ELSE 0 END), 0) AS Thursday,
COALESCE(SUM(CASE WHEN DAYOFWEEK(CONVERT_TZ(created, β€˜+00:00’, β€˜-04:00’)) = 6 THEN 1 ELSE 0 END), 0) AS Friday,
COALESCE(SUM(CASE WHEN DAYOFWEEK(CONVERT_TZ(created, β€˜+00:00’, β€˜-04:00’)) = 7 THEN 1 ELSE 0 END), 0) AS Saturday
FROM (
SELECT DISTINCT coalesce(DATE_FORMAT(CONVERT_TZ(created, β€˜+00:00’, β€˜-04:00’), β€˜%l:00 %p’)) AS est_hour
FROM β€˜table’
) all_hours
LEFT JOIN β€˜table’ ON all_hours.est_hour = DATE_FORMAT(CONVERT_TZ(created, β€˜+00:00’, β€˜-04:00’), β€˜%l:00 %p’)
WHERE $__timeFilter(created)
GROUP BY coalesce(all_hours.est_hour)
ORDER BY STR_TO_DATE(all_hours.est_hour,β€˜%l:00 %p’) asc;

At this point, we have to go to transforms:

Join the two tables then merge the corrisponding day’s:

Then use orginize and hide everything but your hours and the combined day’s:

Finally rename your combined day’s
image

This should give you a heatmap, with all hours/days reperesented reguardless of data or not.

3 Likes

Thanks for providing this explanation. I would like to know which panel did you use to create the heatmap. I tried the built in one from Grafana, but it wouldn’t work.

You should be able to achieve this with the normal β€˜Table’ Panel.
You can style the different cells with colors depending on value or state.

e.g. Value mapping, Tresholds or Field Overide.

1 Like

Thanks it works now.

2 Likes

great!

1 Like