Module: Coupler::Extensions::Results

Defined in:
lib/coupler/extensions/results.rb

Defined Under Namespace

Modules: Helpers

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/coupler/extensions/results.rb', line 15

def self.registered(app)
  app.helpers Helpers

  app.get '/projects/:project_id/scenarios/:scenario_id/results' do
    @scenario = @project.scenarios_dataset[:id => params[:scenario_id]]
    raise ScenarioNotFound  unless @scenario
    @results = @scenario.results_dataset.order(:id.desc)
    erb 'results/index'.to_sym
  end

  app.get '/projects/:project_id/scenarios/:scenario_id/results/:id' do
    @scenario = @project.scenarios_dataset[:id => params[:scenario_id]]
    raise ScenarioNotFound  unless @scenario

    id, format = params[:id].split('.')
    @result = @scenario.results_dataset[:id => id]
    raise ResultNotFound    unless @result

    if format == 'csv'
      filename = "#{@scenario.slug}-run-#{@result.created_at.strftime('%Y%m%d-%H%M')}.csv"
      content_type('text/csv')
      attachment(filename)
      @result.to_csv
    else
      html = nil
      @result.groups_dataset do |groups_dataset|
        html = erb(:"results/show", {}, {:groups_dataset => groups_dataset})
      end
      html
    end
  end

  app.get '/projects/:project_id/scenarios/:scenario_id/results/:id/details/:group_id' do
    @scenario = @project.scenarios_dataset[:id => params[:scenario_id]]
    raise ScenarioNotFound  unless @scenario
    @result = @scenario.results_dataset[:id => params[:id]]

    @scenario.local_database do |scenario_db|
      groups_ds = scenario_db[@result.groups_table_name]
      @group = groups_ds.filter(:id => params[:group_id]).first
    end
    erb(:"results/details", :layout => false)
  end

  app.post '/projects/:project_id/scenarios/:scenario_id/results/:id/details/:group_id/record' do
    @scenario = @project.scenarios_dataset[:id => params[:scenario_id]]
    raise ScenarioNotFound  unless @scenario
    @result = @scenario.results_dataset[:id => params[:id]]
    @index = params[:index].to_i
    @which = params[:which] ? params[:which].to_i : nil

    # FIXME: this could be in a model method or stored in the resources table
    @num_columns = @scenario.resources.collect { |r| r.selected_fields_dataset.count }.max

    # Get total for group
    # FIXME: Result method instead yo
    @result.groups_dataset do |ds|
      @total = ds.filter(:id => params[:group_id]).get(:"resource_#{@which ? @which + 1 : 1}_count")
    end

    # FIXME: Result method instead yo
    record_id = nil
    @result.groups_records_dataset do |ds|
      record_id = ds.filter(:group_id => params[:group_id], :which => @which).
        limit(1, @index).first[:record_id]
    end
    resource = @which == 1 ? @scenario.resource_2 : @scenario.resource_1
    resource.final_dataset do |ds|
      @columns = ds.columns
      @record = ds.select(*@columns).filter(resource.primary_key_sym => record_id).first
    end
    erb(:"results/record", :layout => false)
  end

  app.get '/projects/:project_id/scenarios/:scenario_id/results/:id.csv' do
    @scenario = @project.scenarios_dataset[:id => params[:scenario_id]]
    raise ScenarioNotFound  unless @scenario
    @result = @scenario.results_dataset[:id => params[:id]]
    raise ResultNotFound    unless @result

    #erb :'results/show', :layout => false
  end
end