Class: LogwormCompute

Inherits:
Object
  • Object
show all
Defined in:
lib/logworm_utils/compute.rb

Instance Method Summary collapse

Constructor Details

#initialize(table, function, field, options) ⇒ LogwormCompute

Returns a new instance of LogwormCompute.



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/logworm_utils/compute.rb', line 2

def initialize(table, function, field, options)
  @table    = table
  @function = function
  @field    = field
  @options  = options

  @valuefield = @field ? "#{@function}(#{@field})" : @function

  begin
    @db    = Logworm::DB.from_config_or_die(@options[:app])
    spec   = {:aggregate_function => @function, :aggregate_argument => @field}.merge(@options)
    @query = Logworm::QueryBuilder.new(spec)
  rescue Exception => e
    $stderr.puts "There was an error: #{e}"
    exit(-1)
  end
  
end

Instance Method Details

#runObject



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/logworm_utils/compute.rb', line 21

def run
  # Create a resource for the query
  begin
    query_data = @db.query(@table, @query.to_json)
    url = query_data["results_uri"]
    rows = @db.results(url + "?nocache=1")["results"]
  rescue Logworm::DatabaseException, Logworm::ForbiddenAccessException  => e
    $stderr.puts "Error: #{e}"
    exit(-1)
  rescue Logworm::InvalidQueryException => e
    $stderr.puts "#{e}, #{@query.to_json}"  
    exit(-1)
  rescue Exception  => e
    $stderr.puts "Error: #{e}"
    exit(-1)
  end

  if @options[:debug]
    puts "logworm query: #{@query.to_json}"
    puts "logworm query url: #{query_data["self_uri"]}"
    puts "logworm results url: #{url}"
    puts
  end
    
  if @query.groups.length > 0
    results = {}
    rows.each do |r|
      grp = []
      @query.groups.each do |g|
        grp << "#{g}:#{r[g]}"
      end
      key = "[#{grp.join(', ')}]"
      value = r[@function]
      results[key] = {:value => value, :keys => r.dup}
      results[key][:keys].delete(@function)
    end
    results.keys.sort.each do |k|
      puts "#{k} \t ==> #{@valuefield} = #{results[k][:value]}"
    end
  else
    puts "#{@valuefield} = #{rows.first[@function]}"
  end
  
end