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

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, #initialize, #listed?, #project, #reload!, #remove_tag, replace, #replace!, replace_bracketed, replace_quoted, #save, #save_as, #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

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



21
22
23
# File 'lib/gooddata/models/metadata/report_definition.rb', line 21

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

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



136
137
138
139
140
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
# File 'lib/gooddata/models/metadata/report_definition.rb', line 136

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



43
44
45
46
47
48
49
50
51
# File 'lib/gooddata/models/metadata/report_definition.rb', line 43

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:

Returns:

  • (Array<Hash>)

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



58
59
60
61
62
63
# File 'lib/gooddata/models/metadata/report_definition.rb', line 58

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



34
35
36
37
38
39
# File 'lib/gooddata/models/metadata/report_definition.rb', line 34

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

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



25
26
27
28
29
30
# File 'lib/gooddata/models/metadata/report_definition.rb', line 25

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/gooddata/models/metadata/report_definition.rb', line 65

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



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/gooddata/models/metadata/report_definition.rb', line 119

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



81
82
83
84
85
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
# File 'lib/gooddata/models/metadata/report_definition.rb', line 81

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



180
181
182
183
184
185
# File 'lib/gooddata/models/metadata/report_definition.rb', line 180

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



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

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



203
204
205
# File 'lib/gooddata/models/metadata/report_definition.rb', line 203

def chart?
  !table?
end

#execute(opts = {}) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/gooddata/models/metadata/report_definition.rb', line 221

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

#filtersObject



242
243
244
# File 'lib/gooddata/models/metadata/report_definition.rb', line 242

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

#labelsObject



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

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

#metric_partsObject Also known as: measure_parts



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

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

#metricsObject



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

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:



250
251
252
253
254
255
# File 'lib/gooddata/models/metadata/report_definition.rb', line 250

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)
  GoodData::MdObject.replace_bracketed(x, vals)
end

#reset_color_mapping!GoodData::ReportDefinition

Removes the color mapping from report definition

Returns:



194
195
196
197
198
# File 'lib/gooddata/models/metadata/report_definition.rb', line 194

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



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

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