Skip to content

Instantly share code, notes, and snippets.

@davidcelis
Created September 9, 2011 18:27
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 davidcelis/1206955 to your computer and use it in GitHub Desktop.
Save davidcelis/1206955 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'time'
require 'fileutils'
desc 'generate edi reports'
task :edi_reports => :environment do
MONTHS_AGO = 0 # 0 is last month/current report
BASE_PATH = '/tmp/'
sdge_sums = {}
pge_sums = {}
sce_sums = {}
[
{:data_key_id => 301, :meter_id => '04614-1', :csi_application => 'SD-CSI-04614', :first_report => '01/01/2010', :format => :sdge},
{:data_key_id => 300, :meter_id => '04614-2', :csi_application => 'SD-CSI-04614', :first_report => '01/01/2010', :format => :sdge},
{:data_key_id => 302, :meter_id => '04614-3', :csi_application => 'SD-CSI-04614', :first_report => '01/01/2010', :format => :sdge},
{:data_key_id => 322, :meter_id => '04628-1', :csi_application => 'SD-CSI-04628', :first_report => '03/01/2010', :format => :sdge},
{:data_key_id => 4833, :meter_id => '04449-1', :csi_application => 'SD-CSI-04449', :first_report => '05/01/2011', :format => :sdge},
{:data_key_id => 4836, :meter_id => '04449-2', :csi_application => 'SD-CSI-04449', :first_report => '05/01/2011', :format => :sdge},
{:data_key_id => 4837, :meter_id => '04449-3', :csi_application => 'SD-CSI-04449', :first_report => '05/01/2011', :format => :sdge},
{:data_key_id => 4842, :meter_id => '04449-4', :csi_application => 'SD-CSI-04449', :first_report => '05/01/2011', :format => :sdge},
{:data_key_id => 4843, :meter_id => '04449-5', :csi_application => 'SD-CSI-04449', :first_report => '05/01/2011', :format => :sdge},
{:data_key_id => 2608, :meter_id => '04449-6', :csi_application => 'SD-CSI-04449', :first_report => '05/01/2011', :format => :sdge},
{:data_key_id => 3143, :meter_id => '04449-7', :csi_application => 'SD-CSI-04449', :first_report => '05/01/2011', :format => :sdge},
{:data_key_id => 4839, :meter_id => '04449-8', :csi_application => 'SD-CSI-04449', :first_report => '05/01/2011', :format => :sdge},
{:data_key_id => 4838, :meter_id => '04449-9', :csi_application => 'SD-CSI-04449', :first_report => '05/01/2011', :format => :sdge},
{:data_key_id => 4840, :meter_id => '04449-10', :csi_application => 'SD-CSI-04449', :first_report => '05/01/2011', :format => :sdge},
{:data_key_id => 4841, :meter_id => '04449-11', :csi_application => 'SD-CSI-04449', :first_report => '05/01/2011', :format => :sdge},
{:data_key_id => 4844, :meter_id => '04449-12', :csi_application => 'SD-CSI-04449', :first_report => '05/01/2011', :format => :sdge},
{:data_key_id => 4845, :meter_id => '04449-13', :csi_application => 'SD-CSI-04449', :first_report => '05/01/2011', :format => :sdge},
{:data_key_id => 4846, :meter_id => '04449-14', :csi_application => 'SD-CSI-04449', :first_report => '05/01/2011', :format => :sdge},
{:data_key_id => 4847, :meter_id => '04449-15', :csi_application => 'SD-CSI-04449', :first_report => '05/01/2011', :format => :sdge},
{:data_key_id => 4834, :meter_id => '04449-16', :csi_application => 'SD-CSI-04449', :first_report => '05/01/2011', :format => :sdge},
{:data_key_id => 4835, :meter_id => '04449-17', :csi_application => 'SD-CSI-04449', :first_report => '05/01/2011', :format => :sdge},
{:data_key_id => 3382, :meter_id => '06028-1', :csi_application => 'SD-CSI-06028', :first_report => '04/01/2011', :format => :sdge},
{:data_key_id => 4540, :meter_id => '05504-1', :csi_application => 'SD-CSI-05504', :first_report => '06/01/2011', :format => :sdge},
{:data_key_id => 5280, :meter_id => '05504-2', :csi_application => 'SD-CSI-05504', :first_report => '06/01/2011', :format => :sdge},
{:data_key_id => 2272, :meter_id => '06567-001', :csi_application => 'SD-CSI-06567', :first_report => '03/16/2011', :format => :sdge},
{:data_key_id => 4095, :meter_id => '150070716', :csi_application => 'SD-CSI-06592', :first_report => '03/16/2011', :format => :sdge},
{:data_key_id => 3730, :meter_id => '2780910101207', :csi_application => 'SD-CSI-03087', :first_report => '04/16/2011', :format => :sdge},
{:data_key_id => 6436, :meter_id => '07421-001', :csi_application => 'SD-CSI-07421', :first_report => '08/16/2011', :format => :sdge},
{:data_key_id => 5640, :meter_id => '05406-1', :csi_application => 'SD-CSI-05406', :first_report => '10/01/2011', :format => :sdge},
{:data_key_id => 698, :meter_id => 'CS00556', :csi_application => 'PGE-CSI-20071', :first_report => '07/01/11', :format => :pge, :channel_id => 'C0666'},
{:data_key_id => 254, :meter_id => 'CS00436', :csi_application => 'PGE-CSI-13122', :first_report => '12/01/09', :format => :pge, :channel_id => 'C0539'},
{:data_key_id => 281, :meter_id => 'CS00437', :csi_application => 'PGE-CSI-13122', :first_report => '12/01/09', :format => :pge, :channel_id => 'C0540'},
{:data_key_id => 1368, :meter_id => 'CS00739', :csi_application => 'PGE-CSI-28362', :first_report => '01/01/11', :format => :pge, :channel_id => 'C0739'},
{:data_key_id => 1754, :meter_id => 'CS00629', :csi_application => 'PGE-CSI-23759', :first_report => '11/01/11', :format => :pge, :channel_id => 'C0738'},
{:data_key_id => 3393, :meter_id => 'CS00737', :csi_application => 'PGE-CSI-28032', :first_report => '04/01/11', :format => :pge, :channel_id => 'C0846'},
{:data_key_id => 1908, :meter_id => 'CS00606', :csi_application => 'PGE-CSI-28285', :first_report => '11/01/11', :format => :pge, :channel_id => 'C0715'},
{:data_key_id => 3034, :meter_id => 'CS00739', :csi_application => 'PGE-CSI-29507', :first_report => '03/01/11', :format => :pge, :channel_id => 'C0848'},
{:data_key_id => 3102, :meter_id => 'CS00676', :csi_application => 'PGE-CSI-32151', :first_report => '02/01/11', :format => :pge, :channel_id => 'C0785'},
{:data_key_id => 2383, :meter_id => 'CS00672', :csi_application => 'PGE-CSI-16651', :first_report => '03/01/11', :format => :pge, :channel_id => 'C0781'},
{:data_key_id => 418, :meter_id => 'CS00552', :csi_application => 'PGE-CSI-22527', :first_report => '08/01/11', :format => :pge, :channel_id => 'C0662'},
{:data_key_id => 1158, :meter_id => 'CS00680', :csi_application => 'PGE-CSI-24624', :first_report => '02/01/11', :format => :pge, :channel_id => 'C0789'},
{:data_key_id => 1160, :meter_id => 'CS00681', :csi_application => 'PGE-CSI-24624', :first_report => '02/01/11', :format => :pge, :channel_id => 'C0790'},
{:data_key_id => 1460, :meter_id => 'CS00576', :csi_application => 'PGE-CSI-25766', :first_report => '01/01/11', :format => :pge, :channel_id => 'C0686'},
{:data_key_id => 1461, :meter_id => 'CS00577', :csi_application => 'PGE-CSI-25766', :first_report => '01/01/11', :format => :pge, :channel_id => 'C0687'},
{:data_key_id => 1462, :meter_id => 'CS00578', :csi_application => 'PGE-CSI-25766', :first_report => '01/01/11', :format => :pge, :channel_id => 'C0688'},
{:data_key_id => 1463, :meter_id => 'CS00579', :csi_application => 'PGE-CSI-25766', :first_report => '01/01/11', :format => :pge, :channel_id => 'C0689'},
{:data_key_id => 405, :meter_id => 'CS00594', :csi_application => 'PGE-CSI-17539', :first_report => '11/01/10', :format => :pge, :channel_id => 'C0704'},
{:data_key_id => 406, :meter_id => 'CS00596', :csi_application => 'PGE-CSI-17539', :first_report => '11/01/10', :format => :pge, :channel_id => 'C0705'},
{:data_key_id => 407, :meter_id => 'CS00597', :csi_application => 'PGE-CSI-17539', :first_report => '11/01/10', :format => :pge, :channel_id => 'C0706'},
{:data_key_id => 1525, :meter_id => 'CS00573', :csi_application => 'PGE-CSI-22760', :first_report => '09/01/10', :format => :pge, :channel_id => 'C0683'},
{:data_key_id => 4065, :meter_id => 'CS00744', :csi_application => 'PGE-CSI-25828', :first_report => '05/01/11', :format => :pge, :channel_id => 'C0853'},
{:data_key_id => 4070, :meter_id => 'CS00743', :csi_application => 'PGE-CSI-25828', :first_report => '05/01/11', :format => :pge, :channel_id => 'C0852'},
{:data_key_id => 4071, :meter_id => 'CS00745', :csi_application => 'PGE-CSI-25828', :first_report => '05/01/11', :format => :pge, :channel_id => 'C0854'},
{:data_key_id => 1068, :meter_id => 'CS00541', :csi_application => 'PGE-CSI-23173', :first_report => '07/01/10', :format => :pge, :channel_id => 'C0651', :channel => 1147},
{:data_key_id => 1367, :meter_id => 'CS00609', :csi_application => 'PGE-CSI-21396', :first_report => '01/01/11', :format => :pge, :channel_id => 'C0718'},
{:data_key_id => 903, :meter_id => 'CS00584', :csi_application => 'PGE-CSI-20272', :first_report => '08/16/10', :format => :pge, :channel_id => ''},
{:data_key_id => 2070, :meter_id => 'CS00687', :csi_application => 'PGE-CSI-16352', :first_report => '12/16/10', :format => :pge, :channel_id => 'C0796'},
{:data_key_id => 1454, :meter_id => 'CS00586', :csi_application => 'PGE-CSI-27663', :first_report => '08/16/10', :format => :pge, :channel_id => 'C0696'},
{:data_key_id => 1453, :meter_id => 'CS00588', :csi_application => 'PGE-CSI-27663', :first_report => '08/16/10', :format => :pge, :channel_id => 'C0698'},
{:data_key_id => 2111, :meter_id => 'CS00757', :csi_application => 'PGE-CSI-28071', :first_report => '12/16/10', :format => :pge, :channel_id => 'C0767'},
{:data_key_id => 2112, :meter_id => 'CS00758', :csi_application => 'PGE-CSI-28071', :first_report => '12/16/10', :format => :pge, :channel_id => 'C0867'},
{:data_key_id => 2919, :meter_id => 'CS00698', :csi_application => 'PGE-CSI-27662', :first_report => '02/16/11', :format => :pge, :channel_id => 'C0807'},
{:data_key_id => 4150, :meter_id => 'CS00760', :csi_application => 'PGE-CSI-33631', :first_report => '04/16/11', :format => :pge, :channel_id => 'C0869', :channel => 4338},
{:data_key_id => 3880, :meter_id => 'CS00724', :csi_application => 'PGE-CSI-26894', :first_report => '03/16/11', :format => :pge, :channel_id => 'C0833'},
{:data_key_id => 3881, :meter_id => 'CS00725', :csi_application => 'PGE-CSI-26894', :first_report => '03/16/11', :format => :pge, :channel_id => 'C0834'},
{:data_key_id => 3882, :meter_id => 'CS00726', :csi_application => 'PGE-CSI-26894', :first_report => '03/16/11', :format => :pge, :channel_id => 'C0835'},
{:data_key_id => 1720, :meter_id => 'CS00543', :csi_application => 'PGE-CSI-22617', :first_report => '10/16/10', :format => :pge, :channel_id => ''},
{:data_key_id => 2872, :meter_id => 'CS00654', :csi_application => 'PGE-CSI-26541', :first_report => '01/16/11', :format => :pge, :channel_id => 'C0763'},
{:data_key_id => 2918, :meter_id => 'CS00711', :csi_application => 'PGE-CSI-26193', :first_report => '03/16/11', :format => :pge, :channel_id => 'C0820'},
{:data_key_id => 3301, :meter_id => 'CS00738', :csi_application => 'PGE-CSI-27860', :first_report => '03/16/11', :format => :pge, :channel_id => 'C0847'},
{:data_key_id => 1955, :meter_id => 'CS00613', :csi_application => 'PGE-CSI-25910', :first_report => '12/16/10', :format => :pge, :channel_id => 'C0722'},
{:data_key_id => 1350, :meter_id => 'CS00581', :csi_application => 'PGE-CSI-27134', :first_report => '10/16/10', :format => :pge, :channel_id => 'C0691'},
{:data_key_id => 1755, :meter_id => 'CS00605', :csi_application => 'PGE-CSI-24581', :first_report => '11/16/10', :format => :pge, :channel_id => 'C0714'},
{:data_key_id => 416, :meter_id => 'CS00526', :csi_application => 'PGE-CSI-19964', :first_report => '05/16/10', :format => :pge, :channel_id => 'C0629'},
{:data_key_id => 1559, :meter_id => 'CS00565', :csi_application => 'PGE-CSI-19940', :first_report => '07/16/10', :format => :pge, :channel_id => 'C0675'},
{:data_key_id => 1118, :meter_id => 'CS00531', :csi_application => 'PGE-CSI-06433', :first_report => '05/16/10', :format => :pge, :channel_id => 'C0634'},
{:data_key_id => 3292, :meter_id => 'CS00717', :csi_application => 'PGE-CSI-27653', :first_report => '03/16/11', :format => :pge, :channel_id => 'C0826'},
{:data_key_id => 3420, :meter_id => 'CS00718', :csi_application => 'PGE-CSI-27653', :first_report => '03/16/11', :format => :pge, :channel_id => 'C0827'},
{:data_key_id => 3291, :meter_id => 'CS00723', :csi_application => 'PGE-CSI-27654', :first_report => '03/16/11', :format => :pge, :channel_id => 'C0832'},
{:data_key_id => 5751, :meter_id => 'CS00778', :csi_application => 'PGE-CSI-24246', :first_report => '06/16/10', :format => :pge, :channel_id => 'C0887'},
{:data_key_id => 1150, :meter_id => '09385-001', :csi_application => 'SCE-CSI-09385', :first_report => '9/1/2010', :format => :sce},
{:data_key_id => 1151, :meter_id => '09385-002', :csi_application => 'SCE-CSI-09385', :first_report => '9/1/2010', :format => :sce},
{:data_key_id => 287, :meter_id => '07996-001', :csi_application => 'SCE-CSI-07996', :first_report => '3/1/2010', :format => :sce},
{:data_key_id => 1587, :meter_id => '11189-001', :csi_application => 'SCE-CSI-11189', :first_report => '12/1/2010', :format => :sce},
{:data_key_id => 2705, :meter_id => '14830-001', :csi_application => 'SCE-CSI-14830', :first_report => '3/1/2011', :format => :sce},
{:data_key_id => 981, :meter_id => '09549-001', :csi_application => 'SCE-CSI-09549', :first_report => '11/1/2010', :format => :sce},
{:data_key_id => 1371, :meter_id => '10221-001', :csi_application => 'SCE-CSI-10221', :first_report => '9/1/2010', :format => :sce},
{:data_key_id => 260, :meter_id => '08194-001', :csi_application => 'SCE-CSI-08194', :first_report => '3/1/2010', :format => :sce},
{:data_key_id => 2114, :meter_id => '12800-001', :csi_application => 'SCE-CSI-12800', :first_report => '12/1/2010', :format => :sce},
{:data_key_id => 372, :meter_id => '05457-001', :csi_application => 'SCE-CSI-05457', :first_report => '4/1/2010', :format => :sce},
{:data_key_id => 3317, :meter_id => '14303-001', :csi_application => 'SCE-CSI-14303', :first_report => '4/1/2011', :format => :sce},
{:data_key_id => 2485, :meter_id => '15271-001', :csi_application => 'SCE-CSI-15271', :first_report => '3/1/2011', :format => :sce},
{:data_key_id => 5744, :meter_id => '12991-001', :csi_application => 'SCE-CSI-12991', :first_report => '5/1/2011', :format => :sce},
{:data_key_id => 4888, :meter_id => '14425-001', :csi_application => 'SCE-CSI-14425', :first_report => '6/1/2011', :format => :sce},
{:data_key_id => 3086, :meter_id => '18557-001', :csi_application => 'SCE-CSI-18557', :first_report => '2/1/2011', :format => :sce},
{:data_key_id => 2588, :meter_id => '12785-001', :csi_application => 'SCE-CSI-12785', :first_report => '3/1/2011', :format => :sce},
{:data_key_id => 1128, :meter_id => '05965-001', :csi_application => 'SCE-CSI-05965', :first_report => '8/1/2010', :format => :sce},
{:data_key_id => 3252, :meter_id => '10943-001', :csi_application => 'SCE-CSI-10943', :first_report => '4/1/2011', :format => :sce},
{:data_key_id => 3224, :meter_id => '08359-001', :csi_application => 'SCE-CSI-08359', :first_report => '3/1/2011', :format => :sce},
{:data_key_id => 6944, :meter_id => '14166-001', :csi_application => 'SCE-CSI-14166', :first_report => '5/1/2011', :format => :sce, :formula_node => true},
{:data_key_id => 6945, :meter_id => '14167-001', :csi_application => 'SCE-CSI-14167', :first_report => '6/1/2011', :format => :sce, :formula_node => true},
{:data_key_id => 2089, :meter_id => '15835-001', :csi_application => 'SCE-CSI-15835', :first_report => '2/1/2011', :format => :sce},
{:data_key_id => 660, :meter_id => '08272-001', :csi_application => 'SCE-CSI-08272', :first_report => '8/1/2010', :format => :sce},
{:data_key_id => 659, :meter_id => '08272-002', :csi_application => 'SCE-CSI-08272', :first_report => '8/1/2010', :format => :sce},
{:data_key_id => 675, :meter_id => '08800-001', :csi_application => 'SCE-CSI-08800', :first_report => '8/1/2010', :format => :sce},
{:data_key_id => 395, :meter_id => '08798-001', :csi_application => 'SCE-CSI-08798', :first_report => '3/1/2010', :format => :sce},
{:data_key_id => 3204, :meter_id => '12885-001', :csi_application => 'SCE-CSI-12885', :first_report => '3/1/2011', :format => :sce},
{:data_key_id => 1770, :meter_id => '10933-001', :csi_application => 'SCE-CSI-10933', :first_report => '3/1/2011', :format => :sce},
{:data_key_id => 496, :meter_id => '08034-001', :csi_application => 'SCE-CSI-08034', :first_report => '5/1/2010', :format => :sce},
{:data_key_id => 3245, :meter_id => '11601-001', :csi_application => 'SCE-CSI-11604', :first_report => '4/1/2011', :format => :sce},
{:data_key_id => 1272, :meter_id => '10837-001', :csi_application => 'SCE-CSI-10837', :first_report => '9/1/2010', :format => :sce},
{:data_key_id => 5487, :meter_id => '15052-001', :csi_application => 'SCE-CSI-15052', :first_report => '6/1/2011', :format => :sce},
{:data_key_id => 2036, :meter_id => '06720-001', :csi_application => 'SCE-CSI-6720', :first_report => '1/1/2011', :format => :sce},
{:data_key_id => 3599, :meter_id => '11073-001', :csi_application => 'SCE-CSI-11073', :first_report => '6/1/2011', :format => :sce},
{:data_key_id => 5254, :meter_id => '17850-001', :csi_application => 'SCE-CSI-17850', :first_report => '7/1/2011', :format => :sce},
{:data_key_id => 6543, :meter_id => '11727-001', :csi_application => 'SCE-CSI-11727', :first_report => '7/1/2011', :format => :sce},
{:data_key_id => 3337, :meter_id => '16404-001', :csi_application => 'SCE-CSI-16404', :first_report => '7/1/2011', :format => :sce},
{:data_key_id => 4580, :meter_id => '16408-001', :csi_application => 'SCE-CSI-16408', :first_report => '7/1/2011', :format => :sce},
{:data_key_id => 3993, :meter_id => '11394-001', :csi_application => 'SCE-CSI-11394', :first_report => '5/1/2011', :format => :sce},
{:data_key_id => 3394, :meter_id => '16606-001', :csi_application => 'SCE-CSI-16606', :first_report => '7/1/2011', :format => :sce},
{:data_key_id => 3594, :meter_id => '11072-001', :csi_application => 'SCE-CSI-11072', :first_report => '6/1/2011', :format => :sce},
{:data_key_id => 5665, :meter_id => '11863-001', :csi_application => 'SCE-CSI-11863', :first_report => '9/1/2011', :format => :sce},
{:data_key_id => 5666, :meter_id => '11863-002', :csi_application => 'SCE-CSI-11863', :first_report => '9/1/2011', :format => :sce}
].each do |edi|
#next unless [3394].include? edi[:data_key_id] # simplify testing and 1 offs
next unless edi[:format] == :sce
dk = DataKey.find(edi[:data_key_id])
next unless dk
puts edi[:data_key_id]
table = dk.new_prepared_table #'deck_DATA_6.generation_kbm_2_prepared_1'
generation_date = Time.zone.now - MONTHS_AGO.months # normally now, but allows for back filling missing reports
first_report_date = Time.zone.parse(edi[:first_report])
report_begin = generation_date - (first_report_date.mday <= generation_date.mday ? 1 : 2).months
report_begin = Time.zone.local(report_begin.year, report_begin.month, first_report_date.mday, 8, 0)
report_end = report_begin + 1.month
report_number = 0
report_number += 1 until report_begin <= first_report_date + report_number.months
rows = times_for_range(report_begin, report_end)
rows.sort.each do |time, row|
if edi[:formula_node]
results = mysql_ex("select generation_formula, power_formula from #{table} where time='#{time.strftime('%Y-%m-%dT%H:%M:%S')}' and period=900;")
results.each_hash do |result|
row[:kwh] = result["generation_formula"] unless result["generation_formula"].nil?
row[:kw] = result["power_formula"] unless result["power_formula"].nil?
end
elsif edi[:channel]
channel_id = edi[:channel]
results = mysql_ex("select ch#{channel_id}_generation_cumul, ch#{channel_id}_power_avg from #{table} where time='#{time.strftime('%Y-%m-%dT%H:%M:%S')}' and period=900;")
results.each_hash do |result|
row[:kwh] = result["generation_cumul"] unless result["generation_cumul"].nil?
row[:kw] = result["power_avg"] unless result["power_avg"].nil?
end
else
results = mysql_ex("select generation_cumul, power_avg from #{table} where time='#{time.strftime('%Y-%m-%dT%H:%M:%S')}' and period=900;")
results.each_hash do |result|
row[:kwh] = result["generation_cumul"] unless result["generation_cumul"].nil?
row[:kw] = result["power_avg"] unless result["power_avg"].nil?
end
end
end
# write csvs
path = "#{BASE_PATH}edi/#{report_begin.year}/#{report_begin.month}/#{report_begin.mday}/#{edi[:format]}/"
FileUtils.mkdir_p(path)
file = File.open("#{path}#{edi[:meter_id]}.csv", "w")
puts "Writing to overview file:"
puts ['CSI Application', 'Total kWh Generated', "#{report_begin.month}/#{report_begin.mday}/#{report_begin.year} - #{report_end.month}/#{report_end.mday}/#{report_end.year}"].join(',') << "\n"
overview_file = File.open("#{path}overview.csv", "w")
overview_file.write(['CSI Application', 'Total kWh Generated', "#{report_begin.month}/#{report_begin.mday}/#{report_begin.year} - #{report_end.month}/#{report_end.mday}/#{report_end.year}"].join(',') << "\n")
overview_file.close
sorted_times = rows.keys.sort
case edi[:format]
when :sdge
# output header
file.write ['H', format_time_report(report_begin), format_time_report(report_end),
edi[:meter_id], edi[:csi_application], report_number].join(',') + "\r\n"
sorted_times.shift
sdge_sums[edi[:csi_application]] ||= {}
sdge_sums[edi[:csi_application]]["#{report_begin.year}/#{report_begin.month}/#{report_begin.mday}"] ||= 0
sorted_times.each do |time|
file.write hash_to_row(rows[time], edi[:format]) + "\r\n"
sdge_sums[edi[:csi_application]]["#{report_begin.year}/#{report_begin.month}/#{report_begin.mday}"] += rows[time][:kwh].to_f
end
when :sce
# output header
file.write ['H', format_time_report(report_begin), format_time_report(report_end),
edi[:meter_id], edi[:csi_application], report_number].join(',') + "\r\n"
sorted_times.shift
sce_sums[edi[:csi_application]] ||= {}
sce_sums[edi[:csi_application]]["#{report_begin.year}/#{report_begin.month}/#{report_begin.mday}"] ||= 0
sorted_times.each do |time|
file.write hash_to_row(rows[time], edi[:format]) + "\r\n"
sce_sums[edi[:csi_application]]["#{report_begin.year}/#{report_begin.month}/#{report_begin.mday}"] += rows[time][:kwh].to_f
end
when :pge
# output header
file.write ['H', format_time_report(Time.parse(report_begin.to_s.gsub("UTC", "Pacific")).utc), format_time_report(Time.parse(report_end.to_s.gsub("UTC", "Pacific")).utc),
edi[:csi_application], edi[:meter_id], edi[:channel_id], report_number].join(',') + "\r\n"
sorted_times.shift
pge_sums[edi[:csi_application]] ||= {}
pge_sums[edi[:csi_application]]["#{report_begin.year}/#{report_begin.month}/#{report_begin.mday}"] ||= 0
sorted_times.each do |time|
file.write hash_to_row(rows[time], edi[:format]) + "\r\n"
pge_sums[edi[:csi_application]]["#{report_begin.year}/#{report_begin.month}/#{report_begin.mday}"] += rows[time][:kwh].to_f
end
end
end
puts "Generating overview CSVs..."
sdge_sums.each_pair do |csi, times|
times.each_pair do |time, sum|
File.open("#{BASE_PATH}edi/#{time}/sdge/overview.csv", "a") { |f| f.write([csi, sum, ''].join(',') << "\n") }
end
end
pge_sums.each_pair do |csi, times|
times.each_pair do |time, sum|
File.open("#{BASE_PATH}edi/#{time}/pge/overview.csv", "a") { |f| f.write([csi, sum, ''].join(',') << "\n") }
end
end
sce_sums.each_pair do |csi, times|
times.each_pair do |time, sum|
puts "Writing to overview file:"
puts [csi, sum, '']
File.open("#{BASE_PATH}edi/#{time}/sce/overview.csv", "a") { |f| f.write([csi, sum, ''].join(',') << "\n") }
end
end
puts "DONE!"
end
def hash_to_row(hash, format)
hash[:time] = format_time_report(Time.parse(hash[:time].to_s.gsub("UTC", "Pacific")).utc) if format == :pge || format == :sce
row = [ hash[:row_type], hash[:time], hash[:kwh], hash[:kw], '', '' ]
row << '' if format == :pge
row.join(',')
end
def times_for_range(report_begin, report_end)
times = {}
current_time = report_begin
until current_time > report_end do
times[current_time] = {:row_type => 'D', :time => format_time_report(current_time), :kwh => 0, :kw => 0 }
current_time += 15.minutes
end
times
end
def format_time_report(time)
time.strftime('%Y%m%d%H%M')
end
def mysql_ex(str)
ActiveRecord::Base.connection.execute(str)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment