Class: GoodData::ReportDataResult

Inherits:
GoodData::Rest::Resource show all
Defined in:
lib/gooddata/models/report_data_result.rb

Instance Attribute Summary

Attributes inherited from GoodData::Rest::Object

#client, #json, #project

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::ObjId

#obj_id

Methods inherited from GoodData::Rest::Object

client, default_client, #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::RootKeyGetter

#root_key

Methods included from Mixin::ContentGetter

#content

Constructor Details

#initialize(opts) ⇒ GoodData::ReportDataResult

Returns

Options Hash (opts):

  • :data (Array<Array>)

    The data as a matrix. First rows then cols

  • :top (Number)

    Number of rows that are representing the top header

  • :left (Number)

    Number of cols that are representing the left header



73
74
75
76
77
# File 'lib/gooddata/models/report_data_result.rb', line 73

def initialize(opts)
  @data = opts[:data]
  @top_headers_rows_nums = opts[:top]
  @left_headers_cols_nums = opts[:left]
end

Class Method Details

.from_xtab(data) ⇒ GoodData::ReportDataResult

Does all the needed parsing on the apyload coming from the API and returns an instance of ReportDataResult



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gooddata/models/report_data_result.rb', line 14

def from_xtab(data)
  top = top_headers(data)
  left = left_headers(data)
  jank = GoodData::Helpers.zeroes(rows(top), cols(left), nil)
  d = data(data)
  stuff = d.empty? ? GoodData::Helpers.zeroes(rows(left), cols(top), nil) : d

  a = jank.zip(top).map { |x, y| x + y }
  b = left.zip(stuff).map { |x, y| x + y }
  result = a + b
  ReportDataResult.new(data: result, top: rows(top), left: cols(left))
end

Instance Method Details

#-(other) ⇒ Array<Array>

Implements subtraction. Works only on reports that have same number of columns. Gives you columns that are not in other.



251
252
253
254
255
256
# File 'lib/gooddata/models/report_data_result.rb', line 251

def -(other)
  fail 'Seems you are not using a data result as a parameter' unless other.respond_to?(:size)
  message = 'Results do not have compatible sizes. Subtracting the dataresults works row wise so you have to have the same number of columns'
  fail message if size.last != other.size.last
  to_a - other.to_a
end

#==(other) ⇒ Object



238
239
240
241
# File 'lib/gooddata/models/report_data_result.rb', line 238

def ==(other)
  return false if size != other.size
  @data == other.to_a
end

#[](index) ⇒ Array Also known as: row

Allows to pick particular row inside the report result



161
162
163
# File 'lib/gooddata/models/report_data_result.rb', line 161

def [](index)
  @data[index]
end

#column(index) ⇒ Array

Allows to pick particular column inside the report result



169
170
171
# File 'lib/gooddata/models/report_data_result.rb', line 169

def column(index)
  transpose[index]
end

#dataArray

Gives you data as a new ReportDataResult



114
115
116
# File 'lib/gooddata/models/report_data_result.rb', line 114

def data
  slice(@top_headers_rows_nums, @left_headers_cols_nums)
end

#data_sizeArray<Number>

Returns the size of the the data portion of report



234
235
236
# File 'lib/gooddata/models/report_data_result.rb', line 234

def data_size
  data.size
end

#diff(other) ⇒ Hash

Implements diff. Works only on reports that have same number of columns (because it uses #- behind the scene).



262
263
264
265
266
267
268
# File 'lib/gooddata/models/report_data_result.rb', line 262

def diff(other)
  {
    added: other - self,
    removed: self - other,
    same: @data & other.to_a
  }
end

#eachObject Also known as: each_line, each_row



118
119
120
# File 'lib/gooddata/models/report_data_result.rb', line 118

def each
  to_a.each
end

#each_columnObject



124
125
126
127
128
129
# File 'lib/gooddata/models/report_data_result.rb', line 124

def each_column
  size.last.times.map do |i|
    col = map { |row| row[i] }
    yield(col)
  end
end

#empty?Array

Is the report without any data? This can be caused by the fact that the filters are too restrictive or data are not loaded in



176
177
178
179
# File 'lib/gooddata/models/report_data_result.rb', line 176

def empty?
  row, cols = size
  row.zero? && cols.zero?
end

#eq?(other) ⇒ Boolean



243
244
245
# File 'lib/gooddata/models/report_data_result.rb', line 243

def eq?(other)
  self == other
end

#include_column?(col) ⇒ Array

Allows you to test if a report contains a column.

It is looking for the whole row. If the headers are getting in the way use #without_left_headers or #without_top_headers



197
198
199
# File 'lib/gooddata/models/report_data_result.rb', line 197

def include_column?(col)
  transpose.include_row?(col)
end

#include_row?(row) ⇒ Array

Allows you to test if a report contains a row.

It is looking for the whole row. If the headers are getting in the way use #without_left_headers or #without_top_headers



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

def include_row?(row)
  @data.include?(row)
end

#left_headersArray

Gives you left headers as an Array



96
97
98
99
100
# File 'lib/gooddata/models/report_data_result.rb', line 96

def left_headers
  return nil if @left_headers_cols_nums.zero?
  top = @left_headers_cols_nums - 1
  without_top_headers.slice(0, [0, top]).to_a
end

#sizeArray<Number>

Returns the size of the report



204
205
206
# File 'lib/gooddata/models/report_data_result.rb', line 204

def size
  [@data.size, @data.empty? ? 0 : @data.first.size]
end

#slice(rows, cols) ⇒ GoodData::ReportDataResult

Gives you report result with a subset of data starting at position rows, cols



220
221
222
223
224
225
226
227
228
229
# File 'lib/gooddata/models/report_data_result.rb', line 220

def slice(rows, cols)
  rows = rows.is_a?(Enumerable) ? rows : [rows, size.first]
  cols = cols.is_a?(Enumerable) ? cols : [cols, size.last]
  new_data = @data[rows.first..rows.last].map { |col| col[cols.first..cols.last] }
  if client
    client.create(ReportDataResult, data: new_data, top: @top_headers_rows_nums - rows.first, left: @left_headers_cols_nums - cols.first, project: project)
  else
    ReportDataResult.new(data: new_data, top: @top_headers_rows_nums - rows.first, left: @left_headers_cols_nums - cols.first, project: project)
  end
end

#to_aArray Also known as: to_table

Gives you data as a new ReportDataResult



134
135
136
# File 'lib/gooddata/models/report_data_result.rb', line 134

def to_a
  @data
end

#to_s(options = {}) ⇒ String

Gives report as a table suitable for printing out



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/gooddata/models/report_data_result.rb', line 142

def to_s(options = {})
  with_indices = options[:index]

  a = to_table.to_a
  data = a.transpose
  data.unshift((1..a.length).to_a) if with_indices
  processed_data = data.each_with_index.map do |col, i|
    col.unshift(i.zero? ? nil : i) if with_indices # inserts row labels #
    w = col.map { |cell| cell.to_s.length }.max # w = "column width" #
    col.each_with_index.map do |cell, j|
      j.zero? ? cell.to_s.center(w) : cell.to_s.ljust(w)
    end # alligns the column #
  end
  processed_data.transpose.map { |row| "[#{row.join(' | ')}]" }.unshift('').join("\n")
end

#top_headersArray

Gives you right headers as an Array



105
106
107
108
109
# File 'lib/gooddata/models/report_data_result.rb', line 105

def top_headers
  return nil if @top_headers_rows_nums.zero?
  top = @top_headers_rows_nums - 1
  without_left_headers.slice([0, top], 0).to_a
end

#transposeGoodData::ReportDataResult

Transposes data and returns as new data result



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

def transpose
  ReportDataResult.new(data: to_a.transpose, top: @left_headers_cols_nums, left: @top_headers_rows_nums)
end

#without_left_headersGoodData::ReportDataResult

Gives you new report result with left headers removed



89
90
91
# File 'lib/gooddata/models/report_data_result.rb', line 89

def without_left_headers
  slice(0, @left_headers_cols_nums)
end

#without_top_headersGoodData::ReportDataResult

Gives you new report result with top headers removed



82
83
84
# File 'lib/gooddata/models/report_data_result.rb', line 82

def without_top_headers
  slice(@top_headers_rows_nums, 0)
end