Skip to content

Commit

Permalink
Added stats to the api
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Farmer committed Dec 17, 2010
1 parent 209aaf3 commit 0444177
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 7 deletions.
9 changes: 5 additions & 4 deletions kynetx_am_api.gemspec
@@ -1,12 +1,12 @@
require 'rake'
#require 'rake'

Gem::Specification.new do |s|
s.name = %q{kynetx_am_api}
s.version = "0.1.29"
s.version = "0.1.30"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Michael Farmer, Cid Dennis"]
s.date = %q{2010-10-04}
s.date = %q{2010-12-17}
s.email = %q{oss@kynetx.com}
s.extra_rdoc_files = ["LICENSE"]
s.homepage = %q{http://github.com/kynetx/Kynetx-Application-Manager-API}
Expand All @@ -22,7 +22,8 @@ Gem::Specification.new do |s|
the api server.
EOF

s.files = FileList['lib/**/*.rb', 'rails/**/*', 'spec/**/*', 'init.rb', 'Rakefile',"LICENSE"].to_a
s.files = Dir['lib/**/*.rb', 'rails/**/*', 'spec/**/*', 'init.rb', 'Rakefile',"LICENSE"].to_a
#s.files = FileList['lib/**/*.rb', 'rails/**/*', 'spec/**/*', 'init.rb', 'Rakefile',"LICENSE"].to_a

if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
Expand Down
17 changes: 17 additions & 0 deletions lib/kynetx_am_api/application.rb
Expand Up @@ -135,6 +135,23 @@ def reload
load_base
load_versions
end

#----- Stats Methods

def kpis(range="previous_day")
return @api.get_app_stats_kpis(@application_id, range)
end

def stats(k,d,c=nil,r=nil)
# Accept kpis and dims as a String or Array
kpis = k.class == Array ? k.split(",") : k
dims = d.class == Array ? d.split(",") : d
return @api.get_stats_query(kpis,dims,c,r)
end

def logging(range="previous_day")
return @api.get_stats_logging(@application_id, range)
end

#----- Distrubution Methods

Expand Down
40 changes: 37 additions & 3 deletions lib/kynetx_am_api/direct_api.rb
Expand Up @@ -4,8 +4,7 @@ module KynetxAmApi
require 'json'
require 'net/http/post/multipart'

class DirectApi

class DirectApi
attr_accessor :oauth

#
Expand Down Expand Up @@ -158,6 +157,31 @@ def get_user_info
return user
end

def get_app_stats_kpis(application_id, range)
return get_response("app/#{application_id}/stats/kpis/#{range}", :json)
end

def get_stats_query(k,d,c=nil,r=nil)
q_params = QueryParams.new.merge({:k => k, :d => d})
if c
# c are the conditions and they are supplied like so:
# [ {:field => "fieldname", :value => "value"}, ...]
c.each do |condition|
q_params["where_#{condition[:field]}".to_sym] = condition[:value]
end
end
q_params[:r] = r if r
return get_response("stats/query" + q_params.to_params, :json)
end

def get_stats_interface
return get_response("stats/interface", :json)
end

def get_stats_logging(application_id, range)
return get_response("app/#{application_id}/stats/logging/#{range}", :json)
end

private

def get_response(api_method, format = nil)
Expand Down Expand Up @@ -207,4 +231,14 @@ def post_response(api_method, data, format=nil, additional_headers=nil)


end
end


class QueryParams < Hash
def to_params
return "" if self.empty?
p = "?"
self.each {|k,v| p += "#{CGI::escape k.to_s}=#{CGI::escape v.to_s}&"}
return p.chop
end
end
end
13 changes: 13 additions & 0 deletions lib/kynetx_am_api/user.rb
Expand Up @@ -102,6 +102,7 @@ def duplicate_application(application_id)

def owns_current?
return false unless @current_application
return false unless @current_application.owner
return @current_application.owner["kynetxuserid"].to_i == self.userid.to_i
end

Expand All @@ -118,6 +119,18 @@ def to_h
}
end

def kpis(rulesets=[], range=nil)
conditions = rulesets.empty? ? nil : []
rulesets.each do |ruleset|
conditions.push << {:field => "ruleset", :value => ruleset}
end
return api.get_stats_query("rse,brse,rules,rules_fired,actions,callbacks", 'ruleset,day', conditions, range)
end

def stats_interface
return api.get_stats_interface
end


end
end

0 comments on commit 0444177

Please sign in to comment.