try to aggressively update signups

This commit is contained in:
Seth Call 2025-03-07 16:38:44 -06:00
parent 39dfbadbd8
commit 2a3d0784e2
3 changed files with 38 additions and 4 deletions

View File

@ -9,7 +9,7 @@ const JKAffiliateSignup = ({signup}) => {
{greaterThan.sm ? (
<tr>
<td>{signup.month}</td>
<td className="text-center">{signup.visits}</td>
{/*<td className="text-center">{signup.visits}</td>*/}
<td className="text-center">{signup.signups}</td>
</tr>
) : (
@ -17,11 +17,11 @@ const JKAffiliateSignup = ({signup}) => {
<Row className="mb-1">
<Col>{signup.month}</Col>
</Row>
<Row className="mb-1">
{/*<Row className="mb-1">
<Col>
<small>Visits: {signup.visits}</small>
</Col>
</Row>
</Row>*/}
<Row className="mb-1">
<Col>
<small>Signups: {signup.signups}</small>

View File

@ -227,7 +227,8 @@ class JamRuby::AffiliatePartner < ActiveRecord::Base
tally_monthly_payments(quarter_info[:year], quarter_info[:quarter])
tally_quarterly_payments(quarter_info[:year], quarter_info[:quarter])
tally_traffic_totals(GenericState.affiliate_tallied_at, day)
# we aren't tracking visits anymore, so we don't need to tally_traffic_totals
#tally_traffic_totals(GenericState.affiliate_tallied_at, day)
tally_partner_totals
@ -437,6 +438,34 @@ class JamRuby::AffiliatePartner < ActiveRecord::Base
close_months(year, quarter)
end
# This was added because the tally_traffic_totals runs once a day, which is not often enough to get a fresh count of signups
# so as users sign up, we increment the signups count for the day
# jam=# \d affiliate_traffic_totals
# Table "public.affiliate_traffic_totals"
# Column | Type | Modifiers
#----------------------+-----------------------------+------------------------
# day | date | not null
# signups | integer | not null default 0
# visits | integer | not null default 0
# affiliate_partner_id | integer | not null
# created_at | timestamp without time zone | not null default now()
def self.increment_signups(user)
sql = "SELECT count(day) FROM affiliate_traffic_totals WHERE day = '#{user.created_at.to_date}' AND affiliate_partner_id = #{user.affiliate_referral_id}"
count = ActiveRecord::Base.connection.execute(sql)
if count > 0
sql = %{
UPDATE affiliate_traffic_totals SET signups = signups + 1 WHERE day = '#{user.created_at.to_date}' AND affiliate_partner_id = #{user.affiliate_referral_id}
}
else
sql = %{
INSERT INTO affiliate_traffic_totals (day, signups, visits, affiliate_partner_id) VALUES ('#{user.created_at.to_date}', 1, 0, #{user.affiliate_referral_id})
}
end
ActiveRecord::Base.connection.execute(sql)
end
def self.tally_partner_totals
sql = %{
UPDATE affiliate_partners SET
@ -460,6 +489,8 @@ class JamRuby::AffiliatePartner < ActiveRecord::Base
return
end
# Because we now increment_signups, as users sign up, it's possible this row already exists. however, if there were no signups and only visits, there may still not be a row here.
# So we need to insert the rows, and if they already exist, the INSERT will be a no-op, as long as we also update this statement to not fail if the row already exists.
sql = %{
INSERT INTO affiliate_traffic_totals(SELECT day, 0, 0, ap.id FROM affiliate_partners AS ap CROSS JOIN (select (generate_series('#{start_date}', '#{end_date - 1}', '1 day'::interval))::date as day) AS lurp)
}

View File

@ -1865,6 +1865,9 @@ module JamRuby
if user.affiliate_referral = AffiliatePartner.find_by_id(affiliate_referral_id)
user.save
if !user.errors.any?
AffiliatePartner.increment_signups(user)
end
end if affiliate_referral_id.present?