29 行
1.0 KiB
Ruby
29 行
1.0 KiB
Ruby
# Coordinates carry their aggregation depth; SQL NULL remains an ordinary axis value.
|
|
class CountsPivotLayout
|
|
def self.depths(keys, sorts, measures: true)
|
|
return [] if keys.length < 2 || sorts.first&.first == 'count' ||
|
|
sorts.first&.first&.start_with?('measure_')
|
|
|
|
relevant = sorts.map(&:first).select { keys.include?(_1) || (measures && (_1 == 'count' ||
|
|
_1.start_with?('measure_'))) }
|
|
order = (relevant + keys).uniq
|
|
(1...keys.length).select { order.first(_1).sort == keys.first(_1).sort }
|
|
end
|
|
|
|
def self.headers(values, depths, next_values: nil, grand: false)
|
|
result = []
|
|
values.each_with_index do |tuple, index|
|
|
result << { values: tuple, kind: 'data' }
|
|
following = values[index + 1] || next_values
|
|
depths.reverse_each do |depth|
|
|
prefix = tuple.first(depth)
|
|
next if following && following.first(depth) == prefix
|
|
|
|
result << { values: prefix, kind: 'subtotal' }
|
|
end
|
|
end
|
|
result << { values: [], kind: 'grand_total' } if grand
|
|
result
|
|
end
|
|
end
|