Skip to content

Commit

Permalink
Fix updating all_time value for *other* sponsors (#854)
Browse files Browse the repository at this point in the history
For sponsors who are not tracked through OpenCollective we need to update the `all_time` value explicitly.
#710 introduced an automation for this but it was completely flawed and never actually worked as intended (see #851 (comment)).

With this patch we keep track of `time_last_payment` in `_data/others.json`. The original value is set to March 2024 when we last updated the current `all_time` values.
The merge script now automatically adds the amount of the current montly contribution for every month since the last update. Usually the script runs at least once per month, so it should only add one amount. But it also catches up gracefully if there's an interruption (and it works for the initial catch up).
The script then writes the updated values for `all_time` and `time_last_payment` (set to now) back to `_data/others.json` and commits it so we keep track of which monthly contribution has already been processed.
  • Loading branch information
straight-shoota authored Oct 10, 2024
1 parent cc9c6c2 commit 6cd606f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 27 deletions.
6 changes: 4 additions & 2 deletions _data/others.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"url": "https://manas.tech/",
"logo": "manas-orange.svg",
"last_payment": 5000,
"all_time": 1300000,
"time_last_payment": "Oct 8, 2024",
"all_time": 1335000,
"since": "Jun 19, 2009"
},
{
Expand All @@ -18,7 +19,8 @@
"url": "https://www.84codes.com/",
"logo": "sponsors/84.png",
"last_payment": 22000,
"all_time": 369000,
"time_last_payment": "Oct 8, 2024",
"all_time": 523000,
"currency": "",
"since": "Apr 1, 2018"
},
Expand Down
4 changes: 2 additions & 2 deletions _data/sponsor_logos_l.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
logo,name,url,last_payment,all_time,since,level
sponsors/84.png,84codes,https://www.84codes.com/,"€22,000","€391,000","Apr 1, 2018",5000
manas-orange.svg,Manas.Tech,https://manas.tech/,"$5,000","$1,305,000","Jun 19, 2009",5000
sponsors/84.png,84codes,https://www.84codes.com/,"€22,000","€523,000","Apr 1, 2018",5000
manas-orange.svg,Manas.Tech,https://manas.tech/,"$5,000","$1,335,000","Jun 19, 2009",5000
sponsors/buy_google_reviews.jpg,Buy Google Reviews,https://buyreviewz.com/buy-google-reviews,$750,"$1,500","Aug 7, 2024",750
sponsors/buy_instagram_followers_thunderclapit.png,Buy Instagram Followers Thunderclapit,https://thunderclap.it/buy-instagram-followers,$750,"$1,500","Aug 7, 2024",750
4 changes: 2 additions & 2 deletions _data/sponsors.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
logo,name,url,last_payment,all_time,since,level
sponsors/84.png,84codes,https://www.84codes.com/,"€22,000","€391,000","Apr 1, 2018",5000
manas-orange.svg,Manas.Tech,https://manas.tech/,"$5,000","$1,305,000","Jun 19, 2009",5000
sponsors/84.png,84codes,https://www.84codes.com/,"€22,000","€523,000","Apr 1, 2018",5000
manas-orange.svg,Manas.Tech,https://manas.tech/,"$5,000","$1,335,000","Jun 19, 2009",5000
sponsors/buy_google_reviews.jpg,Buy Google Reviews,https://buyreviewz.com/buy-google-reviews,$750,"$1,500","Aug 7, 2024",750
sponsors/buy_instagram_followers_thunderclapit.png,Buy Instagram Followers Thunderclapit,https://thunderclap.it/buy-instagram-followers,$750,"$1,500","Aug 7, 2024",750
sponsors/placeos.png,PlaceOS,https://place.technology/,$250,"$8,750","Jul 26, 2021",75
Expand Down
37 changes: 16 additions & 21 deletions scripts/merge.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ end
all_sponsors_map = Hash(UInt64, Sponsor).new
overrides = Array(Sponsor).new

update_other_sponsor_totals = ENV["UPDATE_OTHER_SPONSOR_TOTALS"]? == "1" || Time.utc.day == 1 # only do this on the first day of the month

SPONSOR_DATA = begin
csv = CSV.new(File.read("#{__DIR__}/../_data/sponsors.csv"), headers: true)
hash = Hash(UInt64, Int32).new
Expand All @@ -28,28 +26,25 @@ SPONSOR_DATA = begin
end

%w(opencollective.json bountysource.json others.json).each do |filename|
File.open("#{__DIR__}/../_data/#{filename}") do |file|
sponsors = Array(Sponsor).from_json(file)

if filename == "others.json"
sponsors, overrides = sponsors.partition(&.overrides.nil?)
if update_other_sponsor_totals
sponsors.map! do |sponsor|
prev_value = SPONSOR_DATA[sponsor.id]?
if !prev_value
Log.warn { "Can't find sponsor '#{sponsor.name}' in sponsors.csv" }
prev_value = 0
end
sponsor.all_time = prev_value + sponsor.last_payment
sponsor
end
path = "#{__DIR__}/../_data/#{filename}"
sponsors = Array(Sponsor).from_json(File.read(path))

if filename == "others.json"
now = Time.utc
sponsors.map!(&.update_all_time!(now))
File.open(path, "w") do |file|
JSON.build(file, indent: 2) do |builder|
sponsors.to_json(builder)
end
file.puts # write newline at end of file
end

sponsors.each do |sponsor|
prev_sponsor = all_sponsors_map[sponsor.id]?
all_sponsors_map[sponsor.id] = prev_sponsor ? sponsor.merge(prev_sponsor) : sponsor
end
sponsors, overrides = sponsors.partition(&.overrides.nil?)
end

sponsors.each do |sponsor|
prev_sponsor = all_sponsors_map[sponsor.id]?
all_sponsors_map[sponsor.id] = prev_sponsor ? sponsor.merge(prev_sponsor) : sponsor
end
end

Expand Down
31 changes: 31 additions & 0 deletions scripts/sponsors.cr
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,37 @@ record Sponsor, name : String, url : String?, logo : String?, last_payment : Flo
return {other.last_payment, other.time_last_payment} if time_last_payment.nil? || time_last_payment.not_nil! < other.time_last_payment.not_nil!
{last_payment, time_last_payment}
end

def update_all_time!(now)
return self if overrides || last_payment.zero?
time_last_payment = self.time_last_payment || return self

months_since_last_payment = (now.year - time_last_payment.year) * 12 + now.month - time_last_payment.month
return self unless months_since_last_payment > 0

self.time_last_payment = now
self.all_time += (months_since_last_payment * last_payment)
self
end

def to_json(builder : JSON::Builder)
builder.object do
builder.field "overrides", overrides if overrides
builder.field "name", name
builder.field "url", url if url
builder.field "logo", logo if logo
builder.field "last_payment", last_payment.to_i
if time_last_payment = self.time_last_payment
builder.field "time_last_payment", time_last_payment.to_s("%b %-d, %Y")
end
builder.field "all_time", all_time.to_i
builder.field "currency", currency if currency
if since = self.since
builder.field "since", since.to_s("%b %-d, %Y")
end
builder.field "listed", listed? unless listed?
end
end
end

class SponsorsBuilder
Expand Down

0 comments on commit 6cd606f

Please sign in to comment.