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
- .guess(config) ⇒ Object
- .resume(task, columns, count, &control) ⇒ Object
- .task_from_config(config) ⇒ Object
- .transaction(config, &control) ⇒ Object
Instance Method Summary collapse
- #calculate_next_times(client_time_zone, fetched_latest_time) ⇒ Object
- #init ⇒ Object
- #preview? ⇒ Boolean
- #run ⇒ Object
Class Method Details
.canonicalize_column_name(name) ⇒ Object
80 81 82 83 |
# File 'lib/embulk/input/google_analytics/plugin.rb', line 80 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
72 73 74 75 76 77 78 |
# File 'lib/embulk/input/google_analytics/plugin.rb', line 72 def self.columns_from_task(task) [ task["time_series"], task["dimensions"], task["metrics"], ].flatten.uniq end |
.guess(config) ⇒ Object
85 86 87 88 |
# File 'lib/embulk/input/google_analytics/plugin.rb', line 85 def self.guess(config) Embulk.logger.warn "Don't needed to guess for this plugin" return {} end |
.resume(task, columns, count, &control) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/embulk/input/google_analytics/plugin.rb', line 48 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
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/embulk/input/google_analytics/plugin.rb', line 55 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 41 42 43 44 45 46 |
# 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 = if col_info[:attributes] # standard dimension case col_info[:attributes][:dataType] when "STRING" :string when "INTEGER" :long when "PERCENT", "FLOAT", "CURRENCY", "TIME" :double end else # custom dimension :string 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 columns << Column.new(nil, "view_id", :string) resume(task, columns, 1, &control) end |
Instance Method Details
#calculate_next_times(client_time_zone, fetched_latest_time) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/embulk/input/google_analytics/plugin.rb', line 142 def calculate_next_times(client_time_zone, fetched_latest_time) task_report = {} if fetched_latest_time # Convert fetched_last_time to user timezone timezone = ActiveSupport::TimeZone[client_time_zone] task_report[:start_date] = timezone.nil? ? fetched_latest_time.strftime("%Y-%m-%d") : timezone.parse(fetched_latest_time.to_s).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. else task_report[:end_date] = task["end_date"] 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
90 91 92 93 94 95 |
# File 'lib/embulk/input/google_analytics/plugin.rb', line 90 def init # PLT-6753 if task["start_date"] && !task["end_date"] task["end_date"] = "today" end end |
#preview? ⇒ Boolean
136 137 138 139 140 |
# File 'lib/embulk/input/google_analytics/plugin.rb', line 136 def preview? org.embulk.spi.Exec.isPreview() rescue java.lang.NullPointerException false end |
#run ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/embulk/input/google_analytics/plugin.rb', line 97 def run client = Client.new(task, preview?) columns = self.class.columns_from_task(task) + ["view_id"] last_record_time = task["last_record_time"] ? Time.parse(task["last_record_time"]) : nil latest_time_series = nil skip_counter, total_counter = 0, 0 client.each_report_row do |row| time = row[task["time_series"]] total_counter += 1 if !preview? && last_record_time && time <= last_record_time skip_counter += 1 next end values = row.values_at(*columns) page_builder.add values latest_time_series = [ latest_time_series, time, ].compact.max end page_builder.finish Embulk.logger.info "Total: #{total_counter} rows." if skip_counter > 0 Embulk.logger.info "#{skip_counter} rows were ignored because the rows' date is " + "before \"last_record_time\": #{last_record_time}." end if task["incremental"] calculate_next_times(client.get_profile[:timezone], latest_time_series) else {} end end |