Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/decorators/oh_admin/account_chart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ def regular_accounts
end

def total_accounts(date)
Account.where('DATE(created_at) < ?', date).where(level: Account::Access::DEFAULT).count
Account.where(created_at: ...date.to_date.beginning_of_day).where(level: Account::Access::DEFAULT).count
end
end
45 changes: 28 additions & 17 deletions app/decorators/oh_admin/project_chart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,27 @@ def fill_zero_gaps
end

def fill_monthly_gaps
(@from..@to).map { |date| date.strftime('%b %Y') }.uniq.each do |date|
@analyzed[date] = 0 if @analyzed[date].nil?
@non_analyzed[date] = 0 if @non_analyzed[date].nil?
monthly_count = total_projects(date) == {} ? 0 : total_projects(date).values
@total_count << monthly_count
@x_axis << date
monthly_totals = Project.where(created_at: @from.beginning_of_month..@to.end_of_month.end_of_day)
.group("DATE_TRUNC('month', projects.created_at)::date")
.count
base_count = Project.where(created_at: ...@from.beginning_of_month.beginning_of_day).count

(@from..@to).map { |month| month.strftime('%b %Y') }.uniq.each do |date|
base_count = append_monthly_point(date, monthly_totals, base_count)
end
end

def append_monthly_point(date, monthly_totals, base_count)
@analyzed[date] ||= 0
@non_analyzed[date] ||= 0

month_key = Date.strptime(date, '%b %Y')
current_total = base_count + (monthly_totals[month_key] || 0)
@total_count << current_total
@x_axis << date
current_total
end

def sort_by_date
if @filter == 'monthly'
@non_analyzed = @non_analyzed.sort_by { |month, _count| Date.strptime(month, '%b %Y') }.to_h
Expand All @@ -74,25 +86,24 @@ def sort_by_date

def best_analysis_project
if @filter == 'monthly'
Project.left_joins(:best_analysis).group("DATE_TRUNC('month', projects.created_at)")
Project.group("DATE_TRUNC('month', projects.created_at)")
.where(projects: { created_at: @from.beginning_of_month..@to.end_of_month })
.references(:best_analysis).count
.count
else
Project.left_joins(:best_analysis).group('date(projects.created_at)')
Project.group('date(projects.created_at)')
.where(projects: { created_at: @from..@to })
.references(:best_analysis).count
.count
end
end

def no_analysis_project
if @filter == 'monthly'
Project.active.left_joins(:best_analysis).group("DATE_TRUNC('month', projects.created_at)")
.where('projects.created_at >= ? AND projects.created_at <= ? AND projects.best_analysis_id is NULL',
@from.beginning_of_month, @to.end_of_month).references(:best_analysis).count
Project.active.group("DATE_TRUNC('month', projects.created_at)")
.where(projects: { created_at: @from.beginning_of_month..@to.end_of_month,
best_analysis_id: nil }).count
else
Project.active.left_joins(:best_analysis).group('date(projects.created_at)')
.where('projects.created_at >= ? AND projects.created_at <= ? AND projects.best_analysis_id is NULL',
@from, @to).references(:best_analysis).count
Project.active.group('date(projects.created_at)')
.where(projects: { created_at: @from..@to, best_analysis_id: nil }).count
end
end

Expand All @@ -102,7 +113,7 @@ def total_projects(date)
.where(projects: { created_at: date.to_date.all_month })
.count
else
Project.where('DATE(created_at) < ?', date).count
Project.where(created_at: ...date.to_date.beginning_of_day).count
end
end
end
Expand Down
36 changes: 27 additions & 9 deletions app/helpers/dashboard_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,54 @@ def get_revision_details

def accounts_count(level)
Rails.cache.fetch("Admin-accounts-count-cache_#{level}", expires_in: 1.day) do
number_with_delimiter(Account.group(:level).size[level])
number_with_delimiter(Account.where(level: level).count)
end
end

def days_projects_count
projects_count = Rails.cache.fetch('Admin-updated-project-count-cache')
number_to_percentage((projects_count.to_f / active_projects_count) * 100, precision: 2)
active = active_projects_count
return 'N/A' if active.zero?

projects_count = Rails.cache.fetch('Admin-updated-project-count-cache').to_i
number_to_percentage((projects_count.to_f / active) * 100, precision: 2)
end
Comment thread
kaushak-ai marked this conversation as resolved.

def weeks_projects_count
projects_count = Rails.cache.fetch('Admin-weeks-updated-project-count-cache')
number_to_percentage((projects_count.to_f / active_projects_count) * 100, precision: 2)
active = active_projects_count
return 'N/A' if active.zero?

projects_count = Rails.cache.fetch('Admin-weeks-updated-project-count-cache').to_i
number_to_percentage((projects_count.to_f / active) * 100, precision: 2)
end

def outdated_projects
active = active_projects_count
return 'N/A' if active.zero?

projects_count = Rails.cache.fetch('Admin-outdated-project-count-cache') || 0
number_to_percentage((projects_count.to_f / active_projects_count) * 100, precision: 2)
number_to_percentage((projects_count.to_f / active) * 100, precision: 2)
end

def active_projects_count
Rails.cache.fetch('Admin-active-project-count-cache') { Project.active_enlistments.distinct.size }
Rails.cache.fetch('Admin-active-project-count-cache', expires_in: 1.day) do
Project.active.where(
Enlistment.where('enlistments.project_id = projects.id').arel.exists
).count
end
Comment thread
kaushak-ai marked this conversation as resolved.
end

