Class: Worklog::Worklog
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
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#storage ⇒ Object
readonly
Returns the value of attribute storage.
Instance Method Summary collapse
-
#add(message, options = {}) ⇒ void
Add new entry to the work log.
- #edit(options = {}) ⇒ Object
-
#initialize(config = nil) ⇒ Worklog
constructor
A new instance of Worklog.
- #people(person = nil, _options = {}) ⇒ Object
- #person_detail(all_logs, all_people, person) ⇒ Object
- #projects(_options = {}) ⇒ Object
- #remove(options = {}) ⇒ Object
-
#server ⇒ Object
Start webserver.
- #show(options = {}) ⇒ Object
-
#start_end_date(options) ⇒ Array
Parse the start and end date based on the options provided.
- #stats(_options = {}) ⇒ Object
- #summary(options = {}) ⇒ Object
-
#tag_detail(tag, options) ⇒ void
Show detailed information about a specific tag.
- #tag_overview ⇒ Object
-
#tags(tag = nil, options = {}) ⇒ void
Show all tags used in the work log or details for a specific tag.
- #validate_projects!(project_key) ⇒ Object
Methods included from StringHelper
Constructor Details
#initialize(config = nil) ⇒ Worklog
Returns a new instance of Worklog.
28 29 30 31 32 33 |
# File 'lib/worklog.rb', line 28 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
#config ⇒ Object (readonly)
Returns the value of attribute config.
26 27 28 |
# File 'lib/worklog.rb', line 26 def config @config end |
#storage ⇒ Object (readonly)
Returns the value of attribute storage.
26 27 28 |
# File 'lib/worklog.rb', line 26 def storage @storage end |
Instance Method Details
#add(message, options = {}) ⇒ void
This method returns an undefined value.
Add new entry to the work log.
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 |
# File 'lib/worklog.rb', line 45 def add(, = {}) # Remove leading and trailing whitespaces # Raise an error if the message is empty = .strip raise ArgumentError, 'Message cannot be empty' if .empty? date = Date.strptime([:date], '%Y-%m-%d') time = Time.strptime([:time], '%H:%M:%S') @storage.create_file_skeleton(date) # Validate that the project exists if provided validate_projects!([:project]) if [:project] && ![:project].empty? daily_log = @storage.load_log!(@storage.filepath(date)) new_entry = LogEntry.new(time:, tags: [:tags], ticket: [:ticket], url: [:url], epic: [:epic], message:, project: [:project]) daily_log.entries << new_entry # Sort by time in case an entry was added later out of order. daily_log.entries.sort_by!(&:time) @storage.write_log(@storage.filepath([:date]), daily_log) people_hash = @storage.load_people_hash (new_entry.people - people_hash.keys).each do |handle| WorkLogger.warn "Person with handle #{handle} not found. Consider adding them to people.yaml" end WorkLogger.info Rainbow("Added entry on #{[:date]}: #{}").green end |
#edit(options = {}) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/worklog.rb', line 76 def edit( = {}) date = Date.strptime([:date], '%Y-%m-%d') # Load existing log log = @storage.load_log(@storage.filepath(date)) unless log WorkLogger.error "No work log found for #{[: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 #{[:date]}").green end |
#people(person = nil, _options = {}) ⇒ Object
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 |
# File 'lib/worklog.rb', line 110 def people(person = nil, = {}) 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) person = people_map[handle] print "#{Rainbow(person.name).gold} (#{handle})" print " (#{person.team})" if person.team else print handle end puts ": #{v} #{pluralize(v, 'occurrence')}" end end end |
#person_detail(all_logs, all_people, person) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/worklog.rb', line 146 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 |
#projects(_options = {}) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/worklog.rb', line 165 def projects( = {}) project_storage = ProjectStorage.new(@config) projects = project_storage.load_projects puts Rainbow('Projects:').gold projects.each_value do |project| puts "#{Rainbow(project.name).gold} (#{project.key})" puts " Description: #{project.description}" if project.description puts " Start date: #{project.start_date}" if project.start_date puts " End date: #{project.end_date}" if project.end_date puts " Status: #{project.status}" if project.status end puts 'No projects found.' if projects.empty? end |
#remove(options = {}) ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/worklog.rb', line 281 def remove( = {}) date = Date.strptime([:date], '%Y-%m-%d') unless File.exist?(@storage.filepath(date)) WorkLogger.error Rainbow("No work log found for #{[:date]}. Aborting.").red exit 1 end daily_log = @storage.load_log!(@storage.filepath([:date])) if daily_log.entries.empty? WorkLogger.error Rainbow("No entries found for #{[: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.}").green end |
#server ⇒ Object
Start webserver
300 301 302 303 |
# File 'lib/worklog.rb', line 300 def server app = WorkLogApp.new(@storage) WorkLogServer.new(app).start end |
#show(options = {}) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/worklog.rb', line 94 def show( = {}) people = @storage.load_people! printer = Printer.new(people) start_date, end_date = start_end_date() 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, [:epics_only]) end end end |
#start_end_date(options) ⇒ Array
Parse the start and end date based on the options provided
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/worklog.rb', line 309 def start_end_date() if [:days] # Safeguard against negative days raise ArgumentError, 'Number of days cannot be negative' if [:days].negative? start_date = Date.today - [:days] end_date = Date.today elsif [:from] start_date = DateParser.parse_date_string!([:from], true) end_date = DateParser.parse_date_string!([:to], false) if [:to] elsif [:date] start_date = Date.strptime([: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
233 234 235 236 237 238 239 240 241 |
# File 'lib/worklog.rb', line 233 def stats( = {}) 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
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/worklog.rb', line 243 def summary( = {}) start_date, end_date = start_end_date() 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 # List all the epics epics = entries.filter(&:epic) puts Rainbow("Found #{epics.size} epics.").green if epics.any? epics.each do |entry| puts "#{entry.time.strftime('%b %d, %Y')} #{entry.}" end # List all the tags and their count = entries.map(&:tags).flatten.compact.tally puts Rainbow("Found #{.size} tags.").green if .any? .each do |tag, count| print "#{tag} (#{count}x), " end puts '' if .any? # List all the people and their count people = entries.map(&:people).flatten.compact.tally.sort_by { |_, count| -count }.filter { |_, count| count > 1 } puts Rainbow("Found #{people.size} people.").green if people.any? people.each do |person, count| print "#{person} (#{count}x), " end puts '' if people.any? # # Print the summary # summary = Summary.new(entries) # puts summary.to_s end |
#tag_detail(tag, options) ⇒ void
This method returns an undefined value.
Show detailed information about a specific tag
218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/worklog.rb', line 218 def tag_detail(tag, ) printer = Printer.new(@storage.load_people!) start_date, end_date = start_end_date() @storage.days_between(start_date, end_date).each do |daily_log| next unless daily_log..include?(tag) daily_log.entries.each do |entry| next unless entry..include?(tag) printer.print_entry(daily_log, entry, true) end end end |
#tag_overview ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/worklog.rb', line 196 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 = 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 = .empty? ? 0 : .keys.map(&:length).max + 1 .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
188 189 190 191 192 193 194 |
# File 'lib/worklog.rb', line 188 def (tag = nil, = {}) if tag.nil? || tag.empty? tag_overview else tag_detail(tag, ) end end |
#validate_projects!(project_key) ⇒ Object
328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/worklog.rb', line 328 def validate_projects!(project_key) project_storage = ProjectStorage.new(@config) begin projects = project_storage.load_projects rescue Errno::ENOENT raise ProjectNotFoundError, 'No projects found. Please create a project first.' end return if projects.key?(project_key) raise ProjectNotFoundError, "Project with key '#{project_key}' does not exist." end |