Created
August 22, 2017 03:21
-
-
Save tmd45/7f6e49acbc35937b50de25a5886622b0 to your computer and use it in GitHub Desktop.
Separate GCP billing by project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rake' | |
require 'csv' | |
# | |
# Usage: | |
# | |
# Need files(.csv) in same folder "junk". | |
# | |
# $ ruby junk/classify_gcp_payments.rb project-name | |
# | |
project = ARGV[0] | |
p project | |
file_list = FileList.new('junk/account_activities_*.csv') | |
regexp_filename = /^junk\/account_activities_(.*)\.csv$/ | |
csv_opt = { | |
headers: true, | |
return_headers: false | |
} | |
results = {} | |
file_list.each do |filename| | |
term = regexp_filename.match(filename).to_a[1] # filename から YYYYMM を取得 | |
amounts = [] | |
CSV.foreach(filename, "r:bom|utf-8") do |row| | |
next unless row[0] == 'COSTS' | |
next unless row[1].include? project | |
amounts << row[2].to_f | |
end | |
results[term] = amounts.inject(:+).round(2) | |
end | |
p results.keys | |
p results.values |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
表示によってエクスポートできるCSV形式がことなるので注意 😱