Class: Embulk::Input::GoogleAnalytics::Plugin
- Inherits:
-
InputPlugin
- Object
- InputPlugin
- Embulk::Input::GoogleAnalytics::Plugin
- Defined in:
- lib/embulk/input/google_analytics/plugin.rb
Class Method Summary collapse
- .canonicalize_column_name(name) ⇒ Object
- .columns_from_task(task) ⇒ Object
- .resume(task, columns, count, &control) ⇒ Object
- .task_from_config(config) ⇒ Object
- .transaction(config, &control) ⇒ Object
Instance Method Summary collapse
- #calculate_next_times(fetched_latest_time) ⇒ Object
- #init ⇒ Object
- #preview? ⇒ Boolean
- #run ⇒ Object
Class Method Details
.canonicalize_column_name(name) ⇒ Object
74 75 76 77 |
# File 'lib/embulk/input/google_analytics/plugin.rb', line 74 def self.canonicalize_column_name(name) # ga:dateHour -> date_hour name.gsub(/^ga:/, "").gsub(/[A-Z]+/, "_\\0").gsub(/^_/, "").downcase end |
.columns_from_task(task) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/embulk/input/google_analytics/plugin.rb', line 66 def self.columns_from_task(task) [ task["time_series"], task["dimensions"], task["metrics"], ].flatten.uniq end |
.resume(task, columns, count, &control) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/embulk/input/google_analytics/plugin.rb', line 42 def self.resume(task, columns, count, &control) task_reports = yield(task, columns, count) next_config_diff = task_reports.first return next_config_diff end |
.task_from_config(config) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/embulk/input/google_analytics/plugin.rb', line 49 def self.task_from_config(config) json_key_content = config.param("json_key_content", :string) { "json_key_content" => json_key_content, "view_id" => config.param("view_id", :string), "dimensions" => config.param("dimensions", :array, default: []), "metrics" => config.param("metrics", :array, default: []), "time_series" => config.param("time_series", :string), "start_date" => config.param("start_date", :string, default: nil), "end_date" => config.param("end_date", :string, default: nil), "incremental" => config.param("incremental", :bool, default: true), "last_record_time" => config.param("last_record_time", :string, default: nil), "retry_limit" => config.param("retry_limit", :integer, default: 5), "retry_initial_wait_sec" => config.param("retry_initial_wait_sec", :integer, default: 2), } end |
.transaction(config, &control) ⇒ Object
9 10 11 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 |
# File 'lib/embulk/input/google_analytics/plugin.rb', line 9 def self.transaction(config, &control) task = task_from_config(config) unless %w(ga:date ga:dateHour).include?(task["time_series"]) raise ConfigError.new("Unknown time_series '#{task["time_series"]}'. Use 'ga:dateHour' or 'ga:date'") end columns_list = Client.new(task).get_columns_list columns = columns_from_task(task).map do |col_name| col_info = columns_list.find{|col| col[:id] == col_name} raise ConfigError.new("Unknown metric/dimension '#{col_name}'") unless col_info col_type = case col_info[:attributes][:dataType] when "STRING" :string when "INTEGER" :long when "PERCENT", "FLOAT", "CURRENCY" :double when "TIME" :timestamp end # time_series column should be timestamp if col_name == task["time_series"] col_type = :timestamp end Column.new(nil, canonicalize_column_name(col_name), col_type) end resume(task, columns, 1, &control) end |
Instance Method Details
#calculate_next_times(fetched_latest_time) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/embulk/input/google_analytics/plugin.rb', line 116 def calculate_next_times(fetched_latest_time) task_report = {} if fetched_latest_time task_report[:start_date] = fetched_latest_time.strftime("%Y-%m-%d") # if end_date specified as statically YYYY-MM-DD, it will be conflict with start_date (end_date < start_date) # Or when end_date is nil, only start_date will be filled on next run but it is illegal API request. # Modify end_date as "today" to be safe if task["end_date"].nil? || task["end_date"].match(/[0-9]{4}-[0-9]{2}-[0-9]{2}/) task_report[:end_date] = "today" # "today" means now. running at 03:30 AM, will got 3 o'clock data. end # "start_date" format is YYYY-MM-DD, but ga:dateHour will return records by hourly. # If run at 2016-07-03 05:00:00, start_date will set "2016-07-03" and got records until 2016-07-03 05:00:00. # Then next run at 2016-07-04 05:00, will got records between 2016-07-03 00:00:00 and 2016-07-04 05:00:00. # It will evantually duplicated between 2016-07-03 00:00:00 and 2016-07-03 05:00:00 # # Date| 2016-07-03 | 2016-07-04 # Hour| 5 | 5 # 1st run ------|----| | # 2nd run |------------------------|----- # ^^^^^ duplicated # # "last_record_time" option solves that problem # # Date| 2016-07-03 | 2016-07-04 # Hour| 5 | 5 # 1st run ------|----| | # 2nd run #####|-------------------|----- # ^^^^^ ignored (skipped) # task_report[:last_record_time] = fetched_latest_time.strftime("%Y-%m-%d %H:%M:%S %z") else # no records fetched, don't modify config_diff task_report = { start_date: task["start_date"], end_date: task["end_date"], last_record_time: task["last_record_time"], } end task_report end |
#init ⇒ Object
79 80 |
# File 'lib/embulk/input/google_analytics/plugin.rb', line 79 def init end |
#preview? ⇒ Boolean
110 111 112 113 114 |
# File 'lib/embulk/input/google_analytics/plugin.rb', line 110 def preview? org.embulk.spi.Exec.isPreview() rescue java.lang.NullPointerException false end |
#run ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/embulk/input/google_analytics/plugin.rb', line 82 def run client = Client.new(task, preview?) columns = self.class.columns_from_task(task) last_record_time = task["last_record_time"] ? Time.parse(task["last_record_time"]) : nil latest_time_series = nil client.each_report_row do |row| time = row[task["time_series"]] next if last_record_time && time <= last_record_time values = row.values_at(*columns) page_builder.add values latest_time_series = [ latest_time_series, time, ].compact.max end page_builder.finish if task["incremental"] calculate_next_times(latest_time_series) else {} end end |