Class: Droid::JSONServer

Inherits:
EM::Connection
  • Object
show all
Includes:
EM::HttpServer
Defined in:
lib/droid/json_server.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.hash_to_metrics(hash) ⇒ Object

utility method to convert a ruby hash to a metrics format that can be consumed by cloudkick



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/droid/json_server.rb', line 84

def self.hash_to_metrics(hash)
  hash.collect do |k,v|
    name = k.to_s
    value = v
    type = if v.kind_of?(Integer)
      'int'
    elsif v.kind_of?(Float)
      'float'
    else
      'string'
    end

    # bool -> int conversion
    if [TrueClass, FalseClass].include?(v.class)
      value = v ? 1 : 0
      type = 'int'
    end

    # if type is really string then it should respond to .to_s
    value = value.to_s if type == 'string'

    { 'name' => name, 'type' => type, 'value' => value }
  end
end

Instance Method Details

#default_responseObject



38
39
40
41
42
43
44
# File 'lib/droid/json_server.rb', line 38

def default_response
  response = ::EM::DelegatedHttpResponse.new(self)
  response.status = 200
  response.content_type 'application/json'
  response.content = {"status" => "OK"}.to_json
  response.send_response
end

#generate_response(method_name) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/droid/json_server.rb', line 28

def generate_response(method_name)
  status, data, content_type = self.send(method_name)

  response = ::EM::DelegatedHttpResponse.new(self)
  response.status = status
  response.content_type content_type
  response.content = data
  response.send_response
end

#get_droidObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/droid/json_server.rb', line 54

def get_droid
  report_data = Droid::Utilization.report_data

  metrics = {}
  report_data.each do |topic, data|
    metrics[topic] = data['msgs']
  end
  metrics['latency'] = Droid::Utilization.latency

  summary = Droid::Utilization.report_summary(report_data)

  metrics['total_msgs'] = summary['msgs']

  status = "AMQP: #{summary['msgs']} msgs processed since #{Droid::Utilization.start.utc}"

  # reset metrics data
  Droid::Utilization.reinit

  data = {
    'status' => status,
    'state' => 'ok',
    'metrics' => hash_to_metrics(metrics)
  }
  [200, data.to_json, "application/json"]
end

#hash_to_metrics(hash) ⇒ Object



80
# File 'lib/droid/json_server.rb', line 80

def hash_to_metrics(hash); self.class.hash_to_metrics(hash); end

#not_found_responseObject



46
47
48
49
50
51
52
# File 'lib/droid/json_server.rb', line 46

def not_found_response
  response = ::EM::DelegatedHttpResponse.new(self)
  response.status = 404
  response.content_type 'text/plain'
  response.content = "Not Found"
  response.send_response
end

#post_initObject



8
9
10
11
# File 'lib/droid/json_server.rb', line 8

def post_init
  super
  no_environment_strings
end

#process_http_requestObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/droid/json_server.rb', line 13

def process_http_request
  return not_found_response if @http_request_method != "GET"

  if @http_request_uri == "/"
    default_response
  else
    method_name = "get_#{@http_request_uri.split("/")[1]}".gsub(/[^\d\w_]/,'').downcase
    if public_methods.map(&:to_sym).include?(method_name.to_sym)
      generate_response(method_name)
    else
      not_found_response
    end
  end
end