Sha256: 91054319eb977f5ec1280f130346afd27d29030e2e27b4c53ab39a9cabed4349

Contents?: true

Size: 1.68 KB

Versions: 9

Compression:

Stored size: 1.68 KB

Contents

require 'rest-client'
require 'multi_json'
require 'cgi'

module DashingContrib
  module Pingdom
    module Uptime
      extend self

      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

      private
      def make_request(credentials, id, from_time, to_time)
        request_url = uptime_request_url(credentials, id, from_time, to_time)
        headers = { 'App-Key' => credentials.api_key }
        headers['Account-Email'] = credentials.team_account unless credentials.team_account.empty?
        response = RestClient.get(request_url, headers)
        MultiJson.load(response.body, { symbolize_keys: true })
      end

      def uptime_request_url(credentials, id, from_time, to_time)
        "https://#{credentials.username}:#{credentials.password}@api.pingdom.com/api/2.0/summary.average/#{id}?from=#{from_time}&to=#{to_time}&includeuptime=true"
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
dashing-contrib-0.2.9 lib/dashing-contrib/bottles/pingdom/uptime.rb
dashing-contrib-0.2.8 lib/dashing-contrib/bottles/pingdom/uptime.rb
dashing-contrib-0.2.7 lib/dashing-contrib/bottles/pingdom/uptime.rb
dashing-contrib-0.2.6 lib/dashing-contrib/bottles/pingdom/uptime.rb
dashing-contrib-0.2.5 lib/dashing-contrib/bottles/pingdom/uptime.rb
dashing-contrib-0.2.4 lib/dashing-contrib/bottles/pingdom/uptime.rb
dashing-contrib-0.2.3 lib/dashing-contrib/bottles/pingdom/uptime.rb
dashing-contrib-0.2.2 lib/dashing-contrib/bottles/pingdom/uptime.rb
dashing-contrib-0.2.1 lib/dashing-contrib/bottles/pingdom/uptime.rb