Created
July 10, 2012 02:19
-
-
Save sawanoboly/3080589 to your computer and use it in GitHub Desktop.
Convert json to csv for Joyent SmartDataCenter network billing.
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
require 'csv' | |
require 'json' | |
## machines | |
owner_uuid = "oooooooo-oooo-oooo-oooo-oooooooooooo" | |
zone_uuids = [ | |
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", | |
"yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy", | |
"zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz" ] | |
csvh = ["zone_uuid","net_if","period_start","period_end","Megabytes_sent_delta","Megabytes_received_delta"] | |
nifs = ["net0", "net1"] | |
jfile = "data/bill.json" | |
rfile = "result/result.csv" | |
jj = JSON.load(File.read(jfile)) | |
zoneset = jj[owner_uuid] | |
CSV.open(rfile, "wb",:force_quotes => true) do |writer| | |
writer << csvh | |
zone_uuids.each do |zone| | |
nifs.each do |nif| | |
period_start = zoneset[zone]["metering"]["network"][nif]["period_start"] | |
period_end = zoneset[zone]["metering"]["network"][nif]["period_end"] | |
sent_delta = zoneset[zone]["metering"]["network"][nif]["bytes_sent_delta"] / 1024 / 1024 | |
received_delta = zoneset[zone]["metering"]["network"][nif]["bytes_received_delta"] / 1024 / 1024 | |
writer << [zone, nif, period_start, period_end, sent_delta, received_delta] | |
end | |
end | |
end | |
## -- result sample | |
# "zone_uuid","net_if","period_start","period_end","Megabytes_sent_delta","Megabytes_received_delta" | |
# "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx","net0","2012-01-01T00:00:00.005Z","2012-01-31T23:59:59.000Z","9288","272" | |
# "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx","net1","2012-01-01T00:00:00.005Z","2012-01-31T23:59:59.000Z","130","69" | |
# "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy","net0","2012-01-01T00:00:00.012Z","2012-01-31T23:59:59.000Z","9999","363" | |
# "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy","net1","2012-01-01T00:00:00.012Z","2012-01-31T23:59:59.000Z","90","69" | |
# "zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz","net0","2012-01-01T00:00:00.022Z","2012-01-31T23:59:59.000Z","9383","276" | |
# "zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz","net1","2012-01-01T00:00:00.022Z","2012-01-31T23:59:59.000Z","104","70" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment