Last active
May 22, 2019 17:15
-
-
Save humphriesjm/5b922d2f72b92bfcffaa8fc4cc840269 to your computer and use it in GitHub Desktop.
Listings in agents' networks & Predicted (starred) contacts who listed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1. given a brokerage (or all users), how many listings did each agent have? | |
# 2. from those listings, how many did First "predict"? | |
# 3 star over the last 12 months = predicted | |
def show_listings_proof(brokerage_name=nil, since_date=Time.now.beginning_of_year) | |
show_listings_counts_per_agent_for_brokerage(brokerage_name, since_date) | |
end | |
def show_listings_counts_per_agent_for_brokerage(brokerage_name, since_date) | |
if brokerage_name.nil? | |
users = User.not_admin.live_customers | |
brokerage_name = "All Users" | |
else | |
users = User.where(brokerage: brokerage_name) | |
end | |
puts "name,listings_count_for_user,three_star_count,two_star_count,one_star_count,three_star_hit_rate_user,two_star_hit_rate_user,one_star_hit_rate_user" | |
brokerage_summary = { | |
one_star: 0, | |
two_star: 0, | |
three_star: 0, | |
total_listings: 0, | |
} | |
users.each do |u| | |
user_summary = { | |
one_star: 0, | |
two_star: 0, | |
three_star: 0, | |
total_listings: 0, | |
} | |
listings_found_in_network = | |
u.contacts | |
.of_interest | |
.joins(preferred_person_match: {property: {address: :pre_mover_records}}) | |
.where("date(pre_mover_records.created_at) > ?", since_date) | |
num_listings_found_in_network = listings_found_in_network.select("people.id").distinct.count | |
listings_found_in_network_by_person = | |
listings_found_in_network | |
.group("people.id") | |
.select("people.id as contact_id, min(pre_mover_records.pub_date) first_premover_record_pub_date") | |
user_summary[:total_listings] = num_listings_found_in_network | |
brokerage_summary[:total_listings] += num_listings_found_in_network | |
listings_found_in_network_by_person.each do |c| | |
star_rating = show_star_rating_for_contact_listing(c) | |
if star_rating == 1 | |
user_summary[:one_star] += 1 | |
brokerage_summary[:one_star] += 1 | |
elsif star_rating == 2 | |
user_summary[:two_star] += 1 | |
brokerage_summary[:two_star] += 1 | |
elsif star_rating == 3 | |
contact = Person.find(c.contact_id) | |
user_summary[:three_star] += 1 | |
brokerage_summary[:three_star] += 1 | |
end | |
end | |
listings_count_for_user = user_summary[:total_listings] | |
three_star_count = user_summary[:three_star] | |
two_star_count = user_summary[:two_star] | |
one_star_count = user_summary[:one_star] | |
three_star_hit_rate_user = (1.0 * three_star_count / listings_count_for_user).round(4) | |
two_star_hit_rate_user = (1.0 * two_star_count / listings_count_for_user).round(4) | |
one_star_hit_rate_user = (1.0 * one_star_count / listings_count_for_user).round(4) | |
if listings_count_for_user == 0 | |
three_star_hit_rate_user = 0 | |
two_star_hit_rate_user = 0 | |
one_star_hit_rate_user = 0 | |
end | |
puts "#{u.full_name},#{listings_count_for_user},#{three_star_count},#{two_star_count},#{one_star_count},#{three_star_hit_rate_user},#{two_star_hit_rate_user},#{one_star_hit_rate_user}" | |
end | |
listings_count_for_brokerage = brokerage_summary[:total_listings] | |
three_star_count = brokerage_summary[:three_star] | |
two_star_count = brokerage_summary[:two_star] | |
one_star_count = brokerage_summary[:one_star] | |
three_star_hit_rate_brokerage = (1.0 * three_star_count / listings_count_for_brokerage).round(4) | |
two_star_hit_rate_brokerage = (1.0 * two_star_count / listings_count_for_brokerage).round(4) | |
one_star_hit_rate_brokerage = (1.0 * one_star_count / listings_count_for_brokerage).round(4) | |
if listings_count_for_brokerage == 0 | |
three_star_hit_rate_brokerage = 0 | |
two_star_hit_rate_brokerage = 0 | |
one_star_hit_rate_brokerage = 0 | |
end | |
puts "#{brokerage_name},#{listings_count_for_brokerage},#{three_star_count},#{two_star_count},#{one_star_count},#{three_star_hit_rate_brokerage},#{two_star_hit_rate_brokerage},#{one_star_hit_rate_brokerage}" | |
return nil | |
end | |
def get_stars(percentile) | |
return unless percentile.present? | |
if percentile >= 90 | |
return 3 | |
elsif percentile < 90 && percentile >= 85 | |
return 2 | |
elsif percentile < 85 && percentile >= 77 | |
return 1 | |
else | |
return 0 | |
end | |
end | |
def show_star_rating_for_contact_listing(contact_listing) | |
contact_id = contact_listing.contact_id | |
date_end = contact_listing.first_premover_record_pub_date # - 2.months | |
date_start = contact_listing.first_premover_record_pub_date - 1.year | |
results = Person | |
.where(id: contact_id) | |
.joins(preferred_person_match: [property: {address: [address_seller_scores: :predictive_model]}]) | |
.where("predictive_models.created_at > ?", date_start) | |
.where("predictive_models.created_at <= ?", date_end) | |
.group("people.id") | |
.select("people.id AS id, MAX(address_seller_scores.percentile) AS max_percentile") | |
return unless results.present? | |
return get_stars(results.first.max_percentile) | |
end | |
# This shows listings info for ALL USERS | |
show_listings_proof(nil) | |
show_listings_proof('@properties') | |
show_listings_proof('Ansley') | |
show_listings_proof('Beverly-Hanks') | |
show_listings_proof('C21 Advantage Gold') | |
show_listings_proof('C21 Results') | |
show_listings_proof('Climb') | |
show_listings_proof('Dwellings') | |
show_listings_proof('Hometown Advisor') | |
show_listings_proof('KY Select') | |
show_listings_proof('Moving The Mitten') | |
show_listings_proof('One South') | |
show_listings_proof('Savvy + Co.') | |
show_listings_proof('Warburg') | |
show_listings_proof('West + Main') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment