Add monthly summary report
authorChris Jaekl <chris@localhost>
Thu, 16 Feb 2023 03:52:12 +0000 (22:52 -0500)
committerChris Jaekl <chris@localhost>
Thu, 16 Feb 2023 03:52:12 +0000 (22:52 -0500)
lib/report.rb [new file with mode: 0644]
lib/stats.rb
run_report.rb [new file with mode: 0644]

diff --git a/lib/report.rb b/lib/report.rb
new file mode 100644 (file)
index 0000000..45a981f
--- /dev/null
@@ -0,0 +1,80 @@
+# 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
index a67b57e5e5ddd882911983f8347ceea3189976c8..fe840add6afcac18b29d130e2ff889a60a6fcd9f 100644 (file)
@@ -20,25 +20,12 @@ class Stats
     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
 
@@ -83,4 +70,31 @@ class Stats
 
     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
diff --git a/run_report.rb b/run_report.rb
new file mode 100644 (file)
index 0000000..645e51c
--- /dev/null
@@ -0,0 +1,5 @@
+# frozen_string_literal: true
+
+require "report"
+
+Report.new.run(base_path: ARGV[0], prefix: ARGV[1])