WIP refactor the query to get the data for the graphs