Class: Spree::AgentTools::QueryReport

Inherits:
Spree::AgentTool show all
Defined in:
app/tools/spree/agent_tools/query_report.rb

Overview

Answers a reporting question — revenue by month, units by product, orders by channel — against the same semantic registry the dashboard charts read, so an agent and a chart cannot disagree about what a number means.

Constant Summary collapse

DEFAULT_LIMIT =

Rows land in the model's context verbatim, so a query that would have returned thousands is capped rather than truncated silently.

50
MAX_LIMIT =
500
TARGET_POINTS =

A date dimension given without a grain gets the finest one that keeps the series about this long: a month by day, a quarter by week, a year by month. The reporting default, hour, fits almost no question.

31
TIME_RANGE_ALIASES =

Models often write a range as from/to; both are read as since/until.

{ 'from' => 'since', 'to' => 'until' }.freeze
TIME_RANGE_KEYS =
%w[preset since until].freeze

Constants inherited from Spree::AgentTool

Spree::AgentTool::PARENT_ID_DESCRIPTION

Instance Attribute Summary

Attributes inherited from Spree::AgentTool

#context

Instance Method Summary collapse

Methods inherited from Spree::AgentTool

additive!, audience, description, destructive?, #initialize, input_schema, mutating!, mutating?, param, params, permission, #permitted?, tool_name, #unknown_resource

Constructor Details

This class inherits a constructor from Spree::AgentTool

Instance Method Details

#call(metrics:, dimensions: nil, time_range: nil, filters: nil, sort: nil, limit: nil, currency: nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/tools/spree/agent_tools/query_report.rb', line 47

def call(metrics:, dimensions: nil, time_range: nil, filters: nil, sort: nil, limit: nil, currency: nil)
  row_limit = [(limit || DEFAULT_LIMIT).to_i, MAX_LIMIT].min
  raise Spree::Reporting::InvalidQuery, "limit must be between 1 and #{MAX_LIMIT}" if row_limit < 1

  # An unsorted date series is reported whole or not at all: cut at the
  # limit, it would end early next to totals for the whole range. A
  # sorted one ("the five best days") is a ranking, cut on purpose.
  dimensions = Array(dimensions).map { |entry| entry.is_a?(Hash) ? entry.stringify_keys : entry }
  series = sort.blank? && dimensions.any? { |entry| time_dimension_for(entry) }

  query = build_query(metrics: metrics, dimensions: dimensions, time_range: time_range, filters: filters,
                      sort: sort, currency: currency, limit: (row_limit unless series),
                      target_points: series && dimensions.one? ? [TARGET_POINTS, row_limit].min : TARGET_POINTS)

  refusal = unauthorized_members(query)
  return refusal if refusal

  result = query.execute
  if series && Array(result.rows).size > row_limit
    return { error: "This report has #{result.rows.size} rows, more than the limit of #{row_limit}. " \
                    "Shorten the range, use a coarser grain, or raise limit (up to #{MAX_LIMIT})." }
  end

  shape(result)
rescue Spree::Reporting::UnknownMember => e
  { error: e.message, valid: Array(e.valid).map(&:to_s) }
rescue Spree::Reporting::InvalidQuery => e
  { error: e.message }
end

#summary(arguments) ⇒ Object



77
78
79
80
81
82
# File 'app/tools/spree/agent_tools/query_report.rb', line 77

def summary(arguments)
  metrics = Array(arguments[:metrics]).join(', ')
  dimensions = Array(arguments[:dimensions])

  dimensions.any? ? "Report #{metrics} by #{dimensions.join(', ')}" : "Report #{metrics}"
end