Skip to content

Commit

Permalink
Merge pull request #381 from Fullscreen/remove-by-week
Browse files Browse the repository at this point in the history
Remove 'by: :week' option from code
  • Loading branch information
kangkyu committed Feb 20, 2020
2 parents 3bb1c25 + d50f002 commit 8a588f7
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 93 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,20 @@ For more information about changelogs, check
[Vandamme](http://tech-angels.github.io/vandamme).


## Unreleased

If your code calls reports methods such as `views`, `likes`, or `reports`,
do not include `by: :week` option since `7DayTotals` dimension will no longer be
supported by YouTube API as of [April 15, 2020](https://developers.google.com/youtube/analytics/revision_history#october-15,-2019).

Use `by: :day` option instead and add up the numbers from each day.

If you keep using `by: :week` option after this change it will raise an error
(before the gem upgrade) or it will run with `day` dimension instead (after
the gem upgrade, like any other random input).

* [REMOVAL] Remove `by: :week` option for reports.

## 0.32.6 - 2020-02-07

* [FEATURE] Allow partnered channels to delete playlist item.
Expand Down
23 changes: 9 additions & 14 deletions lib/yt/associations/has_reports.rb
Expand Up @@ -10,7 +10,7 @@ module HasReports
# @option options [Array<Symbol>] :only The metrics to generate reports
# for.
# @option options [Symbol] :by (:day) The dimension to collect metrics
# by. Accepted values are: +:day+, +:week+, +:month+.
# by. Accepted values are: +:day+, +:month+.
# @option options [#to_date] :since The first day of the time-range.
# Also aliased as +:from+.
# @option options [#to_date] :until The last day of the time-range.
Expand Down Expand Up @@ -40,11 +40,6 @@ module HasReports
# @example Get the $1 for this and last month:
# resource.$1 since: 1.month.ago, by: :month
# # => {Wed, 01 Apr 2014..Thu, 30 Apr 2014 => 12.0, Fri, 01 May 2014..Sun, 31 May 2014 => 34.0, …}
# @return [Hash<Range<Date, Date>, $2>] if grouped by week, the $1
# for each week in the time-range.
# @example Get the $1 for this and last week:
# resource.$1 since: 1.week.ago, by: :week
# # => {Wed, 01 Apr 2014..Tue, 07 Apr 2014 => 20.0, Wed, 08 Apr 2014..Tue, 14 Apr 2014 => 13.0, …}
# @macro report

# @!macro [new] report_with_range
Expand Down Expand Up @@ -75,19 +70,19 @@ module HasReports

# @!macro [new] report_by_day
# @option options [Symbol] :by (:day) The dimension to collect $1 by.
# Accepted values are: +:day+, +:week+, +:month+.
# Accepted values are: +:day+, +:month+.
# @macro report_with_day

# @!macro [new] report_by_day_and_country
# @option options [Symbol] :by (:day) The dimension to collect $1 by.
# Accepted values are: +:day+, +:week+, +:month+, :+range+.
# Accepted values are: +:day+, +:month+, :+range+.
# @macro report_with_day
# @macro report_with_range
# @macro report_with_country

# @!macro [new] report_by_day_and_state
# @option options [Symbol] :by (:day) The dimension to collect $1 by.
# Accepted values are: +:day+, +:week+, +:month+, :+range+.
# Accepted values are: +:day+, +:month+, :+range+.
# @macro report_with_day
# @macro report_with_range
# @macro report_with_country_and_state
Expand Down Expand Up @@ -128,7 +123,7 @@ module HasReports

# @!macro [new] report_by_video_dimensions
# @option options [Symbol] :by (:day) The dimension to collect $1 by.
# Accepted values are: +:day+, +:week+, +:month+, +:range+,
# Accepted values are: +:day+, +:month+, +:range+,
# +:traffic_source+,+:search_term+, +:playback_location+,
# +:related_video+, +:embedded_player_location+.
# @option options [Array<Symbol>] :includes ([:id]) if grouped by
Expand Down Expand Up @@ -162,7 +157,7 @@ module HasReports

# @!macro [new] report_by_channel_dimensions
# @option options [Symbol] :by (:day) The dimension to collect $1 by.
# Accepted values are: +:day+, +:week+, +:month+, +:range+,
# Accepted values are: +:day+, +:month+, +:range+,
# +:traffic_source+, +:search_term+, +:playback_location+, +:video+,
# +:related_video+, +:playlist+, +:embedded_player_location+.
# @return [Hash<Symbol, $2>] if grouped by embedded player location,
Expand All @@ -175,7 +170,7 @@ module HasReports

# @!macro [new] report_by_playlist_dimensions
# @option options [Symbol] :by (:day) The dimension to collect $1 by.
# Accepted values are: +:day+, +:week+, +:month+, +:range+,
# Accepted values are: +:day+, +:month+, +:range+,
# +:traffic_source+, +:playback_location+, +:related_video+, +:video+,
# +:playlist+.
# @macro report_with_channel_dimensions
Expand Down Expand Up @@ -228,7 +223,7 @@ def has_report(metric, type)
def define_reports_method(metric, type)
(@metrics ||= {})[metric] = type
define_method :reports do |options = {}|
from = options[:since] || options[:from] || (options[:by].in?([:day, :week, :month]) ? 5.days.ago : '2005-02-01')
from = options[:since] || options[:from] || (options[:by].in?([:day, :month]) ? 5.days.ago : '2005-02-01')
to = options[:until] || options[:to] || Date.today
location = options[:in]
country = location.is_a?(Hash) ? location[:country] : location
Expand All @@ -252,7 +247,7 @@ def define_reports_method(metric, type)

def define_metric_method(metric)
define_method metric do |options = {}|
from = options[:since] || options[:from] || (options[:by].in?([:day, :week, :month]) ? 5.days.ago : '2005-02-01')
from = options[:since] || options[:from] || (options[:by].in?([:day, :month]) ? 5.days.ago : '2005-02-01')
to = options[:until] || options[:to] || Date.today
location = options[:in]
country = location.is_a?(Hash) ? location[:country] : location
Expand Down
6 changes: 0 additions & 6 deletions lib/yt/collections/reports.rb
Expand Up @@ -6,7 +6,6 @@ module Collections
class Reports < Base
DIMENSIONS = Hash.new({name: 'day', parse: ->(day, *values) { @metrics.keys.zip(values.map{|v| {Date.iso8601(day) => v}}).to_h} }).tap do |hash|
hash[:month] = {name: 'month', parse: ->(month, *values) { @metrics.keys.zip(values.map{|v| {Range.new(Date.strptime(month, '%Y-%m').beginning_of_month, Date.strptime(month, '%Y-%m').end_of_month) => v} }).to_h} }
hash[:week] = {name: '7DayTotals', parse: ->(last_day_of_week, *values) { @metrics.keys.zip(values.map{|v| {Range.new(Date.strptime(last_day_of_week) - 6, Date.strptime(last_day_of_week)) => v} }).to_h} }
hash[:range] = {parse: ->(*values) { @metrics.keys.zip(values.map{|v| {total: v}}).to_h } }
hash[:traffic_source] = {name: 'insightTrafficSourceType', parse: ->(source, *values) { @metrics.keys.zip(values.map{|v| {TRAFFIC_SOURCES.key(source) => v}}).to_h} }
hash[:playback_location] = {name: 'insightPlaybackLocationType', parse: ->(location, *values) { @metrics.keys.zip(values.map{|v| {PLAYBACK_LOCATIONS.key(location) => v}}).to_h} }
Expand Down Expand Up @@ -140,11 +139,6 @@ def within(days_range, country, state, dimension, videos, historical, max_retrie
end
if dimension == :month
hash = hash.transform_values{|h| h.sort_by{|range, v| range.first}.to_h}
elsif dimension == :week
hash = hash.transform_values do |h|
h.select{|range, v| range.last.wday == days_range.last.wday}.
sort_by{|range, v| range.first}.to_h
end
elsif dimension == :day
hash = hash.transform_values{|h| h.sort_by{|day, v| day}.to_h}
elsif dimension.in? [:traffic_source, :country, :state, :playback_location, :device_type, :operating_system, :subscribed_status]
Expand Down
24 changes: 0 additions & 24 deletions spec/requests/as_content_owner/channel_spec.rb
Expand Up @@ -45,18 +45,6 @@
expect(result[metric].values).to all(be_a type)
end
end

specify 'by week' do
range = {since: ENV['YT_TEST_PARTNER_VIDEO_DATE'], until: Date.parse(ENV['YT_TEST_PARTNER_VIDEO_DATE']) + 9}
result = channel.reports range.merge(only: metrics, by: :week)
metrics.each do |metric, type|
expect(result[metric].size).to be <= 2
expect(result[metric].keys).to all(be_a Range)
expect(result[metric].keys.map{|range| range.first.wday}.uniq).to be_one
expect(result[metric].keys.map{|range| range.last.wday}.uniq).to be_one
expect(result[metric].values).to all(be_a type)
end
end
end

[:views, :comments, :likes, :dislikes, :shares,
Expand Down Expand Up @@ -104,18 +92,6 @@
expect(result.keys.map &:last).to eq result.keys.map(&:last).map(&:end_of_month)
end
end

describe "#{metric} can be grouped by week and returns non-overlapping periods" do
let(:metric) { metric }
let(:range) { {since: ENV['YT_TEST_PARTNER_VIDEO_DATE'], until: Date.parse(ENV['YT_TEST_PARTNER_VIDEO_DATE']) + 9} }
let(:result) { channel.public_send metric, range.merge(by: :week)}
specify do
expect(result.size).to be <= 2
expect(result.keys).to all(be_a Range)
expect(result.keys.map{|range| range.first.wday}.uniq).to be_one
expect(result.keys.map{|range| range.last.wday}.uniq).to be_one
end
end
end

{views: Integer, comments: Integer, likes: Integer, dislikes: Integer,
Expand Down
26 changes: 1 addition & 25 deletions spec/requests/as_content_owner/playlist_spec.rb
Expand Up @@ -35,18 +35,6 @@
expect(result[metric].values).to all(be_a type)
end
end

specify 'by week' do
range = {since: ENV['YT_TEST_PARTNER_VIDEO_DATE'], until: Date.parse(ENV['YT_TEST_PARTNER_VIDEO_DATE']) + 9}
result = playlist.reports range.merge(only: metrics, by: :week)
metrics.each do |metric, type|
expect(result[metric].size).to be <= 2
expect(result[metric].keys).to all(be_a Range)
expect(result[metric].keys.map{|range| range.first.wday}.uniq).to be_one
expect(result[metric].keys.map{|range| range.last.wday}.uniq).to be_one
expect(result[metric].values).to all(be_a type)
end
end
end

{views: Integer, estimated_minutes_watched: Integer,
Expand Down Expand Up @@ -88,18 +76,6 @@
end
end

describe "#{metric} can be grouped by week and returns non-overlapping periods" do
let(:metric) { metric }
let(:range) { {since: ENV['YT_TEST_PARTNER_VIDEO_DATE'], until: Date.parse(ENV['YT_TEST_PARTNER_VIDEO_DATE']) + 9} }
let(:result) { playlist.public_send metric, range.merge(by: :week)}
specify do
expect(result.size).to be <= 2
expect(result.keys).to all(be_a Range)
expect(result.keys.map{|range| range.first.wday}.uniq).to be_one
expect(result.keys.map{|range| range.last.wday}.uniq).to be_one
end
end

describe "#{metric} can be grouped by range" do
let(:metric) { metric }

Expand Down Expand Up @@ -764,4 +740,4 @@
end
end
end
end
end
24 changes: 0 additions & 24 deletions spec/requests/as_content_owner/video_spec.rb
Expand Up @@ -58,18 +58,6 @@
end
end

describe "#{metric} can be grouped by week and returns non-overlapping periods" do
let(:metric) { metric }
let(:range) { {since: ENV['YT_TEST_PARTNER_VIDEO_DATE'], until: Date.parse(ENV['YT_TEST_PARTNER_VIDEO_DATE']) + 9} }
let(:result) { video.public_send metric, range.merge(by: :week)}
specify do
expect(result.size).to be <= 2
expect(result.keys).to all(be_a Range)
expect(result.keys.map{|range| range.first.wday}.uniq).to be_one
expect(result.keys.map{|range| range.last.wday}.uniq).to be_one
end
end

describe "#{metric} can be retrieved for a single country" do
let(:result) { video.public_send metric, options }

Expand Down Expand Up @@ -208,18 +196,6 @@
expect(result[metric].values).to all(be_a type)
end
end

specify 'by week' do
range = {since: ENV['YT_TEST_PARTNER_VIDEO_DATE'], until: Date.parse(ENV['YT_TEST_PARTNER_VIDEO_DATE']) + 9}
result = video.reports range.merge(only: metrics, by: :week)
metrics.each do |metric, type|
expect(result[metric].size).to be <= 2
expect(result[metric].keys).to all(be_a Range)
expect(result[metric].keys.map{|range| range.first.wday}.uniq).to be_one
expect(result[metric].keys.map{|range| range.last.wday}.uniq).to be_one
expect(result[metric].values).to all(be_a type)
end
end
end

describe 'estimated_revenue can be grouped by day' do
Expand Down

0 comments on commit 8a588f7

Please sign in to comment.