Module: TwitterWithAutoPagination::Analysis::Timelines

Includes:
REST::Utils
Included in:
API
Defined in:
lib/twitter_with_auto_pagination/analysis/timelines.rb

Constant Summary collapse

EVERY_DAY =
(0..6)
WDAY_COUNT =
EVERY_DAY.map { |n| [n, 0] }.to_h
WDAY_NIL_COUNT =
EVERY_DAY.map { |n| [n, nil] }.to_h
EVERY_HOUR =
(0..23)
HOUR_COUNT =
EVERY_HOUR.map { |n| [n, 0] }.to_h
HOUR_NIL_COUNT =
EVERY_HOUR.map { |n| [n, nil] }.to_h

Constants included from REST::Utils

REST::Utils::DEFAULT_CALL_LIMIT

Instance Method Summary collapse

Methods included from Collector

#collect_with_cursor, #collect_with_max_id

Instance Method Details

#count_hour(times) ⇒ Object



20
21
22
# File 'lib/twitter_with_auto_pagination/analysis/timelines.rb', line 20

def count_hour(times)
  times.each_with_object(HOUR_COUNT.dup) { |time, memo| memo[time.hour] += 1 }
end

#count_wday(times) ⇒ Object



16
17
18
# File 'lib/twitter_with_auto_pagination/analysis/timelines.rb', line 16

def count_wday(times)
  times.each_with_object(WDAY_COUNT.dup) { |time, memo| memo[time.wday] += 1 }
end

#twitter_addiction_series(times, day_names:) ⇒ Object

[

{:name=>"Sun", :y=>14.778310502283107},
{:name=>"Mon", :y=>12.273439878234399},
{:name=>"Tue", :y=>10.110578386605784},
{:name=>"Wed", :y=>9.843683409436835},
{:name=>"Thu", :y=>10.547945205479452},
{:name=>"Fri", :y=>10.61773211567732},
{:name=>"Sat", :y=>12.115753424657534}

]



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/twitter_with_auto_pagination/analysis/timelines.rb', line 100

def twitter_addiction_series(times, day_names:)
  max_duration = 5.minutes
  wday_count =
    EVERY_DAY.each_with_object(WDAY_NIL_COUNT.dup) do |wday, memo|
      target_times = times.select { |t| t.wday == wday }
      memo[wday] =
        if target_times.empty?
          nil
        else
          target_times.each_cons(2).map { |newer, older| (newer - older) < max_duration ? newer - older : max_duration }.sum
        end
    end
  days = times.map { |t| t.to_date.to_s(:long) }.uniq.size
  weeks = [days / 7.0, 1.0].max
  wday_count.map do |wday, seconds|
    {name: day_names[wday], y: (seconds.nil? ? nil : seconds / weeks / 60)}
  end
end

#usage_stats(tweet_times, day_names: %w(Sun Mon Tue Wed Thu Fri Sat))) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/twitter_with_auto_pagination/analysis/timelines.rb', line 119

def usage_stats(tweet_times, day_names: %w(Sun Mon Tue Wed Thu Fri Sat))
  [
    usage_stats_wday_series_data(tweet_times, day_names: day_names),
    usage_stats_wday_drilldown_series(tweet_times, day_names: day_names),
    usage_stats_hour_series_data(tweet_times),
    usage_stats_hour_drilldown_series(tweet_times, day_names: day_names),
    twitter_addiction_series(tweet_times, day_names: day_names)
  ]
end

#usage_stats_hour_drilldown_series(times, day_names:) ⇒ Object

[

{:name=>"0", :id=>"0", :data=>[["Sun", 7], ["Mon", 22], ["Tue", 8], ["Wed", 9], ["Thu", 9], ["Fri", 6], ["Sat", 5]]},
{:name=>"1", :id=>"1", :data=>[["Sun", 12], ["Mon", 11], ["Tue", 5], ["Wed", 5], ["Thu", 0], ["Fri", 8], ["Sat", 6]]},
...

]



80
81
82
83
84
85
86
87
88
89
# File 'lib/twitter_with_auto_pagination/analysis/timelines.rb', line 80

def usage_stats_hour_drilldown_series(times, day_names:)
  counts =
    EVERY_HOUR.each_with_object(HOUR_NIL_COUNT.dup) do |hour, memo|
      memo[hour] = count_wday(times.select { |t| t.hour == hour })
    end

  counts.map do |hour, wday_count|
    {name: hour.to_s, id: hour.to_s, data: wday_count.map { |wday, count| [day_names[wday], count] }}
  end
end

#usage_stats_hour_series_data(times) ⇒ Object

[

{:name=>"0", :y=>66, :drilldown=>"0"},
{:name=>"1", :y=>47, :drilldown=>"1"},
...
{:name=>"22", :y=>73, :drilldown=>"22"},
{:name=>"23", :y=>87, :drilldown=>"23"}

]



69
70
71
72
73
# File 'lib/twitter_with_auto_pagination/analysis/timelines.rb', line 69

def usage_stats_hour_series_data(times)
  count_hour(times).map do |hour, count|
    {name: hour.to_s, y: count, drilldown: hour.to_s}
  end
end

#usage_stats_wday_drilldown_series(times, day_names:) ⇒ Object

[

{
  :name=>"Sun",
  :id=>"Sun",
  :data=> [ ["0", 7], ["1", 12], ... , ["22", 10], ["23", 12] ]
},
...
{
  :name=>"Mon",
  :id=>"Mon",
  :data=> [ ["0", 22], ["1", 11], ... , ["22", 9], ["23", 14] ]
}


51
52
53
54
55
56
57
58
59
60
# File 'lib/twitter_with_auto_pagination/analysis/timelines.rb', line 51

def usage_stats_wday_drilldown_series(times, day_names:)
  counts =
    EVERY_DAY.each_with_object(WDAY_NIL_COUNT.dup) do |wday, memo|
      memo[wday] = count_hour(times.select { |t| t.wday == wday })
    end

  counts.map { |wday, hour_count| [day_names[wday], hour_count] }.map do |wday, hour_count|
    {name: wday, id: wday, data: hour_count.map { |hour, count| [hour.to_s, count] }}
  end
end

#usage_stats_wday_series_data(times, day_names:) ⇒ Object

[

{:name=>"Sun", :y=>111, :drilldown=>"Sun"},
{:name=>"Mon", :y=>95,  :drilldown=>"Mon"},
{:name=>"Tue", :y=>72,  :drilldown=>"Tue"},
{:name=>"Wed", :y=>70,  :drilldown=>"Wed"},
{:name=>"Thu", :y=>73,  :drilldown=>"Thu"},
{:name=>"Fri", :y=>81,  :drilldown=>"Fri"},
{:name=>"Sat", :y=>90,  :drilldown=>"Sat"}

]



33
34
35
36
37
# File 'lib/twitter_with_auto_pagination/analysis/timelines.rb', line 33

def usage_stats_wday_series_data(times, day_names:)
  count_wday(times).map do |wday, count|
    {name: day_names[wday], y: count, drilldown: day_names[wday]}
  end
end