Skip to content

Instantly share code, notes, and snippets.

@botanicus
Created January 20, 2015 07:33
Show Gist options
  • Save botanicus/e385d457d7d34d8c3d0f to your computer and use it in GitHub Desktop.
Save botanicus/e385d457d7d34d8c3d0f to your computer and use it in GitHub Desktop.
Bill your clients based on times when a Terminal with their project was running (assuming that's the time you spent developing it).
#!/usr/bin/env ruby
require 'terminal.com'
require 'date'
# Usage: ./billable-hours.rb 2015/9/1
invoice_from = Date.parse(ARGV.shift).to_time
# response = Terminal.terminal_usage_history("user_token", "access_token")
response = JSON.parse(DATA.read)
projects = response['usage'].values.reduce(Hash.new) do |projects, blob|
blob['intervals'].each do |interval|
start_timestamp, end_timestamp = interval.values_at('start', 'end')
start_time, end_time = Time.at(start_timestamp.to_i), Time.at(end_timestamp.to_i)
if start_time < invoice_from
projects[blob['subdomain']] ||= Array.new
projects[blob['subdomain']] << (start_time..end_time)
end
end
projects
end
projects.each do |project, billable_intervals|
dates = billable_intervals.group_by { |interval| interval.first.strftime('%d/%m') }
dates.each do |date, billable_intervals|
seconds_spent = billable_intervals.reduce(0) do |sum, interval|
sum + (interval.end - interval.begin)
end
puts "#{date}: You spent #{(seconds_spent / 3600).round(2)} hours on #{project}"
end
end
# projects = response['usage'].values.reduce(Hash.new) do |projects, blob|
# projects[blob['subdomain']] ||= Array.new
# blob['intervals'].each do |interval|
# start_time, end_time = interval.values_at('start', 'end')
# difference = Time.at(end_time.to_i) - Time.at(start_time.to_i)
# projects[blob['subdomain']] << difference
# end
# projects
# end
# sort_by { |blob| blob['subdomain'] }#.keys
__END__
{
"usage": {
"00000000-0000-0000-0000-000000000000": {
"subdomain": "test1",
"container_key": "00000000-0000-0000-0000-000000000000",
"intervals": []
},
"00000000-0000-0000-0000-000000000001": {
"subdomain": "test2",
"container_key": "00000000-0000-0000-0000-000000000001",
"intervals": [
{
"start": "1399075766",
"end": "1401230005",
"state": {
"cpus": 2,
"ram": 256,
"disk": 10317112,
"status": "running"
}
}
]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment