Class: AnalyticsOps::Clients::Data

Inherits:
Object
  • Object
show all
Includes:
DataResultNormalization
Defined in:
lib/analytics_ops/clients/data.rb,
sig/analytics_ops.rbs

Overview

Narrow adapter around Google's generated Data API client.

Constant Summary collapse

PACKAGE_REQUIREMENT =
Gem::Requirement.new("~> 0.9.0")
MATCH_TYPES =
{
  "exact" => :EXACT,
  "begins_with" => :BEGINS_WITH,
  "ends_with" => :ENDS_WITH,
  "contains" => :CONTAINS,
  "full_regexp" => :FULL_REGEXP,
  "partial_regexp" => :PARTIAL_REGEXP
}.freeze
NUMERIC_OPERATIONS =
{
  "equal" => :EQUAL,
  "less_than" => :LESS_THAN,
  "less_than_or_equal" => :LESS_THAN_OR_EQUAL,
  "greater_than" => :GREATER_THAN,
  "greater_than_or_equal" => :GREATER_THAN_OR_EQUAL
}.freeze
QUOTA_FIELDS =
%i[
  tokens_per_day
  tokens_per_hour
  tokens_per_project_per_hour
  concurrent_requests
  server_errors_per_project_per_hour
  potentially_thresholded_requests_per_hour
].freeze
Reports =

Returns:

  • (:Definition definition)

Instance Method Summary collapse

Constructor Details

#initialize(client: nil, service_account: nil, transport: :grpc, timeout: nil, logger: nil) ⇒ Data

Returns a new instance of Data.



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/analytics_ops/clients/data.rb', line 37

def initialize(client: nil, service_account: nil, transport: :grpc, timeout: nil, logger: nil)
  unless .nil? || .is_a?(ServiceAccount)
    raise ConfigurationError, "service_account must be an AnalyticsOps::ServiceAccount"
  end

  @client = client
  @service_account = 
  @transport = validate_transport(transport)
  @timeout = validate_timeout(timeout)
  @logger = logger
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


119
120
121
122
123
124
# File 'lib/analytics_ops/clients/data.rb', line 119

def available?
  generated_client = translate_errors { client }
  generated_client.respond_to?(:run_report) && generated_client.respond_to?(:run_realtime_report) &&
    generated_client.respond_to?(:batch_run_reports) && generated_client.respond_to?(:get_metadata) &&
    generated_client.respond_to?(:check_compatibility)
end

#batch(property_id, definitions) ⇒ Array[Reports::Result]

Parameters:

Returns:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/analytics_ops/clients/data.rb', line 63

def batch(property_id, definitions)
  validate_batch_definitions!(definitions)

  property = property_name(property_id)
  requests = definitions.map do |definition|
    standard_request(property_id, definition).reject { |key| key == :property }
  end
  response = invoke(:batch_run_reports, property:, requests:)
  reports = array_field(response, :reports)
  unless reports.length == definitions.length
    raise RemoteError, "Google Data API returned a batch size that does not match the request"
  end

  definitions.zip(reports).map { |definition, report| normalize_result(definition, report) }.freeze
end

#check_compatibility(property_id, definition) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/analytics_ops/clients/data.rb', line 92

def check_compatibility(property_id, definition)
  unless definition.is_a?(Reports::Definition) && !definition.realtime?
    raise InvalidRequestError, "compatibility requires a standard report definition"
  end

  response = invoke(
    :check_compatibility,
    property: property_name(property_id),
    dimensions: definition.dimensions.map { |name| { name: } },
    metrics: definition.metrics.map { |name| { name: } },
    dimension_filter: filter_request(definition.dimension_filter),
    metric_filter: filter_request(definition.metric_filter)
  )
  dimensions = compatibility_entries(response, :dimension_compatibilities)
  metrics = compatibility_entries(response, :metric_compatibilities)
  unless compatibility_names(dimensions) == definition.dimensions.sort &&
         compatibility_names(metrics) == definition.metrics.sort
    raise RemoteError, "Google Data API returned compatibility fields that do not match the request"
  end

  Reports::Compatibility.new(
    report_name: definition.name,
    dimensions:,
    metrics:
  )
end

#compatibilityrecord

Returns:

  • (record)


126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/analytics_ops/clients/data.rb', line 126

def compatibility
  specification = Gem::Specification.find_by_name("google-analytics-data")
  Canonical.immutable(
    "package" => specification.name,
    "version" => specification.version.to_s,
    "requirement" => PACKAGE_REQUIREMENT.to_s,
    "supported" => PACKAGE_REQUIREMENT.satisfied_by?(specification.version),
    "transport" => @transport.to_s
  )
rescue Gem::LoadError => error
  raise UnsupportedCapabilityError, Redaction.message(error.message)
end

#metadata(property_id) ⇒ Reports::Metadata

Parameters:

  • property_id (String)

Returns:



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/analytics_ops/clients/data.rb', line 79

def (property_id)
  property = property_name(property_id)
  @metadata_cache ||= {}
  @metadata_cache[property] ||= begin
    response = invoke(:get_metadata, name: "#{property}/metadata")
    Reports::Metadata.new(
      property_id:,
      dimensions: array_field(response, :dimensions).map { |value| normalize_field(value, "dimension") },
      metrics: array_field(response, :metrics).map { |value| normalize_field(value, "metric") }
    )
  end
end

#run(property_id, definition) ⇒ Reports::Result

Parameters:

Returns:



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/analytics_ops/clients/data.rb', line 49

def run(property_id, definition)
  unless definition.is_a?(Reports::Definition)
    raise InvalidRequestError, "report must be an AnalyticsOps::Reports::Definition"
  end

  response = if definition.realtime?
               invoke(:run_realtime_report, realtime_request(property_id, definition))
             else
               invoke(:run_report, standard_request(property_id, definition))
             end

  normalize_result(definition, response)
end