Class: LWAC::Exporter

Inherits:
Object
  • Object
show all
Defined in:
lib/lwac/export.rb

Constant Summary collapse

AVAILABLE_FORMATTERS =

Points at the various formatter objects available

{
  :csv            => CSVFormatter,
  :multicsv       => MultiCSVFormatter,
  :json           => JSONFormatter,
  :multitemplate  => MultiTemplateFormatter,
  :multixml       => MultiXMLFormatter
}

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Exporter

Returns a new instance of Exporter.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/lwac/export.rb', line 108

def initialize(config)
  @config = config

  # Create a new formatter
  @formatter = AVAILABLE_FORMATTERS[@config[:output][:formatter]].new( @config[:output][:formatter_opts] )
  
  prepare_filters
  
  load_server_config

  load_storage_resources

  validate_samples


  summarise
end

Instance Method Details

#exportObject

Export according to config



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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/lwac/export.rb', line 127

def export
  # -----------------------------------------------------------------------------
  # At this point we have a list of samples that are valid
  # We should now probably do something with them :-)
  # They all go in the structure below
  data                  = Resource.new(Data, {:server => nil, :sample => nil, :datapoint => nil})

  # Fire up some accounting variables
  count     = 0
  progress  = [count, Time.now]


  # Open the output system 
  $log.debug "Opening formatter for writing..."
  @formatter.open_output

  # Write headers
  if @config[:output][:headers]
    $log.debug "Writing headers (line #{count+=1}/#{@estimated_lines})."
    @formatter.write_header
    progress = announce(count, progress, @estimated_lines, @config[:output][:announce])
  end 

  
  # -----------------------------------------------------------------------------
  # Construct the server (static) resource
  $log.debug "Constructing server resource..."
  server = {:links                  => @storage.read_link_ids.to_a,
            :complete_sample_count  => @available_samples.length,
            :complete_samples       => @available_samples.map{|as| as.id},
            :next_sample_date       => @storage.state.next_sample_due,
            :current_sample_id      => @storage.state.current_sample.id,
            :config                 => @server_config,
            :version                => @storage.state.version
           }
  data.server = Resource.new("server", server)
  #puts server.describe


  
  # If we wish to output at the server level, do so.
  if(@config[:output][:level] == :server) then
      # output at server level
      $log.debug "Writing output at server level (line #{count+=1}/#{@estimated_lines})."
      @formatter << data
      progress = announce(count, progress, @estimated_lines, @config[:output][:announce])
      #.values
  else
    # ...continue to sample at a lower level
    # -----------------------------------------------------------------------------
    # One level deep, loop through samples and construct their resource
    $log.debug "Constructing sample resources..."
    @available_samples.each{|as|
      sample = {:id                   => as.id,
                :start_time           => as.sample_start_time,
                :end_time             => as.sample_end_time,
                :complete             => as.complete?,
                :open                 => as.open?,
                :size                 => as.size,
                :duration             => (as.sample_end_time && as.sample_start_time) ? as.sample_end_time - as.sample_start_time : 0,
                :start_time_s         => as.sample_start_time.to_i,
                :end_time_s           => as.sample_end_time.to_i,
                # :num_pending_links    => as.pending.length,
                # Either form takes way too long to compute on large servers
                # :pending_links        => data.server.links - (data.server.links.clone.delete_if{|x| x > as.last_dp_id} - as.pending.to_a),
                # :pending_links        => data.server.links.clone.to_a.delete_if{|id| (not as.pending.to_a.include?(id)) or (id > as.last_dp_id) },
                :size_on_disk         => as.approx_filesize,
                :last_contiguous_id   => as.last_dp_id,
                :dir                  => @storage.get_sample_filepath(as.id),
                :path                 => File.join(@storage.get_sample_filepath(as.id), @server_config[:storage][:sample_filename]) 
               }
      data.sample = Resource.new("sample", sample)
      # puts data.describe




      # If this sample is filtered out, ignore it regardless of sampling level
      if(OutputFilter::filter(data, @config[:output][:filters][:sample])) then
        # If we wish to sample at the sample level, do so
        if(@config[:output][:level] == :sample) then
            # output at server level
            $log.debug "Writing output at sample level (line #{count+=1}/#{@estimated_lines})."
            @formatter << data
        else
          # ...continue and build more info
          # -----------------------------------------------------------------------------
          # Two levels deep, loop through datapoints and construct their resources.
          $log.debug "Constructing datapoint resources..."
          data.server.links.each{|link_id|
            # Load from disk
            dp = @storage.read_datapoint( link_id, as )

            datapoint = {:id            => dp.link.id     || "",
                         :uri           => dp.link.uri    || "",
                         :dir           => File.dirname(@storage.get_dp_filepath(link_id, data.sample.id)), 
                         :path          => @storage.get_dp_filepath(link_id, data.sample.id),
                         :client_id     => dp.client_id   || "",
                         :error         => dp.error       || "",
                         :headers       => dp.headers     || {},
                         :head          => dp.head        || "",
                         :body          => dp.body        || "",
                         :response      => dp.response_properties || {}
                        }
          
            data.datapoint = Resource.new("datapoint", datapoint)
            # puts data.describe


            # Filter out individual datapoints if necessary
            if(OutputFilter::filter(data, @config[:output][:filters][:datapoint])) then
              # At this point we are at the finest-grained output possible, so
              # just output!
              $log.debug "Writing output at datapoint level (line #{count+=1}/#{@estimated_lines})."
              @formatter << data
              progress = announce(count, progress, @estimated_lines, @config[:output][:announce] ) 
            else
              @estimated_lines -= 1
              $log.debug "Discarded datapoint #{data.datapoint.id} due to filter (revised estimate: #{@estimated_lines} lines)."
            end
          } # end per-datapoint loop
        end # end sample if


      else # else filter out this sample
        @estimated_lines -= data.sample.size
        $log.debug  "Discarded sample #{data.sample.id} due to filter (revised estimate: #{@estimated_lines} lines)."
      end # end filter IF


    } # end per-sample loop 
    end # end server if

  @formatter.close_output
  $log.info "Done."
end