From 18629f267fc6e2430e65924f7dfb4a1b7bf6af1e Mon Sep 17 00:00:00 2001 From: Nathan Broadbent Date: Fri, 1 Feb 2013 22:45:58 +1300 Subject: [PATCH] Added simple stats API to return name, last_error_time and unresolved_errors count for a given app, authenticated by API key. (Building team_dashboard integration) --- app/controllers/api/v1/stats_controller.rb | 41 +++++++++++++++++++++++++++++++++++++++++ config/routes.rb | 7 ++++++- 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 app/controllers/api/v1/stats_controller.rb diff --git a/app/controllers/api/v1/stats_controller.rb b/app/controllers/api/v1/stats_controller.rb new file mode 100644 index 0000000..653ae0a --- /dev/null +++ b/app/controllers/api/v1/stats_controller.rb @@ -0,0 +1,41 @@ +class Api::V1::StatsController < ApplicationController + respond_to :json, :xml + + # The stats API only requires an api_key for the given app. + skip_before_filter :authenticate_user! + before_filter :require_api_key_or_authenticate_user! + + def app + if problem = @app.problems.order_by(:last_notice_at.desc).first + @last_error_time = problem.last_notice_at + end + + stats = { + :name => @app.name, + :last_error_time => @last_error_time, + :unresolved_errors => @app.problems.unresolved.count + } + + respond_to do |format| + format.html { render :json => Yajl.dump(stats) } # render JSON if no extension specified on path + format.json { render :json => Yajl.dump(stats) } + format.xml { render :xml => stats } + end + end + + + protected + + def require_api_key_or_authenticate_user! + if params[:api_key].present? + if @app = App.where(:api_key => params[:api_key]).first + return true + end + end + + authenticate_user! + end + +end + + diff --git a/config/routes.rb b/config/routes.rb index 5ff11a4..f90d96b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -44,7 +44,12 @@ Errbit::Application.routes.draw do namespace :api do namespace :v1 do resources :problems, :only => [:index], :defaults => { :format => 'json' } - resources :notices, :only => [:index], :defaults => { :format => 'json' } + resources :notices, :only => [:index], :defaults => { :format => 'json' } + resources :stats, :only => [], :defaults => { :format => 'json' } do + collection do + get :app + end + end end end -- libgit2 0.21.2