Class: Hodor::Oozie::Query

Inherits:
Job
  • Object
show all
Defined in:
lib/hodor/api/oozie/query.rb

Instance Attribute Summary collapse

Attributes inherited from Job

#columns, #conf, #id, #index, #parent_id, #rest_call, #skip_to

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Job

#child_columns, #children, #children_title, #conf_map, #definition, #display_as_array, #display_as_time, #display_children, #display_properties, #indexed_job_id, #log, #oozie, #parse_time, #sanitize, #set_index

Constructor Details

#initialize(filter = {}) ⇒ Query



19
20
21
22
23
24
# File 'lib/hodor/api/oozie/query.rb', line 19

def initialize(filter = {} )
  @filter = filter
  @jobtype = filter[:jobtype] || "coord"
  @json = run_query(filter)
  @request = session.last_query
end

Instance Attribute Details

#filterObject (readonly)

Returns the value of attribute filter.



4
5
6
# File 'lib/hodor/api/oozie/query.rb', line 4

def filter
  @filter
end

#jobsObject (readonly)

Returns the value of attribute jobs.



4
5
6
# File 'lib/hodor/api/oozie/query.rb', line 4

def jobs
  @jobs
end

#jsonObject (readonly)

Returns the value of attribute json.



4
5
6
# File 'lib/hodor/api/oozie/query.rb', line 4

def json
  @json
end

#requestObject (readonly)

Returns the value of attribute request.



4
5
6
# File 'lib/hodor/api/oozie/query.rb', line 4

def request
  @request
end

Class Method Details

.default_columnsObject



14
15
16
# File 'lib/hodor/api/oozie/query.rb', line 14

def default_columns
  [:index, :name, :status, :id, :start_time, :time_unit, :external_id]
end

.suppress_propertiesObject



11
12
13
# File 'lib/hodor/api/oozie/query.rb', line 11

def suppress_properties
  true
end

Instance Method Details

#expandObject



76
77
78
79
80
81
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
109
110
111
112
# File 'lib/hodor/api/oozie/query.rb', line 76

def expand
  # expand immediate children
  if @json.has_key?("workflows")
    all_jobs = @json["workflows"].map do |item|
      Hodor::Oozie::Workflow.new(item)
    end.compact
    if @running_first
      more_json = run_query( { status: [:killed, :succeeded, :failed, :suspended] } )
      if more_json.has_key?("workflows")
        all_jobs += more_json["workflows"].map do |item|
          Hodor::Oozie::Workflow.new(item)
        end.compact
      end
    end
  elsif @json.has_key?("coordinatorjobs")
    all_jobs = @json["coordinatorjobs"].map do |item|
      Hodor::Oozie::Coordinator.new(item)
    end.compact
    if @running_first
      more_json = run_query( { status: [:succeeded, :killed, :failed, :suspended] } )
      if more_json.has_key?("coordinatorjobs")
        all_jobs += more_json["coordinatorjobs"].map do |item|
          Hodor::Oozie::Coordinator.new(item)
        end.compact
      end
    end
  end
  if @filter[:match]
    pattern = @filter[:match]
    @jobs = all_jobs.select { | job| 
      job.name.include?(pattern)
    }
  else
    @jobs = all_jobs
  end
  @jobs
end

#run_query(filter) ⇒ Object



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
# File 'lib/hodor/api/oozie/query.rb', line 26

def run_query(filter)
  params = []
  if filter.has_key?(:user)
    params << "user%3D#{filter[:user]}"
  end

  if filter.has_key?(:status)
    stats = filter[:status]
    params += stats.map { |val|
      case val
      when :running_first;
        @running_first = true
        "status%3DRUNNING"
      when :running; "status%3DRUNNING"
      when :killed; "status%3DKILLED"
      when :suspended; "status%3DSUSPENDED"
      when :timedout; "status%3DTIMEDOUT"
      when :failed; "status%3DFAILED"
      when :succeeded; "status%3DSUCCEEDED"
      end
    }
  end

  if params.size > 0
    filter_exp = "filter=#{params.join(';')}"
  else
    filter_exp = ""
  end

  pagination = []
  pagination << "offset=#{session.offset}"
  pagination << "len=#{session.len.to_i+1}"
  pagination = pagination.join('&')

  response = session.search_jobs("jobtype=#{@jobtype}", filter_exp, pagination)
  @json = JSON.parse(response)
end

#sessionObject



6
7
8
# File 'lib/hodor/api/oozie/query.rb', line 6

def session
  Hodor::Oozie::Session.instance
end

#titleObject



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hodor/api/oozie/query.rb', line 64

def title
  if @jobtype.start_with?('coord')
    "List of Coordinators"
  elsif @jobtype.start_with?('w')
    "List of Workflows"
  elsif @jobtype.start_with?('b')
    "List of Bundles"
  else
    "List of Matching Jobs"
  end
end