Class: TimeSheet::Time::Cmd

Inherits:
Object
  • Object
show all
Defined in:
lib/time_sheet/time/cmd.rb

Instance Method Summary collapse

Instance Method Details

#commandObject



148
149
150
# File 'lib/time_sheet/time/cmd.rb', line 148

def command
  options.arguments.shift || 'default'
end

#configObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/time_sheet/time/cmd.rb', line 88

def config
  defaults = {
    'location' => "#{ENV['HOME']}/time",
    'carry_over_activity' => 'previous'
  }

  file = "#{ENV['HOME']}/.time-sheet.conf"
  return defaults unless File.exist?(file)

  result = YAML.load_file(file)
  result = defaults.merge(result)

  unless result['location'].is_a?(Array)
    result['location'] = [result['location']]
  end

  result
end

#convert_to_timeObject



152
153
154
155
156
157
158
159
# File 'lib/time_sheet/time/cmd.rb', line 152

def convert_to_time
  if options[:from]
    options[:from] = options[:from].to_time
  end
  if options[:to].is_a?(Date)
    options[:to] = options[:to].to_time + 24 * 60 * 60
  end
end

#invoiceObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/time_sheet/time/cmd.rb', line 183

def invoice
  convert_to_time

  data = TimeSheet::Time.invoice(options)

  data.each do |package|
    tp = TimeSheet::TablePrinter.new package, options
    puts tp.generate
    puts "\n"
  end

  if options[:package]
    package = (data.last.nil? ? 0 : data.last.map{|entry| entry[1]}.sum)
    total = options[:package] * 60
    percent = (package / total.to_f * 100)

    puts [
      "current package: #{package}/#{total}",
      "(#{(package / 60.0).round(2)}/#{(total / 60.0).round(2)} hours,",
      "#{percent.round 2}%)"
    ].join(' ')
  end
end

#optionsObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/time_sheet/time/cmd.rb', line 107

def options
  @options ||= Slop.parse do |o|
    o.banner = [
      "usage: time.rb [command] [options]\n",
      "visit https://github.com/moritzschepp/time-sheet for further information\n",
      'available commands:',
      "  report (default): list entries conforming to given criteria",
      "  invoice: compress similar entries and filter petty ones. Optionally package for e.g. monthly invoicing",
      "  verify: check syntax and semantics in your input spreadsheets",
      "\n  general options:"
    ].join("\n")

    o.boolean '-h', '--help', 'show help'
    o.boolean '--version', 'show the version'
    o.array('-l', '--location', 'a location to gather data from (file, directory or google docs share-url)',
      default: config['location']
    )
    o.string '-f', '--from', 'ignore entries older than the date given'
    o.string '-t', '--to', 'ignore entries more recent than the date given'
    o.string '-p', '--project', 'take only entries of this project into account'
    o.string '-a', '--activity', 'take only entries of this activity into account'
    o.string '--tags', 'filter by tag (comma separated, not case sensitive, prefix tag with ! to exclude)'
    o.string '-d', '--description', 'consider only entries matching this description'
    o.string '-e', '--employee', 'consider only entries for this employee'
    o.string(
      '--carry-over-activity',
      'mode for carrying over activity (previous|same-description)',
      default: config['carry_over_activity']
    )
    o.string '--default-activity', 'default activity', default: config['default_activity']
    o.float '-r', '--rate', 'use an alternative hourly rate (default: 80.0)', default: 80.00
    o.boolean '-s', '--summary', 'when reporting, add summary section'
    o.boolean '--trim', 'compact the output for processing as CSV', default: false
    o.boolean '-v', '--verbose', 'be more verbose'
    o.boolean '--debug', 'drop into a REPL on errors'
    o.separator "\n  invoice options:"
    o.float '--package', 'for invoice output: build packages of this duration in hours', default: 0.0
    o.integer '--petty', 'fold records under a certain threshold into a "misc" activity', default: 0
  end
end

#reportObject



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/time_sheet/time/cmd.rb', line 207

