Class: Worklog

Inherits:
Object
  • Object
show all
Includes:
StringHelper
Defined in:
lib/worklog.rb

Overview

Main class providing all worklog functionality. This class is the main entry point for the application. It handles command line arguments, configuration, and logging.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StringHelper

#format_left, #pluralize

Constructor Details

#initialize(config = nil) ⇒ Worklog

Returns a new instance of Worklog.



26
27
28
29
30
31
# File 'lib/worklog.rb', line 26

def initialize(config = nil)
  @config = config || Configuration.new
  @storage = Storage.new(@config)

  WorkLogger.level = @config.log_level == :debug ? Logger::Severity::DEBUG : Logger::Severity::INFO
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



24
25
26
# File 'lib/worklog.rb', line 24

def config
  @config
end

Instance Method Details

#add(message, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/worklog.rb', line 33

def add(message, options = {})
  # Remove leading and trailing whitespaces
  # Raise an error if the message is empty
  message = message.strip
  raise ArgumentError, 'Message cannot be empty' if message.empty?

  date = Date.strptime(options[:date], '%Y-%m-%d')
  time = Time.strptime(options[:time], '%H:%M:%S')
  @storage.create_file_skeleton(date)

  daily_log = @storage.load_log!(@storage.filepath(date))
  daily_log.entries << LogEntry.new(time:, tags: options[:tags], ticket: options[:ticket], url: options[:url],
                                    epic: options[:epic], message:)

  # Sort by time in case an entry was added later out of order.
  daily_log.entries.sort_by!(&:time)

  @storage.write_log(@storage.filepath(options[:date]), daily_log)

  WorkLogger.info Rainbow("Added entry on #{options[:date]}: #{message}").green
end

#edit(options = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/worklog.rb', line 55

def edit(options = {})
  date = Date.strptime(options[:date], '%Y-%m-%d')

  # Load existing log
  log = @storage.load_log(@storage.filepath(date))
  unless log
    WorkLogger.error "No work log found for #{options[:date]}. Aborting."
    exit 1
  end

  txt = Editor::EDITOR_PREAMBLE.result_with_hash(content: YAML.dump(log))
  return_val = Editor.open_editor(txt)

  @storage.write_log(@storage.filepath(date),
                     YAML.load(return_val, permitted_classes: [Date, Time, DailyLog, LogEntry]))
  WorkLogger.info Rainbow("Updated work log for #{options[:date]}").green
end

#people(person = nil, _options = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/worklog.rb', line 89

def people(person = nil, _options = {})
  all_people = @storage.load_people!
  people_map = all_people.to_h { |p| [p.handle, p] }
  all_logs = @storage.all_days

  if person
    unless people_map.key?(person)
      WorkLogger.error Rainbow("No person found with handle #{person}.").red
      return
    end
    person_detail(all_logs, all_people, people_map[person.strip])
  else
    puts 'People mentioned in the work log:'

    mentions = {}

    all_logs.map(&:people).each do |people|
      mentions.merge!(people) { |_key, oldval, newval| oldval + newval }
    end

    # Sort the mentions by handle
    mentions = mentions.to_a.sort_by { |handle, _| handle }

    mentions.each do |handle, v|
      if people_map.key?(handle)
        print "#{Rainbow(people_map[handle].name).gold} (#{handle})"
      else
        print handle
      end
      puts ": #{v} #{pluralize(v, 'occurrence')}"
    end
  end
end

#person_detail(all_logs, all_people, person) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/worklog.rb', line 123

def person_detail(all_logs, all_people, person)
  printer = Printer.new(all_people)
  puts "All interactions with #{Rainbow(person.name).gold}"

  if person.notes
    puts 'Notes:'
    person.notes.each do |note|
      puts "* #{note}"
    end
  end

  puts 'Interactions:'
  all_logs.each do |daily_log|
    daily_log.entries.each do |entry|
      printer.print_entry(daily_log, entry, true) if entry.people.include?(person.handle)
    end
  end
end

#remove(options = {}) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/worklog.rb', line 218

def remove(options = {})
  date = Date.strptime(options[:date], '%Y-%m-%d')
  unless File.exist?(@storage.filepath(date))
    WorkLogger.error Rainbow("No work log found for #{options[:date]}. Aborting.").red
    exit 1
  end

  daily_log = @storage.load_log!(@storage.filepath(options[:date]))
  if daily_log.entries.empty?
    WorkLogger.error Rainbow("No entries found for #{options[:date]}. Aborting.").red
    exit 1
  end

  removed_entry = daily_log.entries.pop
  @storage.write_log(@storage.filepath(date), daily_log)
  WorkLogger.info Rainbow("Removed entry: #{removed_entry.message}").green
