Class: SimpleApm::Action

Inherits:
Object
  • Object
show all
Defined in:
app/models/simple_apm/action.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h) ⇒ Action

Returns a new instance of Action.



7
8
9
10
11
# File 'app/models/simple_apm/action.rb', line 7

def initialize(h)
  h.each do |k, v|
    send("#{k}=", v)
  end
end

Instance Attribute Details

#cached_queries_countObject

Returns the value of attribute cached_queries_count.



4
5
6
# File 'app/models/simple_apm/action.rb', line 4

def cached_queries_count
  @cached_queries_count
end

#click_countObject

Returns the value of attribute click_count.



4
5
6
# File 'app/models/simple_apm/action.rb', line 4

def click_count
  @click_count
end

#db_timeObject

Returns the value of attribute db_time.



4
5
6
# File 'app/models/simple_apm/action.rb', line 4

def db_time
  @db_time
end

#fast_idObject

Returns the value of attribute fast_id.



4
5
6
# File 'app/models/simple_apm/action.rb', line 4

def fast_id
  @fast_id
end

#fast_timeObject

Returns the value of attribute fast_time.



4
5
6
# File 'app/models/simple_apm/action.rb', line 4

def fast_time
  @fast_time
end

#http_timeObject

Returns the value of attribute http_time.



4
5
6
# File 'app/models/simple_apm/action.rb', line 4

def http_time
  @http_time
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'app/models/simple_apm/action.rb', line 4

def name
  @name
end

#queries_countObject

Returns the value of attribute queries_count.



4
5
6
# File 'app/models/simple_apm/action.rb', line 4

def queries_count
  @queries_count
end

#slow_idObject

Returns the value of attribute slow_id.



4
5
6
# File 'app/models/simple_apm/action.rb', line 4

def slow_id
  @slow_id
end

#slow_timeObject

Returns the value of attribute slow_time.



4
5
6
# File 'app/models/simple_apm/action.rb', line 4

def slow_time
  @slow_time
end

#timeObject

Returns the value of attribute time.



4
5
6
# File 'app/models/simple_apm/action.rb', line 4

def time
  @time
end

Class Method Details

.action_list_keyObject



87
88
89
# File 'app/models/simple_apm/action.rb', line 87

def action_list_key
  SimpleApm::RedisKey['action-names']
end

.all_namesArray<String>

Returns:

  • (Array<String>)


83
84
85
# File 'app/models/simple_apm/action.rb', line 83

def all_names
  SimpleApm::Redis.smembers(action_list_key)
end

.find(action_name) ⇒ Object



46
47
48
# File 'app/models/simple_apm/action.rb', line 46

def find(action_name)
  SimpleApm::Action.new SimpleApm::Redis.hgetall(info_key(action_name)).merge(name: action_name)
end

.info_key(action_name) ⇒ Object



91
92
93
# File 'app/models/simple_apm/action.rb', line 91

def info_key(action_name)
  SimpleApm::RedisKey["action-info:#{action_name}"]
end

.update_by_request(h) ⇒ Object

Parameters:

  • h (Hash)

    一次request请求的信息



51
52
53
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
79
80
# File 'app/models/simple_apm/action.rb', line 51

def update_by_request(h)
  SimpleApm::Redis.sadd(action_list_key, h['action_name'])
  _key = info_key h['action_name']
  _request_store = false
  # 点击次数
  SimpleApm::Redis.hincrby _key, 'click_count', 1
  # 总时间
  SimpleApm::Redis.hincrbyfloat _key, 'time', h['during']
  # 数据库访问时间
  SimpleApm::Redis.hincrbyfloat _key, 'db_time', h['db_runtime']
  # SQL执行次数
  SimpleApm::Redis.hincrby _key, 'queries_count', h['queries_count'].to_i
  # SQL缓存命中次数
  SimpleApm::Redis.hincrby _key, 'cached_queries_count', h['cached_queries_count'].to_i
  # 外部http访问时间
  SimpleApm::Redis.hincrbyfloat _key, 'http_time', h['net_http_during']
  _slow = SimpleApm::Redis.hget _key, 'slow_time'
  if _slow.nil? || h['during'].to_f > _slow.to_f
    # 记录最慢访问
    SimpleApm::Redis.hmset _key, 'slow_time', h['during'], 'slow_id', h['request_id']
    _request_store = true
  end
  _fast = SimpleApm::Redis.hget _key, 'fast_time'
  if _fast.nil? || h['during'].to_f < _fast.to_f
    # 记录最快访问
    SimpleApm::Redis.hmset _key, 'fast_time', h['during'], 'fast_id', h['request_id']
    _request_store = true
  end
  _request_store
end

Instance Method Details

#avg_cached_queries_countObject



37
38
39
# File 'app/models/simple_apm/action.rb', line 37

def avg_cached_queries_count
  cached_queries_count.to_f/click_count.to_i
end

#avg_db_timeObject



29
30
31
# File 'app/models/simple_apm/action.rb', line 29

def avg_db_time
  db_time.to_f/click_count.to_i
end

#avg_http_timeObject



25
26
27
# File 'app/models/simple_apm/action.rb', line 25

def avg_http_time
  http_time.to_f/click_count.to_i
end

#avg_queries_countObject



33
34
35
# File 'app/models/simple_apm/action.rb', line 33

def avg_queries_count
  queries_count.to_f/click_count.to_i
end

#avg_timeObject



41
42
43
# File 'app/models/simple_apm/action.rb', line 41

def avg_time
  time.to_f/click_count.to_i
end

#fastest_requestObject



13
14
15
# File 'app/models/simple_apm/action.rb', line 13

def fastest_request
  @fastest_request ||= SimpleApm::Request.find(fast_id)
end

#slow_requests(limit = 20, offset = 0) ⇒ Array<SimpleApm::SlowRequest>

Returns:



21
22
23
# File 'app/models/simple_apm/action.rb', line 21

def slow_requests(limit = 20, offset = 0)
  @slow_requests ||= SimpleApm::SlowRequest.list_by_action(name, limit, offset)
end

#slowest_requestObject



16
17
18
# File 'app/models/simple_apm/action.rb', line 16

def slowest_request
  @slowest_request ||= SimpleApm::Request.find(slow_id)
end