diff options
author | Godin <mandrikov@gmail.com> | 2010-12-09 16:00:43 +0000 |
---|---|---|
committer | Godin <mandrikov@gmail.com> | 2010-12-09 16:00:43 +0000 |
commit | 337a1b5251924bfe12d52c8b49ce0a8eae020446 (patch) | |
tree | 7c6e9412a7cc95916a3ad61f58b9c37effa81a77 /sonar-server/src | |
parent | 5a5ba60d55d1e1ad8c6a42019cf7dcd3660e460f (diff) | |
download | sonarqube-337a1b5251924bfe12d52c8b49ce0a8eae020446.tar.gz sonarqube-337a1b5251924bfe12d52c8b49ce0a8eae020446.zip |
SONAR-833: New Web Service to get history of measures (time machine)
Diffstat (limited to 'sonar-server/src')
-rw-r--r-- | sonar-server/src/main/webapp/WEB-INF/app/controllers/api/timemachine_controller.rb | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/timemachine_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/timemachine_controller.rb new file mode 100644 index 00000000000..cf34d238d0d --- /dev/null +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/timemachine_controller.rb @@ -0,0 +1,92 @@ +# +# Sonar, open source software quality management tool. +# Copyright (C) 2009 SonarSource SA +# mailto:contact AT sonarsource DOT com +# +# Sonar is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 3 of the License, or (at your option) any later version. +# +# Sonar is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with Sonar; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 +# +class Api::TimemachineController < Api::ApiController + MAX_IN_ELEMENTS=990 + # + # TODO date range + # GET /api/timemachine + # + def index + begin + resource_id = params[:resource] + metric_keys = params[:metrics].split(',') + metrics = Metric.by_keys(metric_keys) + + @resource=Project.by_key(resource_id) + if @resource.nil? + raise ApiException.new 404, "Resource not found: #{resource_id}" + end + + snapshots = Snapshot.find(:all, + :conditions => {:project_id => @resource.id, :status => Snapshot::STATUS_PROCESSED}, + :order => 'created_at') + + # Oracle limitation : no more than 1000 elements in IN clause + if snapshots.length > MAX_IN_ELEMENTS + size=snapshots.size + snapshots=snapshots[size-MAX_IN_ELEMENTS .. size-1] + end + + measures = find_measures(metrics, snapshots) + + result = [] + if !measures.empty? + measures_by_sid = {} + measures.each do |measure| + measures_by_sid[measure.snapshot_id]||=[] + measures_by_sid[measure.snapshot_id]<<measure + end + + snapshots.each do |snapshot| + snapshot_measures = measures_by_sid[snapshot.id] || [] + values_by_key = {} + snapshot_measures.each do |measure| + values_by_key[measure.metric.name] = measure.value.to_f if measure.value + end + + values = [] + metric_keys.each do |metric| + values<<values_by_key[metric] + end + result<<{format_datetime(snapshot.created_at) => values} + end + end + + # ---------- FORMAT RESPONSE + respond_to do |format| + format.json { render :json => jsonp(result) } + format.xml { render :xml => xml_not_supported } + format.text { render :text => text_not_supported } + end + rescue ApiException => e + render_error(e.msg, e.code) + end + end + + private + + def find_measures(metrics, snapshots) + ProjectMeasure.find(:all, + :select => 'project_measures.id,project_measures.value,project_measures.metric_id,project_measures.snapshot_id', + :conditions => ['rules_category_id IS NULL AND rule_id IS NULL AND rule_priority IS NULL AND metric_id IN (?) AND snapshot_id IN (?)', + metrics.select{|m| m.id}, snapshots.map{|s| s.id}]) + end + +end |