3 changed files with 88 additions and 51 deletions
@ -1,29 +1,50 @@ |
|||||
# app/controllers/dashboards_controller.rb |
|
||||
class DashboardsController < ApplicationController |
class DashboardsController < ApplicationController |
||||
def show |
def show |
||||
@entries = current_user.entries.order(date: :desc) |
|
||||
|
@spent_minutes_matrix = {} |
||||
|
@remaining_minutes_matrix = {} |
||||
|
|
||||
valid_entries = @entries.select { |e| e.date.present? } |
|
||||
|
# Alle Kombinationen holen |
||||
|
@time_by_typ_art = current_user.entries |
||||
|
.group(:praktikums_typ, :entry_art) |
||||
|
.select(:praktikums_typ, :entry_art) |
||||
|
.group_by(&:praktikums_typ) |
||||
|
.transform_values { |entries| entries.map(&:entry_art).uniq } |
||||
|
|
||||
@report = valid_entries.group_by { |e| e.date.beginning_of_month } |
|
||||
.transform_values do |entries| |
|
||||
entries.group_by { |e| [e.praktikums_typ, e.entry_art] } |
|
||||
.transform_values do |subset| |
|
||||
minutes = subset.sum(&:total_minutes) |
|
||||
{ hours: minutes / 60, minutes: minutes % 60 } |
|
||||
|
# Matrix-Daten aufbauen (muss zuerst passieren!) |
||||
|
@time_by_typ_art.each do |typ, arts| |
||||
|
@spent_minutes_matrix[typ] ||= {} |
||||
|
@remaining_minutes_matrix[typ] ||= {} |
||||
|
|
||||
|
arts.each do |art| |
||||
|
spent_minutes = current_user.entries |
||||
|
.where(praktikums_typ: typ, entry_art: art) |
||||
|
.sum { |e| e.total_minutes.to_i } |
||||
|
|
||||
|
target = current_user.required_hours_matrix.dig(typ, art).to_i * 60 |
||||
|
remaining = [target - spent_minutes, 0].max |
||||
|
|
||||
|
@spent_minutes_matrix[typ][art] = spent_minutes |
||||
|
@remaining_minutes_matrix[typ][art] = remaining |
||||
end |
end |
||||
end |
end |
||||
|
|
||||
@monthly_minutes_chart_data = @report.transform_keys { |date| date.strftime("%b") } |
|
||||
.transform_values do |groups| |
|
||||
groups.values.sum { |data| data[:hours] * 60 + data[:minutes] } |
|
||||
|
# Jetzt können wir sauber summieren! |
||||
|
@total_minutes = 0 |
||||
|
@remaining_total = 0 |
||||
|
|
||||
|
@spent_minutes_matrix.each do |typ, arts| |
||||
|
arts.each do |art, spent| |
||||
|
@total_minutes += spent.to_i |
||||
|
@remaining_total += @remaining_minutes_matrix.dig(typ, art).to_i |
||||
|
end |
||||
end |
end |
||||
|
|
||||
@monthly_targets = Hash.new(1500) # z.B. 25h im Monat als Ziel → 1500min |
|
||||
|
@completed_percent = if (@total_minutes + @remaining_total) > 0 |
||||
|
((@total_minutes.to_f / (@total_minutes + @remaining_total)) * 100).round(1) |
||||
|
else |
||||
|
0 |
||||
|
end |
||||
|
|
||||
@total_minutes = @entries.sum(&:total_minutes) |
|
||||
@last_entry = @entries.first |
|
||||
@completed_percent = ((@total_minutes.to_f / 10000) * 100).round(1) |
|
||||
@remaining_total = [10000 - @total_minutes, 0].max |
|
||||
|
@last_entry = current_user.entries.order(date: :desc).first |
||||
end |
end |
||||
end |
end |
||||
Loading…
Reference in new issue