Skip to content

Instantly share code, notes, and snippets.

@katherinewallace
Last active August 29, 2015 14:22
Show Gist options
  • Save katherinewallace/af619ec39715b1ee91ee to your computer and use it in GitHub Desktop.
Save katherinewallace/af619ec39715b1ee91ee to your computer and use it in GitHub Desktop.
# +----------+------------------+
# | COUNT(*) | missed_threshold |
# +----------+------------------+
# | 677 | NULL |
# | 870 | 4 |
# | 487 | 8 |
# | 14 | 9 |
# | 1 | 11 |
# | 95 | 12 |
# | 188 | 13 |
# +----------+------------------+
CSV.open("/tmp/blank_days.csv", "wb") do |csv|
csv << ["user_id", "old_blanks", "new_blanks", "diff_champ"]
User.includes(:profile).where("profiles.missed_threshold != 4") # null is default set to 4
.references(:profile)
.where("email NOT LIKE '%example.com'")
.where("email NOT LIKE '%monaeo.com'").each do |user| # 447 Users
old_cal = Gonaeo.calendar(user, user.profile.data_start_date, Date.today)
old_missed_threshold = user.profile.missed_threshold
user.profile.update_attribute(:missed_threshold, 4)
Gonaeo.clear_cache(user)
new_cal = Gonaeo.calendar(user, user.profile.data_start_date, Date.today)
old_blanks, new_blanks, diff_champ = "", "", ""
old_cal.each_with_index do |day, idx|
date = Date.parse(day[:date])
next if date == Date.today
if (user.profile.track_weekends || date.wday.between?(1, 5))
if [:spans].empty?
old_blanks += day[:date] + ","
elsif !day[:missed]
old_champ = day[:spans][day[:champion]]
end
if new_cal[idx][:spans].empty?
new_blanks += day[:date] + ","
elsif !day[:missed]
new_champ = new_cal[idx][:spans][new_cal[idx][:champion]]
end
if old_champ && new_champ &&
(old_champ[:location_id] != new_champ[:location_id] ||
old_champ[:activity] != new_champ[:activity])
diff_champ += day[:date] + ","
end
end
end
if [old_blanks, new_blanks, diff_champ].any?(&:present?)
csv << [user.id, old_blanks, new_blanks, diff_champ]
File.write("/tmp/#{user.id}_old.txt", old_cal.to_json)
File.write("/tmp/#{user.id}_new.txt", new_cal.to_json)
end
user.profile.update(missed_threshold: old_missed_threshold)
Gonaeo.clear_cache(user)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment