Class: GoodData::Report
- Inherits:
-
MdObject
- Object
- GoodData::Rest::Object
- GoodData::Rest::Resource
- MdObject
- GoodData::Report
- 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
Constants included from Mixin::MdObjectQuery
Mixin::MdObjectQuery::ERROR_MESSAGE_NO_PROJECT
Instance Attribute Summary
Attributes inherited from GoodData::Rest::Object
Class Method Summary collapse
-
.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.
- .create(options = { :client => GoodData.connection, :project => GoodData.project }) ⇒ Object
- .data_result(result, options = {}) ⇒ Object
Instance Method Summary collapse
-
#add_definition(report_definition) ⇒ GoodData::Report
Add a report definition to a report.
-
#add_definition!(report_definition) ⇒ GoodData::Report
Add a report definition to a report.
-
#definition ⇒ GoodData::ReportDefinition
(also: #latest_report_definition)
Returns the newest (current version) report definition as an object.
-
#definition_uri ⇒ String
(also: #latest_report_definition_uri)
Returns the newest (current version) report definition uri.
-
#definition_uris ⇒ Array<String>
Gets list of uris of report definitions (versions) of this report.
-
#definitions ⇒ Array<GoodData::ReportDefinition>
(also: #report_definitions)
Gets a report definitions (versions) of this report as objects.
-
#delete ⇒ GoodData::Report
Deletes report along with its report definitions.
-
#execute(options = {}) ⇒ GoodData::DataResult
Computes the report and returns the result.
-
#export(format, options = {}) ⇒ String
Returns binary data of the exported report in a given format.
-
#export_raw(filename) ⇒ Net::HTTPResponse
Exports a report too large to be computed on the UI.
-
#exportable? ⇒ Boolean
Returns true if you can export and object.
-
#purge_report_of_unused_definitions! ⇒ String
Returns the newest (current version) report definition uri.
-
#remove_definition(definition) ⇒ GoodData::Report
Removes definition from the report.
-
#remove_definition_but_latest ⇒ GoodData::Report
TODO: Cover with test.
-
#replace(mapping) ⇒ GoodData::Report
Method used for replacing values in their state according to mapping.
-
#update_definition(opts = { :new_definition => true }, &block) ⇒ GoodData::ReportDefinition
Update report definition and reflect the change in report.
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, #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
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
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
Methods included from Mixin::Links
Methods inherited from GoodData::Rest::Resource
Methods inherited from GoodData::Rest::Object
client, default_client, #initialize, #saved?
Methods included from Mixin::DataPropertyReader
Methods included from Mixin::DataPropertyWriter
Methods included from Mixin::MetaPropertyReader
Methods included from Mixin::MetaPropertyWriter
Methods included from Mixin::MetaGetter
Methods included from Mixin::DataGetter
Methods included from Mixin::RootKeyGetter
Methods included from Mixin::ContentGetter
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.
23 24 25 |
# File 'lib/gooddata/models/metadata/report.rb', line 23 def all( = { :client => GoodData.connection, :project => GoodData.project }) query('report', Report, ) end |
.create(options = { :client => GoodData.connection, :project => GoodData.project }) ⇒ Object
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 55 56 57 |
# File 'lib/gooddata/models/metadata/report.rb', line 27 def create( = { :client => GoodData.connection, :project => GoodData.project }) client, project = GoodData.get_client_and_project() title = [:title] fail 'Report needs a title specified' unless title summary = [:summary] || '' = .dup .delete(:identifier) rd = [:rd] || ReportDefinition.create() 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'] = [:identifier] if [:identifier] client.create(Report, report, :project => project) end |
.data_result(result, options = {}) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/gooddata/models/metadata/report.rb', line 59 def data_result(result, = {}) client = [:client] data_result_uri = result['execResult']['dataResult'] begin result = client.poll_on_response(data_result_uri, ) 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.to_s.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.
87 88 89 90 91 |
# File 'lib/gooddata/models/metadata/report.rb', line 87 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.
97 98 99 100 |
# File 'lib/gooddata/models/metadata/report.rb', line 97 def add_definition!(report_definition) res = add_definition(report_definition) res.save end |
#definition ⇒ GoodData::ReportDefinition Also known as: latest_report_definition
Returns the newest (current version) report definition as an object
105 106 107 |
# File 'lib/gooddata/models/metadata/report.rb', line 105 def definition project.report_definitions(latest_report_definition_uri) end |
#definition_uri ⇒ String Also known as: latest_report_definition_uri
Returns the newest (current version) report definition uri
114 115 116 |
# File 'lib/gooddata/models/metadata/report.rb', line 114 def definition_uri definition_uris.last end |
#definition_uris ⇒ Array<String>
Gets list of uris of report definitions (versions) of this report.
131 132 133 |
# File 'lib/gooddata/models/metadata/report.rb', line 131 def definition_uris content['definitions'] end |
#definitions ⇒ Array<GoodData::ReportDefinition> Also known as: report_definitions
Gets a report definitions (versions) of this report as objects.
123 124 125 |
# File 'lib/gooddata/models/metadata/report.rb', line 123 def definitions content['definitions'].pmap { |uri| project.report_definitions(uri) } end |
#delete ⇒ GoodData::Report
Deletes report along with its report definitions.
138 139 140 141 142 143 |
# File 'lib/gooddata/models/metadata/report.rb', line 138 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.
149 150 151 152 153 154 155 156 157 158 |
# File 'lib/gooddata/models/metadata/report.rb', line 149 def execute( = {}) time = [:time] report_req = { 'report' => uri } report_req['timestamp'] = time.to_i if time 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/projects/#{project.pid}/execute", 'report_req' => report_req GoodData::Report.data_result(result, .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'.
171 172 173 174 175 |
# File 'lib/gooddata/models/metadata/report.rb', line 171 def export(format, = {}) result = client.post("/gdc/projects/#{project.pid}/execute", 'report_req' => { 'report' => uri }) result1 = client.post('/gdc/exporter/executor', :result_req => { :format => format, :result => result }) client.poll_on_code(result1['uri'], .merge(process: false)) end |
#export_raw(filename) ⇒ Net::HTTPResponse
Exports a report too large to be computed on the UI. It executes in raw form. Saves the result into file.
182 183 184 185 |
# File 'lib/gooddata/models/metadata/report.rb', line 182 def export_raw(filename) result = client.post("/gdc/app/projects/#{project.pid}/execute/raw", 'report_req' => { 'report' => uri }) client.download(result['uri'], filename, url_encode: false) end |
#exportable? ⇒ Boolean
Returns true if you can export and object
163 164 165 |
# File 'lib/gooddata/models/metadata/report.rb', line 163 def exportable? true end |
#purge_report_of_unused_definitions! ⇒ String
Returns the newest (current version) report definition uri
190 191 192 193 194 195 196 197 198 |
# File 'lib/gooddata/models/metadata/report.rb', line 190 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[]
205 206 207 208 209 210 |
# File 'lib/gooddata/models/metadata/report.rb', line 205 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_latest ⇒ GoodData::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.
217 218 219 220 221 222 223 |
# File 'lib/gooddata/models/metadata/report.rb', line 217 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.
231 232 233 234 235 236 237 |
# File 'lib/gooddata/models/metadata/report.rb', line 231 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
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/gooddata/models/metadata/report.rb', line 244 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 |