My goal is to generate a report showing the average occupancy of a garage (y-axis) at a given day of the week and/or time of day. My data model is as follows:
- Garage
has_many
Cars and Garagehas_many
Appointments,through: :cars
- Car
has_many
Appointments - Appointment has fields such as:
- picked_up_at (datetime)
- returned_at (datetime)
Also, Garage has a field capacity (integer)
, which is the maximum number of cars that will fit in the garage.
If I have a list of Appointments spanning the last 6 months, and I would like to generate a line-graph with the x-axis showing each day of the week, broken down into 4-hour intervals, and the y-axis showing the average % occupancy (# of cars in the garage / capacity) over the 6 month period for the given day/hour interval, how can I go about gathering this data to report on?
E.g. a car is In
from the time of one Appointment's return until the next Appointment's pickup, and Out
from the Appointment's pickup until it's returned_at
time.
I am having a lot of trouble making the connection from these data points to the best way to meaningfully report on and present them to the end user.
I am using Rails 4.1 and Ruby 2.0.
Edit: SQL Fiddle - http://sqlfiddle.com/#!9/a72fe/1