-
Notifications
You must be signed in to change notification settings - Fork 0
/
cal.rb
89 lines (73 loc) · 2.02 KB
/
cal.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require 'date'
require 'optparse'
=begin
A simple script to track anything done per day and produce averages over time.
Input file is a file with a date, a pipe, a number/count (1 or 2 digit),
one entry per line, like:
YYYY-MM-DD | NN
=end
class Days
attr_accessor :first, :last, :days, :total_count, :total_days, :avg
def initialize(file)
lines = IO.readlines(file)
@first = Date.strptime(lines[0].split('|')[0].strip, "%Y-%m-%d")
@last = Date.strptime(lines.last.split('|')[0].strip, "%Y-%m-%d")
@days = []
@total_count = 0;
@total_days = 0;
lines.each do |line|
date = Date.strptime(line.split('|')[0], "%Y-%m-%d")
count = line.split('|')[1].strip
@total_count += count.to_f
@total_days += 1
@days.push( Day.new(date, count) )
end
end
def avg
return @total_count / @total_days
end
end
class Day
attr_accessor :date, :count
def initialize(date, count)
@date = date
@count = count || 0
end
end
class WeeklyReport
def initialize(ds)
@ds = ds
@by_weeks = {};
@ds.days.each do |d|
year_week = d.date.cwyear.to_s + " " + sprintf("%02d", d.date.cweek).to_s
@by_weeks[ year_week ] ||= []
@by_weeks[ year_week ].push(sprintf("%02d", d.count))
end
end
def simple
@by_weeks.keys.sort.each do |d|
print "#{d} : "
count = 0;
days_in_week = 0;
@by_weeks[d].each do |c|
print "#{c} "
count = count + c.to_f # to_f for float here: ruby seems to round DOWN. I <3 ruby
days_in_week = days_in_week + 1
end
print "\tcount: #{count} "
wavg = 0.0
wavg = count / days_in_week
print "\tavg: #{wavg}\n"
end
end
end
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: cal.rb [options]"
opts.on('-d', '--data FILE', 'data file') { |v| options[:data] = v }
end.parse!
ds = Days.new(options[:data])
print "#{ds.first} .. #{ds.last}\n"
print "total average for #{ds.total_days} days: #{ds.avg}\n";
weekly = WeeklyReport.new(ds)
weekly.simple