end

#serverObject

Start webserver



237
238
239
240
# File 'lib/worklog.rb', line 237

def server
  app = WorkLogApp.new(@storage)
  WorkLogServer.new(app).start
end

#show(options = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/worklog.rb', line 73

def show(options = {})
  people = @storage.load_people!
  printer = Printer.new(people)

  start_date, end_date = start_end_date(options)

  entries = @storage.days_between(start_date, end_date)
  if entries.empty?
    printer.no_entries(start_date, end_date)
  else
    entries.each do |entry|
      printer.print_day(entry, entries.size > 1, options[:epics_only])
    end
  end
end

#start_end_date(options) ⇒ Array

Parse the start and end date based on the options provided

Parameters:

  • options (Hash)

    the options hash

Returns:

  • (Array)

    the start and end date as an array



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/worklog.rb', line 246

def start_end_date(options)
  if options[:days]
    # Safeguard against negative days
    raise ArgumentError, 'Number of days cannot be negative' if options[:days].negative?

    start_date = Date.today - options[:days]
    end_date = Date.today
  elsif options[:from]
    start_date = DateParser.parse_date_string!(options[:from], true)
    end_date = DateParser.parse_date_string!(options[:to], false) if options[:to]
  elsif options[:date]
    start_date = Date.strptime(options[:date], '%Y-%m-%d')
    end_date = start_date
  else
    raise ArgumentError, 'No date range specified. Use --days, --from, --to or --date options.'
  end
  [start_date, end_date]
end

#stats(_options = {}) ⇒ Object



196
197
198
199
200
201
202
203
204
# File 'lib/worklog.rb', line 196

def stats(_options = {})
  stats = Statistics.new(@config).calculate
  puts "#{format_left('Total days')}: #{stats.total_days}"
  puts "#{format_left('Total entries')}: #{stats.total_entries}"
  puts "#{format_left('Total epics')}: #{stats.total_epics}"
  puts "#{format_left('Entries per day')}: #{format('%.2f', stats.avg_entries)}"
  puts "#{format_left('First entry')}: #{stats.first_entry}"
  puts "#{format_left('Last entry')}: #{stats.last_entry}"
end

#summary(options = {}) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
# File 'lib/worklog.rb', line 206

def summary(options = {})
  start_date, end_date = start_end_date(options)
  entries = @storage.days_between(start_date, end_date).map(&:entries).flatten

  # Do nothing if no entries are found.
  if entries.empty?
    Printer.new.no_entries(start_date, end_date)
    return
  end
  puts Summary.new(@config).generate_summary(entries)
end

#tag_detail(tag, options) ⇒ void

This method returns an undefined value.

Show detailed information about a specific tag

Examples:

worklog.tag_detail('example_tag', from: '2023-10-01', to: '2023-10-31')

Parameters:

  • tag (String)

    the tag to show details for

  • options (Hash)

    the options hash containing date range



181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/worklog.rb', line 181

def tag_detail(tag, options)
  printer = Printer.new(@storage.load_people!)
  start_date, end_date = start_end_date(options)

  @storage.days_between(start_date, end_date).each do |daily_log|
    next unless daily_log.tags.include?(tag)

    daily_log.entries.each do |entry|
      next unless entry.tags.include?(tag)

      printer.print_entry(daily_log, entry, true)
    end
  end
end

#tag_overviewObject



159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/worklog.rb', line 159

def tag_overview
  all_logs = @storage.all_days
  puts Rainbow('Tags used in the work log:').gold

  # Count all tags used in the work log
  tags = all_logs.map(&:entries).flatten.map(&:tags).flatten.compact.tally

  # Determine length of longest tag for formatting
  # Add one additonal space for formatting
  max_len = tags.empty? ? 0 : tags.keys.map(&:length).max + 1

  tags.sort.each { |k, v| puts "#{Rainbow(k.ljust(max_len)).gold}: #{v} #{pluralize(v, 'occurrence')}" }
end

#tags(tag = nil, options = {}) ⇒ void

This method returns an undefined value.

Show all tags used in the work log or details for a specific tag

Examples:

worklog.tags('example_tag', from: '2023-10-01', to: '2023-10-31')
worklog.tags(nil) # Show all tags for all time

Parameters:

  • tag (String, nil) (defaults to: nil)

    the tag to show details for, or nil to show all tags

  • options (Hash) (defaults to: {})

    the options hash containing date range



151
152
153
154
155
156
157
# File 'lib/worklog.rb', line 151

def tags(tag = nil, options = {})
  if tag.nil? || tag.empty?
    tag_overview
  else
    tag_detail(tag, options)
  end
end