Skip to content

Commit

Permalink
working commit
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipDeFraties committed Dec 2, 2024
1 parent ca408f5 commit b1bf079
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 63 deletions.
143 changes: 110 additions & 33 deletions app/controllers/practices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,28 +259,115 @@ def overview
render 'practices/form/overview'
end

# old version
# def metrics
# @duration = params[:duration] || "30"
# @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)
# if @duration === '30'
# @adoptions_by_practice = fetch_adoption_counts_by_practice_last_30_days(@practice)
# 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)
# end
# @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

# render 'practices/form/metrics'
# end

# new version
def metrics
@duration = params[:duration] || "30"
if @duration != "30"
@duration = "365"
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)
page_views = fetch_page_views_for_practice_over_duration(@practice.id, @duration)
# @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)
@page_views_for_practice_count = page_views.size
@unique_visitors_for_practice_count = page_views.uniq.size
@bookmarks_by_practice = fetch_bookmarks_by_practice(@practice.id, @duration)

adoptions_all_time = DiffusionHistory.get_with_practice(@practice)
.includes(:va_facility)
.select(:id, :va_facility_id, :created_at)

# Use Active Record scopes to filter
adoptions_past_30_days = adoptions_all_time.where(created_at: 30.days.ago.beginning_of_day..Time.now)


@adoptions_total_30 = adoptions_past_30_days.size
@adoptions_total_at = adoptions_all_time.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)
@adoptions_by_practice = @adoptions_total_at
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 = adoptions_past_30_days.get_by_successful_status.size
@adoptions_successful_total_at = adoptions_all_time.get_by_successful_status.size

@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')
@adoptions_in_progress_total_30 = adoptions_past_30_days.get_by_in_progress_status.size
@adoptions_in_progress_total_at = adoptions_all_time.get_by_in_progress_status.size

@adoptions_unsuccessful_total_30 = adoptions_past_30_days.get_by_unsuccessful_status.size
@adoptions_unsuccessful_total_at = adoptions_all_time.get_by_unsuccessful_status.size

@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")
Expand All @@ -301,28 +388,14 @@ def metrics
@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)
end
@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

start_date = @duration.to_i.days.ago.to_date
end_date = Date.today
grouped_page_views = page_views.group_by { |pv| pv.time.to_date }
@dates = (start_date..end_date).map(&:to_s)
@views = @dates.map { |date| grouped_page_views[Date.parse(date)]&.count || 0 }
@unique_visitors = @dates.map { |date| grouped_page_views[Date.parse(date)]&.map(&:user_id)&.uniq&.count || 0 }

render 'practices/form/metrics'
end
Expand Down Expand Up @@ -524,6 +597,10 @@ def close_edit_session

private

def calculate_facility_metrics(facility_ids, va_facilities, attribute, value)
va_facilities.where(station_number: facility_ids, attribute => value).count
end

def skip_timeout
request.env["devise.skip_trackable"] = true
end
Expand Down
60 changes: 30 additions & 30 deletions app/views/practices/form/_metrics.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,38 @@
data.addColumn('number', 'Unique visitors');
<% ctr = 0 %>
data.addRows([
<%= while ctr < @dates.count do %>
<% if ctr < @dates.count %>
<%
month = @dates[ctr].split("-")[1]
day = @dates[ctr].split("-")[2]
if day[0] == "0"
day = day[1]
end
@dates[ctr] = @month_names[month.to_i - 1] + ". " + day + " " + @dates[ctr].split("-")[0]
%>
<% end %>
['<%= @dates[ctr] %>', <%= @views[ctr] %>, <%= @unique_visitors[ctr] %>],
<% ctr += 1 %>
<% end %>
<% while ctr < @dates.count do %>
<% if ctr < @dates.count %>
<%
month = @dates[ctr].split("-")[1]
day = @dates[ctr].split("-")[2]
if day[0] == "0"
day = day[1]
end
@dates[ctr] = @month_names[month.to_i - 1] + ". " + day + " " + @dates[ctr].split("-")[0]
%>
<% end %>
['<%= @dates[ctr] %>', <%= @views[ctr] %>, <%= @unique_visitors[ctr] %>],
<% ctr += 1 %>
<% end %>
]);

var options = {
hAxis: {
title: 'TIME'
},
vAxis: {
title: 'TOTAL',
format: 'short'
},
series: {
1: { lineDashStyle: [2, 2] }
},
backgroundColor: { fill:'transparent' },
legend: { position: 'top', alignment: 'start' },
colors: ['#1A4480', '#D6AB3A']
};
var options = {
hAxis: {
title: 'TIME'
},
vAxis: {
title: 'TOTAL',
format: 'short'
},
series: {
1: { lineDashStyle: [2, 2] }
},
backgroundColor: { fill:'transparent' },
legend: { position: 'top', alignment: 'start' },
colors: ['#1A4480', '#D6AB3A']
};

var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
}
10 changes: 10 additions & 0 deletions lib/modules/practice_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ def fetch_page_views_for_practice(practice_id, duration = "30")
page_views
end


def fetch_page_views_for_practice_over_duration(practice_id, duration = "30")
start_date = Time.now - duration.to_i.days

Ahoy::Event.where(
name: 'Practice show',
properties: { practice_id: practice_id }
).where('time >= ?', start_date)
end

def fetch_unique_visitors_by_practice_count(practice_id, duration = "30")
sql = "select distinct user_id from ahoy_events where name = 'Practice show' and properties = $1"
param1 = "{\"practice_id\": #{practice_id}}"
Expand Down

0 comments on commit b1bf079

Please sign in to comment.