]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-833: New Web Service to get history of measures (time machine)
authorGodin <mandrikov@gmail.com>
Tue, 14 Dec 2010 00:53:14 +0000 (00:53 +0000)
committerGodin <mandrikov@gmail.com>
Tue, 14 Dec 2010 00:53:14 +0000 (00:53 +0000)
sonar-server/src/main/webapp/WEB-INF/app/controllers/api/timemachine_controller.rb

index 6ccd0ffa929c12dceca25bf9f5d6a742f7d5170e..62d0788e2117adad3942069b6a43500af426bb7e 100644 (file)
@@ -17,6 +17,8 @@
 # License along with Sonar; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 #
+require 'fastercsv'
+
 class Api::TimemachineController < Api::ApiController
   MAX_IN_ELEMENTS=990
 
@@ -105,6 +107,11 @@ class Api::TimemachineController < Api::ApiController
       respond_to do |format|
         format.json { render :json => jsonp(to_json(objects)) }
         format.xml  { render :xml  => to_xml(objects) }
+        format.csv  {
+          send_data(to_csv(objects),
+            :type => 'text/csv; charset=utf-8; header=present',
+            :disposition => 'attachment; filename=measures.csv')
+        }
         format.text { render :text => text_not_supported }
       end
     rescue ApiException => e
@@ -168,6 +175,34 @@ class Api::TimemachineController < Api::ApiController
     end
   end
 
+  def to_csv(objects)
+    snapshots = objects[:snapshots]
+    measures_by_sid = objects[:measures_by_sid]
+    metric_keys = objects[:metric_keys]
+
+    FasterCSV.generate do |csv|
+      header = ['date']
+      header.concat(metric_keys)
+      csv << header
+      snapshots.each do |snapshot|
+        snapshot_to_csv(csv, snapshot, measures_by_sid[snapshot.id], metric_keys)
+      end
+    end
+  end
+
+  def snapshot_to_csv(csv, snapshot, measures, metric_keys)
+    values_by_key = {}
+    measures.each do |measure|
+      values_by_key[measure.metric.name] = measure.value.to_f if measure.value
+    end
+    values = []
+    values << format_datetime(snapshot.created_at)
+    metric_keys.each do |metric|
+      values << values_by_key[metric]
+    end
+    csv << values
+  end
+
   def add_characteristic_filters(measures_conditions, measures_values)
     @characteristics=[]
     @characteristic_by_id={}