Class: GoodData::ReportDefinition

Inherits:
MdObject show all
Defined in:
lib/gooddata/models/metadata/report_definition.rb

Overview

Report Definition TODO: Add more doc ...

Constant Summary

Constants inherited from MdObject

MdObject::IDENTIFIERS_CFG, MdObject::MD_OBJ_CTG

Constants included from Mixin::MdIdToUri

Mixin::MdIdToUri::IDENTIFIERS_CFG

Constants included from Mixin::MdObjectIndexer

Mixin::MdObjectIndexer::MD_OBJ_CTG

Constants included from Mixin::MdObjectQuery

Mixin::MdObjectQuery::ERROR_MESSAGE_NO_PROJECT

Instance Attribute Summary

Attributes inherited from GoodData::Rest::Object

#client, #json, #project

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MdObject

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

Methods included from Mixin::MdIdToUri

#identifier_to_uri

Methods included from Mixin::MdObjectIndexer

#[]

Methods included from Mixin::MdObjectQuery

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

Methods included from Mixin::MdFinders

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

Methods included from Mixin::MdObjId

#uri_obj_id

Methods included from Mixin::MdGrantees

#change_permission, #grant, #grantees, #revoke

Methods included from Mixin::MdRelations

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

Methods included from Mixin::ObjId

#obj_id

Methods included from Mixin::Links

#links

Methods inherited from GoodData::Rest::Resource

#initialize

Methods inherited from GoodData::Rest::Object

client, default_client, #initialize, #saved?

Methods included from Mixin::DataPropertyReader

#data_property_reader

Methods included from Mixin::DataPropertyWriter

#data_property_writer

Methods included from Mixin::MetaPropertyReader

#metadata_property_reader

Methods included from Mixin::MetaPropertyWriter

#metadata_property_writer

Methods included from Mixin::MetaGetter

#meta

Methods included from Mixin::DataGetter

#data

Methods included from Mixin::RootKeyGetter

#root_key

Methods included from 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

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.

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

Returns:

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

    Return the appropriate metadata objects or their representation



24
25
26
# File 'lib/gooddata/models/metadata/report_definition.rb', line 24

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

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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/gooddata/models/metadata/report_definition.rb', line 141

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

  left = Array(options[:left])
  top = Array(options[:top])
  filters = options[:filters] || []

  left = ReportDefinition.find(left, options)
  top = ReportDefinition.find(top, options)

  # TODO: Put somewhere for i18n
  fail_msg = 'All metrics in report definition must be saved'
  fail fail_msg unless (left + top).all?(&:saved?)

  pars = {
    'reportDefinition' => {
      'content' => {
        'grid' => {
          'sort' => {
            'columns' => [],
            'rows' => []
          },
          'columnWidths' => [],
          'columns' => ReportDefinition.create_part(top),
          'metrics' => ReportDefinition.create_metrics_part(left, top),
          'rows' => ReportDefinition.create_part(left)
        },
        'format' => 'grid',
        'filters' => ReportDefinition.create_filters_part(filters, :project => project)
      },
      'meta' => {
        'tags' => '',
        'summary' => '',
        'title' => 'Untitled report definition'
      }
    }
  }
  # TODO: write test for report definitions with explicit identifiers
  pars['reportDefinition']['meta']['identifier'] = options[:identifier] if options[:identifier]

  client.create(ReportDefinition, pars, :project => project)
end

.create_attribute_part(attrib) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/gooddata/models/metadata/report_definition.rb', line 46

def create_attribute_part(attrib)
  {
    'attribute' => {
      'alias' => '',
      'totals' => [],
      'uri' => attrib.uri
    }
  }
end

.create_filters_part(filters, options = {}) ⇒ Array<Hash>

Method creates the list of filter representaion suitable for posting on the api. It can currently recognize 2 types of filters. Variable filters and attribute filters. Method for internal usage.

Parameters:

  • filters (GoodData::Variable|Array<Array>)
  • options (Hash) (defaults to: {})

    the options hash

Returns:

  • (Array<Hash>)

    Returns the structure that is stored internally in the report definition and later psted on the API



63
64
65
66
67
68
# File 'lib/gooddata/models/metadata/report_definition.rb', line 63

def create_filters_part(filters, options = {})
  project = options[:project]
  vars = filters.select { |f| f.is_a?(GoodData::Variable) }.map { |v| { expression: "[#{v.uri}]" } }
  category = filters.select { |f| f.is_a?(Array) }.map { |v| GoodData::SmallGoodZilla.create_category_filter(v, project) }
  vars + category
end

.create_metric_part(metric) ⇒ Object Also known as: create_measure_part



37
38
39
40
41
42
# File 'lib/gooddata/models/metadata/report_definition.rb', line 37

def create_metric_part(metric)
  {
    'alias' => metric.title,
    'uri' => metric.uri
  }
end

.create_metrics_part(left, top) ⇒ Object Also known as: create_measures_part



28
29
30
31
32
33
# File 'lib/gooddata/models/metadata/report_definition.rb', line 28

def create_metrics_part(left, top)
  stuff = Array(left) + Array(top)
  stuff.select { |item| item.respond_to?(:metric?) && item.metric? }.map do |metric|
    create_metric_part(metric)
  end
end

