Module: TranzitoUtils::GraphingHelper
- Included in:
- Helpers
- Defined in:
- lib/tranzito_utils/helpers/graphing_helper.rb
Instance Method Summary collapse
- #group_by_format(time_range, group_period = nil) ⇒ Object
- #group_by_method(time_range) ⇒ Object
- #humanized_time_range(time_range) ⇒ Object
- #humanized_time_range_column(time_range_column, return_value_for_all: false) ⇒ Object
-
#period_in_words(seconds) ⇒ Object
Initially just used by scheduled jobs display, but now used by other things!.
- #time_range_amounts(collection:, column: "created_at", amount_column: "amount_cents", time_range: nil) ⇒ Object
- #time_range_counts(collection:, column: "created_at", time_range: nil) ⇒ Object
- #time_range_duration(collection:, column: "created_at", duration_column: "duration_seconds", time_range: nil) ⇒ Object
Instance Method Details
#group_by_format(time_range, group_period = nil) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/tranzito_utils/helpers/graphing_helper.rb', line 44 def group_by_format(time_range, group_period = nil) group_period ||= group_by_method(time_range) if group_period == :group_by_minute "%l:%M %p" elsif group_period == :group_by_hour "%a%l %p" elsif group_period == :group_by_week "%Y-%-m-%-d" elsif i[group_by_day group_by_week].include?(group_period) || time_range.present? && time_range.last - time_range.first < 2.weeks.to_i "%a %Y-%-m-%-d" elsif group_period == :group_by_month "%Y-%-m" end end |
#group_by_method(time_range) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/tranzito_utils/helpers/graphing_helper.rb', line 30 def group_by_method(time_range) if time_range.last - time_range.first < 3601 # 1.hour + 1 second :group_by_minute elsif time_range.last - time_range.first < 500_000 # around 6 days :group_by_hour elsif time_range.last - time_range.first < 5_000_000 # around 60 days :group_by_day elsif time_range.last - time_range.first < 31449600 # 364 days (52 weeks) :group_by_week else :group_by_month end end |
#humanized_time_range(time_range) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/tranzito_utils/helpers/graphing_helper.rb', line 69 def humanized_time_range(time_range) return nil if @period == "all" unless @period == "custom" period_display = @period.match?("next_") ? @period.tr("_", " ") : "past #{@period}" return "in the #{period_display}" end group_by = group_by_method(time_range) precision_class = if group_by == :group_by_minute "preciseTimeSeconds" elsif group_by == :group_by_hour "preciseTime" else "" end content_tag(:span) do concat "from " concat content_tag(:em, l(time_range.first, format: :convert_time), class: "convertTime #{precision_class}") concat " to " if time_range.last > Time.current - 5.minutes concat content_tag(:em, "now") else concat content_tag(:em, l(time_range.last, format: :convert_time), class: "convertTime #{precision_class}") end end end |
#humanized_time_range_column(time_range_column, return_value_for_all: false) ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/tranzito_utils/helpers/graphing_helper.rb', line 59 def humanized_time_range_column(time_range_column, return_value_for_all: false) return_value_for_all = true if @render_chart return nil unless return_value_for_all || !(@period == "all") humanized_text = time_range_column.to_s.gsub("_at", "").humanize.downcase return humanized_text.gsub("start", "starts") if time_range_column&.match?("start_at") return humanized_text.gsub("end", "ends") if time_range_column&.match?("end_at") return humanized_text.gsub("needs", "need") if time_range_column&.match?("needs_renewal_at") humanized_text end |
#period_in_words(seconds) ⇒ Object
Initially just used by scheduled jobs display, but now used by other things!
99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/tranzito_utils/helpers/graphing_helper.rb', line 99 def period_in_words(seconds) return "" if seconds.blank? seconds = seconds.to_i.abs if seconds < 1.minute pluralize(seconds, "second") elsif seconds >= 1.minute && seconds < 1.hour pluralize((seconds / 60.0).round(1), "minute") elsif seconds >= 1.hour && seconds < 24.hours pluralize((seconds / 3600.0).round(1), "hour") elsif seconds >= 24.hours pluralize((seconds / 86400.0).round(1), "day") end.gsub(".0 ", " ") # strip out the empty zero end |
#time_range_amounts(collection:, column: "created_at", amount_column: "amount_cents", time_range: nil) ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/tranzito_utils/helpers/graphing_helper.rb', line 12 def time_range_amounts(collection:, column: "created_at", amount_column: "amount_cents", time_range: nil) time_range ||= @time_range # Note: by specifying the range parameter, we force it to display empty days collection.send(group_by_method(time_range), column, range: time_range, format: group_by_format(time_range)) .sum(amount_column) .map { |k, v| [k, (v.to_f / 100.00).round(2)] } # Convert cents to dollars .to_h end |
#time_range_counts(collection:, column: "created_at", time_range: nil) ⇒ Object
5 6 7 8 9 10 |
# File 'lib/tranzito_utils/helpers/graphing_helper.rb', line 5 def time_range_counts(collection:, column: "created_at", time_range: nil) time_range ||= @time_range # Note: by specifying the range parameter, we force it to display empty days collection.send(group_by_method(time_range), column, range: time_range, format: group_by_format(time_range)) .count end |
#time_range_duration(collection:, column: "created_at", duration_column: "duration_seconds", time_range: nil) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/tranzito_utils/helpers/graphing_helper.rb', line 21 def time_range_duration(collection:, column: "created_at", duration_column: "duration_seconds", time_range: nil) time_range ||= @time_range # Note: by specifying the range parameter, we force it to display empty days collection.send(group_by_method(time_range), column, range: time_range, format: group_by_format(time_range)) .sum(duration_column) .map { |k, v| [k, (v.to_f / 3600.00).round(2)] } # Convert seconds to decimal hours .to_h end |