Class: LogStash::Inputs::GoogleAnalytics

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/inputs/googleanalytics.rb

Overview

Generate a repeating message.

This plugin is intented only as an example.

Instance Method Summary collapse

Instance Method Details

#registerObject



98
99
100
# File 'lib/logstash/inputs/googleanalytics.rb', line 98

def register
  require 'google/api_client'
end

#run(queue) ⇒ Object

def register



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
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
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
# File 'lib/logstash/inputs/googleanalytics.rb', line 102

def run(queue)
  # we can abort the loop if stop? becomes true
  while !stop?
    start = Time.now
    client, analytics = get_service
    results_index = @start_index
    while !stop?
      results = client.execute(
        :api_method => analytics.data.ga.get,
        :parameters => client_options(results_index))

      if results.data.rows.first
        query = results.data.query.to_hash
        profile_info = results.data.profile_info.to_hash
        column_headers = results.data.column_headers.map { |c|
          c.name
        }

        results.data.rows.each do |r|
          event = LogStash::Event.new()
          decorate(event)
          event['containsSampledData'] = results.data.containsSampledData
          event['query'] = query if @store_query
          event['profileInfo'] = profile_info if @store_profile
          column_headers.zip(r).each do |head,data|
            if is_num(data)
              float_data = Float(data)
              # Sometimes GA returns infinity. if so, the number it invalid
              # so set it to zero.
              if float_data == Float::INFINITY
                event[head.gsub(':','_')] = 0.0
              else
                event[head.gsub(':','_')] = float_data
              end
            else
              event[head.gsub(':','_')] = data
            end
          end
          # Try to add a date unless it was already added
          if @start_date == @end_date
            if !event.include?('ga_date')
              if @start_date == 'today'
                event['ga_date'] = Date.parse(Time.now().strftime("%F"))
              elsif @start_date == 'yesterday'
                event['ga_date'] = Date.parse(Time.at(Time.now.to_i - 86400).strftime("%F"))
              elsif @start_date.include?('daysAgo')
                days_ago = @start_date.sub('daysAgo','').to_i
                event['ga_date'] = Date.parse(Time.at(Time.now.to_i - (days_ago*86400)).strftime("%F"))
              else
                event['ga_date'] = Date.parse(@start_date)
              end
            else
              event['ga_date'] = Date.parse(event['ga_date'].to_i.to_s)
            end
          else
            # Convert YYYYMMdd to YYYY-MM-dd
            event['ga_date'] = Date.parse(event['ga_date'].to_i.to_s)
          end
          event['ga_date'] = event['ga_date'].to_s
          queue << event
        end
      end
      nextLink = results.data.nextLink rescue nil
      if nextLink
        start_index+=@max_results
      else
        break
      end
    end
    if @interval.nil?
      break
    else
      duration = Time.now - start
      # Sleep for the remainder of the interval, or 0 if the duration ran
      # longer than the interval.
      sleeptime = [0, @interval - duration].max
      if sleeptime == 0
        @logger.warn("Execution ran longer than the interval. Skipping sleep.",
                     :duration => duration,
                     :interval => @interval)
      else
        Stud.stoppable_sleep(sleeptime) { stop? }
      end
    end
  end # loop
end

#stopObject

def run



189
190
# File 'lib/logstash/inputs/googleanalytics.rb', line 189

def stop
end