Calculating Longest Streak for the Current Year
December 6, 2013
Today I was able to replace the code where I parse the entire users profile page for their longest streak, which is very expensive, with a much lesser expensive request where I only get the calendar data which is returned in a javascript array.
I can then parse that array and calculate their longest streak for the year. I still will need to figure out a way to calculate the longest streak for those users who have a streak greater than 366 days.
I’m my slow netbook which I have running this script it now takes around 20 seconds to collect 100 users and calculate their profile page. On my macbook it takes 10 seconds per 100 users. The only reason why I have it running on my netbook is because it is up 100% of time since it just sits their on my desk. I’ll probably move everything to rackspace or ec2, I just don’t feel like paying for an extra server right now.
Once I parse the javascript array and I only have the number of commits for each day I Then just count the number of consecutive days that aren’t 0’s:
def calc_longest_streak(arr)
longest = 0
current = 0
arr.each do |a|
if a > 0
current = current + 1
else
if current > longest
longest = current
end
current = 0
end
if current > longest
longest = current
end
end
longest
end