Class: Admin::MetricsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/admin/metrics_controller.rb

Instance Method Summary collapse

Instance Method Details

#calculateObject



76
77
78
79
80
81
82
83
84
85
# File 'app/controllers/admin/metrics_controller.rb', line 76

def calculate
  threads = []
  flash[:notice] = "Started calculating..."
  threads << Thread.new do 
    CalculateMetrics.calculate_metrics_for_id(params[:id])
  end
  threads.each(&:join)
  flash[:notice] = "Completed calculating"
  redirect_to admin_metrics_url
end

#cloneObject



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/admin/metrics_controller.rb', line 62

def clone
  @metric_to_clone = Metric.find(params[:id])
  @metric = @metric_to_clone.clone
  @metric.title = "Copy of #{@metric.title}"
  @metric.sort = Metric.maximum(:sort, :conditions=>["resource_type_id = ?", @metric_to_clone.resource_type_id ]).to_i + 1
  if @metric.save
    flash[:notice] = "#{@metric.title} has been cloned"
    redirect_to edit_admin_metric_url(@metric)
  else
    flash[:notice] = "There was an error"
    render :action => :new, :status => :unprocessable_entity
  end
end

#createObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/admin/metrics_controller.rb', line 41

def create
  @metric = Metric.new(params[:metric])
  @metric.has_detail = 1
  @metric.data_type = "integer"
  if @metric.save
    flash[:success] = "#{@metric.title} has been created"
    redirect_to admin_metrics_url
  else
    flash[:notice] = "There was an error"
    render :action => :new, :status => :unprocessable_entity
  end
end

#destroyObject



29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/admin/metrics_controller.rb', line 29

def destroy

  if @metric = Metric.find(params[:id])
    @metric.destroy
    flash[:success] = "Metric has been removed."
    redirect_to admin_metrics_url
  else
    flash[:alert] = "Metric has not been removed."
    redirect_to admin_metrics_url
  end
end

#editObject



15
16
17
18
19
20
21
# File 'app/controllers/admin/metrics_controller.rb', line 15

def edit
  @metric_categories = ResourceType.all
  @metric = Metric.find(params[:id])
  @resources = Resource.all
  @resource_type = ResourceType.find_by_id(@metric.resource_type_id)
  1.times { @metric.metric_summaries.build }
end

#evaluateObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/controllers/admin/metrics_controller.rb', line 87

def evaluate
  metric_logic = params["logic"].gsub(":staff", params[:logic_staff])
  error = {}

  res = Thread.new{
    require 'rubygems'
    require 'active_support'

    begin
      @has_error = false
      ActiveRecord::Base.establish_connection "needles_#{RAILS_ENV}"
      @result, @results = eval(metric_logic)
      ActiveRecord::Base.verify_active_connections!
      [@result,@results]
    rescue Exception => e
      logger.info "Error Message: #{e.message}"
      @has_error = true
      @errors = [e.message, e.backtrace.join("\r\n")].join("\r\n")
    end
  }

  @result, @results =  res.value
  @results = @results

  respond_to do |format|
    format.js
  end

end

#indexObject



6
7
8
# File 'app/controllers/admin/metrics_controller.rb', line 6

def index
  @metric_categories = ResourceType.all
end

#newObject



23
24
25
26
27
# File 'app/controllers/admin/metrics_controller.rb', line 23

def new
  @metric = Metric.new
  @metric.resource_type_id = params[:resource_type_id]
  @metric.resource_type = params[:resource_type]
end

#showObject



10
11
12
13
# File 'app/controllers/admin/metrics_controller.rb', line 10

def show
  @metric = Metric.find(params[:id])
  @metricnote = MetricNote.new
end

#sortObject



54
55
56
57
58
59
60
# File 'app/controllers/admin/metrics_controller.rb', line 54

def sort
  params[:metric].each_with_index do |metric_id, i|
    @metric = Metric.find(metric_id)
    @metric.update_attribute(:sort, i)
  end
  render :text => "sorted"
end

#updateObject

PUT /admin/metrics/id




119
120
121
122
123
124
125
126
127
128
129
# File 'app/controllers/admin/metrics_controller.rb', line 119

def update
  @metric = Metric.find_by_id(params[:metric][:id])

  if @metric.update_attributes(params[:metric])
    flash[:notice] = "#{@metric.title} has been updated"
    # CalculateMetrics.calculate_metrics_for_id(@metric.id)
    redirect_to :action => :edit
  else
    render :action => :index, :status => :unprocessable_entity
  end
end