def report
  convert_to_time

  data = TimeSheet::Time.report(options)
  tp = TimeSheet::TablePrinter.new data['entries'], options
  puts tp.generate


  if options[:summary]
    puts "\nSummary:"

    tdata = [['project', 'activity', 'time [m]', 'time [h]', 'price [€]']]
    tdata << [
      'all',
      '',
      TimeSheet::Time::Util.minutes(data['total']),
      TimeSheet::Time::Util.hours(data['total']),
      TimeSheet::Time::Util.price(data['total'], options[:rate])
    ]

    data['projects'].sort_by{|k, v| v['total']}.reverse.to_h.each do |pname, pdata|
      previous = nil

      tdata << '-'
      tdata << [
        pname,
        'all',
        TimeSheet::Time::Util.minutes(pdata['total']),
        TimeSheet::Time::Util.hours(pdata['total']),
        TimeSheet::Time::Util.price(pdata['total'], options[:rate])
      ]

      pdata['activities'].sort_by{|k, v| v}.reverse.to_h.each do |aname, atotal|
        tdata << [
          '',
          aname,
          TimeSheet::Time::Util.minutes(atotal),
          TimeSheet::Time::Util.hours(atotal),
          TimeSheet::Time::Util.price(atotal, options[:rate])
        ]
        previous = pname
      end
    end

    tdata << '-'

    tp = TimeSheet::TablePrinter.new tdata, options
    puts tp.generate

    puts [
      "days: #{data['averages']['days']}",
      "worked: h/day: #{data['averages']['hours_per_day'].round(2)}",
      "h/workday: #{data['averages']['hours_per_workday'].round(2)}",
      "h/week: #{data['averages']['hours_per_week'].round(2)}",
      "h/month(30 days): #{data['averages']['hours_per_month'].round(2)}"
    ].join(', ')
  end
end

#runObject



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
# File 'lib/time_sheet/time/cmd.rb', line 7

def run
  if d = options[:from]
    if d.match(/^\d\d?-\d\d?$/)
      d = "#{TimeSheet::Time::Util.now.year}-#{d}"
    end

    if d.match(/^\d{4}$/)
      d = "#{d}-01-01"
    end

    options[:from] = Time.parse(d)
  end

  if d = options[:to]
    if d.match(/^\d\d?-\d\d?$/)
      d = "#{TimeSheet::Time::Util.now.year}-#{d}"
    end

    if d.match(/^\d{4}$/)
      d = "#{d}-12-31"
    end

    options[:to] = Time.parse(d)
  end

  if options[:help]
    puts options
  elsif options[:version]
    puts TimeSheet::VERSION
  else
    case command
      when 'invoice'
        invoice
      when 'report', 'default'
        report
      when 'verify'
        verify
      when 'today', 't'
        options[:from] = TimeSheet::Time::Util.today
        options[:summary] = true
        report
      when 'yesterday', 'y'
        options[:from] = TimeSheet::Time::Util.yesterday
        options[:to] = TimeSheet::Time::Util.yesterday
        options[:summary] = true
        report
      when 'week', 'w'
        options[:from] = TimeSheet::Time::Util.week_start
        options[:summary] = true
        report
      when 'last-week', 'lw'
        options[:from] = TimeSheet::Time::Util.week_start(-1)
        options[:to] = TimeSheet::Time::Util.week_end(-1)
        options[:summary] = true
        report
      when 'month', 'm'
        options[:from] = TimeSheet::Time::Util.month_start
        options[:summary] = true
        report
      when 'last-month', 'lm'
        options[:from] = TimeSheet::Time::Util.month_start(-1)
        options[:to] = TimeSheet::Time::Util.month_end(-1)
        options[:summary] = true
        report
      when 'year-to-day', 'year'
        options[:from] = TimeSheet::Time::Util.year_start(-1)
        options[:summary] = true
        report
      when 'overview'
        overview
      else
        raise "unknown command: #{command}"
    end

    if options[:verbose]
      puts "\noptions:"
      p options.to_h
    end
  end
end

#verifyObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/time_sheet/time/cmd.rb', line 161

def verify
  TimeSheet.options = options

  convert_to_time

  entries = TimeSheet::Time::Parser.new(options[:location]).entries

  puts 'checking for changes in project with carried-over description ...'
  entries.each do |entry|
    next unless entry.matches?(options)

    if entry.prev && entry.prev.project != entry.project
      # we check for the same object because that means that the value has
      # been carried over from the previous entry and that likely represents
      # an oversight in these circumstances
      if entry.prev.description.equal?(entry.description)
        puts "-> | #{entry}"
      end
    end
  end
end