Class: Spree::Reporting::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/spree/reporting/schema.rb

Overview

The self-describing contract for one caller: labels and descriptions (localized), metric formats with the store currency, per-dimension metric compatibility, filter ops and enumerated values, the time-range grammar, and store meta — filtered to the members the caller may reference, so pickers never offer and agents never propose a member that would be refused. Drives GET /reporting/schema and the agent tool schema.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store:, registry: Spree.reporting, allowed: ->(_member) { true }) ⇒ Schema

Returns a new instance of Schema.

Parameters:

  • store (Spree::Store)
  • registry (Registry) (defaults to: Spree.reporting)
  • allowed (Proc) (defaults to: ->(_member) { true }) —

    ->(member) { true/false } — permission gate, asked about metrics and dimensions alike



14
15
16
17
18
# File 'lib/spree/reporting/schema.rb', line 14

def initialize(store:, registry: Spree.reporting, allowed: ->(_member) { true })
  @store = store
  @registry = registry
  @allowed = allowed
end

Class Method Details

.value_label(dimension, value) ⇒ String

Enumerated values are labelled server-side so a plugin dimension declaring values: needs no dashboard locale edit; the schema, the result hydration and the CSV export all read the same label.

Parameters:

  • dimension (Registry::Dimension)
  • value (String)

Returns:

  • (String)


119
120
121
# File 'lib/spree/reporting/schema.rb', line 119

def self.value_label(dimension, value)
  I18n.t("spree.reporting.values.#{dimension.name}.#{value}", default: value.to_s.humanize)
end

Instance Method Details

#label_for(group, name) ⇒ String

Returns localized label (humanized name when none registered).

Parameters:

  • group (Symbol) —

    :metrics | :dimensions

Returns:

  • (String) —

    localized label (humanized name when none registered)



52
53
54
# File 'lib/spree/reporting/schema.rb', line 52

def label_for(group, name)
  catalog_text(group, name, :label)
end

#to_h ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/spree/reporting/schema.rb', line 20

def to_h
  metrics = @registry.metrics.values.select { |m| @allowed.call(m) }
  dimensions = @registry.dimensions.values.select { |d| @allowed.call(d) }
  {
    meta: {
      currency: @store.default_currency,
      timezone: (Time.find_zone(@store.preferred_timezone) || Time.zone).name,
      supported_currencies: @store.supported_currencies_list.map(&:iso_code)
    },
    # Published so a builder can group the pickers and never offer a
    # cross-family pair the query would refuse.
    families: families(metrics),
    metrics: metrics.map { |m| metric_entry(m) },
    dimensions: dimensions.map { |d| dimension_entry(d, metrics) },
    time_range: {
      presets: (Query::PRESETS + Query::RELATIVE_PRESETS).map { |p| { name: p, label: catalog_text('presets', p, :label) } },
      relative: %w[last_<n>_days last_<n>_weeks last_<n>_months],
      absolute: 'ISO 8601 dates or datetimes in `since` / `until`, resolved in the store timezone'
    },
    # Ranking defaults the compiler applies, published so a client never
    # has to mirror them.
    limits: { default: Query::DEFAULT_VALUE_LIMIT, max: Query::MAX_LIMIT,
              max_buckets: Query::MAX_BUCKETS },
    # Filtering on an aggregate (HAVING) is a separate contract key from
    # `filters`, which narrow on dimensions before aggregation.
    metric_filter_ops: Query::METRIC_FILTER_OPS,
    compare_modes: Query::COMPARE_MODES
  }
end