Skip to content

Instantly share code, notes, and snippets.

@thescubageek
Last active August 23, 2017 17:47
Show Gist options
  • Save thescubageek/827d3912017879c427090036005a9d8a to your computer and use it in GitHub Desktop.
Save thescubageek/827d3912017879c427090036005a9d8a to your computer and use it in GitHub Desktop.
HUB: Sort Clients by Location Count
RISK_FACTORS = {
"Senior-Living": 32,
"Apartments": 15,
"Self-Storage": 20,
"Veterinarian": 9
}.freeze
def clients_by_vertical_and_location_count(vertical, g5_internal=false)
Client.where(vertical: vertical)
.where(g5_internal: g5_internal)
.map { |c| {urn: c.urn, count: c.locations.count, name: c.name, risk: c.locations.count * RISK_FACTORS[c.vertical.to_sym]} }
.sort { |a,b| a[:count] <=> b[:count] }
end
def clients_by_scope_risk
clients_by_vert = RISK_FACTORS.keys.inject([]) do |arr, vertical|
arr << clients_by_vertical_and_location_count(vertical)
arr
end
clients_by_vert.flatten.sort { |a,b| a[:risk] <=> b[:risk] }
end
def group_clients_by_risk
risked_clients = clients_by_scope_risk.map do |c|
G5HerokuAppNameFormatter::Formatter.new(c[:urn], 'cms').cms_app_name
end
ret = {green: [], yellow: [], red: []}
one_fourth = (risked_clients.count*0.25).floor
three_fourths = (risked_clients.count*0.75).floor
ret[:green] = risked_clients.slice(0, one_fourth)
ret[:yellow] = risked_clients.slice(one_fourth, three_fourths-one_fourth)
ret[:red] = risked_clients.slice(three_fourths, risked_clients.count - three_fourths)
ret
end
def client_risk_files
group_clients_by_risk.each do |risk, cs|
cs.to_file("#{risk}.txt")
end
end
class Array
def to_file(filepath, sep="\n")
File.open(filepath, 'w') { |f| f.write(join(sep)) } unless blank?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment