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.
309 lines
7.4 KiB
309 lines
7.4 KiB
11 years ago
|
#-- copyright
|
||
5 years ago
|
# OpenProject is an open source project management software.
|
||
|
# Copyright (C) 2012-2020 the OpenProject GmbH
|
||
11 years ago
|
#
|
||
5 years ago
|
# This program is free software; you can redistribute it and/or
|
||
|
# modify it under the terms of the GNU General Public License version 3.
|
||
|
#
|
||
|
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
|
||
|
# Copyright (C) 2006-2017 Jean-Philippe Lang
|
||
|
# Copyright (C) 2010-2013 the ChiliProject Team
|
||
11 years ago
|
#
|
||
|
# This program is free software; you can redistribute it and/or
|
||
|
# modify it under the terms of the GNU General Public License
|
||
5 years ago
|
# as published by the Free Software Foundation; either version 2
|
||
|
# of the License, or (at your option) any later version.
|
||
11 years ago
|
#
|
||
|
# 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.
|
||
5 years ago
|
#
|
||
|
# See docs/COPYRIGHT.rdoc for more details.
|
||
11 years ago
|
#++
|
||
|
|
||
14 years ago
|
class Report::Result
|
||
14 years ago
|
include Report::QueryUtils
|
||
|
|
||
15 years ago
|
class Base
|
||
15 years ago
|
attr_accessor :parent, :type, :important_fields
|
||
15 years ago
|
attr_accessor :key
|
||
15 years ago
|
attr_reader :value
|
||
|
alias values value
|
||
15 years ago
|
include Enumerable
|
||
14 years ago
|
include Report::QueryUtils
|
||
15 years ago
|
|
||
|
def initialize(value)
|
||
14 years ago
|
@important_fields ||= []
|
||
15 years ago
|
@type = :direct
|
||
15 years ago
|
@value = value
|
||
|
end
|
||
|
|
||
10 years ago
|
def recursive_each_with_level(level = 0, _depth_first = true, &block)
|
||
15 years ago
|
block.call(level, self)
|
||
|
end
|
||
|
|
||
|
def recursive_each
|
||
10 years ago
|
recursive_each_with_level { |_level, result| yield result }
|
||
15 years ago
|
end
|
||
|
|
||
15 years ago
|
def to_hash
|
||
|
fields.dup
|
||
|
end
|
||
|
|
||
15 years ago
|
def [](key)
|
||
|
fields[key]
|
||
|
end
|
||
15 years ago
|
|
||
14 years ago
|
##
|
||
|
# Override if you want to influence the result grouping.
|
||
|
#
|
||
|
# @return A value for grouping or nil if the given field should
|
||
|
# not be considered for grouping.
|
||
10 years ago
|
def map_group_by_value(_key, value)
|
||
14 years ago
|
value
|
||
|
end
|
||
|
|
||
|
##
|
||
|
# This method is called when this result is requested as #grouped_by something
|
||
|
# just before the result is returned.
|
||
|
#
|
||
|
# @param data This result's grouped data.
|
||
10 years ago
|
def group_by_data_ready(_data)
|
||
14 years ago
|
# good to know!
|
||
|
end
|
||
|
|
||
15 years ago
|
def grouped_by(fields, type, important_fields = [])
|
||
15 years ago
|
@grouped_by ||= {}
|
||
|
list = begin
|
||
|
@grouped_by[fields] ||= begin
|
||
|
# sub results, have fields
|
||
|
# i.e. grouping by foo, bar
|
||
|
data = group_by do |entry|
|
||
|
# index for group is a hash
|
||
|
# i.e. { :foo => 10, :bar => 20 } <= this is just the KEY!!!!
|
||
14 years ago
|
fields.inject({}) do |hash, key|
|
||
|
val = map_group_by_value(key, entry.fields[key])
|
||
|
hash.merge key => val
|
||
|
end
|
||
14 years ago
|
end
|
||
14 years ago
|
group_by_data_ready(data)
|
||
15 years ago
|
# map group back to array, all fields with same key get grouped into one list
|
||
14 years ago
|
data.keys.map { |f| engine::Result.new data[f], f, type, important_fields }
|
||
15 years ago
|
end
|
||
15 years ago
|
end
|
||
|
# create a single result from that list
|
||
14 years ago
|
engine::Result.new list, {}, type, important_fields
|
||
15 years ago
|
end
|
||
|
|
||
|
def inspect
|
||
|
"<##{self.class}: @fields=#{fields.inspect} @type=#{type.inspect} " \
|
||
14 years ago
|
"@size=#{size} @count=#{count} @units=#{units}>"
|
||
15 years ago
|
end
|
||
|
|
||
|
def row?
|
||
|
type == :row
|
||
|
end
|
||
|
|
||
|
def column?
|
||
|
type == :column
|
||
|
end
|
||
|
|
||
|
def direct?
|
||
|
type == :direct
|
||
15 years ago
|
end
|
||
15 years ago
|
|
||
|
def each_row
|
||
|
end
|
||
|
|
||
15 years ago
|
def final?(type)
|
||
14 years ago
|
type? type and (direct? or size == 0 or first.type != type)
|
||
15 years ago
|
end
|
||
|
|
||
|
def type?(type)
|
||
|
self.type == type
|
||
|
end
|
||
|
|
||
|
def depth_of(type)
|
||
|
if type? type or (type == :column and direct?) then 1
|
||
|
else 0
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def final_number(type)
|
||
|
return 1 if final? type
|
||
|
return 0 if direct?
|
||
|
@final_number ||= {}
|
||
|
@final_number[type] ||= sum { |v| v.final_number type }
|
||
|
end
|
||
|
|
||
15 years ago
|
def final_row?
|
||
15 years ago
|
final? :row
|
||
|
end
|
||
|
|
||
|
def final_column?
|
||
|
final? :column
|
||
|
end
|
||
|
|
||
|
def render(keys = important_fields)
|
||
10 years ago
|
fields.map { |k, v| yield(k, v) if keys.include? k }.join
|
||
15 years ago
|
end
|
||
15 years ago
|
|
||
15 years ago
|
def set_key(index = [])
|
||
15 years ago
|
self.key = index.map { |k| map_field(k, fields[k]) }
|
||
|
end
|
||
15 years ago
|
end
|
||
|
|
||
|
class DirectResult < Base
|
||
|
alias fields values
|
||
|
|
||
|
def has_children?
|
||
|
false
|
||
|
end
|
||
|
|
||
|
def count
|
||
10 years ago
|
self['count'].to_i
|
||
15 years ago
|
end
|
||
|
|
||
|
def units
|
||
10 years ago
|
self['units'].to_d
|
||
15 years ago
|
end
|
||
|
|
||
|
##
|
||
|
# @return [Integer] Number of child results
|
||
|
def size
|
||
|
0
|
||
|
end
|
||
15 years ago
|
|
||
15 years ago
|
def each
|
||
15 years ago
|
return enum_for(__method__) unless block_given?
|
||
15 years ago
|
yield self
|
||
|
end
|
||
|
|
||
10 years ago
|
def each_direct_result(_cached = false)
|
||
15 years ago
|
return enum_for(__method__) unless block_given?
|
||
|
yield self
|
||
|
end
|
||
15 years ago
|
|
||
|
def sort!(force = false)
|
||
|
force
|
||
|
end
|
||
15 years ago
|
end
|
||
|
|
||
|
class WrappedResult < Base
|
||
|
include Enumerable
|
||
|
|
||
15 years ago
|
def set_key(index = [])
|
||
|
values.each { |v| v.set_key index }
|
||
|
super
|
||
|
end
|
||
|
|
||
|
def sort!(force = false)
|
||
|
return false if @sorted and not force
|
||
10 years ago
|
values.sort! { |a, b| compare a.key, b.key }
|
||
15 years ago
|
values.each { |e| e.sort! force }
|
||
|
@sorted = true
|
||
15 years ago
|
end
|
||
|
|
||
15 years ago
|
def depth_of(type)
|
||
|
super + first.depth_of(type)
|
||
|
end
|
||
|
|
||
15 years ago
|
def has_children?
|
||
|
true
|
||
|
end
|
||
|
|
||
|
def count
|
||
|
sum_for :count
|
||
|
end
|
||
|
|
||
|
def units
|
||
|
sum_for :units
|
||
|
end
|
||
|
|
||
|
def sum_for(field)
|
||
|
@sum_for ||= {}
|
||
14 years ago
|
@sum_for[field] ||= sum { |v| v.send(field) || 0 }
|
||
15 years ago
|
end
|
||
|
|
||
15 years ago
|
def recursive_each_with_level(level = 0, depth_first = true, &block)
|
||
|
if depth_first
|
||
|
super
|
||
|
each { |c| c.recursive_each_with_level(level + 1, depth_first, &block) }
|
||
10 years ago
|
else # width-first
|
||
15 years ago
|
to_evaluate = [self]
|
||
|
lvl = level
|
||
10 years ago
|
while !to_evaluate.empty?
|
||
15 years ago
|
# evaluate all stored results and find the results we need to evaluate soon
|
||
|
to_evaluate_soon = []
|
||
|
to_evaluate.each do |r|
|
||
10 years ago
|
block.call(lvl, r)
|
||
15 years ago
|
to_evaluate_soon.concat r.values if r.size > 0
|
||
|
end
|
||
|
# take new results to evaluate
|
||
10 years ago
|
lvl = lvl + 1
|
||
15 years ago
|
to_evaluate = to_evaluate_soon
|
||
|
end
|
||
|
end
|
||
15 years ago
|
|
||
|
def each_row
|
||
|
return enum_for(:each_row) unless block_given?
|
||
|
if final_row? then yield self
|
||
|
else each { |c| c.each_row(&Proc.new) }
|
||
|
end
|
||
|
end
|
||
15 years ago
|
end
|
||
|
|
||
15 years ago
|
def to_a
|
||
|
values
|
||
|
end
|
||
|
|
||
15 years ago
|
def each(&block)
|
||
|
values.each(&block)
|
||
|
end
|
||
|
|
||
15 years ago
|
def each_direct_result(cached = true)
|
||
|
return enum_for(__method__) unless block_given?
|
||
|
if @direct_results
|
||
|
@direct_results.each { |r| yield(r) }
|
||
|
else
|
||
|
values.each do |value|
|
||
|
value.each_direct_result(false) do |result|
|
||
|
(@direct_results ||= []) << result if cached
|
||
|
yield result
|
||
|
end
|
||
|
end
|
||
|
end
|
||
15 years ago
|
end
|
||
|
|
||
15 years ago
|
def fields
|
||
|
@fields ||= {}.with_indifferent_access
|
||
|
end
|
||
|
|
||
|
##
|
||
|
# @return [Integer] Number of child results
|
||
|
def size
|
||
|
values.size
|
||
|
end
|
||
|
end
|
||
|
|
||
15 years ago
|
def self.new(value, fields = {}, type = nil, important_fields = [])
|
||
15 years ago
|
result = begin
|
||
|
case value
|
||
10 years ago
|
when ActiveRecord::Result, Array then engine::Result::WrappedResult.new value.map { |e| new e, {}, nil, important_fields }
|
||
14 years ago
|
when Hash then engine::Result::DirectResult.new value.with_indifferent_access
|
||
15 years ago
|
when Base then value
|
||
|
else raise ArgumentError, "Cannot create Result from #{value.inspect}"
|
||
|
end
|
||
|
end
|
||
|
result.fields.merge! fields
|
||
15 years ago
|
result.type = type if type
|
||
15 years ago
|
result.important_fields = important_fields unless result == value
|
||
15 years ago
|
result
|
||
|
end
|
||
|
end
|