kanbanworkflowstimelinescrumrubyroadmapproject-planningproject-managementopenprojectangularissue-trackerifcgantt-chartganttbug-trackerboardsbcf
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
214 lines
5.3 KiB
214 lines
5.3 KiB
11 years ago
|
#-- copyright
|
||
|
# ReportingEngine
|
||
|
#
|
||
|
# Copyright (C) 2010 - 2014 the OpenProject Foundation (OPF)
|
||
|
#
|
||
|
# This program is free software; you can redistribute it and/or
|
||
|
# modify it under the terms of the GNU General Public License
|
||
|
# version 3.
|
||
|
#
|
||
|
# This program 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 General Public License for more details.
|
||
|
#
|
||
|
# You should have received a copy of the GNU General Public License
|
||
|
# along with this program; if not, write to the Free Software
|
||
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
|
#++
|
||
|
|
||
14 years ago
|
class Report < ActiveRecord::Base
|
||
15 years ago
|
extend Forwardable
|
||
15 years ago
|
include Enumerable
|
||
14 years ago
|
include Engine
|
||
14 years ago
|
|
||
14 years ago
|
belongs_to :user
|
||
14 years ago
|
belongs_to :project
|
||
14 years ago
|
|
||
14 years ago
|
before_save :serialize
|
||
|
serialize :serialized, Hash
|
||
13 years ago
|
|
||
14 years ago
|
self.abstract_class = true # lets have subclasses have their own SQL tables
|
||
15 years ago
|
|
||
|
def self.accepted_properties
|
||
14 years ago
|
@@accepted_properties ||= []
|
||
15 years ago
|
end
|
||
|
|
||
14 years ago
|
def self.reporting_connection
|
||
|
connection
|
||
|
end
|
||
|
|
||
14 years ago
|
def self.chain_initializer
|
||
14 years ago
|
@chain_initializer ||= []
|
||
14 years ago
|
end
|
||
|
|
||
10 years ago
|
def self.deserialize(hash, object = new)
|
||
14 years ago
|
object.tap do |q|
|
||
10 years ago
|
hash[:filters].each { |name, opts| q.filter(name, opts) }
|
||
|
hash[:group_bys].each { |name, opts| q.group_by(name, opts) }
|
||
14 years ago
|
end
|
||
|
end
|
||
|
|
||
|
def serialize
|
||
14 years ago
|
# have to take the reverse group_bys to retain the original order when deserializing
|
||
10 years ago
|
self.serialized = { filters: (filters.map(&:serialize).reject(&:nil?).sort { |a, b| a.first <=> b.first }),
|
||
|
group_bys: group_bys.map(&:serialize).reject(&:nil?).reverse }
|
||
14 years ago
|
end
|
||
|
|
||
14 years ago
|
def deserialize
|
||
14 years ago
|
unless @chain
|
||
|
hash = serialized || serialize
|
||
|
self.class.deserialize(hash, self)
|
||
|
else
|
||
10 years ago
|
raise ArgumentError, 'Cannot deserialize a report which already has a chain'
|
||
14 years ago
|
end
|
||
14 years ago
|
end
|
||
|
|
||
14 years ago
|
# Convenience method to generate a params hash readable by Controller#determine_settings
|
||
|
def to_params
|
||
14 years ago
|
params = {}
|
||
|
filters.select { |f| f.class.display? }.each do |f|
|
||
14 years ago
|
filter_name = f.class.underscore_name
|
||
|
params[:fields] << filter_name
|
||
|
params[:operators].merge! filter_name => f.operator.to_s
|
||
|
params[:values].merge! filter_name => f.values
|
||
|
end
|
||
|
group_bys.each do |g|
|
||
10 years ago
|
params[:groups] ||= { rows: [], columns: [] }
|
||
14 years ago
|
params[:groups][g.row? ? :rows : :columns] << g.class.underscore_name
|
||
|
end
|
||
|
params
|
||
|
end
|
||
|
|
||
14 years ago
|
##
|
||
|
# Migrates this report to look like the given report.
|
||
|
# This may be used to alter report properties without
|
||
|
# creating a new report in a database.
|
||
|
def migrate(report)
|
||
14 years ago
|
[:@chain, :@query, :@transformer, :@walker, :@table, :@depths, :@chain_initializer].each do |inst_var|
|
||
14 years ago
|
instance_variable_set inst_var, (report.instance_variable_get inst_var)
|
||
|
end
|
||
|
end
|
||
|
|
||
15 years ago
|
def available_filters
|
||
14 years ago
|
self.class::Filter.all
|
||
15 years ago
|
end
|
||
|
|
||
15 years ago
|
def transformer
|
||
14 years ago
|
@transformer ||= self.class::Transformer.new self
|
||
15 years ago
|
end
|
||
14 years ago
|
|
||
15 years ago
|
def walker
|
||
14 years ago
|
@walker ||= self.class::Walker.new self
|
||
15 years ago
|
end
|
||
15 years ago
|
|
||
15 years ago
|
def add_chain(type, name, options)
|
||
14 years ago
|
chain type.const_get(name.to_s.camelcase), options
|
||
15 years ago
|
@transformer, @table, @depths, @walker = nil, nil, nil, nil
|
||
15 years ago
|
self
|
||
15 years ago
|
end
|
||
|
|
||
|
def chain(klass = nil, options = {})
|
||
14 years ago
|
build_new_chain unless @chain
|
||
14 years ago
|
if klass
|
||
|
@chain = klass.new @chain, options
|
||
|
@chain.engine = self.class
|
||
|
end
|
||
15 years ago
|
@chain = @chain.parent until @chain.top?
|
||
|
@chain
|
||
|
end
|
||
|
|
||
14 years ago
|
def build_new_chain
|
||
10 years ago
|
# FIXME: is there a better way to load all filter and groups?
|
||
14 years ago
|
self.class::Filter.all && self.class::GroupBy.all
|
||
14 years ago
|
|
||
14 years ago
|
minimal_chain!
|
||
14 years ago
|
self.class.chain_initializer.each { |block| block.call self }
|
||
14 years ago
|
self
|
||
14 years ago
|
end
|
||
|
|
||
15 years ago
|
def filter(name, options = {})
|
||
14 years ago
|
add_chain self.class::Filter, name, options
|
||
15 years ago
|
end
|
||
|
|
||
|
def group_by(name, options = {})
|
||
10 years ago
|
add_chain self.class::GroupBy, name, options.reverse_merge(type: :column)
|
||
15 years ago
|
end
|
||
|
|
||
|
def column(name, options = {})
|
||
10 years ago
|
group_by name, options.merge(type: :column)
|
||
15 years ago
|
end
|
||
|
|
||
|
def row(name, options = {})
|
||
10 years ago
|
group_by name, options.merge(type: :row)
|
||
15 years ago
|
end
|
||
|
|
||
15 years ago
|
def table
|
||
14 years ago
|
@table = self.class::Table.new(self)
|
||
15 years ago
|
end
|
||
15 years ago
|
|
||
10 years ago
|
def group_bys(type = nil)
|
||
14 years ago
|
chain.select { |c| c.group_by? && (type.nil? || c.type == type) }
|
||
15 years ago
|
end
|
||
15 years ago
|
|
||
|
def filters
|
||
10 years ago
|
chain.select(&:filter?)
|
||
15 years ago
|
end
|
||
15 years ago
|
|
||
15 years ago
|
def depth_of(name)
|
||
|
@depths ||= {}
|
||
|
@depths[name] ||= chain.inject(0) { |sum, child| child.type == name ? sum + 1 : sum }
|
||
|
end
|
||
|
|
||
10 years ago
|
def_delegators :transformer, :column_first, :row_first
|
||
|
def_delegators :chain, :empty_chain, :top, :bottom, :chain_collect, :sql_statement, :all_group_fields, :child, :clear, :result
|
||
|
def_delegators :result, :each_direct_result, :recursive_each, :recursive_each_with_level, :each, :each_row, :count,
|
||
|
:units, :final_number
|
||
|
def_delegators :table, :row_index, :colum_index
|
||
15 years ago
|
|
||
|
def to_a
|
||
|
chain.to_a
|
||
|
end
|
||
|
|
||
|
def to_s
|
||
|
chain.to_s
|
||
|
end
|
||
14 years ago
|
|
||
14 years ago
|
def size
|
||
|
size = 0
|
||
10 years ago
|
recursive_each { |r| size += r.size }
|
||
14 years ago
|
size
|
||
|
end
|
||
|
|
||
14 years ago
|
def cache_key
|
||
|
deserialize unless @chain
|
||
|
parts = [self.class.table_name.sub('_reports', '')]
|
||
10 years ago
|
parts.concat [filters.sort, group_bys].map { |l| l.map(&:cache_key).join(' ') }
|
||
14 years ago
|
parts.join '/'
|
||
14 years ago
|
end
|
||
|
|
||
14 years ago
|
def self.engine
|
||
|
self
|
||
|
end
|
||
|
|
||
14 years ago
|
def minimal_chain!
|
||
14 years ago
|
@chain = self.class::Filter::NoFilter.new
|
||
14 years ago
|
end
|
||
14 years ago
|
|
||
|
def public!
|
||
14 years ago
|
self.is_public = true
|
||
14 years ago
|
end
|
||
|
|
||
|
def public?
|
||
10 years ago
|
is_public
|
||
14 years ago
|
end
|
||
|
|
||
|
def private!
|
||
14 years ago
|
self.is_public = false
|
||
14 years ago
|
end
|
||
|
|
||
|
def private?
|
||
14 years ago
|
!public?
|
||
14 years ago
|
end
|
||
15 years ago
|
end
|