Module: Timet::TagDistribution

Included in:
TimeReport
Defined in:
lib/timet/tag_distribution.rb

Overview

The TagDistribution module provides functionality to format and display the distribution of tags based on their durations. This is particularly useful for visualizing how time is distributed across different tags in a project or task management system.

Constant Summary collapse

MAX_BAR_LENGTH =
70
BLOCK_CHAR =
''
TAG_SIZE =
12

Instance Method Summary collapse

Instance Method Details

#calculate_value_and_bar_length(duration, total) ⇒ Array<(Float, Integer)>

Calculates the percentage value and bar length for a given duration and total duration.

Examples:

calculate_value_and_bar_length(50, 100, 2) #=> [50.0, 25]

Parameters:

  • duration (Numeric)

    The duration for the current tag.

  • total (Numeric)

    The total duration.

Returns:

  • (Array<(Float, Integer)>)

    An array containing the calculated value and bar length.



116
117
118
119
120
121
# File 'lib/timet/tag_distribution.rb', line 116

def calculate_value_and_bar_length(duration, total)
  value = duration.to_f / total
  percentage_value = (value * 100).round(1)
  bar_length = (value * MAX_BAR_LENGTH).round
  [percentage_value, bar_length]
end

#generate_horizontal_bar(bar_length, color_index) ⇒ String

Generates a horizontal bar for display based on the bar length and color index.

Parameters:

  • bar_length (Numeric)

    The length of the bar to generate.

  • color_index (Numeric)

    The color index to use for the bar.

Returns:

  • (String)

    The generated horizontal bar string.



92
93
94
# File 'lib/timet/tag_distribution.rb', line 92

def generate_horizontal_bar(bar_length, color_index)
  (BLOCK_CHAR * bar_length).to_s.color(color_index + 1)
end

#generate_stats(tag, time_stats) ⇒ String

Generates the statistics string for a given tag.

Parameters:

  • tag (String)

    The tag for which to generate the statistics.

  • time_stats (Object)

    An object containing time statistics for the tags.

Returns:

  • (String)

    The generated statistics string.



101
102
103
104
105
106
# File 'lib/timet/tag_distribution.rb', line 101

def generate_stats(tag, time_stats)
  total_hours = (time_stats.total_duration_by_tag[tag] / 3600.0).round(1)
  avg_minutes = (time_stats.average_by_tag[tag] / 60.0).round(1)
  sd_minutes = (time_stats.standard_deviation_by_tag[tag] / 60).round(1)
  "T: #{total_hours}h, AVG: #{avg_minutes}min SD: #{sd_minutes}min".gray
end

This method returns an undefined value.

Prints the footer information.



50
51
52
53
54
55
# File 'lib/timet/tag_distribution.rb', line 50

def print_footer
  puts '-' * 45
  puts 'T:'.rjust(4).red + 'The total duration'.gray
  puts 'AVG:'.rjust(4).red + 'The average duration'.gray
  puts 'SD:'.rjust(4).red + 'The standard deviation of the durations'.gray
end

This method returns an undefined value.

Prints the summary information including total duration, average duration, and standard deviation.

Parameters:

  • time_stats (Object)

    An object containing the time statistics, including totals.

  • total (Numeric)

    The total duration of all tags combined.



62
63
64
65
66
67
68
# File 'lib/timet/tag_distribution.rb', line 62

def print_summary(time_stats, total)
  avg = (time_stats.totals[:avg] / 60.0).round(1)
  sd = (time_stats.totals[:sd] / 60.0).round(1)
  summary = "#{' ' * TAG_SIZE} #{'Summary'.underline}: "
  summary += "[T: #{(total / 3600.0).round(1)}h, AVG: #{avg}min SD: #{sd}min]".white
  puts summary
end

This method returns an undefined value.

Prints the detailed information for each tag.

Parameters:

  • time_stats (Object)

    An object containing the time statistics, including sorted durations by tag.

  • total (Numeric)

    The total duration of all tags combined.

  • colors (Object)

    An object containing color formatting methods.



76
77
78
79
80
81
82
83
84
85
# File 'lib/timet/tag_distribution.rb', line 76

def print_tags_info(time_stats, total, colors)
  time_stats.sorted_duration_by_tag.each do |tag, duration|
    value, bar_length = calculate_value_and_bar_length(duration, total)
    horizontal_bar = generate_horizontal_bar(bar_length, colors[tag])
    formatted_tag = tag[0...TAG_SIZE].rjust(TAG_SIZE)
    stats = generate_stats(tag, time_stats)

    puts "#{formatted_tag}: #{value.to_s.rjust(5)}%  #{horizontal_bar} [#{stats}]"
  end
end

#process_and_print_tags(time_stats, total, colors) ⇒ void

This method returns an undefined value.

Processes and prints the tag distribution information.

Parameters:

  • time_stats (Object)

    An object containing the time statistics, including totals and sorted durations by tag.

  • total (Numeric)

    The total duration of all tags combined.

  • colors (Object)

    An object containing color formatting methods.



41
42
43
44
45
# File 'lib/timet/tag_distribution.rb', line 41

def process_and_print_tags(time_stats, total, colors)
  print_summary(time_stats, total)
  print_tags_info(time_stats, total, colors)
  print_footer
end

#tag_distribution(colors) ⇒ void

This method returns an undefined value.

Formats and displays the tag distribution.

Examples:

duration_by_tag = { "timet" => 3600, "nextjs" => 1800 }
colors = { "timet" => "\e[31m", "nextjs" => "\e[32m" }
Formatter.format_tag_distribution(duration_by_tag, colors)
# Output:
#  \e[31m timet:   66.67%  ==================== \e[0m
#  \e[32m nextjs:   33.33%  ========== \e[0m

Parameters:

  • duration_by_tag (Hash<String, Integer>)

    A hash where keys are tags and values are durations in seconds.

  • colors (Hash<String, String>)

    A hash where keys are tags and values are color codes for display.



26
27
28
29
30
31
32
33
# File 'lib/timet/tag_distribution.rb', line 26

def tag_distribution(colors)
  time_stats = TimeStatistics.new(@items)
  total = time_stats.total_duration

  return unless total.positive?

  process_and_print_tags(time_stats, total, colors)
end