Class: Timet::TimeStatistics
- Inherits:
-
Object
- Object
- Timet::TimeStatistics
- Defined in:
- lib/timet/time_statistics.rb
Instance Attribute Summary collapse
-
#duration_by_tag ⇒ Hash
readonly
with each tag.
-
#total_duration ⇒ Integer
readonly
The total duration (in seconds) of all time intervals across all tags.
Instance Method Summary collapse
-
#additional_stats_by_tag ⇒ Hash<String, Hash>
Returns a hash where keys are tags and values are additional descriptive statistics for the durations of each tag.
-
#average_by_tag ⇒ Hash<String, Float>
Returns a hash where keys are tags and values are the average duration (in seconds) for each tag.
-
#calculate_durations_by_tag ⇒ void
Calculates the duration for each tag and updates the @duration_by_tag and @total_duration attributes.
-
#initialize(data) ⇒ TimeStatistics
constructor
Initializes a new instance of TimeStatistics.
-
#sorted_duration_by_tag ⇒ Array<Array>
Returns an array of arrays where each sub-array contains a tag and its total duration, sorted by duration in descending order.
-
#standard_deviation_by_tag ⇒ Hash<String, Float>
Returns a hash where keys are tags and values are the standard deviation of durations for each tag.
-
#total_duration_by_tag ⇒ Hash<String, Integer>
Returns a hash where keys are tags and values are the total duration (in seconds) for each tag.
-
#totals ⇒ Hash
Returns a hash containing the total duration, average duration, and standard deviation of durations.
Constructor Details
#initialize(data) ⇒ TimeStatistics
Initializes a new instance of TimeStatistics.
23 24 25 26 27 28 |
# File 'lib/timet/time_statistics.rb', line 23 def initialize(data) @data = data @duration_by_tag = Hash.new { |hash, key| hash[key] = [] } @total_duration = 0 calculate_durations_by_tag end |
Instance Attribute Details
#duration_by_tag ⇒ Hash (readonly)
with each tag.
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 87 88 89 90 91 92 |
# File 'lib/timet/time_statistics.rb', line 12 class TimeStatistics attr_reader :duration_by_tag, :total_duration # Initializes a new instance of TimeStatistics. # # @param data [Array<Array>] An array of arrays where each sub-array contains: # - [0] An identifier (not used in calculations) # - [1] The start time (in seconds since the epoch) # - [2] The end time (in seconds since the epoch), or nil if the interval is ongoing # - [3] The tag associated with the time interval # @return [TimeStatistics] A new instance of TimeStatistics. def initialize(data) @data = data @duration_by_tag = Hash.new { |hash, key| hash[key] = [] } @total_duration = 0 calculate_durations_by_tag end # Returns a hash containing the total duration, average duration, and standard deviation of durations. # # @return [Hash] A hash with the following keys: # - :total [Numeric] The total duration. # - :avg [Numeric] The average duration. # - :sd [Numeric] The standard deviation of the durations. def totals durations = @duration_by_tag.values.flatten { total: @total_duration, avg: durations.mean, sd: durations.standard_deviation } end # Calculates the duration for each tag and updates the @duration_by_tag and @total_duration attributes. # # @return [void] def calculate_durations_by_tag @data.each do |row| start_time = row[1] end_time = row[2] || Time.now.to_i tag = row[3] duration = end_time - start_time @duration_by_tag[tag] << duration @total_duration += duration end end # Returns a hash where keys are tags and values are the total duration (in seconds) for each tag. # # @return [Hash<String, Integer>] A hash mapping tags to their total durations. def total_duration_by_tag @duration_by_tag.transform_values(&:sum) end # Returns an array of arrays where each sub-array contains a tag and its total duration, sorted by duration in # descending order. # # @return [Array<Array>] An array of [tag, total_duration] pairs sorted by total_duration in descending order. def sorted_duration_by_tag @duration_by_tag.map { |tag, durations| [tag, durations.sum] }.sort_by { |_, sum| -sum } end # Returns a hash where keys are tags and values are the average duration (in seconds) for each tag. # # @return [Hash<String, Float>] A hash mapping tags to their average durations. def average_by_tag @duration_by_tag.transform_values { |durations| durations.sum.to_f / durations.size } end # Returns a hash where keys are tags and values are the standard deviation of durations for each tag. # # @return [Hash<String, Float>] A hash mapping tags to their standard deviations. def standard_deviation_by_tag @duration_by_tag.transform_values(&:standard_deviation) end # Returns a hash where keys are tags and values are additional descriptive statistics for the durations of each tag. # # @return [Hash<String, Hash>] A hash mapping tags to a hash of descriptive statistics # (e.g., min, max, median, etc.). def additional_stats_by_tag @duration_by_tag.transform_values(&:descriptive_statistics) end end |
#total_duration ⇒ Integer (readonly)
Returns The total duration (in seconds) of all time intervals across all tags.
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 87 88 89 90 91 92 |
# File 'lib/timet/time_statistics.rb', line 12 class TimeStatistics attr_reader :duration_by_tag, :total_duration # Initializes a new instance of TimeStatistics. # # @param data [Array<Array>] An array of arrays where each sub-array contains: # - [0] An identifier (not used in calculations) # - [1] The start time (in seconds since the epoch) # - [2] The end time (in seconds since the epoch), or nil if the interval is ongoing # - [3] The tag associated with the time interval # @return [TimeStatistics] A new instance of TimeStatistics. def initialize(data) @data = data @duration_by_tag = Hash.new { |hash, key| hash[key] = [] } @total_duration = 0 calculate_durations_by_tag end # Returns a hash containing the total duration, average duration, and standard deviation of durations. # # @return [Hash] A hash with the following keys: # - :total [Numeric] The total duration. # - :avg [Numeric] The average duration. # - :sd [Numeric] The standard deviation of the durations. def totals durations = @duration_by_tag.values.flatten { total: @total_duration, avg: durations.mean, sd: durations.standard_deviation } end # Calculates the duration for each tag and updates the @duration_by_tag and @total_duration attributes. # # @return [void] def calculate_durations_by_tag @data.each do |row| start_time = row[1] end_time = row[2] || Time.now.to_i tag = row[3] duration = end_time - start_time @duration_by_tag[tag] << duration @total_duration += duration end end # Returns a hash where keys are tags and values are the total duration (in seconds) for each tag. # # @return [Hash<String, Integer>] A hash mapping tags to their total durations. def total_duration_by_tag @duration_by_tag.transform_values(&:sum) end # Returns an array of arrays where each sub-array contains a tag and its total duration, sorted by duration in # descending order. # # @return [Array<Array>] An array of [tag, total_duration] pairs sorted by total_duration in descending order. def sorted_duration_by_tag @duration_by_tag.map { |tag, durations| [tag, durations.sum] }.sort_by { |_, sum| -sum } end # Returns a hash where keys are tags and values are the average duration (in seconds) for each tag. # # @return [Hash<String, Float>] A hash mapping tags to their average durations. def average_by_tag @duration_by_tag.transform_values { |durations| durations.sum.to_f / durations.size } end # Returns a hash where keys are tags and values are the standard deviation of durations for each tag. # # @return [Hash<String, Float>] A hash mapping tags to their standard deviations. def standard_deviation_by_tag @duration_by_tag.transform_values(&:standard_deviation) end # Returns a hash where keys are tags and values are additional descriptive statistics for the durations of each tag. # # @return [Hash<String, Hash>] A hash mapping tags to a hash of descriptive statistics # (e.g., min, max, median, etc.). def additional_stats_by_tag @duration_by_tag.transform_values(&:descriptive_statistics) end end |
Instance Method Details
#additional_stats_by_tag ⇒ Hash<String, Hash>
Returns a hash where keys are tags and values are additional descriptive statistics for the durations of each tag.
(e.g., min, max, median, etc.).
89 90 91 |
# File 'lib/timet/time_statistics.rb', line 89 def additional_stats_by_tag @duration_by_tag.transform_values(&:descriptive_statistics) end |
#average_by_tag ⇒ Hash<String, Float>
Returns a hash where keys are tags and values are the average duration (in seconds) for each tag.
74 75 76 |
# File 'lib/timet/time_statistics.rb', line 74 def average_by_tag @duration_by_tag.transform_values { |durations| durations.sum.to_f / durations.size } end |
#calculate_durations_by_tag ⇒ void
This method returns an undefined value.
Calculates the duration for each tag and updates the @duration_by_tag and @total_duration attributes.
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/timet/time_statistics.rb', line 44 def calculate_durations_by_tag @data.each do |row| start_time = row[1] end_time = row[2] || Time.now.to_i tag = row[3] duration = end_time - start_time @duration_by_tag[tag] << duration @total_duration += duration end end |
#sorted_duration_by_tag ⇒ Array<Array>
Returns an array of arrays where each sub-array contains a tag and its total duration, sorted by duration in descending order.
67 68 69 |
# File 'lib/timet/time_statistics.rb', line 67 def sorted_duration_by_tag @duration_by_tag.map { |tag, durations| [tag, durations.sum] }.sort_by { |_, sum| -sum } end |
#standard_deviation_by_tag ⇒ Hash<String, Float>
Returns a hash where keys are tags and values are the standard deviation of durations for each tag.
81 82 83 |
# File 'lib/timet/time_statistics.rb', line 81 def standard_deviation_by_tag @duration_by_tag.transform_values(&:standard_deviation) end |
#total_duration_by_tag ⇒ Hash<String, Integer>
Returns a hash where keys are tags and values are the total duration (in seconds) for each tag.
59 60 61 |
# File 'lib/timet/time_statistics.rb', line 59 def total_duration_by_tag @duration_by_tag.transform_values(&:sum) end |
#totals ⇒ Hash
Returns a hash containing the total duration, average duration, and standard deviation of durations.
36 37 38 39 |
# File 'lib/timet/time_statistics.rb', line 36 def totals durations = @duration_by_tag.values.flatten { total: @total_duration, avg: durations.mean, sd: durations.standard_deviation } end |