Class: GoodData::Report

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

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 included from Mixin::Lockable

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

Methods inherited from MdObject

#==, #add_tag, #browser_uri, #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



20
21
22
# File 'lib/gooddata/models/metadata/report.rb', line 20

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

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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gooddata/models/metadata/report.rb', line 24

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

  title = options[:title]
  fail 'Report needs a title specified' unless title
  summary = options[:summary] || ''

  options_rd = options.dup
  options_rd.delete(:identifier)

  rd = options[:rd] || ReportDefinition.create(options_rd)
  rd.save

  report = {
    'report' => {
      'content' => {
        'domains' => [],
        'definitions' => [rd.uri]
      },
      'meta' => {
        'tags' => '',
        'deprecated' => '0',
        'summary' => summary,
        'title' => title
      }
    }
  }
  # TODO: write test for report definitions with explicit identifiers
  report['report']['meta']['identifier'] = options[:identifier] if options[:identifier]
  client.create(Report, report, :project => project)
end

.data_result(result, options = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/gooddata/models/metadata/report.rb', line 56

def data_result(result, options = {})
  client = options[:client]
  data_result_uri = result['execResult']['dataResult']
  begin
    result = client.poll_on_response(data_result_uri, options) do |body|
      body && body['taskState'] && body['taskState']['status'] == 'WAIT'
    end
  rescue RestClient::BadRequest => e
    resp = JSON.parse(e.response)
    if GoodData::Helpers.get_path(resp, %w(error component)) == 'MD::DataResult'
      raise GoodData::UncomputableReport
    else
      raise e
    end
  end

  if result.empty?
    ReportDataResult.new(data: [], top: 0, left: 0)
  else
    ReportDataResult.from_xtab(result)
  end
end

Instance Method Details

#add_definition(report_definition) ⇒ GoodData::Report

Add a report definition to a report. This will show on a UI as a new version.

Parameters:

  • report_definition (GoodData::ReportDefinition | String)

    Report definition to add. Either it can be a URI of a report definition or an actual report definition object.

Returns:



84
85
86
87
88
# File 'lib/gooddata/models/metadata/report.rb', line 84

def add_definition(report_definition)
  rep_def = project.report_definitions(report_definition)
  content['definitions'] = definition_uris << rep_def.uri
  self
end

#add_definition!(report_definition) ⇒ GoodData::Report

Add a report definition to a report. This will show on a UI as a new version.

Parameters:

  • report_definition (GoodData::ReportDefinition | String)

    Report definition to add. Either it can be a URI of a report definition or an actual report definition object.

Returns:



94
95
96
97
# File 'lib/gooddata/models/metadata/report.rb', line 94

def add_definition!(report_definition)
  res = add_definition(report_definition)
  res.save
end

#definitionGoodData::ReportDefinition Also known as: latest_report_definition

Returns the newest (current version) report definition as an object

Returns:



102
103
104
# File 'lib/gooddata/models/metadata/report.rb', line 102

def definition
  project.report_definitions(latest_report_definition_uri)
end

#definition_uriString Also known as: latest_report_definition_uri

Returns the newest (current version) report definition uri

Returns:

  • (String)

    Returns uri of the newest report defintion



111
112
113
# File 'lib/gooddata/models/metadata/report.rb', line 111

def definition_uri
  definition_uris.last
end

#definition_urisArray<String>

Gets list of uris of report definitions (versions) of this report.

Returns:

  • (Array<String>)

    Returns list of report definitions' uris. Oldest comes first



128
129
130
# File 'lib/gooddata/models/metadata/report.rb', line 128

def definition_uris
  content['definitions']
end

#definitionsArray<GoodData::ReportDefinition> Also known as: report_definitions

Gets a report definitions (versions) of this report as objects.

Returns:



120
121
122
# File 'lib/gooddata/models/metadata/report.rb', line 120

def definitions
  content['definitions'].pmap { |uri| project.report_definitions(uri) }
end

#deleteGoodData::Report

Deletes report along with its report definitions.

Returns:



135
136
137
138
139
140
# File 'lib/gooddata/models/metadata/report.rb', line 135

def delete
  defs = definitions
  super
  defs.peach(&:delete)
  self
end

#execute(options = {}) ⇒ GoodData::DataResult

Computes the report and returns the result. If it is not computable returns nil.

Returns:

  • (GoodData::DataResult)

    Returns the result



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

def execute(options = {})
  fail 'You have to save the report before executing. If you do not want to do that please use GoodData::ReportDefinition' unless saved?
  result = client.post '/gdc/xtab2/executor3', 'report_req' => { 'report' => uri }
  GoodData::Report.data_result(result, options.merge(client: client))
end

#export(format, options = {}) ⇒ String

Returns binary data of the exported report in a given format. The format can be either 'csv', 'xls', 'xlsx' or 'pdf'.

Returns:



162
163
164
165
166
# File 'lib/gooddata/models/metadata/report.rb', line 162

def export(format, options = {})
  result = client.post('/gdc/xtab2/executor3', 'report_req' => { 'report' => uri })
  result1 = client.post('/gdc/exporter/executor', :result_req => { :format => format, :result => result })
  client.poll_on_code(result1['uri'], options.merge(process: false))
end

#exportable?Boolean

Returns true if you can export and object

Returns:

  • (Boolean)

    Returns whether the report is exportable



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

def exportable?
  true
end

#purge_report_of_unused_definitions!String

Returns the newest (current version) report definition uri

Returns:

  • (String)

    Returns uri of the newest report defintion



171
172
173
174
175
176
177
178
179
# File 'lib/gooddata/models/metadata/report.rb', line 171

def purge_report_of_unused_definitions!
  full_list = definition_uris
  remove_definition_but_latest
  purged_list = definition_uris
  to_remove = full_list - purged_list
  save
  to_remove.each { |uri| client.delete(uri) }
  self
end

#remove_definition(definition) ⇒ GoodData::Report

Removes definition from the report. The definition to remove can be passed in any form that is accepted by GoodData::ReportDefintion[]

Parameters:

Returns:



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

def remove_definition(definition)
  a_def = GoodData::ReportDefinition[definition, project: project, client: client]
  def_uri = a_def.uri
  content['definitions'] = definition_uris.reject { |x| x == def_uri }
  self
end

#remove_definition_but_latestGoodData::Report

TODO: Cover with test. You would probably need something that will be able to create a report easily from a definition Removes all definitions but the latest from the report. This is useful for cleaning up before you create a template out of a project.

Returns:



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

def remove_definition_but_latest
  to_remove = definition_uris - [latest_report_definition_uri]
  to_remove.each do |uri|
    remove_definition(uri)
  end
  self
end

#replace(mapping) ⇒ GoodData::Report

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:



210
211
212
213
214
215
216
# File 'lib/gooddata/models/metadata/report.rb', line 210

def replace(mapping)
  new_defs = definitions.map do |rep_def|
    rep_def.replace(mapping)
  end
  new_defs.pmap(&:save)
  self
end

#update_definition(opts = { :new_definition => true }, &block) ⇒ GoodData::ReportDefinition

Update report definition and reflect the change in report

Parameters:

  • opts (Hash) (defaults to: { :new_definition => true })

    Options

Options Hash (opts):

  • :new_definition (Boolean) — default: true

    If true then new definition will be created

Returns:



223
224
225
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.rb', line 223

def update_definition(opts = { :new_definition => true }, &block)
  # TODO: Cache the latest report definition somehow
  repdef = definition.dup

  block.call(repdef, self) if block_given?

  if opts[:new_definition]
    new_def = GoodData::ReportDefinition.create(:client => client, :project => project)

    rd = repdef.json['reportDefinition']
    rd.delete('links')
    %w(author uri created identifier updated contributor).each { |k| rd['meta'].delete(k) }
    new_def.json['reportDefinition'] = rd
    new_def.save

    add_definition!(new_def)
    return new_def
  else
    repdef.save
  end

  repdef
end