Skip to content

Commit

Permalink
Merge pull request #395 from DefactoSoftware/hugo/fix-billables-memor…
Browse files Browse the repository at this point in the history
…y-issue

[ready] Fix extreme memory usages on /billables for accounts with 7000+ billable entries
  • Loading branch information
fatboypunk authored Jan 16, 2017
2 parents 5ec044a + 53d2c10 commit 66c8a58
Show file tree
Hide file tree
Showing 14 changed files with 113 additions and 301 deletions.
6 changes: 2 additions & 4 deletions app/assets/stylesheets/_billables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@

max-width: 90em;

.submit-billable-entries {
@include media($laptop-screen) {
opacity: 0;
}
#submit-billable-entries-test {
opacity: .0;
}
}
}
Expand Down
16 changes: 7 additions & 9 deletions app/controllers/billables_controller.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
class BillablesController < ApplicationController
def index
@hours_entries = Hour.query(entry_filter_or_default,
[:user, :project, :category])
@mileages_entries = Mileage.query(entry_filter_or_default,
[:user, :project])

@billable_list = BillableList.new(@hours_entries, @mileages_entries)
@filters = EntryFilter.new(entry_filter_or_default)
@projects = projects_with_billable_entries
end

def bill_entries
Expand All @@ -22,7 +16,11 @@ def bill_entries

private

def entry_filter_or_default
params[:entry_filter] || { billed: false }
def projects_with_billable_entries
billable_projects = Project.where(billable: true, archived: false)

billable_projects.select do |project|
project if project.has_billable_entries?
end
end
end
7 changes: 6 additions & 1 deletion app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def current_locale

def billable_entry_checkbox(entry, entry_type)
if entry.billed
""
""
else
tag(:input,
type: "checkbox",
Expand Down Expand Up @@ -77,4 +77,9 @@ def easter?
holiday = Holidays.on(Date.today, :nl)
holiday[0][:name] == "Pasen" if holiday.any?
end

def billable_hours_of(project)
Hour.includes(:category, :user, :project).
where(project: project, billed: false)
end
end
16 changes: 0 additions & 16 deletions app/models/billable_list.rb

This file was deleted.

5 changes: 5 additions & 0 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ def budget_status
budget - hours.sum(:value) if budget
end

def has_billable_entries?
hours.exists?(billed: false) ||
mileages.exists?(billed: false)
end

private

def slug_source
Expand Down
23 changes: 0 additions & 23 deletions app/views/billables/_entries.html.haml

This file was deleted.

8 changes: 0 additions & 8 deletions app/views/billables/_filters.html.haml

This file was deleted.

16 changes: 7 additions & 9 deletions app/views/billables/_hours_entries.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@
%th= t("billables.table.category")
%th= t("billables.table.hours")
%th= t("billables.table.user")
- entries.each do |entry|
- billable_hours_of(project).each do |hour|
%tr.info-row
%td=billable_entry_checkbox(entry, "hours")
%td=I18n.l (entry.date)
%td=entry.category.name
%td.center= entry.value
%td= billable_entry_checkbox(hour, "hours")
%td= I18n.l (hour.date)
%td= hour.category.name
%td.center= hour.value
%td
= link_to entry.user do
= entry.user.full_name
= link_to hour.user do
= hour.user.full_name
12 changes: 6 additions & 6 deletions app/views/billables/_mileages_entries.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
%th
%th= t("entries.index.mileages")
%th= t("billables.table.user")
- entries.each do |entry|
- project.mileages.where(billed: false).each do |mileage|
%tr.info-row
%td=billable_entry_checkbox(entry, "mileages")
%td=I18n.l (entry.date)
%td= billable_entry_checkbox(mileage, "mileages")
%td= I18n.l (mileage.date)
%td
%td.center= entry.value
%td.center= mileage.value
%td
= link_to entry.user do
= entry.user.full_name
= link_to mileage.user do
= mileage.user.full_name
10 changes: 0 additions & 10 deletions app/views/billables/_projects.html.haml

This file was deleted.

19 changes: 8 additions & 11 deletions app/views/billables/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@
%button{id: "submit-billable-entries", disabled: true}
=t("billables.buttons.bill_selected")
.billables
.sidebar
.container
= render partial: "filters", locals: { filter_url: billables_path }
.outer
%h1= t("billables.billable_entries")
- unless @hours_entries.any? || @mileages_entries.any?
.info
%p= t("info.no_billable_entries_html")
= form_tag("/billables", method: "post", remote: true, id: "billable-entries-form", autocomplete: "off") do
- @billable_list.clients.each do |client|
%h1= client.name
= render "projects", projects: client.projects
- @projects.each do |project|
.container
%h1= project.name
- if project.hours.exists?(billed: false)
= render "hours_entries", project: project
- if project.mileages.exists?(billed: false)
= render "mileages_entries", project: project
%button#submit-billable-entries-test= t("billables.buttons.bill_selected")
185 changes: 0 additions & 185 deletions spec/features/user_manages_billables_spec.rb

This file was deleted.

Loading

0 comments on commit 66c8a58

Please sign in to comment.