Skip to content

Instantly share code, notes, and snippets.

@tir38
Last active January 21, 2020 17:54
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 tir38/b21fe2940201046a1439b53934bc2598 to your computer and use it in GitHub Desktop.
Save tir38/b21fe2940201046a1439b53934bc2598 to your computer and use it in GitHub Desktop.
Find number of days of RescueTime data *above* a threshold. Example: How many days in 2019 did I work more than 15 minutes.

Find number of days of RescueTime data above a threshold.

Example: How many days in 2019 did I work more than 15 minutes?

$ ruby script.rb

Processed data for 281 days;
Found 259 above threshold of 15 minutes of activity.

I find this useful for getting the number of days in a year that I actually did work. If I logged in to my work machine just to look up a contact or find a bookmark, I don't want to count those as "work days". I personally set the threshold at 15 minutes, but it's up to you.

require 'net/http'
require 'json'
# make web request
yourApiKey = "YOUR_API_KEY" # https://www.rescuetime.com/anapi/manage
url = "https://www.rescuetime.com/anapi/data?key=#{yourApiKey}&format=json&perspective=interval&restrict_kind=overview&resolution_time=day&restrict_begin=2019-01-01&restrict_end=2019-12-31"
uri = URI(url)
response = Net::HTTP.get(uri)
object = JSON.parse(response, object_class: OpenStruct)
puts "found #{object.rows.size} rows of data"
# iterate and find days above threshold
# remember: each row is a category, so there are multiple rows per day
currentDate = ""
totalTimeForDay = 0
dayCount = 0
thresholdMinutes = 15
dayAboveThresholdCount = 0
object.rows.each do |item|
date = item[0]
if currentDate != date
totalTimeMinutes = totalTimeForDay / 60
puts "\t#{currentDate} total time is: #{totalTimeMinutes} minutes"
dayCount += 1
if totalTimeMinutes >= thresholdMinutes
dayAboveThresholdCount += 1
end
# reset for next day
currentDate = date
totalTimeForDay = 0
end
totalTimeForDay += item[1]
end
puts "Processed data for #{dayCount} days;
Found #{dayAboveThresholdCount} above threshold of #{thresholdMinutes} minutes of activity."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment