Commit dc4023c9d2c6d14bd248538740a3f4be4a80044b
1 parent
5fb5c16f
Exists in
master
and in
1 other branch
don't instantiate problems before rendering them for the API
Showing
2 changed files
with
11 additions
and
50 deletions
Show diff stats
app/controllers/api/v1/problems_controller.rb
... | ... | @@ -2,18 +2,21 @@ class Api::V1::ProblemsController < ApplicationController |
2 | 2 | respond_to :json, :xml |
3 | 3 | |
4 | 4 | def index |
5 | + query = {} | |
6 | + fields = %w{app_id app_name environment message where first_notice_at last_notice_at resolved resolved_at notices_count} | |
7 | + | |
5 | 8 | if params.key?(:start_date) && params.key?(:end_date) |
6 | - start_date = Date.parse(params[:start_date]) | |
7 | - end_date = Date.parse(params[:end_date]) | |
8 | - @problems = Problem.in_date_range(start_date..end_date) | |
9 | - else | |
10 | - @problems = Problem.all | |
9 | + start_date = Time.parse(params[:start_date]).utc | |
10 | + end_date = Time.parse(params[:end_date]).utc | |
11 | + query = {:first_notice_at=>{"$lte"=>end_date}, "$or"=>[{:resolved_at=>nil}, {:resolved_at=>{"$gte"=>start_date}}]} | |
11 | 12 | end |
12 | 13 | |
14 | + results = benchmark("[api/v1/problems_controller] query time") { Mongoid.master["problems"].find(query, fields: fields).to_a } | |
15 | + | |
13 | 16 | respond_to do |format| |
14 | - format.html { render json: ProblemPresenter.new(@problems) } # render JSON if no extension specified on path | |
15 | - format.json { render json: ProblemPresenter.new(@problems) } | |
16 | - format.xml { render xml: ProblemPresenter.new(@problems) } | |
17 | + format.html { render json: Yajl.dump(results) } # render JSON if no extension specified on path | |
18 | + format.json { render json: Yajl.dump(results) } | |
19 | + format.xml { render xml: results } | |
17 | 20 | end |
18 | 21 | end |
19 | 22 | ... | ... |
app/presenters/problem_presenter.rb
... | ... | @@ -1,42 +0,0 @@ |
1 | -class ProblemPresenter | |
2 | - | |
3 | - def initialize(model_or_collection) | |
4 | - @model_or_collection = model_or_collection | |
5 | - end | |
6 | - | |
7 | - def to_xml(options={}) | |
8 | - as_json(options).to_xml(options) | |
9 | - end | |
10 | - | |
11 | - def to_json(options={}) | |
12 | - Yajl.dump(as_json(options)) | |
13 | - end | |
14 | - | |
15 | - def as_json(options={}) | |
16 | - if collection? | |
17 | - @model_or_collection.map { |model| model_as_json(model, options) } | |
18 | - else | |
19 | - model_as_json(@model_or_collection, options) | |
20 | - end | |
21 | - end | |
22 | - | |
23 | - def collection? | |
24 | - @model_or_collection.respond_to?(:each) | |
25 | - end | |
26 | - | |
27 | - def model_as_json(problem, options={}) | |
28 | - { | |
29 | - app_id: problem.app_id, | |
30 | - app_name: problem.app_name, | |
31 | - environment: problem.environment, | |
32 | - message: problem.message, | |
33 | - where: problem.where, | |
34 | - first_notice_at: problem.first_notice_at, | |
35 | - last_notice_at: problem.last_notice_at, | |
36 | - resolved: problem.resolved, | |
37 | - resolved_at: problem.resolved_at, | |
38 | - notices_count: problem.notices_count | |
39 | - } | |
40 | - end | |
41 | - | |
42 | -end |