From 52f0fd470710529af9db47590f8e36fe00ac7335 Mon Sep 17 00:00:00 2001 From: yuenmichelle1 Date: Mon, 14 Oct 2024 17:12:45 -0500 Subject: [PATCH 1/6] update to include ordering project contributions by recents --- .../user_classification_count_controller.rb | 16 +++++++++++-- .../user_classification_counts_serializer.rb | 24 ++++++++++++++++--- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/app/controllers/user_classification_count_controller.rb b/app/controllers/user_classification_count_controller.rb index bcce57a..f272920 100644 --- a/app/controllers/user_classification_count_controller.rb +++ b/app/controllers/user_classification_count_controller.rb @@ -5,6 +5,11 @@ class UserClassificationCountController < ApplicationController before_action :sanitize_params before_action :require_login + PROJ_CONTRIBUTIONS_ORDER_OPTIONS = { + recents: 'recents', + count: 'count' + }.freeze + def query current_user['queried_user_id'] = params[:id] authorize :queried_user_context, :show? @@ -17,6 +22,11 @@ def query def validate_params super raise ValidationError, 'Cannot query for project contributions and query by project/workflow' if params[:project_contributions] && (params[:workflow_id] || params[:project_id]) + validate_order_proj_contributions if params[:order_project_contributions_by] + end + + def validate_order_proj_contributions + raise ValidationError, 'Can only order project contributions by recents or count. Default behavior is count' unless PROJ_CONTRIBUTIONS_ORDER_OPTIONS.keys.include? params[:order_project_contributions_by].downcase.to_sym end def sanitize_params @@ -27,10 +37,12 @@ def sanitize_params def serializer_opts_from_params { period: params[:period], time_spent: params[:time_spent], - project_contributions: params[:project_contributions] } + project_contributions: params[:project_contributions], + order_project_contributions_by: params[:order_project_contributions_by] + } end def user_classification_count_params - params.permit(:id, :start_date, :end_date, :period, :workflow_id, :project_id, :project_contributions, :time_spent) + params.permit(:id, :start_date, :end_date, :period, :workflow_id, :project_id, :project_contributions, :time_spent, :order_project_contributions_by) end end diff --git a/app/serializers/user_classification_counts_serializer.rb b/app/serializers/user_classification_counts_serializer.rb index ff92905..9d6959e 100644 --- a/app/serializers/user_classification_counts_serializer.rb +++ b/app/serializers/user_classification_counts_serializer.rb @@ -11,16 +11,22 @@ def as_json(options) serializer_options = options[:serializer_options] show_time_spent = serializer_options[:time_spent] show_project_contributions = serializer_options[:project_contributions] + order_project_contributions_by = serializer_options[:order_project_contributions_by] + total_count = user_classification_counts.sum(&:count).to_i response = { total_count: } calculate_time_spent(user_classification_counts, response) if show_time_spent - response[:project_contributions] = project_contributions if show_project_contributions + response[:project_contributions] = project_contributions(order_project_contributions_by) if show_project_contributions response[:data] = response_data(user_classification_counts, show_project_contributions:, show_time_spent:) if serializer_options[:period] response end private + def order_project_contributions_by_recents?(order_project_contributions_by) + order_project_contributions_by && order_project_contributions_by.downcase == 'recents' + end + def calculate_time_spent(counts, response) total_time_spent = counts.sum(&:session_time).to_f response[:time_spent] = total_time_spent @@ -48,10 +54,22 @@ def response_data(user_counts, show_project_contributions:, show_time_spent:) end end - def project_contributions + def project_contributions(order_by) project_contributions = @user_classification_counts.group_by(&:project_id).transform_values do |counts| counts.sum(&:count) end - project_contributions.map { |project_id, count| { project_id:, count: } }.sort_by { |proj_contribution| proj_contribution[:count] }.reverse + + if order_project_contributions_by_recents?(order_by) + period_to_contributed_project_ids = @user_classification_counts.sort_by { |ucc| ucc.period }.reverse.group_by(&:period).transform_values do |uccs| + uccs.map { |ucc| ucc.project_id} + end + + puts "MDY114 HITS HERE" + puts period_to_contributed_project_ids.values.flatten.uniq + recently_contributed_project_ids = period_to_contributed_project_ids.values.flatten.uniq + recently_contributed_project_ids.map { |project_id| { project_id: , count: project_contributions[project_id] } } + else + project_contributions.map { |project_id, count| { project_id:, count: } }.sort_by { |proj_contribution| proj_contribution[:count] }.reverse + end end end From 1e363dded53c442c8976c7e538614776d990c716 Mon Sep 17 00:00:00 2001 From: yuenmichelle1 Date: Mon, 14 Oct 2024 20:03:26 -0500 Subject: [PATCH 2/6] remove print statements --- app/serializers/user_classification_counts_serializer.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/serializers/user_classification_counts_serializer.rb b/app/serializers/user_classification_counts_serializer.rb index 9d6959e..95e0829 100644 --- a/app/serializers/user_classification_counts_serializer.rb +++ b/app/serializers/user_classification_counts_serializer.rb @@ -63,9 +63,6 @@ def project_contributions(order_by) period_to_contributed_project_ids = @user_classification_counts.sort_by { |ucc| ucc.period }.reverse.group_by(&:period).transform_values do |uccs| uccs.map { |ucc| ucc.project_id} end - - puts "MDY114 HITS HERE" - puts period_to_contributed_project_ids.values.flatten.uniq recently_contributed_project_ids = period_to_contributed_project_ids.values.flatten.uniq recently_contributed_project_ids.map { |project_id| { project_id: , count: project_contributions[project_id] } } else From 28891e3a7c15a2e10e6341814ba4dbbae24b3a4b Mon Sep 17 00:00:00 2001 From: yuenmichelle1 Date: Fri, 18 Oct 2024 12:05:42 -0500 Subject: [PATCH 3/6] adding specs --- ...er_classification_count_controller_spec.rb | 18 +++++++++++++ ...r_classification_counts_serializer_spec.rb | 26 +++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/spec/controllers/user_classification_count_controller_spec.rb b/spec/controllers/user_classification_count_controller_spec.rb index 0f3aa54..aed046f 100644 --- a/spec/controllers/user_classification_count_controller_spec.rb +++ b/spec/controllers/user_classification_count_controller_spec.rb @@ -126,6 +126,24 @@ get :query, params: { id: 1, time_spent: 'true' } expect(controller.params[:time_spent]).to eq(true) end + + context 'order_project_contributions validations' do + it 'errors if not a valid order_project_contributions_by option' do + get :query, params: { id: 1, order_project_contributions_by: 'blank' } + expect(response.status).to eq(400) + expect(response.body).to include('Can only order project contributions by recents or count') + end + + it 'allows allowable order_project_contributions_by option' do + get :query, params: { id: 1, order_project_contributions_by: 'recents' } + expect(response.status).to eq(200) + end + + it 'allows order_project_contributions_by to be case-insensitive' do + get :query, params: { id: 1, order_project_contributions_by: 'COUNT' } + expect(response.status).to eq(200) + end + end end end end diff --git a/spec/serializers/user_classification_counts_serializer_spec.rb b/spec/serializers/user_classification_counts_serializer_spec.rb index a3fce9a..ac24d30 100644 --- a/spec/serializers/user_classification_counts_serializer_spec.rb +++ b/spec/serializers/user_classification_counts_serializer_spec.rb @@ -59,7 +59,7 @@ let(:user_diff_period_classification_count) { build(:user_diff_period_classification_count) } let(:serializer) { described_class.new([user_diff_period_classification_count, user_classification_count, user_diff_proj_count]) } - it 'shows project_contributions ordered desc by count' do + it 'shows project_contributions ordered desc by count when order_proj_contribution_by by not given' do serialized = serializer.as_json(serializer_options: { project_contributions: true }) expect(serialized[:project_contributions].length).to eq(2) expect(serialized[:project_contributions][0][:project_id]).to eq(user_classification_count.project_id) @@ -68,7 +68,29 @@ expect(serialized[:project_contributions][1][:count]).to eq(user_diff_proj_count.count) end - it 'shows response data bucketed by period when querying top_projects' do + context 'when order_project_contributions_by param is given' do + it 'shows project_contributions ordered desc by count when order_proj_contribution_by is count' do + serialized = serializer.as_json(serializer_options: { project_contributions: true, order_project_contributions_by: 'count' }) + expect(serialized[:project_contributions].length).to eq(2) + expect(serialized[:project_contributions][0][:project_id]).to eq(user_classification_count.project_id) + expect(serialized[:project_contributions][0][:count]).to eq(user_classification_count.count + user_diff_period_classification_count.count) + expect(serialized[:project_contributions][1][:project_id]).to eq(user_diff_proj_count.project_id) + expect(serialized[:project_contributions][1][:count]).to eq(user_diff_proj_count.count) + end + + it 'shows project_contributions ordered by recents when order_proj_contribution_by is recents' do + classification_count_diff_project_created_yesterday = build(:user_diff_proj_classification_count, period: Date.today - 1) + serializer = described_class.new([classification_count_diff_project_created_yesterday,user_classification_count]) + serialized = serializer.as_json(serializer_options: { project_contributions: true, order_project_contributions_by: 'recents' }) + expect(serialized[:project_contributions].length).to eq(2) + expect(serialized[:project_contributions][0][:project_id]).to eq(user_classification_count.project_id) + expect(serialized[:project_contributions][0][:count]).to eq(user_classification_count.count) + expect(serialized[:project_contributions][1][:project_id]).to eq(classification_count_diff_project_created_yesterday.project_id) + expect(serialized[:project_contributions][1][:count]).to eq(classification_count_diff_project_created_yesterday.count) + end + end + + it 'shows response data bucketed by period when querying project_contributions by count' do serialized = serializer.as_json(serializer_options: { project_contributions: true, period: 'day' }) expect(serialized[:data].length).to eq(2) expect(serialized[:data][0][:period]).to eq(user_diff_period_classification_count.period) From 2fbdd52b31d7f7f3520c7be7ceba6a997c9da3d7 Mon Sep 17 00:00:00 2001 From: yuenmichelle1 Date: Fri, 18 Oct 2024 12:34:17 -0500 Subject: [PATCH 4/6] fixing hound sniffs --- app/controllers/user_classification_count_controller.rb | 3 +-- spec/serializers/user_classification_counts_serializer_spec.rb | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/controllers/user_classification_count_controller.rb b/app/controllers/user_classification_count_controller.rb index f272920..c3c170a 100644 --- a/app/controllers/user_classification_count_controller.rb +++ b/app/controllers/user_classification_count_controller.rb @@ -38,8 +38,7 @@ def serializer_opts_from_params { period: params[:period], time_spent: params[:time_spent], project_contributions: params[:project_contributions], - order_project_contributions_by: params[:order_project_contributions_by] - } + order_project_contributions_by: params[:order_project_contributions_by] } end def user_classification_count_params diff --git a/spec/serializers/user_classification_counts_serializer_spec.rb b/spec/serializers/user_classification_counts_serializer_spec.rb index ac24d30..b31a866 100644 --- a/spec/serializers/user_classification_counts_serializer_spec.rb +++ b/spec/serializers/user_classification_counts_serializer_spec.rb @@ -80,7 +80,7 @@ it 'shows project_contributions ordered by recents when order_proj_contribution_by is recents' do classification_count_diff_project_created_yesterday = build(:user_diff_proj_classification_count, period: Date.today - 1) - serializer = described_class.new([classification_count_diff_project_created_yesterday,user_classification_count]) + serializer = described_class.new([classification_count_diff_project_created_yesterday, user_classification_count]) serialized = serializer.as_json(serializer_options: { project_contributions: true, order_project_contributions_by: 'recents' }) expect(serialized[:project_contributions].length).to eq(2) expect(serialized[:project_contributions][0][:project_id]).to eq(user_classification_count.project_id) From ec1ea381257761bbae6bee69b57429be642cbabf Mon Sep 17 00:00:00 2001 From: yuenmichelle1 Date: Fri, 18 Oct 2024 15:46:38 -0500 Subject: [PATCH 5/6] Update app/serializers/user_classification_counts_serializer.rb Co-authored-by: Zach Wolfenbarger --- app/serializers/user_classification_counts_serializer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/serializers/user_classification_counts_serializer.rb b/app/serializers/user_classification_counts_serializer.rb index 95e0829..b4bac63 100644 --- a/app/serializers/user_classification_counts_serializer.rb +++ b/app/serializers/user_classification_counts_serializer.rb @@ -60,7 +60,7 @@ def project_contributions(order_by) end if order_project_contributions_by_recents?(order_by) - period_to_contributed_project_ids = @user_classification_counts.sort_by { |ucc| ucc.period }.reverse.group_by(&:period).transform_values do |uccs| + period_to_contributed_project_ids = @user_classification_counts.sort_by(&:period).reverse.group_by(&:period).transform_values do |uccs| uccs.map { |ucc| ucc.project_id} end recently_contributed_project_ids = period_to_contributed_project_ids.values.flatten.uniq From 412748249b076eb9cf7d2aff774dbe7279b5dfb3 Mon Sep 17 00:00:00 2001 From: yuenmichelle1 Date: Fri, 18 Oct 2024 15:46:47 -0500 Subject: [PATCH 6/6] Update app/serializers/user_classification_counts_serializer.rb Co-authored-by: Zach Wolfenbarger --- app/serializers/user_classification_counts_serializer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/serializers/user_classification_counts_serializer.rb b/app/serializers/user_classification_counts_serializer.rb index b4bac63..f0480ff 100644 --- a/app/serializers/user_classification_counts_serializer.rb +++ b/app/serializers/user_classification_counts_serializer.rb @@ -61,7 +61,7 @@ def project_contributions(order_by) if order_project_contributions_by_recents?(order_by) period_to_contributed_project_ids = @user_classification_counts.sort_by(&:period).reverse.group_by(&:period).transform_values do |uccs| - uccs.map { |ucc| ucc.project_id} + uccs.map(&:project_id) end recently_contributed_project_ids = period_to_contributed_project_ids.values.flatten.uniq recently_contributed_project_ids.map { |project_id| { project_id: , count: project_contributions[project_id] } }