Class: Spree::Reporting::Query
- Inherits:
-
Object
- Object
- Spree::Reporting::Query
- Defined in:
- lib/spree/reporting/query.rb
Overview
Validates a contract query against the registry, normalizes it into an execution plan (resolved members, time ranges, forced store/currency scopes, resolved filter values), and runs it through a storage adapter.
Raises UnknownMember / InvalidQuery for anything outside the registry — unknown members are rejected, never silently dropped.
Constant Summary collapse
- MAX_LIMIT =
1000- DEFAULT_VALUE_LIMIT =
50- MAX_DIMENSIONS =
2- FILTER_OPS =
%w[eq in].freeze
- METRIC_FILTER_OPS =
Comparison ops for metric filters. A metric filter compiles to HAVING, so it takes scalar comparisons rather than the set membership a dimension filter uses.
%w[eq gt gte lt lte].freeze
- COMPARE_MODES =
%w[previous_period previous_year].freeze
- MAX_BUCKETS =
Hourly buckets over a wide range produce a series nothing can read (a year is 8,760 points), so the grain is refused rather than silently coarsened (Decision 4).
2_000- LAST_N_RANGE =
/\Alast_(\d+)_(days|weeks|months)\z/- PRESETS =
Named presets, resolved in the store's timezone (see #time_zone).
%w[today yesterday week_to_date month_to_date quarter_to_date year_to_date last_week last_month last_quarter].freeze
- RELATIVE_PRESETS =
The
last_<n>_<unit>ranges worth a labelled entry in pickers; any other count/unit pair is accepted through LAST_N_RANGE. %w[last_7_days last_30_days last_4_weeks last_90_days last_12_months].freeze
Instance Attribute Summary collapse
-
#compare ⇒ Object
readonly
Returns the value of attribute compare.
-
#currency ⇒ Object
readonly
Returns the value of attribute currency.
-
#dimensions ⇒ Object
readonly
Returns the value of attribute dimensions.
-
#filters ⇒ Object
readonly
Returns the value of attribute filters.
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#metric_filters ⇒ Object
readonly
Returns the value of attribute metric_filters.
-
#metrics ⇒ Object
readonly
Returns the value of attribute metrics.
-
#registry ⇒ Object
readonly
Returns the value of attribute registry.
-
#sort ⇒ Object
readonly
Returns the value of attribute sort.
-
#store ⇒ Object
readonly
Returns the value of attribute store.
-
#time_range ⇒ Object
readonly
Returns the value of attribute time_range.
Instance Method Summary collapse
-
#aggregated_metrics ⇒ Object
All metrics the adapter must aggregate: requested non-derived metrics plus the hidden components of requested ratios.
-
#authorized_members ⇒ Object
Every member whose definition names an authorization subject: the dimensions the query groups or filters by, and the metrics that expose money living outside the order itself.
- #compare? ⇒ Boolean
- #execute(adapter: Spree::Dependencies.reporting_adapter.constantize.new) ⇒ Object
-
#include_empty? ⇒ Boolean
Whether a dimension's whole population leads the query, so members with no matching rows still produce a row.
-
#initialize(store:, params:, registry: Spree.reporting) ⇒ Query
constructor
A new instance of Query.
-
#previous_time_range ⇒ Object
The period this query compares against.
- #referenced_dimensions ⇒ Object
-
#required_key_scopes ⇒ Object
API-key scopes required beyond
read_reports(which gates the endpoint and covers the order-data floor): thekey_scopeof every referenced member with an authorization subject. -
#required_subjects ⇒ Object
Authorization subjects for this query: order data (the floor for all reporting) plus every referenced member's declared subject.
-
#resolve_filter_values! ⇒ Object
Filter values resolve lazily (prefixed ids → store-scoped record ids) so authorization can run between construction and execution — a forbidden member must 403 before an unknown id can 404.
-
#scope_currency ⇒ Object
The currency a base should filter by, or nil when the question does not involve money.
- #time_dimension ⇒ Object
-
#time_zone ⇒ Object
Day+ grains and relative ranges resolve in the store's timezone (Decision 7); adapters read it from here so every consumer agrees.
-
#unreadable_subjects(ability) ⇒ Array<Class, Symbol>
The subjects a given ability may not read — empty when the query is fully authorized.
Constructor Details
#initialize(store:, params:, registry: Spree.reporting) ⇒ Query
Returns a new instance of Query.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/spree/reporting/query.rb', line 34 def initialize(store:, params:, registry: Spree.reporting) @store = store @registry = registry raise InvalidQuery, 'query must be an object' unless params.respond_to?(:to_h) params = params.to_h.deep_symbolize_keys @currency = params[:currency].presence || store.default_currency @metrics = normalize_metrics(params[:metrics]) @dimensions = normalize_dimensions(params[:dimensions]) @filters = normalize_filters(params[:filters]) @metric_filters = normalize_metric_filters(params[:metric_filters]) @time_range = normalize_time_range(params[:time_range]) @compare = normalize_compare(params[:compare]) @sort = normalize_sort(params[:sort]) @limit = normalize_limit(params[:limit]) validate_bases! validate_include_empty_filters! validate_bucket_count! validate_lifetime_metrics! end |
Instance Attribute Details
#compare ⇒ Object (readonly)
Returns the value of attribute compare.
31 32 33 |
# File 'lib/spree/reporting/query.rb', line 31 def compare @compare end |
#currency ⇒ Object (readonly)
Returns the value of attribute currency.
31 32 33 |
# File 'lib/spree/reporting/query.rb', line 31 def currency @currency end |
#dimensions ⇒ Object (readonly)
Returns the value of attribute dimensions.
31 32 33 |
# File 'lib/spree/reporting/query.rb', line 31 def dimensions @dimensions end |
#filters ⇒ Object (readonly)
Returns the value of attribute filters.
31 32 33 |
# File 'lib/spree/reporting/query.rb', line 31 def filters @filters end |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
31 32 33 |
# File 'lib/spree/reporting/query.rb', line 31 def limit @limit end |
#metric_filters ⇒ Object (readonly)
Returns the value of attribute metric_filters.
31 32 33 |
# File 'lib/spree/reporting/query.rb', line 31 def metric_filters @metric_filters end |
#metrics ⇒ Object (readonly)
Returns the value of attribute metrics.
31 32 33 |
# File 'lib/spree/reporting/query.rb', line 31 def metrics @metrics end |
#registry ⇒ Object (readonly)
Returns the value of attribute registry.
31 32 33 |
# File 'lib/spree/reporting/query.rb', line 31 def registry @registry end |
#sort ⇒ Object (readonly)
Returns the value of attribute sort.
31 32 33 |
# File 'lib/spree/reporting/query.rb', line 31 def sort @sort end |
#store ⇒ Object (readonly)
Returns the value of attribute store.
31 32 33 |
# File 'lib/spree/reporting/query.rb', line 31 def store @store end |
#time_range ⇒ Object (readonly)
Returns the value of attribute time_range.
31 32 33 |
# File 'lib/spree/reporting/query.rb', line 31 def time_range @time_range end |
Instance Method Details
#aggregated_metrics ⇒ Object
All metrics the adapter must aggregate: requested non-derived metrics plus the hidden components of requested ratios.
161 162 163 |
# File 'lib/spree/reporting/query.rb', line 161 def aggregated_metrics metrics.flat_map { |metric| registry.components(metric) }.uniq(&:name) end |
#authorized_members ⇒ Object
Every member whose definition names an authorization subject: the dimensions the query groups or filters by, and the metrics that expose money living outside the order itself. Aggregated rather than requested metrics, so a ratio built on a gated component is gated too.
113 114 115 |
# File 'lib/spree/reporting/query.rb', line 113 def referenced_dimensions + aggregated_metrics end |
#compare? ⇒ Boolean
149 150 151 |
# File 'lib/spree/reporting/query.rb', line 149 def compare? compare.present? end |
#execute(adapter: Spree::Dependencies.reporting_adapter.constantize.new) ⇒ Object
56 57 58 59 |
# File 'lib/spree/reporting/query.rb', line 56 def execute(adapter: Spree::Dependencies.reporting_adapter.constantize.new) resolve_filter_values! adapter.execute(self) end |
#include_empty? ⇒ Boolean
Whether a dimension's whole population leads the query, so members with no matching rows still produce a row.
155 156 157 |
# File 'lib/spree/reporting/query.rb', line 155 def include_empty? dimensions.any? { |d| d[:include_empty] } end |
#previous_time_range ⇒ Object
The period this query compares against.
previous_period shifts by an equal number of calendar days in the
store zone, so a range spanning a DST change keeps its midnight edges.
previous_year shifts by a calendar year instead: "the same month last
year" means February to February, and an equal-day-count shift off a
28-day February would land in January. ActiveSupport maps February 29
to February 28 in a non-leap year.
A range of whole calendar months (a full quarter, last_month) shifts
by the same number of months for the same reason: July – September is
92 days, and shifting it by days would start on March 31, putting a
one-day March bucket at the front of the previous period.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/spree/reporting/query.rb', line 134 def previous_time_range first = time_range.first.in_time_zone(time_zone) last = time_range.last.in_time_zone(time_zone) if compare == 'previous_year' (first - 1.year)..(last - 1.year) elsif first == first.beginning_of_month && last == last.end_of_month months = (last.year * 12 + last.month) - (first.year * 12 + first.month) + 1 (first - months.months)..(last - months.months).end_of_month else days = (last.to_date - first.to_date).to_i + 1 (first - days.days)..(last - days.days) end end |
#referenced_dimensions ⇒ Object
117 118 119 |
# File 'lib/spree/reporting/query.rb', line 117 def referenced_dimensions (dimensions.map { |d| d[:dimension] } + filters.map { |f| f[:dimension] }).uniq end |
#required_key_scopes ⇒ Object
API-key scopes required beyond read_reports (which gates the
endpoint and covers the order-data floor): the key_scope of every
referenced member with an authorization subject.
104 105 106 |
# File 'lib/spree/reporting/query.rb', line 104 def required_key_scopes .filter_map(&:key_scope).uniq end |
#required_subjects ⇒ Object
Authorization subjects for this query: order data (the floor for all
reporting) plus every referenced member's declared subject. Every
consumer — API controller, saved reports, agent tools — must ensure
:read on each before executing.
87 88 89 |
# File 'lib/spree/reporting/query.rb', line 87 def required_subjects ([Spree::Order] + .filter_map(&:subject).map(&:call)).uniq end |
#resolve_filter_values! ⇒ Object
Filter values resolve lazily (prefixed ids → store-scoped record ids) so authorization can run between construction and execution — a forbidden member must 403 before an unknown id can 404.
64 65 66 67 68 69 70 71 |
# File 'lib/spree/reporting/query.rb', line 64 def resolve_filter_values! return if @filter_values_resolved filters.each do |filter| filter[:values] = filter[:values].map { |value| resolve_filter_value(filter[:dimension], value) } end @filter_values_resolved = true end |
#scope_currency ⇒ Object
The currency a base should filter by, or nil when the question does not involve money.
Amounts in different currencies are never converted or added, so a money metric has to be scoped to one. A count or a quantity is not money and carries no such constraint — restricting it anyway would answer "how many orders did we take" with only the share that happened to be priced in one currency, which is wrong rather than partial. A ratio counts as money when either side is (average order value), and not when neither is (sell-through).
175 176 177 |
# File 'lib/spree/reporting/query.rb', line 175 def scope_currency currency if aggregated_metrics.any?(&:money?) end |
#time_dimension ⇒ Object
73 74 75 |
# File 'lib/spree/reporting/query.rb', line 73 def time_dimension dimensions.find { |d| d[:dimension].time? } end |
#time_zone ⇒ Object
Day+ grains and relative ranges resolve in the store's timezone (Decision 7); adapters read it from here so every consumer agrees.
79 80 81 |
# File 'lib/spree/reporting/query.rb', line 79 def time_zone @time_zone ||= Time.find_zone(store.preferred_timezone) || Time.zone end |
#unreadable_subjects(ability) ⇒ Array<Class, Symbol>
The subjects a given ability may not read — empty when the query is fully authorized. The one rule every consumer (API, exports, agent tools) applies, so member-level authorization cannot drift.
97 98 99 |
# File 'lib/spree/reporting/query.rb', line 97 def unreadable_subjects(ability) required_subjects.reject { |subject| ability.can?(:read, subject) } end |