.create_part(stuff) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/gooddata/models/metadata/report_definition.rb', line 70

def create_part(stuff)
  stuff = Array(stuff)
  parts = stuff.reduce([]) do |memo, item|
    if item.respond_to?(:metric?) && item.metric?
      memo
    else
      memo << create_attribute_part(item)
    end
    memo
  end
  if stuff.any? { |item| item.respond_to?(:metric?) && item.metric? }
    parts << 'metricGroup'
  end
  parts
end

.execute(options = {}) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/gooddata/models/metadata/report_definition.rb', line 124

def execute(options = {})
  left = Array(options[:left])
  top = Array(options[:top])

  metrics = (left + top).select { |item| item.respond_to?(:metric?) && item.metric? }

  unsaved_metrics = metrics.reject(&:saved?)
  unsaved_metrics.each { |m| m.title = 'Untitled metric' unless m.title }

  begin
    unsaved_metrics.each(&:save)
    GoodData::ReportDefinition.create(options).execute
  ensure
    unsaved_metrics.each { |m| m.delete if m && m.saved? }
  end
end

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



86
87
88
89
90
91
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
118
119
120
121
122
# File 'lib/gooddata/models/metadata/report_definition.rb', line 86

def find(stuff, opts = { :client => GoodData.connection, :project => GoodData.project })
  _client, project = GoodData.get_client_and_project(opts)

  stuff.map do |item|
    obj = if item.is_a?(String)
            begin
              project.objects(item)
            rescue RestClient::ResourceNotFound
              raise "Object given by id \"#{item}\" could not be found"
            end
          elsif item.is_a?(Hash) && item.keys.include?(:title)
            case item[:type].to_s
            when 'metric'
              GoodData::Metric.find_first_by_title(item[:title], opts)
            when 'attribute'
              GoodData::Attribute.find_first_by_title(item[:title], opts)
            end
          elsif item.is_a?(Hash) && (item.keys.include?(:id) || item.keys.include?(:identifier))
            id = item[:id] || item[:identifier]
            case item[:type].to_s
            when 'metric'
              project.metrics(id)
            when 'attribute'
              project.attributes(id)
            when 'label'
              projects.labels(id)
            end
          else
            item
          end
    if obj.respond_to?(:attribute?) && obj.attribute?
      obj.display_forms.first
    else
      obj
    end
  end
end

Instance Method Details

#attribute_partsObject



185
186
187
188
189
190
# File 'lib/gooddata/models/metadata/report_definition.rb', line 185

def attribute_parts
  cols = content['grid']['columns'] || []
  rows = content['grid']['rows'] || []
  items = cols + rows
  items.select { |item| item.is_a?(Hash) && item.keys.first == 'attribute' }
end

#attributesObject



192
193
194
# File 'lib/gooddata/models/metadata/report_definition.rb', line 192

def attributes
  labels.map(&:attribute)
end

#chart?Boolean

Return true if the report definition is a chart

Returns:

  • (Boolean)

    Return true if report definition is a chart



208
209
210
# File 'lib/gooddata/models/metadata/report_definition.rb', line 208

def chart?
  !table?
end

#execute(opts = {}) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/gooddata/models/metadata/report_definition.rb', line 226

def execute(opts = {})
  result = if saved?
             pars = {
               'report_req' => { 'reportDefinition' => uri }
             }
             client.post "/gdc/projects/#{project.pid}/execute", pars
           else
             data = {
               report_req: {
                 definitionContent: {
                   content: to_hash,
                   projectMetadata: project.links['metadata']
                 }
               }
             }
             uri = "/gdc/projects/#{project.pid}/execute"
             client.post(uri, data)
           end
  GoodData::Report.data_result(result, opts.merge(client: client))
end

#filtersObject



247
248
249
# File 'lib/gooddata/models/metadata/report_definition.rb', line 247

def filters
  content['filters'].map { |f| f['expression'] }
end

#labelsObject



212
213
214
# File 'lib/gooddata/models/metadata/report_definition.rb', line 212

def labels
  attribute_parts.map { |part| project.labels(part['attribute']['uri']) }
end

#metric_partsObject Also known as: measure_parts



216
217
218
# File 'lib/gooddata/models/metadata/report_definition.rb', line 216

def metric_parts
  content['grid']['metrics']
end

#metricsObject



222
223
224
# File 'lib/gooddata/models/metadata/report_definition.rb', line 222

def metrics
  metric_parts.map { |i| project.metrics(i['uri']) }
end

#replace(mapping) ⇒ GoodData::ReportDefinition

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:



257
258
259
260
261
262
263
# File 'lib/gooddata/models/metadata/report_definition.rb', line 257

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

#reset_color_mapping!GoodData::ReportDefinition

Removes the color mapping from report definition

Returns:



199
200
201
202
203
# File 'lib/gooddata/models/metadata/report_definition.rb', line 199

def reset_color_mapping!
  global_chart_options = GoodData::Helpers.get_path(content, %w(chart styles global))
  global_chart_options['colorMapping'] = [] if global_chart_options
  self
end

#table?Boolean

Return true if the report definition is a table

Returns:

  • (Boolean)

    Return true if report definition is a table



268
269
270
# File 'lib/gooddata/models/metadata/report_definition.rb', line 268

def table?
  content['format'] == 'grid'
end