Permalink
Browse files

Add dashboard - statistics

  • Loading branch information...
rociopaez committed Sep 25, 2014
1 parent e02456e commit e3691dde9285e81a0339fccbd4277143ee55513a
Showing with 77 additions and 4 deletions.
  1. +4 −0 models/input.rb
  2. +0 −2 models/output.rb
  3. +7 −0 public/css/migraine.css
  4. +37 −1 routes/users.rb
  5. +29 −1 views/dashboard.mote
@@ -5,4 +5,8 @@ class Input < Ohm::Model
reference :user, :User
reference :type, "::Type"
def description
"#{ type.category } - #{ type.name }"
end
end
@@ -12,8 +12,6 @@ class Output < Ohm::Model
reference :user, :User
attribute :date, Type::Date
attribute :duration, Type::Integer
attribute :scale, Type::Integer
@@ -197,6 +197,13 @@ table {
font-size: 16px;
}
table.statistics {
margin: 20px auto;
line-height: 22px;
width: 95%;
font-size: 14px;
}
td,
tr {
border: 1px solid #0c090b;
@@ -1,7 +1,43 @@
class Users < Cuba
define do
on("dashboard") do
render("dashboard", title: "Dashboard")
outputs = current_user.outputs.to_a
inputs = current_user.inputs.to_a
output_dates = outputs.map { |o| o.date }
input_dates = inputs.map { |i| i.date }
all_dates = output_dates + input_dates
all_years = all_dates.map { |d| d.year }.sort
start_year = all_years.first
end_year = all_years.last
statistics = {}
start_year.upto(end_year).each do |y|
statistics[y] ||= {}
1.upto(12).each do |m|
statistics[y][m] ||= {}
outputs.each do |output|
statistics[y][m][:outputs] ||= []
if output.date.year == y && output.date.month == m
statistics[y][m][:outputs] << output
end
end
inputs.each do |input|
statistics[y][m][:inputs] ||= []
if input.date.year == y && input.date.month == m
statistics[y][m][:inputs] << input
end
end
end
end
render("dashboard", title: "Dashboard", statistics: statistics)
end
on("sign_out") do
@@ -1,3 +1,31 @@
<h3>Dashboard resume statics<h3>
<img src="/img/migraine_scale.jpg" alt="Migraine Scale" id="dashboard">
% statistics.each do |year, data|
<h3>{{ year }}</h3>
<table class="statistics">
<tr>
% data.each do |month, data|
<th width="8.3%">{{ month }}</th>
% end
</tr>
<tr>
% data.each do |month, data|
<td>
% if data[:outputs].length == 0
0
% else
{{ (data[:outputs].map { |o| o.scale }.reduce(:+).to_f / data[:outputs].length).round(2) }}
% end
</td>
% end
</tr>
<tr>
% data.each do |month, data|
<td>
{{ data[:inputs].group_by { |i| i.type_id }.sort_by { |group, values| -values.length }.map { |group, values| "#{ values.first.description } (#{ values.length })" if values.first }.join("<br>") }}
</td>
% end
</tr>
</table>
% end

0 comments on commit e3691dd

Please sign in to comment.