You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.6 KiB
50 lines
1.6 KiB
class DashboardsController < ApplicationController
|
|
def show
|
|
@spent_minutes_matrix = {}
|
|
@remaining_minutes_matrix = {}
|
|
|
|
# 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 }
|
|
|
|
# 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
|
|
|
|
# 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
|
|
|
|
@completed_percent = if (@total_minutes + @remaining_total) > 0
|
|
((@total_minutes.to_f / (@total_minutes + @remaining_total)) * 100).round(1)
|
|
else
|
|
0
|
|
end
|
|
|
|
@last_entry = current_user.entries.order(date: :desc).first
|
|
end
|
|
end
|