Commit 8da436599bee45d4d48a4bace894eece92761c6a

Authored by Stephen Crosby
2 parents aaf30eff b2964a64
Exists in master and in 1 other branch production

Merge pull request #693 from CallumD/add_notices_to_show_action

Add notices to show action
CONTRIBUTORS.md
... ... @@ -42,11 +42,14 @@
42 42 - [@nashby][]
43 43 - [@shingara][]
44 44 - [@tscolari][]
  45 +- [@callumd][]
  46 +
45 47  
46 48 [@anicet]: https://github.com/anicet
47 49 [@nashby]: https://github.com/nashby
48 50 [@shingara]: https://github.com/shingara
49 51 [@tscolari]: https://github.com/tscolari
  52 +[@callumd]: https://github.com/callumd
50 53  
51 54 ## 0.2.0 - 2013-09-11
52 55  
... ...
app/controllers/api/v1/problems_controller.rb
1 1 class Api::V1::ProblemsController < ApplicationController
2 2 respond_to :json, :xml
  3 + FIELDS = %w{app_id app_name environment message where first_notice_at last_notice_at resolved resolved_at notices_count}
  4 +
  5 + def show
  6 + result = benchmark("[api/v1/problems_controller/show] query time") do
  7 + begin
  8 + Problem.only(FIELDS).find(params[:id])
  9 + rescue Mongoid::Errors::DocumentNotFound
  10 + head :not_found
  11 + return false
  12 + end
  13 + end
  14 +
  15 + respond_to do |format|
  16 + format.any(:html, :json) { render :json => result } # render JSON if no extension specified on path
  17 + format.xml { render :xml => result }
  18 + end
  19 + end
3 20  
4 21 def index
5 22 query = {}
6   - fields = %w{app_id app_name environment message where first_notice_at last_notice_at resolved resolved_at notices_count}
7 23  
8 24 if params.key?(:start_date) && params.key?(:end_date)
9 25 start_date = Time.parse(params[:start_date]).utc
... ... @@ -11,8 +27,8 @@ class Api::V1::ProblemsController &lt; ApplicationController
11 27 query = {:first_notice_at=>{"$lte"=>end_date}, "$or"=>[{:resolved_at=>nil}, {:resolved_at=>{"$gte"=>start_date}}]}
12 28 end
13 29  
14   - results = benchmark("[api/v1/problems_controller] query time") do
15   - Problem.where(query).with(:consistency => :strong).only(fields).to_a
  30 + results = benchmark("[api/v1/problems_controller/index] query time") do
  31 + Problem.where(query).with(:consistency => :strong).only(FIELDS).to_a
16 32 end
17 33  
18 34 respond_to do |format|
... ...
app/models/problem.rb
... ... @@ -50,7 +50,6 @@ class Problem
50 50  
51 51 validates_presence_of :last_notice_at, :first_notice_at
52 52  
53   -
54 53 def self.all_else_unresolved(fetch_all)
55 54 if fetch_all
56 55 all
... ...
config/routes.rb
... ... @@ -50,7 +50,7 @@ Errbit::Application.routes.draw do
50 50  
51 51 namespace :api do
52 52 namespace :v1 do
53   - resources :problems, :only => [:index], :defaults => { :format => 'json' }
  53 + resources :problems, :only => [:index, :show], :defaults => { :format => 'json' }
54 54 resources :notices, :only => [:index], :defaults => { :format => 'json' }
55 55 resources :stats, :only => [], :defaults => { :format => 'json' } do
56 56 collection do
... ...
spec/controllers/api/v1/problems_controller_spec.rb
... ... @@ -7,6 +7,70 @@ describe Api::V1::ProblemsController do
7 7 @user = Fabricate(:user)
8 8 end
9 9  
  10 + describe "GET /api/v1/problems/:id" do
  11 + before do
  12 + notice = Fabricate(:notice)
  13 + err = Fabricate(:err, :notices => [notice])
  14 + @problem = Fabricate(:problem, :errs => [err])
  15 + end
  16 +
  17 + it "should return JSON if JSON is requested" do
  18 + get :show, :auth_token => @user.authentication_token, :format => "json", :id => Problem.first.id
  19 + expect { JSON.load(response.body) }.not_to raise_error() #JSON::ParserError
  20 + end
  21 +
  22 + it "should return XML if XML is requested" do
  23 + get :index, :auth_token => @user.authentication_token, :format => "xml", :id => @problem.id
  24 + expect(Nokogiri::XML(response.body).errors).to be_empty
  25 + end
  26 +
  27 + it "should return JSON by default" do
  28 + get :show, :auth_token => @user.authentication_token, :id => @problem.id
  29 + expect { JSON.load(response.body) }.not_to raise_error()#JSON::ParserError)
  30 + end
  31 +
  32 + it "should return the correct problem" do
  33 + get :show, :auth_token => @user.authentication_token, :format => "json", :id => @problem.id
  34 +
  35 + returned_problem = JSON.parse(response.body)
  36 + expect( returned_problem["_id"] ).to eq(@problem.id.to_s)
  37 + end
  38 +
  39 + it "should return only the correct fields" do
  40 + get :show, :auth_token => @user.authentication_token, :format => "json", :id => @problem.id
  41 + returned_problem = JSON.parse(response.body)
  42 +
  43 + expect( returned_problem.keys ).to match_array([
  44 + "app_name",
  45 + "first_notice_at",
  46 + "error_class",
  47 + "messages",
  48 + "hosts",
  49 + "created_at",
  50 + "app_id",
  51 + "last_notice_at",
  52 + "_id",
  53 + "issue_link",
  54 + "resolved",
  55 + "updated_at",
  56 + "resolved_at",
  57 + "last_deploy_at",
  58 + "where",
  59 + "issue_type",
  60 + "notices_count",
  61 + "user_agents",
  62 + "comments_count",
  63 + "message",
  64 + "environment"
  65 + ])
  66 + end
  67 +
  68 + it "returns a 404 if the problem cannot be found" do
  69 + get :show, :auth_token => @user.authentication_token, :format => "json", :id => 'IdontExist'
  70 + expect( response.status ).to eq(404)
  71 + end
  72 + end
  73 +
10 74 describe "GET /api/v1/problems" do
11 75 before do
12 76 Fabricate(:problem, :first_notice_at => Date.new(2012, 8, 01), :resolved_at => Date.new(2012, 8, 02))
... ...