Class: GoodData::Metric

Inherits:
MdObject show all
Includes:
GoodData::Mixin::Lockable
Defined in:
lib/gooddata/models/metadata/metric.rb

Overview

Metric representation

Constant Summary

Constants inherited from MdObject

GoodData::MdObject::IDENTIFIERS_CFG, GoodData::MdObject::MD_OBJ_CTG

Constants included from GoodData::Mixin::MdIdToUri

GoodData::Mixin::MdIdToUri::IDENTIFIERS_CFG

Constants included from GoodData::Mixin::MdObjectIndexer

GoodData::Mixin::MdObjectIndexer::MD_OBJ_CTG

Instance Attribute Summary

Attributes inherited from Rest::Object

#client, #json, #project

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GoodData::Mixin::Lockable

#lock, #lock!, #lock_with_dependencies!, #locked?, #unlock, #unlock!, #unlock_with_dependencies!, #unlocked?

Methods inherited from MdObject

#==, #add_tag, #browser_uri, #delete, #deprecated, #deprecated=, find_replaceable_values, #initialize, #listed?, #project, #reload!, #remove_tag, replace, #replace!, replace_bracketed, replace_quoted, #save, #save_as, #tag_set, #unlisted, #unlisted=

Methods included from GoodData::Mixin::MdIdToUri

#identifier_to_uri

Methods included from GoodData::Mixin::MdObjectIndexer

#[]

Methods included from GoodData::Mixin::MdObjectQuery

#all, #dependency, #dependency?, #query, #usedby, #usedby?, #using, #using?

Methods included from GoodData::Mixin::MdFinders

#find_by_identifier, #find_by_tag, #find_by_title, #find_first_by_identifier, #find_first_by_title

Methods included from GoodData::Mixin::MdObjId

#uri_obj_id

Methods included from GoodData::Mixin::MdGrantees

#change_permission, #grant, #grantees, #revoke

Methods included from GoodData::Mixin::MdRelations

#dependency, #dependency?, #usedby, #usedby?, #using, #using?

Methods included from GoodData::Mixin::ObjId

#obj_id

Methods included from GoodData::Mixin::Links

#links

Methods inherited from Rest::Resource

#initialize

Methods inherited from Rest::Object

client, default_client, #initialize, #saved?

Methods included from GoodData::Mixin::DataPropertyReader

#data_property_reader

Methods included from GoodData::Mixin::DataPropertyWriter

#data_property_writer

Methods included from GoodData::Mixin::MetaPropertyReader

#metadata_property_reader

Methods included from GoodData::Mixin::MetaPropertyWriter

#metadata_property_writer

Methods included from GoodData::Mixin::MetaGetter

#meta

Methods included from GoodData::Mixin::DataGetter

#data

Methods included from GoodData::Mixin::RootKeyGetter

#root_key

Methods included from GoodData::Mixin::ContentGetter

#content

Constructor Details

This class inherits a constructor from GoodData::MdObject

Class Method Details

.all(options = { :client => GoodData.connection, :project => GoodData.project }) ⇒ Array<GoodData::MdObject> | Array<Hash>

Method intended to get all objects of that type in a specified project

Parameters:

  • options (Hash) (defaults to: { :client => GoodData.connection, :project => GoodData.project })

    the options hash

Options Hash (options):

  • :full (Boolean)

    if passed true the subclass can decide to pull in full objects. This is desirable from the usability POV but unfortunately has negative impact on performance so it is not the default

Returns:

  • (Array<GoodData::MdObject> | Array<Hash>)

    Return the appropriate metadata objects or their representation



23
24
25
# File 'lib/gooddata/models/metadata/metric.rb', line 23

def all(options = { :client => GoodData.connection, :project => GoodData.project })
  query('metric', Metric, options)
end

.create(metric, options = { :client => GoodData.connection, :project => GoodData.project }) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
76
77
78
79
80
81
82
83
84
85
# File 'lib/gooddata/models/metadata/metric.rb', line 31

def create(metric, options = { :client => GoodData.connection, :project => GoodData.project })
  client, project = GoodData.get_client_and_project(options)

  if metric.is_a?(String)
    expression = metric || options[:expression]
    extended_notation = options[:extended_notation] || false
    title = options[:title]
    summary = options[:summary]
  else
    metric ||= options
    title = metric[:title] || options[:title]
    summary = metric[:summary] || options[:summary]
    expression = metric[:expression] || options[:expression] || fail('Metric has to have its expression defined')
    extended_notation = metric[:extended_notation] || options[:extended_notation] || false
  end

  expression = if extended_notation
                 dict = {
                   :facts => project.facts.reduce({}) do |memo, item|
                     memo[item.title] = item.uri
                     memo
                   end,
                   :attributes => project.attributes.reduce({}) do |memo, item|
                     memo[item.title] = item.uri
                     memo
                   end,
                   :metrics => project.metrics.reduce({}) do |memo, item|
                     memo[item.title] = item.uri
                     memo
                   end
                 }
                 interpolated_metric = GoodData::SmallGoodZilla.interpolate_metric(expression, dict, options)
                 interpolated_metric
               else
                 expression
               end

  metric = {
    'metric' => {
      'content' => {
        'format' => '#,##0',
        'expression' => expression
      },
      'meta' => {
        'tags' => '',
        'summary' => summary,
        'title' => title
      }
    }
  }
  # TODO: add test for explicitly provided identifier
  metric['metric']['meta']['identifier'] = options[:identifier] if options[:identifier]

  client.create(Metric, metric, :project => project)
end

.execute(expression, options = { :client => GoodData.connection }) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/gooddata/models/metadata/metric.rb', line 87

def execute(expression, options = { :client => GoodData.connection })
  # client = options[:client]
  # fail ArgumentError, 'No :client specified' if client.nil?

  options = expression if expression.is_a?(Hash)

  m = if expression.is_a?(String)
        tmp = {
          :title => 'Temporary metric to be deleted',
          :expression => expression
        }.merge(options)

        GoodData::Metric.create(tmp, options)
      else
        tmp = {
          :title => 'Temporary metric to be deleted'
        }.merge(expression)
        GoodData::Metric.create(tmp, options)
      end
  m.execute
end

.xcreate(metric, options = { :client => GoodData.connection, :project => GoodData.project }) ⇒ Object



27
28
29
# File 'lib/gooddata/models/metadata/metric.rb', line 27

def xcreate(metric, options = { :client => GoodData.connection, :project => GoodData.project })
  create(metric, { extended_notation: true }.merge(options))
end

.xexecute(expression, opts = { :client => GoodData.connection, :project => GoodData.project }) ⇒ Object



109
110
111
112
113
# File 'lib/gooddata/models/metadata/metric.rb', line 109

def xexecute(expression, opts = { :client => GoodData.connection, :project => GoodData.project })
  GoodData.get_client_and_project(opts)

  execute(expression, opts.merge(:extended_notation => true))
end

Instance Method Details

#contain?(item) ⇒ Boolean

Checks that the expression contains certain metadata object. The difference between this and used_by using is in the fact that this is not a transitive closure. it searches only inside the expression

Parameters:

Returns:

  • (Boolean)


145
146
147
148
# File 'lib/gooddata/models/metadata/metric.rb', line 145

def contain?(item)
  uri = item.respond_to?(:uri) ? item.uri : item
  expression[uri] != nil
end

#contain_value?(label, value) ⇒ Boolean

Checks that the expression contains certain element of an attribute. The value is looked up through given label.

Parameters:

  • label (GoodData::Label)

    Label though which the value is looked up

  • value (String)

    Value that will be looked up through the label.

Returns:

  • (Boolean)


154
155
156
157
# File 'lib/gooddata/models/metadata/metric.rb', line 154

def contain_value?(label, value)
  uri = label.find_value_uri(value)
  contain?(uri)
end

#executeObject



116
117
118
119
120
121
122
123
# File 'lib/gooddata/models/metadata/metric.rb', line 116

def execute
  opts = {
    :client => client,
    :project => project
  }
  res = GoodData::ReportDefinition.execute(opts.merge(:left => self))
  res.data[0][0] if res && !res.empty?
end

#expressionObject



125
126
127
# File 'lib/gooddata/models/metadata/metric.rb', line 125

def expression
  content['expression']
end

#expression=(value) ⇒ Object



129
130
131
# File 'lib/gooddata/models/metadata/metric.rb', line 129

def expression=(value)
  content['expression'] = value
end

#metric?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/gooddata/models/metadata/metric.rb', line 138

def metric?
  true
end

#pretty_expressionString

Looks up the readable values of the objects used inside of MAQL epxpressions. Labels and elements titles are based on the primary label.

Returns:

  • (String)

    Ther resulting MAQL like expression



194
195
196
# File 'lib/gooddata/models/metadata/metric.rb', line 194

def pretty_expression
  SmallGoodZilla.pretty_print(expression, client: client, project: project)
end

#replace(mapping) ⇒ GoodData::Metric

Method used for replacing values in their state according to mapping. Can be used to replace any values but it is typically used to replace the URIs. Returns a new object of the same type.

Parameters:

  • Mapping (Array<Array>)

    specifying what should be exchanged for what. As mapping should be used output of GoodData::Helpers.prepare_mapping.

Returns:



163
164
165
166
167
168
# File 'lib/gooddata/models/metadata/metric.rb', line 163

def replace(mapping)
  x = GoodData::MdObject.replace_quoted(self, mapping)
  x = GoodData::MdObject.replace_bracketed(x, mapping)
  vals = GoodData::MdObject.find_replaceable_values(x, mapping)
  GoodData::MdObject.replace_bracketed(x, vals)
end

#replace_value(label, value, for_label, for_value = nil) ⇒ GoodData::Metric

Method used for replacing attribute element values. Looks up certain value of a label in the MAQL expression and exchanges it for a different value of the same label.

Parameters:

  • label (GoodData::Label)

    Label through which the value and for_value are resolved

  • value (String)

    value that is going to be replaced

  • for_value (String) (defaults to: nil)

    value that is going to be the new one

Returns:



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/gooddata/models/metadata/metric.rb', line 175

def replace_value(label, value, for_label, for_value = nil)
  label = label.respond_to?(:primary_label) ? label.primary_label : label
  if for_value
    for_label = for_label.respond_to?(:primary_label) ? for_label.primary_label : for_label
    value_uri = label.find_value_uri(value)
    for_value_uri = for_label.find_value_uri(for_value)
    self.expression = expression.gsub(value_uri, for_value_uri)
    self.expression = expression.gsub(label.attribute.uri, for_label.attribute.uri)
  else
    for_value = for_label
    value_uri = label.find_value_uri(value)
    for_value_uri = label.find_value_uri(for_value)
    self.expression = expression.gsub(value_uri, for_value_uri)
  end
  self
end

#validateObject



133
134
135
136
# File 'lib/gooddata/models/metadata/metric.rb', line 133

def validate
  fail 'Metric needs to have title' if title.nil?
  true
end