--- /dev/null
+# frozen_string_literal: true
+
+require "pry"
+
+class Report
+ def initialize
+ @monthly_totals = {}
+ end
+
+ def run(base_path:, prefix:)
+ totals = {}
+ @cached_months = {}
+
+ Dir["#{base_path}/#{prefix}*"].each do |filename|
+ totals = add_month(totals: totals, filename: filename)
+ end
+
+ headers = ["Month"]
+ routes = totals.keys.reject{|x| x.is_a? Symbol}.sort_by(&:to_i)
+ headers.append(routes)
+
+ puts headers.join(";")
+ @monthly_totals.keys.sort_by(&:to_i).each do |month|
+ values = @monthly_totals[month]
+ puts [
+ "#{values[:year]}-#{values[:month]}",
+ routes.map{|route| values[route] || ""},
+ ].flatten.join(";")
+ end
+
+ puts [
+ "TOTAL",
+ routes.map{|route| totals[route] || ""},
+ ].flatten.join(";")
+ end
+
+ private
+
+ def add_month(totals:, filename:)
+ current_month = parse_month(filename: filename)
+
+ current_month.each do |key, count|
+ sum = totals[key] || 0
+ sum += count.to_i
+ totals[key] = sum
+ end
+
+ @monthly_totals[current_month[:month]] = current_month
+
+ totals
+ end
+
+ def parse_month(filename:)
+ totals = {}
+ File.open(filename, "r") do |fd|
+ fd.readlines.each do |line|
+ tokens = line.split(";")
+ next unless 3 == tokens.count
+
+ date, total, spec = line.split(";")
+ hash = eval(spec)
+ year, month = date.split("-")
+ totals[:year] ||= year
+ totals[:month] ||= month
+
+ sum = totals[:sum] || 0
+ sum += total.to_i
+ totals[:sum] = sum
+
+ hash.each do |route, count|
+ sum = totals[route] || 0
+ sum += count.to_i
+ totals[route] = sum
+ end
+ end
+ end
+
+ totals
+ end
+end
route_totals = {}
(1..12).each do |month|
- last_day = DAYS_IN_MONTH[month]
- last_day += 1 if (month == 2) && (0 == year % 4) # leap year (doesn't account for 2000)
+ monthly_totals = report_month(year: year, month: month)
- (1..last_day).each do |day|
- date_hash = {year: year, month: month, day: day}
- date_string = "#{year}-#{month.to_s.rjust(2, "0")}-#{day.to_s.rjust(2, "0")}"
-
- counts = trip_counts(date_hash: date_hash)
-
- daily_total = 0
- counts.each do |route, count|
- route_total = route_totals[route] || 0
- route_total += count
- route_totals[route] = route_total
-
- daily_total += count
- end
-
- puts "#{date_string};#{daily_total};#{counts.inspect}"
+ monthly_totals.each do |route, count|
+ total = route_totals[route] || 0
+ total += count
+ route_totals[route] = total
end
end
result
end
+
+ def report_month(year:, month:)
+ monthly_totals = {}
+
+ last_day = DAYS_IN_MONTH[month]
+ last_day += 1 if (month == 2) && (0 == year % 4) # leap year (doesn't account for 1900, 2100, ...)
+
+ (1..last_day).each do |day|
+ date_hash = {year: year, month: month, day: day}
+ date_string = "#{year}-#{month.to_s.rjust(2, "0")}-#{day.to_s.rjust(2, "0")}"
+
+ counts = trip_counts(date_hash: date_hash)
+
+ daily_total = 0
+ counts.each do |route, count|
+ route_total = monthly_totals[route] || 0
+ route_total += count
+ monthly_totals[route] = route_total
+
+ daily_total += count
+ end
+
+ puts "#{date_string};#{daily_total};#{counts.inspect}"
+ end
+
+ monthly_totals
+ end
end
--- /dev/null
+# frozen_string_literal: true
+
+require "report"
+
+Report.new.run(base_path: ARGV[0], prefix: ARGV[1])