module GoodData::SmallGoodZilla

Public Class Methods

create_category_filter(spec, project) click to toggle source

Method takes a specification of the attribute filter (category filter) and returns it representation that is suitable for posting on the API. The spec is expected to be an array. First object can be an attribute (id, obj_id or directly an object). Alternativel it can be an attribute (again any representation should work). In case of attribute primary label is taken. The rest of the array are expected to be String represenation of values of particular label.

For example it could look like

‘label.states.name’, ‘California’, ‘New Jersey’, ‘Kansas’

@param spec [Array<Object>] Input MAQL string @return [Array<Hash>] List of Metrics

# File lib/gooddata/goodzilla/goodzilla.rb, line 67
def create_category_filter(spec, project)
  item = project.objects(spec.first)
  label = item.is_a?(GoodData::Attribute) ? item.primary_label : item
  col = spec[1..-1].flat_map do |v|
    case v
    when Range
      v.to_a
    when Symbol
      [v]
    else
      [v.to_s]
    end
  end
  if col.first == :not
    values = col[1..-1].map { |v| label.find_value_uri(v) }
    elements = values.map { |v| "[#{v}]" }.join(', ')
    { expression: "[#{label.attribute.uri}] NOT IN (#{elements})" }
  else
    values = col.map { |v| label.find_value_uri(v) }
    elements = values.map { |v| "[#{v}]" }.join(', ')
    { expression: "[#{label.attribute.uri}] IN (#{elements})" }
  end
end
extract_element_uri_pairs(maql) click to toggle source

Scans the provided MAQL and returns Array pairs of [attribute, element] pairs for each element that is found in the definition @param maql Input MAQL string @return [Array<Array>] Pairs [attribute, attribute_element]

# File lib/gooddata/goodzilla/goodzilla.rb, line 13
def extract_element_uri_pairs(maql)
  arr = maql.scan(%r{(\/gdc\/(?:projects|md)\/[a-zA-Z\d]+\/obj\/\d+)\/elements\?id=(\d+)}).flatten
  evens = arr.select.each_with_index { |_, i| i.even? }
  odds = arr.select.each_with_index { |_, i| i.odd? }.map(&:to_i)
  evens.zip(odds)
end
get_attributes(a_maql_string) click to toggle source

Get Attributes from extendedMAQL string @param a_maql_string Input MAQL string @return [Array<String>] List of Attributes

