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.



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

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

Instance Attribute Details

#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

#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

#nameObject

Returns the value of attribute name.



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

def name
  @name
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



62
63
64
# File 'app/models/simple_apm/action.rb', line 62

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

.all_namesArray<String>

Returns:

  • (Array<String>)


58
59
60
# File 'app/models/simple_apm/action.rb', line 58

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

.find(action_name) ⇒ Object



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

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

.info_key(action_name) ⇒ Object



66
67
68
# File 'app/models/simple_apm/action.rb', line 66

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

.update_by_request(h) ⇒ Object

Parameters:

  • h (Hash)

    一次request请求的信息



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/simple_apm/action.rb', line 34

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']
  _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_timeObject



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

def avg_time
  time.to_f/click_count.to_i
end

#fastest_requestObject



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

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

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

Returns:



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

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

#slowest_requestObject



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

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