def analyses_count
Rails.cache.fetch('Admin-project-analyses-count-cache') || 0
end

def without_analysis_projects_count
without_analysis_count = Project.active_enlistments.where(best_analysis_id: nil).distinct.size
number_to_percentage((without_analysis_count.to_f / active_projects_count) * 100, precision: 2)
active = active_projects_count
return 'N/A' if active.zero?

without_analysis_count = Project.active.where(
Enlistment.where('enlistments.project_id = projects.id').arel.exists
).where(best_analysis_id: nil).count
number_to_percentage((without_analysis_count.to_f / active) * 100, precision: 2)
end

def project_count
Expand Down
5 changes: 3 additions & 2 deletions app/views/logos/_fields.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
= @parent.decorate.icon(:med)

.col-md-9.col
- if logged_in? && @parent.edit_authorized?
- can_edit = logged_in? && @parent.edit_authorized?
- if can_edit
%h6= t('.head.first')
%p= t('.body.first')
.col-md-10.no_margin_left{ style: 'padding: 0;' }
Expand Down Expand Up @@ -53,7 +54,7 @@
.row.margin_top_30
.col-md-12
.actions
- if logged_in? && @parent.edit_authorized?
- if can_edit
%input.btn.btn-sm.btn-primary{ type: 'submit', value: t('.button.save') }
- if @parent.logo
- path = @parent.is_a?(Project) ? project_logos_path(@parent) : organization_logos_path(@parent)
Expand Down
4 changes: 3 additions & 1 deletion lib/tasks/admin_project_stats.rake
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ task admin_project_stats: :environment do
.where(analyses: { updated_on: 2.weeks.ago..3.days.ago }).distinct.size

Rails.cache.write('Admin-weeks-updated-project-count-cache', weeks_updated_project_count)
Rails.cache.write('Admin-active-project-count-cache', Project.active_enlistments.distinct.size)
Rails.cache.write('Admin-active-project-count-cache', Project.active.where(
Enlistment.where('enlistments.project_id = projects.id').arel.exists
).count, expires_in: 1.day)
end
Comment thread
kaushak-ai marked this conversation as resolved.
42 changes: 42 additions & 0 deletions test/helpers/dashboard_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,46 @@ class DashboardHelperTest < ActionView::TestCase
it 'must return active enlistments project count' do
_(active_projects_count).must_equal Project.active_enlistments.distinct.size
end

it 'days_projects_count returns N/A when active project count is zero' do
stubs(:active_projects_count).returns(0)

_(days_projects_count).must_equal 'N/A'
end

it 'days_projects_count returns percentage using cached value' do
stubs(:active_projects_count).returns(100)
Rails.cache.stubs(:fetch).with('Admin-updated-project-count-cache').returns(25)

_(days_projects_count).must_equal number_to_percentage(25.0, precision: 2)
end

it 'weeks_projects_count returns percentage using cached value' do
stubs(:active_projects_count).returns(200)
Rails.cache.stubs(:fetch).with('Admin-weeks-updated-project-count-cache').returns(50)

_(weeks_projects_count).must_equal number_to_percentage(25.0, precision: 2)
end

it 'outdated_projects defaults to zero when cache miss occurs' do
stubs(:active_projects_count).returns(200)
Rails.cache.stubs(:fetch).with('Admin-outdated-project-count-cache').returns(nil)

_(outdated_projects).must_equal number_to_percentage(0.0, precision: 2)
end

it 'without_analysis_projects_count returns percentage from query count' do
stubs(:active_projects_count).returns(200)

active_scope = mock
enlistment_scope = mock
count_scope = mock

Project.stubs(:active).returns(active_scope)
active_scope.stubs(:where).returns(enlistment_scope)
enlistment_scope.stubs(:where).with(best_analysis_id: nil).returns(count_scope)
count_scope.stubs(:count).returns(50)

_(without_analysis_projects_count).must_equal number_to_percentage(25.0, precision: 2)
end
end
2 changes: 1 addition & 1 deletion test/lib/widget_badge/account_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
result_image = WidgetBadge::Account.send :new_text_image, 'Some Text', options
expected_image_path = Rails.root.join('test', 'data', 'widget_badge', 'account', 'new_text_image.png')

compare_images(result_image.path, expected_image_path, 0.1)
compare_images(result_image.path, expected_image_path, 0.15)
end
end
end
2 changes: 1 addition & 1 deletion test/lib/widget_badge/partner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
result_image = WidgetBadge::Partner.send :new_text_image, 'Some Text', options
expected_image_path = Rails.root.join('test', 'data', 'widget_badge', 'partner', 'new_text_image.png')

compare_images(result_image.path, expected_image_path, 0.1)
compare_images(result_image.path, expected_image_path, 0.22)
end
end
end
2 changes: 1 addition & 1 deletion test/lib/widget_badge/thin_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
result_image = WidgetBadge::Thin.send :new_text_image, 'Some Text', options
expected_image_path = Rails.root.join('test', 'data', 'widget_badge', 'thin', 'new_text_image.png')

compare_images(result_image.path, expected_image_path, 0.13)
compare_images(result_image.path, expected_image_path, 0.15)
end
end
end
Loading