Commit 18629f267fc6e2430e65924f7dfb4a1b7bf6af1e
1 parent
17709394
Exists in
master
and in
1 other branch
Added simple stats API to return name, last_error_time and unresolved_errors cou…
…nt for a given app, authenticated by API key. (Building team_dashboard integration)
Showing
2 changed files
with
47 additions
and
1 deletions
Show diff stats
@@ -0,0 +1,41 @@ | @@ -0,0 +1,41 @@ | ||
1 | +class Api::V1::StatsController < ApplicationController | ||
2 | + respond_to :json, :xml | ||
3 | + | ||
4 | + # The stats API only requires an api_key for the given app. | ||
5 | + skip_before_filter :authenticate_user! | ||
6 | + before_filter :require_api_key_or_authenticate_user! | ||
7 | + | ||
8 | + def app | ||
9 | + if problem = @app.problems.order_by(:last_notice_at.desc).first | ||
10 | + @last_error_time = problem.last_notice_at | ||
11 | + end | ||
12 | + | ||
13 | + stats = { | ||
14 | + :name => @app.name, | ||
15 | + :last_error_time => @last_error_time, | ||
16 | + :unresolved_errors => @app.problems.unresolved.count | ||
17 | + } | ||
18 | + | ||
19 | + respond_to do |format| | ||
20 | + format.html { render :json => Yajl.dump(stats) } # render JSON if no extension specified on path | ||
21 | + format.json { render :json => Yajl.dump(stats) } | ||
22 | + format.xml { render :xml => stats } | ||
23 | + end | ||
24 | + end | ||
25 | + | ||
26 | + | ||
27 | + protected | ||
28 | + | ||
29 | + def require_api_key_or_authenticate_user! | ||
30 | + if params[:api_key].present? | ||
31 | + if @app = App.where(:api_key => params[:api_key]).first | ||
32 | + return true | ||
33 | + end | ||
34 | + end | ||
35 | + | ||
36 | + authenticate_user! | ||
37 | + end | ||
38 | + | ||
39 | +end | ||
40 | + | ||
41 | + |
config/routes.rb
@@ -44,7 +44,12 @@ Errbit::Application.routes.draw do | @@ -44,7 +44,12 @@ Errbit::Application.routes.draw do | ||
44 | namespace :api do | 44 | namespace :api do |
45 | namespace :v1 do | 45 | namespace :v1 do |
46 | resources :problems, :only => [:index], :defaults => { :format => 'json' } | 46 | resources :problems, :only => [:index], :defaults => { :format => 'json' } |
47 | - resources :notices, :only => [:index], :defaults => { :format => 'json' } | 47 | + resources :notices, :only => [:index], :defaults => { :format => 'json' } |
48 | + resources :stats, :only => [], :defaults => { :format => 'json' } do | ||
49 | + collection do | ||
50 | + get :app | ||
51 | + end | ||
52 | + end | ||
48 | end | 53 | end |
49 | end | 54 | end |
50 | 55 |