Permalink
Browse files

Added stats to the api

  • Loading branch information...
1 parent 209aaf3 commit 0444177a7f10a38a85a26507f270c2c9babfe994 @mikefarmer committed Dec 17, 2010
Showing with 72 additions and 7 deletions.
  1. +5 −4 kynetx_am_api.gemspec
  2. +17 −0 lib/kynetx_am_api/application.rb
  3. +37 −3 lib/kynetx_am_api/direct_api.rb
  4. +13 −0 lib/kynetx_am_api/user.rb
View
@@ -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}
@@ -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
@@ -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
@@ -4,8 +4,7 @@ module KynetxAmApi
require 'json'
require 'net/http/post/multipart'
- class DirectApi
-
+ class DirectApi
attr_accessor :oauth
#
@@ -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)
@@ -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
View
@@ -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
@@ -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.