Class: Timet::TimeReport

Inherits:
Object
  • Object
show all
Includes:
TagDistribution, TimeReportHelper
Defined in:
lib/timet/time_report.rb

Overview

The TimeReport class is responsible for displaying a report of tracked time entries. It allows filtering the report by time periods and displays a formatted table with the relevant information.

Constant Summary

Constants included from TagDistribution

Timet::TagDistribution::BLOCK_CHAR, Timet::TagDistribution::MAX_BAR_LENGTH, Timet::TagDistribution::TAG_SIZE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TagDistribution

#calculate_value_and_bar_length, #generate_horizontal_bar, #generate_stats, #print_explanation, #print_footer, #print_summary, #print_tags_info, #process_and_print_tags, #tag_distribution

Methods included from TimeReportHelper

#export_csv, #export_icalendar

Constructor Details

#initialize(db, options = {}) ⇒ void

Note:
  • If no filter is provided, all items from the database will be fetched.

  • The ‘@table` instance variable is initialized with the filtered items and filter configuration.

Initializes a new instance of the TimeReport class.

Examples:

Initialize a new TimeReport instance with a filter and tag

TimeReport.new(db, filter: 'today', tag: 'work', csv: 'report.csv', ics: 'icalendar.ics')

Initialize a new TimeReport instance with a date range filter

TimeReport.new(db, filter: '2023-10-01..2023-10-31', tag: 'project')

Parameters:

  • db (Database)

    The database instance to use for fetching data.

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

    A hash containing optional parameters for configuring the report.

Options Hash (options):

  • :filter (String, nil)

    The filter to apply when fetching items. Possible values include:

    • ‘today’: Filters items for the current day.

    • ‘yesterday’: Filters items for the previous day.

    • ‘week’: Filters items for the current week.

    • ‘month’: Filters items for the current month.

    • A date range in the format ‘YYYY-MM-DD..YYYY-MM-DD’: Filters items within the specified date range.

  • :tag (String, nil)

    The tag to filter the items by. Only items with this tag will be included.

  • :csv (String, nil)

    The filename to use when exporting the report to CSV. If provided, the report will be exported to the specified file.

  • :ics (String, nil)

    The filename to use when exporting the report to iCalendar format. If provided, the report will be exported to the specified file.



58
59
60
61
62
63
64
65
66
# File 'lib/timet/time_report.rb', line 58

def initialize(db, options = {})
  @db = db
  @csv_filename = options[:csv]
  @ics_filename = options[:ics]
  @filter = formatted_filter(options[:filter])
  @search_query = options[:search]
  @items = options[:filter] ? filter_items(@filter, options[:tag], @search_query) : @db.all_items
  @table = Table.new(@filter, @items, @db)
end

Instance Attribute Details

#csv_filenameObject (readonly)

Provides access to the CSV filename.



26
27
28
# File 'lib/timet/time_report.rb', line 26

def csv_filename
  @csv_filename
end

#dbObject (readonly)

Provides access to the database instance.



20
21
22
# File 'lib/timet/time_report.rb', line 20

def db
  @db
end

#ics_filenameObject (readonly)

Provides access to the ICS filename.



29
30
31
# File 'lib/timet/time_report.rb', line 29

def ics_filename
  @ics_filename
end

#itemsObject (readonly)

Provides access to the filtered items.



23
24
25
# File 'lib/timet/time_report.rb', line 23

def items
  @items
end

Instance Method Details

#displayvoid

Note:
  • The method checks if there are any tracked time entries. If not, it prints a message and exits.

  • It uses the ‘@table` instance to format and display the table.

  • A time block chart is generated and printed using the ‘TimeBlockChart` class.

  • The tag distribution is calculated and displayed based on the unique colors assigned to tags.

This method returns an undefined value.

Displays the report of tracked time entries.

This method formats and prints the report, including the table header, rows, total duration, a time block chart, and tag distribution. If no tracked time entries are found for the specified filter, it displays a message indicating no data is available.

Examples:

Display the report

time_report.display

See Also:



88
89
90
91
92
93
94
95
96
# File 'lib/timet/time_report.rb', line 88

def display
  return puts 'No tracked time found for the specified filter.' if @items.empty?

  @table.table
  colors = @items.map { |x| x[3] }.uniq.each_with_index.to_h
  chart = TimeBlockChart.new(@table)
  chart.print_time_block_chart(colors)
  tag_distribution(colors)
end

This method returns an undefined value.

Prints the tag distribution explanation. This method is a public wrapper for the private ‘print_explanation` method from the `TagDistribution` module.



103
104
105
106
107
# File 'lib/timet/time_report.rb', line 103

def print_tag_explanation_report
  time_stats = TimeStatistics.new(@items)
  total = time_stats.total_duration
  print_explanation(time_stats, total) if total.positive?
end

#show_row(item) ⇒ void

Note:
  • The method uses the ‘@table` instance to format and display the table header and row.

  • A separator is printed after the row to visually distinguish it from other rows.

  • The total duration is displayed at the end of the row.

This method returns an undefined value.

Displays a single row of the report.

This method formats and prints a single row of the report, including the table header, the specified row, a separator, and the total duration. It is used to display individual time entries in a structured format.

Examples:

Display a single row

time_report.show_row(item)

Parameters:

  • item (Array)

    The item (time entry) to display. The item is expected to contain the necessary data for the row, such as the time, description, and duration.

See Also:

  • #table
  • #display_time_entry


129
130
131
132
133
134
# File 'lib/timet/time_report.rb', line 129

def show_row(item)
  @table.header
  @table.display_time_entry(item)
  puts @table.separator
  @table.total
end