##// END OF EJS Templates
Replace raw SQL with a .count call....
Jean-Philippe Lang -
r7973:6a1830e9e382
parent child
Show More
@@ -1,177 +1,161
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 module Redmine
18 module Redmine
19 module Helpers
19 module Helpers
20 class TimeReport
20 class TimeReport
21 attr_reader :criteria, :columns, :from, :to, :hours, :total_hours, :periods
21 attr_reader :criteria, :columns, :from, :to, :hours, :total_hours, :periods
22
22
23 def initialize(project, issue, criteria, columns, from, to)
23 def initialize(project, issue, criteria, columns, from, to)
24 @project = project
24 @project = project
25 @issue = issue
25 @issue = issue
26
26
27 @criteria = criteria || []
27 @criteria = criteria || []
28 @criteria = @criteria.select{|criteria| available_criteria.has_key? criteria}
28 @criteria = @criteria.select{|criteria| available_criteria.has_key? criteria}
29 @criteria.uniq!
29 @criteria.uniq!
30 @criteria = @criteria[0,3]
30 @criteria = @criteria[0,3]
31
31
32 @columns = (columns && %w(year month week day).include?(columns)) ? columns : 'month'
32 @columns = (columns && %w(year month week day).include?(columns)) ? columns : 'month'
33 @from = from
33 @from = from
34 @to = to
34 @to = to
35
35
36 run
36 run
37 end
37 end
38
38
39 def available_criteria
39 def available_criteria
40 @available_criteria || load_available_criteria
40 @available_criteria || load_available_criteria
41 end
41 end
42
42
43 private
43 private
44
44
45 def run
45 def run
46 unless @criteria.empty?
46 unless @criteria.empty?
47 sql_select = @criteria.collect{|criteria| @available_criteria[criteria][:sql] + " AS " + criteria}.join(', ')
47 scope = TimeEntry.visible.spent_between(@from, @to)
48 sql_group_by = @criteria.collect{|criteria| @available_criteria[criteria][:sql]}.join(', ')
49 sql_condition = ''
50
51 if @issue
48 if @issue
52 sql_condition = "#{Issue.table_name}.root_id = #{@issue.root_id} AND #{Issue.table_name}.lft >= #{@issue.lft} AND #{Issue.table_name}.rgt <= #{@issue.rgt}"
49 scope = scope.on_issue(@issue)
53 else
50 elsif @project
54 sql_condition = Project.allowed_to_condition(User.current, :view_time_entries, :project => @project, :with_subprojects => Setting.display_subprojects_issues?)
51 scope = scope.on_project(@project, Setting.display_subprojects_issues?)
55 end
52 end
56
53 time_columns = %w(tyear tmonth tweek spent_on)
57 sql = "SELECT #{sql_select}, tyear, tmonth, tweek, spent_on, SUM(hours) AS hours"
54 @hours = []
58 sql << " FROM #{TimeEntry.table_name}"
55 scope.sum(:hours, :include => :issue, :group => @criteria.collect{|criteria| @available_criteria[criteria][:sql]} + time_columns).each do |hash, hours|
59 sql << time_report_joins
56 h = {'hours' => hours}
60 sql << " WHERE (%s)" % sql_condition
57 (@criteria + time_columns).each_with_index do |name, i|
61 if @from && @to
58 h[name] = hash[i]
62 sql << " AND (spent_on BETWEEN '%s' AND '%s')" % [ActiveRecord::Base.connection.quoted_date(@from), ActiveRecord::Base.connection.quoted_date(@to)]
59 end
63 elsif @from
60 @hours << h
64 sql << " AND (spent_on BETWEEN >= '%s')" % ActiveRecord::Base.connection.quoted_date(@from)
65 elsif @to
66 sql << " AND (spent_on BETWEEN <= '%s')" % ActiveRecord::Base.connection.quoted_date(@to)
67 end
61 end
68 sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek, spent_on"
62
69
70 @hours = ActiveRecord::Base.connection.select_all(sql)
71
72 @hours.each do |row|
63 @hours.each do |row|
73 case @columns
64 case @columns
74 when 'year'
65 when 'year'
75 row['year'] = row['tyear']
66 row['year'] = row['tyear']
76 when 'month'
67 when 'month'
77 row['month'] = "#{row['tyear']}-#{row['tmonth']}"
68 row['month'] = "#{row['tyear']}-#{row['tmonth']}"
78 when 'week'
69 when 'week'
79 row['week'] = "#{row['tyear']}-#{row['tweek']}"
70 row['week'] = "#{row['tyear']}-#{row['tweek']}"
80 when 'day'
71 when 'day'
81 row['day'] = "#{row['spent_on']}"
72 row['day'] = "#{row['spent_on']}"
82 end
73 end
83 end
74 end
84
75
85 if @from.nil?
76 if @from.nil?
86 min = @hours.collect {|row| row['spent_on']}.min
77 min = @hours.collect {|row| row['spent_on']}.min
87 @from = min ? min.to_date : Date.today
78 @from = min ? min.to_date : Date.today
88 end
79 end
89
80
90 if @to.nil?
81 if @to.nil?
91 max = @hours.collect {|row| row['spent_on']}.max
82 max = @hours.collect {|row| row['spent_on']}.max
92 @to = max ? max.to_date : Date.today
83 @to = max ? max.to_date : Date.today
93 end
84 end
94
85
95 @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
86 @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
96
87
97 @periods = []
88 @periods = []
98 # Date#at_beginning_of_ not supported in Rails 1.2.x
89 # Date#at_beginning_of_ not supported in Rails 1.2.x
99 date_from = @from.to_time
90 date_from = @from.to_time
100 # 100 columns max
91 # 100 columns max
101 while date_from <= @to.to_time && @periods.length < 100
92 while date_from <= @to.to_time && @periods.length < 100
102 case @columns
93 case @columns
103 when 'year'
94 when 'year'
104 @periods << "#{date_from.year}"
95 @periods << "#{date_from.year}"
105 date_from = (date_from + 1.year).at_beginning_of_year
96 date_from = (date_from + 1.year).at_beginning_of_year
106 when 'month'
97 when 'month'
107 @periods << "#{date_from.year}-#{date_from.month}"
98 @periods << "#{date_from.year}-#{date_from.month}"
108 date_from = (date_from + 1.month).at_beginning_of_month
99 date_from = (date_from + 1.month).at_beginning_of_month
109 when 'week'
100 when 'week'
110 @periods << "#{date_from.year}-#{date_from.to_date.cweek}"
101 @periods << "#{date_from.year}-#{date_from.to_date.cweek}"
111 date_from = (date_from + 7.day).at_beginning_of_week
102 date_from = (date_from + 7.day).at_beginning_of_week
112 when 'day'
103 when 'day'
113 @periods << "#{date_from.to_date}"
104 @periods << "#{date_from.to_date}"
114 date_from = date_from + 1.day
105 date_from = date_from + 1.day
115 end
106 end
116 end
107 end
117 end
108 end
118 end
109 end
119
110
120 def load_available_criteria
111 def load_available_criteria
121 @available_criteria = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
112 @available_criteria = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
122 :klass => Project,
113 :klass => Project,
123 :label => :label_project},
114 :label => :label_project},
124 'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
115 'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
125 :klass => Version,
116 :klass => Version,
126 :label => :label_version},
117 :label => :label_version},
127 'category' => {:sql => "#{Issue.table_name}.category_id",
118 'category' => {:sql => "#{Issue.table_name}.category_id",
128 :klass => IssueCategory,
119 :klass => IssueCategory,
129 :label => :field_category},
120 :label => :field_category},
130 'member' => {:sql => "#{TimeEntry.table_name}.user_id",
121 'member' => {:sql => "#{TimeEntry.table_name}.user_id",
131 :klass => User,
122 :klass => User,
132 :label => :label_member},
123 :label => :label_member},
133 'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
124 'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
134 :klass => Tracker,
125 :klass => Tracker,
135 :label => :label_tracker},
126 :label => :label_tracker},
136 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
127 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
137 :klass => TimeEntryActivity,
128 :klass => TimeEntryActivity,
138 :label => :label_activity},
129 :label => :label_activity},
139 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id",
130 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id",
140 :klass => Issue,
131 :klass => Issue,
141 :label => :label_issue}
132 :label => :label_issue}
142 }
133 }
143
134
144 # Add list and boolean custom fields as available criteria
135 # Add list and boolean custom fields as available criteria
145 custom_fields = (@project.nil? ? IssueCustomField.for_all : @project.all_issue_custom_fields)
136 custom_fields = (@project.nil? ? IssueCustomField.for_all : @project.all_issue_custom_fields)
146 custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
137 custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
147 @available_criteria["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Issue' AND c.customized_id = #{Issue.table_name}.id)",
138 @available_criteria["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Issue' AND c.customized_id = #{Issue.table_name}.id)",
148 :format => cf.field_format,
139 :format => cf.field_format,
149 :label => cf.name}
140 :label => cf.name}
150 end if @project
141 end if @project
151
142
152 # Add list and boolean time entry custom fields
143 # Add list and boolean time entry custom fields
153 TimeEntryCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
144 TimeEntryCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
154 @available_criteria["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'TimeEntry' AND c.customized_id = #{TimeEntry.table_name}.id)",
145 @available_criteria["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'TimeEntry' AND c.customized_id = #{TimeEntry.table_name}.id)",
155 :format => cf.field_format,
146 :format => cf.field_format,
156 :label => cf.name}
147 :label => cf.name}
157 end
148 end
158
149
159 # Add list and boolean time entry activity custom fields
150 # Add list and boolean time entry activity custom fields
160 TimeEntryActivityCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
151 TimeEntryActivityCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
161 @available_criteria["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Enumeration' AND c.customized_id = #{TimeEntry.table_name}.activity_id)",
152 @available_criteria["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Enumeration' AND c.customized_id = #{TimeEntry.table_name}.activity_id)",
162 :format => cf.field_format,
153 :format => cf.field_format,
163 :label => cf.name}
154 :label => cf.name}
164 end
155 end
165
156
166 @available_criteria
157 @available_criteria
167 end
158 end
168
169 def time_report_joins
170 sql = ''
171 sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
172 sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id"
173 sql
174 end
175 end
159 end
176 end
160 end
177 end
161 end
General Comments 0
You need to be logged in to leave comments. Login now