# File lib/gooddata/goodzilla/goodzilla.rb, line 44
def get_attributes(a_maql_string)
  a_maql_string.scan(/@\"([^\"]+)\"/).flatten
end
get_facts(a_maql_string) click to toggle source

Get Facts from extendedMAQL string @param a_maql_string Input MAQL string @return [Array<String>] List of Facts

# File lib/gooddata/goodzilla/goodzilla.rb, line 37
def get_facts(a_maql_string)
  a_maql_string.scan(/#\"([^\"]+)\"/).flatten
end
get_ids(a_maql_string) click to toggle source

Get IDs from extendedMAQL string @param a_maql_string Input MAQL string @return [Array<String>] List of IDS

# File lib/gooddata/goodzilla/goodzilla.rb, line 30
def get_ids(a_maql_string)
  a_maql_string.scan(/!\[([^\"\]]+)\]/).flatten.uniq
end
get_measures(a_maql_string)
Alias for: get_metrics
get_metrics(a_maql_string) click to toggle source

Get Metrics from extendedMAQL string @param a_maql_string Input MAQL string @return [Array<String>] List of Metrics

# File lib/gooddata/goodzilla/goodzilla.rb, line 51
def get_metrics(a_maql_string)
  a_maql_string.scan(/\?"([^\"]+)\"/).flatten
end
Also aliased as: get_measures
get_uris(a_maql_string) click to toggle source

Scans the provided MAQL and returns Array of all the URIs included in the MAQL. This basically return anything that is enclosed in aquare brackets [] @param maql Input MAQL string @return [Array<String>] Pairs of URIs

# File lib/gooddata/goodzilla/goodzilla.rb, line 23
def get_uris(a_maql_string)
  a_maql_string.scan(/\[([^\"\]]+)\]/).flatten.uniq
end
interpolate(values, dictionaries) click to toggle source
# File lib/gooddata/goodzilla/goodzilla.rb, line 116
def interpolate(values, dictionaries)
  {
    :facts => interpolate_values(values[:facts], dictionaries[:facts]),
    :attributes => interpolate_values(values[:attributes], dictionaries[:attributes]),
    :metrics => interpolate_values(values[:metrics], dictionaries[:metrics])
  }
end
interpolate_ids(options, *ids) click to toggle source
# File lib/gooddata/goodzilla/goodzilla.rb, line 124
def interpolate_ids(options, *ids)
  ids = ids.flatten
  if ids.empty?
    []
  else
    res = GoodData::MdObject.identifier_to_uri(options, *ids)
    fail 'Not all of the identifiers were resolved' if Array(res).size != ids.size
    res
  end
end
interpolate_measure(metric, dictionary, options = { :client => GoodData.connection, :project => GoodData.project })
Alias for: interpolate_metric
interpolate_metric(metric, dictionary, options = { :client => GoodData.connection, :project => GoodData.project }) click to toggle source
# File lib/gooddata/goodzilla/goodzilla.rb, line 140
def interpolate_metric(metric, dictionary, options = { :client => GoodData.connection, :project => GoodData.project })
  interpolated = interpolate({
                               :facts => GoodData::SmallGoodZilla.get_facts(metric),
                               :attributes => GoodData::SmallGoodZilla.get_attributes(metric),
                               :metrics => GoodData::SmallGoodZilla.get_metrics(metric)
                             }, dictionary)

  ids = GoodData::SmallGoodZilla.get_ids(metric)
  interpolated_ids = ids.zip(Array(interpolate_ids(options, ids)))

  metric = interpolated[:facts].reduce(metric) { |acc, elem| acc.sub("#\"#{elem[0]}\"", "[#{elem[1]}]") }
  metric = interpolated[:attributes].reduce(metric) { |acc, elem| acc.sub("@\"#{elem[0]}\"", "[#{elem[1]}]") }
  metric = interpolated[:metrics].reduce(metric) { |acc, elem| acc.sub("?\"#{elem[0]}\"", "[#{elem[1]}]") }
  metric = interpolated_ids.reduce(metric) { |acc, elem| acc.gsub("![#{elem[0]}]", "[#{elem[1]}]") }
  metric
end
Also aliased as: interpolate_measure
interpolate_values(keys, values) click to toggle source
# File lib/gooddata/goodzilla/goodzilla.rb, line 135
def interpolate_values(keys, values)
  x = values.values_at(*keys)
  keys.zip(x)
end
pretty_print(expression, opts = { client: GoodData.connection, project: GoodData.project }) click to toggle source

Pretty prints the MAQL expression. This basically means it finds out names of objects and elements and print their values instead of URIs @param expression [String] Expression to be beautified @return [String] Pretty printed MAQL expression

# File lib/gooddata/goodzilla/goodzilla.rb, line 94
def pretty_print(expression, opts = { client: GoodData.connection, project: GoodData.project })
  temp = expression.dup
  pairs = get_uris(expression).pmap do |uri|
    if uri =~ /elements/
      begin
        ['element', uri, Attribute.find_element_value(uri, opts)]
      rescue AttributeElementNotFound
        ['element', uri, '(empty value)']
      end
    else
      ['object', uri, GoodData::MdObject[uri, opts].title]
    end
  end
  pairs.sort_by! { |p| p[0] }
  pairs.each do |el|
    uri = el[1]
    obj = el[2]
    temp.gsub!(uri, obj)
  end
  temp
end