-
-
Save 6ewis/87377431f766a6ad1195 to your computer and use it in GitHub Desktop.
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 'csv' | |
Customer = Struct.new(:cust_id, | |
:elect_or_gas, | |
:disconnect_doc, | |
:move_in_date, | |
:move_out_date, | |
:bill_year, | |
:bill_month, | |
:span_days, | |
:meter_read_date, | |
:meter_read_type, | |
:consumption, | |
:exception_code, | |
:data | |
) | |
class Parser | |
attr_accessor :customers | |
def initialize filename | |
self.customers = [] | |
deserialize_users filename | |
serialize_users | |
end | |
def deserialize_users filename | |
CSV.foreach(filename, col_sep: "|") do |row| | |
next if row.empty? | |
#sanitized_values(cust_id, elect_or_gas, disconnect_doc, move_in_date, move_out_date, bill_year, bill_month, span_days, meter_read_date, meter_read_type, consumption, exception_code = row) | |
customers << Customer.new(*row, row) | |
end | |
customers | |
end | |
def serialize_users | |
CSV.open("./serialized_users.txt", "w") do |csv| #is 'a' better? | |
customers.each do |c| | |
csv << c.data | |
end | |
end | |
end | |
end | |
class Reporting | |
attr_accessor :parser, :output_size | |
def initialize data , output_size | |
self.parser = Parser.new data | |
self.output_size = output_size | |
end | |
#STATISTICS | |
def unique_customers | |
parser.customers.uniq(&:cust_id ) | |
end | |
def customers_with_electricity#_ONLY | |
customer = parser.customers.select { |customer| customer.elect_or_gas == "1" || customer.elect_or_gas == "ElecOrGas" } | |
end | |
def customers_with_gas#_ONLY | |
parser.customers.select { |customer| customer.elect_or_gas == "2" || customer.elect_or_gas == "ElecOrGas"} | |
end | |
def customers_bill_month month , resource = nil | |
if resource == :gas | |
customers_with_gas.select {|customer| customer.bill_month == "#{month}"} | |
elsif resource == :electricity | |
customers_with_electricity.select {|customer| customer.bill_month == "#{month}"} | |
else | |
parser.customers.select { |customer| customer.bill_month == "#{month}"} | |
end | |
end | |
def nbr_of_meter_readings_per_customer #number of meter readings per customer | |
end | |
def avg_consumption_month month , resource = nil | |
!customers_bill_month(month, resource).empty? ? ( (customers_bill_month(month, resource).inject(0) { |sum, n| sum + n.consumption.to_i } ) / customers_bill_month(month, resource).length ) : 0 | |
end | |
#REPORT | |
def show_all_data | |
print "\n\nALL CUSTOMERS AND DATA\n\n" | |
puts format_show parser.customers | |
end | |
def show_unique_customers | |
print "\n\nUNIQUE CUSTOMERS\n\n" | |
format_show unique_customers | |
puts "\nNumber of unique customers: #{unique_customers.count - 1}" | |
end | |
def show_customers_with_electricity | |
print "\n\nCUSTOMERS WITH ELECTRICITY ONLY\n\n" | |
format_show customers_with_electricity | |
puts "\nNumber of customers with electricity: #{customers_with_electricity.count - 1}" | |
end | |
def show_customers_with_gas | |
print "\n\nCUSTOMER WITH GAS ONLY\n\n" | |
format_show customers_with_gas | |
puts "\nNumber of customers with gas: #{customers_with_gas.count - 1}" | |
end | |
def show_avg_consumption_per_bill_month resource = nil | |
print "\n\nAVERAGE CONSUMPTION PER BILL MONTH #{'PER ' + resource.to_s.upcase if resource} ACROSS ALL CUSTOMERS\n\n" | |
total_avg_consumption = [] | |
(1..12).each do |i| | |
puts "\n\nMONTH OF #{ Date::MONTHNAMES[i] }: #{avg_consumption_month i, resource}" | |
total_avg_consumption << avg_consumption_month(i, resource) | |
end | |
puts "TOTAL AVERAGE CONSUMPTION: #{total_avg_consumption.inject(:+) / total_avg_consumption.length}" | |
end | |
private | |
def format_show foo | |
foo.map {|customer| puts customer.data.map { |item| item.to_s.center output_size}.join'|'} unless foo.empty? | |
end | |
end | |
r = Reporting.new "./pulse_data.txt", 15 | |
r.show_all_data | |
r.show_unique_customers | |
#in the output theres multiple blank lines between the output of the previous line and the next. a huge gap | |
r.show_customers_with_gas | |
r.show_customers_with_electricity | |
r.show_avg_consumption_per_bill_month | |
r.show_avg_consumption_per_bill_month :electricity | |
r.show_avg_consumption_per_bill_month :gas | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment