Skip to content

Commit

Permalink
Initial set of graphs/analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
francois committed Aug 4, 2016
1 parent c360979 commit b407a84
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
37 changes: 37 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,43 @@ def table_name_from_user_id(user_id)
end

get %r{\A/me/#{UUID_RE}/analytics} do |user_id|
avg_hours_slept_per_weekday_ds = DB[<<-EOSQL, table_name: table_name_from_user_id(user_id)]
SELECT
dow
, sleep_type
, avg_utc_duration
, 100.0 * avg_utc_duration / sum(avg_utc_duration) OVER (PARTITION BY sleep_type) AS pct_duration
FROM (
SELECT
dow
, sleep_type
, extract(hour FROM date_trunc('minute', avg(utc_duration))) + (extract(minute FROM date_trunc('minute', avg(utc_duration))) / 60.0) AS avg_utc_duration
FROM (
SELECT
extract(dow FROM (end_at AT TIME ZONE timezone)) dow
, sleep_type
, end_at - start_at AS utc_duration
FROM :table_name) AS t0
GROUP BY dow, sleep_type) t0
EOSQL

@avg_hours_slept_per_weekday = avg_hours_slept_per_weekday_ds.to_hash_groups([:dow, :sleep_type])
@avg_hours_slept_per_weekday = @avg_hours_slept_per_weekday.map do |key, value|
[key, value.first]
end.to_h

hours_slept_histogram_ds = DB[<<-EOSQL, table_name: table_name_from_user_id(user_id)]
SELECT sleep_type, extract(hour FROM (end_at - start_at)) AS hour, count(*)
FROM :table_name
GROUP BY 1, 2
EOSQL
@hours_slept_histogram = hours_slept_histogram_ds.to_hash_groups([:sleep_type, :hour], :count).map do |key, value|
[key, value.first]
end.to_h
@max_hours = Hash.new
@max_hours["night"] = @hours_slept_histogram.select{|k, _| k[0] == "night"}.map(&:last).compact.sort.last.to_f
@max_hours["nap"] = @hours_slept_histogram.select{|k, _| k[0] == "nap"}.map(&:last).compact.sort.last.to_f

erb :analytics
end

Expand Down
3 changes: 3 additions & 0 deletions public/css/app.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.graph-bar {
background-color: #33c;
}
50 changes: 50 additions & 0 deletions views/analytics.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<div class="row expanded">
<div class="small-12 medium-4 columns">

<% {"night" => "slept", "nap" => "napped"}.each do |key, verb| %>
<table>
<caption>Average <%= verb %> hours per day</caption>
<thead>
<tr>
<th>DOW</th>
<th>Hours</th>
<th width="100%">Graph</th>
</tr>
</thead>
<tbody>
<% (0..6).each do |dow| %>
<tr>
<td><%= %w(Sun Mon Tue Wed Thu Fri Sat)[dow] %></td>
<td><%= "%.1f" % @avg_hours_slept_per_weekday.fetch([dow.to_f, key], {}).fetch(:avg_utc_duration, 0.0) %></td>
<td>
<div class="graph-bar" style="width:<%= "%.1f" % @avg_hours_slept_per_weekday.fetch([dow.to_f, key], {}).fetch(:pct_duration, 0.0) %>%">&nbsp;</div>
</td>
</tr>
<% end %>
</tbody>
</table>

<table>
<caption>Hours <%= verb %> histogram</caption>
<thead>
<tr>
<th>Hours</th>
<th>Freq.</th>
<th width="100%">Graph</th>
</tr>
</thead>
<tbody>
<% (0..23).each do |hour| %>
<tr>
<td><%= hour %></td>
<td><%= @hours_slept_histogram.fetch([key, hour.to_f], "N/A") %></td>
<td>
<div class="graph-bar" style="width: <%= "%.1f" % [ 100.0 * @hours_slept_histogram.fetch([key, hour.to_f], 0.0) / @max_hours.fetch(key, 1.0) ] %>%">&nbsp;</div>
</td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
</div>
</div>

0 comments on commit b407a84

Please sign in to comment.