diff --git a/app/controllers/practices_controller.rb b/app/controllers/practices_controller.rb index 39370ade7..c61e1daa5 100644 --- a/app/controllers/practices_controller.rb +++ b/app/controllers/practices_controller.rb @@ -261,68 +261,76 @@ def overview def metrics @duration = params[:duration] || "30" + + if @duration != "30" + @duration = get_practice_all_time_duration(@practice.id).to_s + end + @page_views_leader_board_30_days = fetch_page_views_leader_board @page_views_leader_board_all_time = fetch_page_views_leader_board(0) - @page_views_for_practice_count = fetch_page_view_for_practice_count(@practice.id, @duration) - @unique_visitors_for_practice_count = fetch_unique_visitors_by_practice_count(@practice.id, @duration) - @bookmarks_by_practice = fetch_bookmarks_by_practice(@practice.id, @duration) + page_views = fetch_page_views_for_practice_over_duration(@practice.id, @duration) + @page_views_for_practice_count = page_views.size + @unique_visitors_for_practice_count = page_views + .select(:user_id) + .distinct + .count + + @bookmarks_by_practice_count = fetch_bookmarks_by_practice(@practice.id, @duration) + + adoptions_all_time = DiffusionHistory.with_practice_and_facilities(@practice) + @adoptions_total_at = adoptions_all_time.size + + adoptions_past_30_days = adoptions_all_time.in_date_range(30.days.ago.beginning_of_day) + @adoptions_total_30 = adoptions_past_30_days.size + if @duration === '30' - @adoptions_by_practice = fetch_adoption_counts_by_practice_last_30_days(@practice) + @adoptions_by_practice = @adoptions_total_30 else - @adoptions_by_practice = fetch_adoption_counts_by_practice_all_time(@practice) - end - - @adoptions_total_30 = fetch_adoption_counts_by_practice_last_30_days(@practice) - @adoptions_total_at = fetch_adoption_counts_by_practice_all_time(@practice) - - @adoptions_successful_total_30 = fetch_adoptions_total_by_practice_and_status_last_30_days(@practice, 'Completed') - @adoptions_successful_total_at = fetch_adoptions_total_by_practice_and_status_all_time(@practice, 'Completed') - @adoptions_in_progress_total_30 = fetch_adoptions_total_by_practice_and_status_last_30_days(@practice, 'In progress') - @adoptions_in_progress_total_at = fetch_adoptions_total_by_practice_and_status_all_time(@practice, 'In progress') - @adoptions_unsuccessful_total_30 = fetch_adoptions_total_by_practice_and_status_last_30_days(@practice, 'Unsuccessful') - @adoptions_unsuccessful_total_at = fetch_adoptions_total_by_practice_and_status_all_time(@practice, 'Unsuccessful') - - @facility_ids_for_practice_30 = fetch_adoption_facilities_for_practice_last_30_days(@practice) - @rural_facility_30 = get_adoption_facility_details_for_practice(@va_facilities, @facility_ids_for_practice_30, "rurality", "R") - @urban_facility_30 = get_adoption_facility_details_for_practice(@va_facilities, @facility_ids_for_practice_30, "rurality", "U") - @a_high_complexity_30 = get_adoption_facility_details_for_practice(@va_facilities, @facility_ids_for_practice_30, "fy17_parent_station_complexity_level", "1a-High Complexity") - @b_high_complexity_30 = get_adoption_facility_details_for_practice(@va_facilities, @facility_ids_for_practice_30, "fy17_parent_station_complexity_level", "1b-High Complexity") - @c_high_complexity_30 = get_adoption_facility_details_for_practice(@va_facilities, @facility_ids_for_practice_30, "fy17_parent_station_complexity_level", "1c-High Complexity") - @medium_complexity_2_30 = get_adoption_facility_details_for_practice(@va_facilities, @facility_ids_for_practice_30, "fy17_parent_station_complexity_level", "2 -Medium Complexity") - @low_complexity_3_30 = get_adoption_facility_details_for_practice(@va_facilities, @facility_ids_for_practice_30, "fy17_parent_station_complexity_level", "3 -Low Complexity") - - @facility_ids_for_practice_at = fetch_adoption_facilities_for_practice_all_time(@practice) - @rural_facility_at = get_adoption_facility_details_for_practice(@va_facilities, @facility_ids_for_practice_at, "rurality", "R") - @urban_facility_at = get_adoption_facility_details_for_practice(@va_facilities, @facility_ids_for_practice_at, "rurality", "U") - @a_high_complexity_at = get_adoption_facility_details_for_practice(@va_facilities, @facility_ids_for_practice_at, "fy17_parent_station_complexity_level", "1a-High Complexity") - @b_high_complexity_at = get_adoption_facility_details_for_practice(@va_facilities, @facility_ids_for_practice_at, "fy17_parent_station_complexity_level", "1b-High Complexity") - @c_high_complexity_at = get_adoption_facility_details_for_practice(@va_facilities, @facility_ids_for_practice_at, "fy17_parent_station_complexity_level", "1c-High Complexity") - @medium_complexity_2_at = get_adoption_facility_details_for_practice(@va_facilities, @facility_ids_for_practice_at, "fy17_parent_station_complexity_level", "2 -Medium Complexity") - @low_complexity_3_at = get_adoption_facility_details_for_practice(@va_facilities, @facility_ids_for_practice_at, "fy17_parent_station_complexity_level", "3 -Low Complexity") - - # Charts..... - @unique_visitors_for_practice = fetch_unique_visitors_by_practice(@practice.id, @duration) - @page_views_for_practice = fetch_page_views_for_practice(@practice.id, @duration) - - if @duration != "30" - @duration = get_practice_all_time_duration(@practice.id) + @adoptions_by_practice = @adoptions_total_at end + + # Adoptions Metrics + adoption_metrics_30 = calculate_adoption_metrics(adoptions_past_30_days) + adoption_metrics_at = calculate_adoption_metrics(adoptions_all_time) + + @adoptions_successful_total_30 = adoption_metrics_30[:successful] + @adoptions_successful_total_at = adoption_metrics_at[:successful] + + @adoptions_in_progress_total_30 = adoption_metrics_30[:in_progress] + @adoptions_in_progress_total_at = adoption_metrics_at[:in_progress] + + @adoptions_unsuccessful_total_30 = adoption_metrics_30[:unsuccessful] + @adoptions_unsuccessful_total_at = adoption_metrics_at[:unsuccessful] + + + # Facility metrics + facility_station_numbers_for_practice_30 = adoptions_past_30_days.map {|dh| dh.station_number if dh.station_number.present?} + facility_station_numbers_for_practice_at = adoptions_all_time.map {|dh| dh.station_number if dh.station_number.present?} + facility_metrics_30 = calculate_facility_metrics(@va_facilities, facility_station_numbers_for_practice_30) + facility_metrics_at = calculate_facility_metrics(@va_facilities, facility_station_numbers_for_practice_at) + + @rural_facility_30 = facility_metrics_30[:rural] + @urban_facility_30 = facility_metrics_30[:urban] + @a_high_complexity_30 = facility_metrics_30[:high_complexity_1a] + @b_high_complexity_30 = facility_metrics_30[:high_complexity_1b] + @c_high_complexity_30 = facility_metrics_30[:high_complexity_1c] + @medium_complexity_2_30 = facility_metrics_30[:medium_complexity_2] + @low_complexity_3_30 = facility_metrics_30[:low_complexity_3] + + @rural_facility_at = facility_metrics_at[:rural] + @urban_facility_at = facility_metrics_at[:urban] + @a_high_complexity_at = facility_metrics_at[:high_complexity_1a] + @b_high_complexity_at = facility_metrics_at[:high_complexity_1b] + @c_high_complexity_at = facility_metrics_at[:high_complexity_1c] + @medium_complexity_2_at = facility_metrics_at[:medium_complexity_2] + @low_complexity_3_at = facility_metrics_at[:low_complexity_3] + + # Google line chart @month_names = "Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul", "Aug", "Sep", "Oct", "Nov", "Dec" - @cur_duration = @duration.to_i - @dates = ((@cur_duration.days.ago.to_date .. 0.days.ago.to_date).to_a).map(&:to_s) - @views = [] - @visitors = [] - @dates.each do |date| - objCtr = 0 - @page_views_for_practice.each do |obj| - objCtr += 1 if obj[:created_at].to_s == date.to_s - end - @views << objCtr - end - @unique_visitors = [] - @dates.each do |date| - @unique_visitors << fetch_unique_visitors_by_practice_and_date(@practice.id, date) - end + page_view_metrics = calculate_page_view_metrics(page_views, @duration) + @dates = page_view_metrics[:dates] + @views = page_view_metrics[:views] + @unique_visitors = page_view_metrics[:unique_visitors] render 'practices/form/metrics' end diff --git a/app/models/diffusion_history.rb b/app/models/diffusion_history.rb index 0f5f99292..58890cc75 100644 --- a/app/models/diffusion_history.rb +++ b/app/models/diffusion_history.rb @@ -19,6 +19,21 @@ class DiffusionHistory < ApplicationRecord scope :get_va_facilities, -> { includes(:va_facility).pluck("va_facilities.station_number") } scope :get_clinical_resource_hubs, -> { includes(:clinical_resource_hub).pluck("clinical_resource_hubs.official_station_name") } scope :get_with_practice, -> (practice) { joins(:practice).where(practice: practice) } + scope :with_practice_and_facilities, ->(practice) { + get_with_practice(practice) + .joins( + "LEFT OUTER JOIN va_facilities ON va_facilities.id = diffusion_histories.va_facility_id + LEFT OUTER JOIN clinical_resource_hubs ON clinical_resource_hubs.id = diffusion_histories.clinical_resource_hub_id" + ) + .select( + 'diffusion_histories.id, + diffusion_histories.created_at, + va_facilities.station_number AS station_number' + ) + } scope :exclude_va_facilities, -> { where(va_facility_id: nil) } scope :exclude_clinical_resource_hubs, -> { where(clinical_resource_hub_id: nil) } + scope :in_date_range, ->(start_date, end_date = Time.now) { + where(created_at: start_date..end_date) + } end diff --git a/app/views/practices/form/metrics.html.erb b/app/views/practices/form/metrics.html.erb index 7f63de92a..156986f95 100644 --- a/app/views/practices/form/metrics.html.erb +++ b/app/views/practices/form/metrics.html.erb @@ -46,7 +46,7 @@