Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def refresh_trend_data
@begin_date = (Date.today - 3.months).end_of_week(:sunday)
@end_date = Date.today.end_of_week(:sunday)
@begin_date = @chart.trend_start_date.end_of_week(:sunday) if @chart.trend_start_date
@end_date = @chart.trend_end_date.end_of_week(:sunday) if @chart.trend_end_date
if @chart.trend_all_movements
@displayed_movements = @movements
else
org_ids = @chart.chart_organizations.where("trend_display = 1").all.collect(&:organization_id)
@displayed_movements = Organization.where("id IN (?)", org_ids)
end
@grouped_criteria_options = get_trend_criteria_options(@displayed_movements)
max_fields = Chart::TREND_MAX_FIELDS
semester_stats_needed = false
interval = @chart.trend_interval
@chart_start_date = @begin_date + (interval - 1) * 7
@chart_end_date = @end_date + interval * 7 - 1 # make sure the last data point includes the data at the end date specified
@fields, @lines = [], {}
(1..max_fields).each do |number|
field = @chart["trend_field_#{number}"]
if field.present? && MovementIndicator.all.include?(field)
@fields << MovementIndicator.translate[field] #Movement Indicator
elsif field.present? && @grouped_criteria_options.flatten.include?(field)
@fields << field #Label
end
semester_stats_needed = true if MovementIndicator.semester.include?(field)
@lines[@fields.last.to_s] = {}
end
unless @fields.empty?
infobase_hash = {
activity_ids: @displayed_movements.collect(&:importable_id),
begin_date: @begin_date,
end_date: @end_date,
interval: interval,
semester: semester_stats_needed
}
begin
resp = RestClient.post(APP_CONFIG['infobase_url'] + "/statistics/collate_stats_intervals", infobase_hash.to_json, content_type: :json, accept: :json, authorization: "Bearer #{APP_CONFIG['infobase_token']}")
json = JSON.parse(resp)
rescue
raise resp.inspect
end
@chart_start_date.step(@chart_end_date, interval * 7) do |date| # step through dates 1 interval at a time
@fields.each do |field|
@lines[field][date] = json[date.to_s][field] if @lines[field] && json[date.to_s]
unless MovementIndicator.translate.values.include?(field)
org_ids = @displayed_movements.pluck(:id) + [0]
label_ids = Label.where(organization_id: org_ids, name: field).pluck(:id)
people_ids = OrganizationalLabel.where(organization_id: org_ids, label_id: label_ids).
where("start_date <= ?", date).where("removed_date is null or removed_date >= ?", date).collect(&:person_id).uniq
value = 0
org_ids.each do |org_id|
org = Organization.find(org_id) if org_id != 0
value += org.all_people_with_archived_by_date(date).where(id: people_ids).count if org
end
@lines[field][date] = value
end
end
end
end
@fields_year_ago, @lines_year_ago = [], {}
if @chart.needs_year_ago_stats?
@fields_year_ago = @fields.clone
@fields_year_ago.each do |field|
@lines_year_ago[field] = {}
end
year_ago_begin = (@begin_date - 1.year).end_of_week(:sunday)
year_ago_end = (@end_date - 1.year).end_of_week(:sunday)
chart_year_ago_start_date = year_ago_begin + (interval - 1) * 7
chart_year_ago_end_date = year_ago_end + interval * 7 - 1 # make sure the last data point includes the data at the end date specified
unless @fields_year_ago.empty?
infobase_hash = {
activity_ids: @displayed_movements.collect(&:importable_id),
begin_date: year_ago_begin,
end_date: year_ago_end,
interval: interval,
semester: semester_stats_needed
}
begin
resp = RestClient.post(APP_CONFIG['infobase_url'] + "/statistics/collate_stats_intervals", infobase_hash.to_json, content_type: :json, accept: :json, authorization: "Bearer #{APP_CONFIG['infobase_token']}")
json = JSON.parse(resp)
rescue
raise resp.inspect
end
(chart_year_ago_start_date).step(chart_year_ago_end_date, interval * 7) do |date| # step through dates 1 interval at a time
@fields_year_ago.each do |field|
# add 364 days to the plot point to line it up with the current year and day of the week
@lines_year_ago[field][date + 364.days] = json[date.to_s][field] if @lines_year_ago[field] && json[date.to_s]
unless MovementIndicator.translate.values.include?(field)
org_ids = @displayed_movements.pluck(:id) + [0]
label_ids = Label.where(organization_id: org_ids, name: field).pluck(:id)
people_ids = OrganizationalLabel.where(organization_id: org_ids, label_id: label_ids).
where("start_date <= ?", date).where("removed_date is null or removed_date >= ?", date).collect(&:person_id).uniq
value = 0
org_ids.each do |org_id|
org = Organization.find(org_id) if org_id != 0
value += org.all_people_with_archived_by_date(date).where(id: people_ids).count if org
end
@lines_year_ago[field][date + 364.days] = value
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.