Skip to content

Instantly share code, notes, and snippets.

@humphriesjm
Last active May 23, 2019 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save humphriesjm/0178c1cf53e33e8f8c4855342dd7a6b7 to your computer and use it in GitHub Desktop.
Save humphriesjm/0178c1cf53e33e8f8c4855342dd7a6b7 to your computer and use it in GitHub Desktop.
Show listings that happened for a given agent
# ----------------------------------------------
# SHOW LISTINGS THAT HAPPENED FOR A GIVEN AGENT
# ----------------------------------------------
# 1. given an agent, how many listings did they have since the beginning of the year?
# 2. for each listing, show the contact's name, address, star rating, and date the listing was first detected
def show_listings_proof(user_id, since_date=Time.now.beginning_of_year)
puts "agent_name,contact_name,address,city,state,max_star_rating_in_12_months_prior_to_listing,max_star_rating_date,date_of_listing"
if user_id.nil?
# no user_id -> show for ALL USERS
show_listings_counts_for_all_agents(since_date)
else
user = User.find(user_id)
show_listings_counts_for_agent(user, since_date)
end
return nil
end
def show_listings_counts_for_all_agents(since_date)
users = User.not_admin.live_customers
users.each do |u|
show_listings_counts_for_agent(u, since_date)
end
return nil
end
def show_listings_counts_for_agent(u, since_date)
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, addresses.address_key, addresses.state, addresses.city")
.select("people.id as contact_id, min(pre_mover_records.pub_date) first_premover_record_pub_date, addresses.address_key, addresses.state, addresses.city")
listings_found_in_network_by_person.each do |c|
contact = Person.find(c.contact_id)
state = c.state
city = c.city
star_rating, star_date = show_star_rating_for_contact_listing(c)
star_rating = "" if star_rating.nil?
puts "\"#{u.full_name}\",\"#{contact.full_name}\",\"#{c.address_key}\",#{city},#{state},#{star_rating},#{star_date},#{c.first_premover_record_pub_date}"
end
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: [{max_address_seller_scores: [address_seller_score: :predictive_model]}]}])
.where("predictive_models.created_at > ?", date_start)
.where("predictive_models.created_at <= ?", date_end)
.select("people.id AS id, max_address_seller_scores.percentile, predictive_models.created_at model_created_at")
return unless results.present?
return get_stars(results.first.percentile), results.first.model_created_at
end
# ALL USERS
show_listings_proof(nil)
# SINGLE USER
show_listings_proof(624)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment