From 8b5d920b54f89cb29f1b40df465d70c217cd07e6 Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Fri, 11 Feb 2011 11:58:51 +0100 Subject: [PATCH] SONAR-2188 API: add parameters from and to to timemachine chart --- .../app/controllers/charts_controller.rb | 16 +++-- .../app/controllers/dashboard_controller.rb | 8 +-- .../WEB-INF/app/helpers/application_helper.rb | 6 +- .../app/models/api/dashboard_configuration.rb | 10 ++- .../webapp/WEB-INF/app/models/snapshot.rb | 12 ++++ .../webapp/WEB-INF/app/models/trends_chart.rb | 62 +++++++++++++------ 6 files changed, 83 insertions(+), 31 deletions(-) diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/charts_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/charts_controller.rb index 69e07954865..6b4dfbb7cf3 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/charts_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/charts_controller.rb @@ -31,18 +31,26 @@ class ChartsController < ApplicationController metric_keys=params[:metrics] - metrics=[] + metric_ids=[] if metric_keys metric_keys.split(',').each do |key| - metrics< 'image/png', :disposition => 'inline' end end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/dashboard_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/dashboard_controller.rb index 38afc36f524..62c6456e142 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/dashboard_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/dashboard_controller.rb @@ -26,21 +26,21 @@ class DashboardController < ApplicationController def index # TODO display error page if no dashboard or no resource - load_dashboard() load_resource() + load_dashboard() load_authorized_widget_definitions() end def configure # TODO display error page if no dashboard or no resource - load_dashboard() load_resource() + load_dashboard() load_widget_definitions() end def edit_layout - load_dashboard() load_resource() + load_dashboard() end def set_layout @@ -169,7 +169,7 @@ class DashboardController < ApplicationController end end @dashboard=(@active ? @active.dashboard : nil) - @dashboard_configuration=Api::DashboardConfiguration.new(@dashboard, :period_index => params[:period]) + @dashboard_configuration=Api::DashboardConfiguration.new(@dashboard, :period_index => params[:period], :snapshot => @snapshot) end def load_resource diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb index 96e5742d1e9..630cc6598c4 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb @@ -77,9 +77,9 @@ module ApplicationHelper def period_label(snapshot, period_index) return nil if snapshot.nil? || snapshot.project_snapshot.nil? - mode=snapshot.project_snapshot.send "period#{period_index}_mode" - mode_param=snapshot.project_snapshot.send "period#{period_index}_param" - date=snapshot.project_snapshot.send "period#{period_index}_date" + mode=snapshot.period_mode(period_index) + mode_param=snapshot.period_param(period_index) + date=snapshot.period_datetime(period_index) label=nil if mode diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/api/dashboard_configuration.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/api/dashboard_configuration.rb index 5b2d189a173..3ae393f9d9e 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/api/dashboard_configuration.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/api/dashboard_configuration.rb @@ -19,12 +19,13 @@ # class Api::DashboardConfiguration - attr_accessor :dashboard, :period_index, :selected_period + attr_accessor :dashboard, :period_index, :selected_period, :snapshot def initialize(dashboard, options={}) @dashboard=dashboard @period_index=options[:period_index].to_i @selected_period=(@period_index>0) + @snapshot=options[:snapshot] end def name @@ -47,4 +48,11 @@ class Api::DashboardConfiguration @selected_period end + def from_datetime + if selected_period? && @snapshot + @snapshot.period_datetime(@period_index) + else + nil + end + end end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/snapshot.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/snapshot.rb index 065bbbe2fa9..bed213aed75 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/snapshot.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/snapshot.rb @@ -200,6 +200,18 @@ class Snapshot < ActiveRecord::Base result end + def period_mode(period_index) + project_snapshot.send "period#{period_index}_mode" + end + + def period_param(period_index) + project_snapshot.send "period#{period_index}_param" + end + + def period_datetime(period_index) + project_snapshot.send "period#{period_index}_date" + end + private def measures_hash diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/trends_chart.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/trends_chart.rb index eb820ad2f92..ecf25e69052 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/trends_chart.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/trends_chart.rb @@ -19,13 +19,13 @@ # class TrendsChart - def self.png_chart(width, height, resource, metrics, snapshots_id, locale, display_legend) + def self.png_chart(width, height, resource, metrics, locale, display_legend, options={}) java_chart = Java::OrgSonarServerChartsJruby::TrendsChart.new(width, height, locale.gsub(/\-/, '_'), display_legend) init_series(java_chart, metrics) metric_ids=metrics.map{|m| m.id} - add_measures(java_chart, time_machine_measures(resource, metric_ids)) - add_measures(java_chart, time_machine_reviews(resource, metric_ids)) + add_measures(java_chart, time_machine_measures(resource, metric_ids, options)) + add_measures(java_chart, time_machine_reviews(resource, metric_ids, options)) add_labels(java_chart, resource); export_chart_as_png(java_chart) @@ -40,32 +40,56 @@ class TrendsChart end end - def self.time_machine_measures(resource, metric_ids) + def self.time_machine_measures(resource, metric_ids, options={}) unless metric_ids.empty? - sql = Project.send(:sanitize_sql, [ - "select s.created_at as created_at, m.value as value, m.metric_id as metric_id " + + sql= "select s.created_at as created_at, m.value as value, m.metric_id as metric_id " + " from project_measures m LEFT OUTER JOIN snapshots s ON s.id=m.snapshot_id " + " where m.rule_id is null " + - " and s.status='%s' " + - " and s.project_id=%s " + - " and m.metric_id in (%s) " + - " and m.rule_priority is null and m.characteristic_id is null", Snapshot::STATUS_PROCESSED, resource.id, metric_ids * ','] ) - ProjectMeasure.connection.select_all( sql ) + " and s.status=? " + + " and s.project_id=? " + + " and m.metric_id in (?) " + + " and m.rule_priority is null and m.characteristic_id is null" + if (options[:from]) + sql += ' and s.created_at>=?' + end + if (options[:to]) + sql += ' and s.created_at<=?' + end + conditions=[sql, Snapshot::STATUS_PROCESSED, resource.id, metric_ids] + if (options[:from]) + conditions<