Module: DashingContrib::Pingdom::Uptime

Extended by:
Uptime
Included in:
Uptime
Defined in:
lib/dashing-contrib/bottles/pingdom/uptime.rb

Instance Method Summary collapse

Instance Method Details

#calc(credentials, id, from_time, to_time, rounding = 2) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/dashing-contrib/bottles/pingdom/uptime.rb', line 10

def calc(credentials, id, from_time, to_time, rounding = 2)
  # Find out when the check was created
  checks  = ::DashingContrib::Pingdom::Checks.fetch(credentials, id)
  used_from_time = from_time
  # if we're looking at time time range over the creation time, start from the creation time
  if from_time <= checks[:check][:created] and checks[:check][:created] <= to_time
    # use the creation time + 1 second (for initial check to settle)
    used_from_time = checks[:check][:created] + 1
  end
  payload = make_request(credentials, id, used_from_time, to_time)
  summary = payload[:summary][:status]
  up     = summary[:totalup]
  unkown = summary[:totalunknown]
  down   = summary[:totaldown]

  uptime = (up.to_f - (unkown.to_f + down.to_f)) * 100 / up.to_f
  uptime.round(rounding)
end