Module: GoodData::SmallGoodZilla

Defined in:
lib/gooddata/goodzilla/goodzilla.rb

Class Method Summary collapse

Class Method Details

.create_category_filter(spec, project) ⇒ Array<Hash>

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']



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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) ⇒ Array<Array>

Scans the provided MAQL and returns Array pairs of [attribute, element] pairs for each element that is found in the definition



13
14
15
16
17
18
# 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) ⇒ Array<String>

Get Attributes from extendedMAQL string



44
45
46
# 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) ⇒ Array<String>

Get Facts from extendedMAQL string



37
38
39
# 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) ⇒ Array<String>

Get IDs from extendedMAQL string



30
31
32
# File 'lib/gooddata/goodzilla/goodzilla.rb', line 30

def get_ids(a_maql_string)
  a_maql_string.scan(/!\[([^\"\]]+)\]/).flatten.uniq
end

.get_metrics(a_maql_string) ⇒ Array<String> Also known as: get_measures

Get Metrics from extendedMAQL string



51
52
53
# File 'lib/gooddata/goodzilla/goodzilla.rb', line 51

def get_metrics(a_maql_string)
  a_maql_string.scan(/\?"([^\"]+)\"/).flatten
end

.get_uris(a_maql_string) ⇒ Array<String>

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 []



23
24
25
# File 'lib/gooddata/goodzilla/goodzilla.rb', line 23

def get_uris(a_maql_string)
  a_maql_string.scan(/\[([^\"\]]+)\]/).flatten.uniq
end

.interpolate(values, dictionaries) ⇒ Object



116
117
118
119
120
121
122
# 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) ⇒ Object



124
125
126
127
128
129
130
131
132
133
# 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_metric(metric, dictionary, options = { :client => GoodData.connection, :project => GoodData.project }) ⇒ Object Also known as: interpolate_measure



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# 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

.interpolate_values(keys, values) ⇒ Object



135
136
137
138
# 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 }) ⇒ String

Pretty prints the MAQL expression. This basically means it finds out names of objects and elements and print their values instead of URIs



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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