class GoodData::Model::DatasetBlueprint
Public Class Methods
Returns anchor of a dataset
@param dataset [Hash] Dataset
blueprint @return [Hash] returns the anchor or nil
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 25 def self.anchor(dataset) find_column_by_type(dataset, :anchor) end
Checks if a dataset has an anchor.
@param dataset [Hash] Dataset
blueprint @return [Boolean] returns true if dataset has an anchor
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 17 def self.anchor?(dataset) columns(dataset).any? { |c| c[:type].to_s == 'anchor' } end
Returns all labels that is referenced by a label
@param dataset [Hash] Dataset
blueprint @return [Array<Hash>] returns the labels or an empty array
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 49 def self.attribute_for_label(dataset, label) find_columns_by_type(dataset, :attribute, :anchor).find { |a| label[:reference] == a[:id] } end
Returns attributes of a dataset
@param dataset [Hash] Dataset
blueprint @return [Array<Hash>] returns the attribute or an empty array
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 33 def self.attributes(dataset) find_columns_by_type(dataset, :attribute, :all) end
Returns attributes and anchor defined on a dataset
@param dataset [Hash] Dataset
blueprint @return [Array<Hash>] returns the attributes
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 41 def self.attributes_and_anchors(dataset) [anchor(dataset)] + attributes(dataset) end
Returns bridges of a dataset
@param dataset [Hash] Dataset
blueprint @return [Array<Hash>] returns the bridges or an empty array
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 173 def self.bridges(dataset) find_columns_by_type(dataset, :bridge) end
Returns all the fields of a dataset. This means facts, attributes, references
@param ds [Hash] Dataset
blueprint @return [Boolean]
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 57 def self.columns(ds) (ds.to_hash[:columns] || []) end
Tells you if the object is a dataset. It consumes both Hash represenation or the GoodData::Model::DatasetBlueprint
@param ds [Object] Value to be tested @return [Boolean]
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 67 def self.dataset_blueprint?(ds) if ds.is_a?(DatasetBlueprint) true elsif ds.respond_to?(:[]) && ds.is_a?(Hash) && ds[:type].to_sym == :dataset true else false end end
Returns date facts of a dataset
@param dataset [Hash] Dataset
blueprint @return [Array<Hash>] returns the attribute or an empty array
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 81 def self.date_facts(dataset) find_column_by_type(dataset, :date_fact) end
Returns label that is marked as default for a particular attribtue. This does not necessarily need to be the first one. This is a default label in terms of what is displayed on the UI
@param dataset [Hash] Dataset
blueprint @return [Array<Hash>] returns the labels or an empty array
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 91 def self.default_label_for_attribute(dataset, attribute) default_label = labels_for_attribute(dataset, attribute).find { |l| l[:default_label] == true } default_label end
Returns facts of a dataset
@param dataset [Hash] Dataset
blueprint @return [Array<Hash>] returns the attribute or an empty array
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 100 def self.facts(dataset) find_columns_by_type(dataset, :fact, :date_fact, :hll) end
Finds a specific column given a name
@param dataset [Hash] Dataset
blueprint @param name [String] Name of a field @param all [Symbol] if :all is passed all mathching objects are returned Otherwise only the first one is @return [Array<Hash>] matching fields
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 111 def self.find_column_by_id(dataset, name, all = nil) if all == :all columns(dataset).select { |c| c[:id].to_s == name } else columns(dataset).find { |c| c[:id].to_s == name } end end
Returns first field of a specified type.
@param dataset [Hash | GoodData::Model::ProjectBlueprint
] Dataset
blueprint @param types [String | Symbol | Array | Array] Type or types you would like to get as third parameter it return all object otherwise it returns the first one @return [Array<Hash>] matching fields
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 125 def self.find_column_by_type(dataset, *types) columns(dataset).find { |c| types.any? { |t| t.to_s == c[:type].to_s } } end
Returns all the fields of a specified type. You can specify more types if you need more than one type.
@param dataset [Hash | GoodData::Model::ProjectBlueprint
] Dataset
blueprint @param types [String | Symbol | Array | Array] Type or types you would like to get @return [Array<Hash>] matching fields
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 135 def self.find_columns_by_type(dataset, *types) columns(dataset).select { |c| types.any? { |t| t.to_s == c[:type].to_s } } end
Returns labels facts of a dataset
@param dataset [Hash] Dataset
blueprint @return [Array<Hash>] returns the label or an empty array
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 143 def self.labels(dataset) find_columns_by_type(dataset, :label) end
Returns labels for a particular attribute
@param dataset [Hash] Dataset
blueprint @param attribute [Hash] Attribute
@return [Array<Hash>] returns the labels or an empty array
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 157 def self.labels_for_attribute(dataset, attribute) labels(dataset).select { |l| l[:reference] == attribute[:id] } end
Creates a DatasetBlueprint
@param dataset [Hash] Dataset
blueprint @return [DatasetBlueprint] returns the labels or an empty array
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 318 def initialize(init_data, blueprint) super @data[:type] = @data.key?('type') ? @data['type'].to_sym : @data[:type] @data[:columns].each do |c| c[:type] = c[:type].to_sym end end
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 147 def self.reference_label_for_attribtue(dataset, attribute) labels = labels_for_attribute(dataset, attribute) labels.find { |label| label[:reference_label] == true } || labels.first end
Returns references of a dataset
@param dataset [Hash] Dataset
blueprint @return [Array<Hash>] returns the references or an empty array
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 165 def self.references(dataset) find_columns_by_type(dataset, :reference, :date) end
Public Instance Methods
Returns anchor of a dataset
@return [Hash] returns the anchor or nil
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 180 def anchor find_column_by_type(:anchor) end
Checks if a dataset has an anchor.
@return [Boolean] returns true if dataset has an anchor
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 187 def anchor? columns.any? { |c| c.type == :anchor } end
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 336 def attribute_for_label(label) l = labels(label) attributes_and_anchors.find { |a| a.id == l.reference } end
Returns attributes of a dataset
@return [Array<Hash>] returns the attribute or an empty array
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 194 def attributes(id = :all) return id if id.is_a?(AttributeBlueprintField) ats = find_columns_by_type(:attribute) id == :all ? ats : ats.find { |a| a.id == id } end
Returns attributes and anchor defined on a dataset
@return [Array<GoodData::Model::DatasetBlueprint>] returns the attributes
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 203 def attributes_and_anchors attributes + [anchor] end
Returns bridges of a dataset
@return [Array<Hash>] returns the bridges or an empty array
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 368 def bridges find_columns_by_type(:bridge) end
Changes the dataset through a builder. You provide a block and an istance of GoodData::Model::SchemaBuilder
is passed in as the only parameter
@return [GoodData::Model::SchemaBlueprint] returns changed dataset blueprint
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 211 def change(&block) builder = SchemaBuilder.create_from_data(self) block.call(builder) @data = builder.to_hash self end
Returns all the fields of a dataset. This means anchor, facts, attributes, references This method will cast them to correct types
@return [Boolean]
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 222 def columns DatasetBlueprint.columns(to_hash).map do |c| case c[:type].to_sym when :anchor GoodData::Model::AnchorBlueprintField.new(c, self) when :attribute GoodData::Model::AttributeBlueprintField.new(c, self) when :fact GoodData::Model::FactBlueprintField.new(c, self) when :label GoodData::Model::LabelBlueprintField.new(c, self) when :bridge GoodData::Model::BridgeBlueprintField.new(c, self) when :reference GoodData::Model::ReferenceBlueprintField.new(c, self) when :date GoodData::Model::ReferenceBlueprintField.new(c, self) else GoodData::Model::BlueprintField.new(c, self) end end end
Creates a metric which counts numnber of lines in dataset. Works for both datasets with or without anchor
@return [Boolean]
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 250 def count(project) anchor.in_project(project).create_metric.execute end
Returns date facts of a dataset
@return [Array<Hash>] returns the attribute or an empty array
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 257 def date_facts find_columns_by_type(:date_fact) end
Duplicates the DatasetBlueprint
. It is done as a deep duplicate
@return [GoodData::Model::DatasetBlueprint] matching fields
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 264 def dup DatasetBlueprint.new(GoodData::Helpers.deep_dup(data), project_blueprint) end
Returns facts of a dataset
@return [Array<Hash>] returns the attribute or an empty array
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 271 def facts(id = :all) return id if id.is_a?(FactBlueprintField) fs = find_columns_by_type(:fact) id == :all ? fs : fs.find { |a| a.id == id } end
Finds a specific column given a col
@param col [GoodData::Model::BlueprintField | Hash] Field @return [GoodData::Model::BlueprintField] matching fields
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 281 def find_column(col) columns.find { |c| c == col } end
Finds a specific column given an id
@param id [String] Id of a field @param all [Symbol] if :all is passed all mathching objects are returned Otherwise only the first one is @return [Array<Hash>] matching fields
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 291 def find_column_by_id(id) id = id.respond_to?(:id) ? id.id : id columns.find { |c| c.id == id } end
Returns first field of a specified type.
@param type [String | Symbol | Array | Array] Type or types you would like to get @return [GoodData::Model::BlueprintField] returns matching field
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 300 def find_column_by_type(*types) columns.find { |c| types.any? { |t| t.downcase.to_sym == c.type } } end
Returns all the fields of a specified type. You can specify more types as an array if you need more than one type.
@param type [String | Symbol | Array | Array] Type or types you would like to get as third parameter it return all object otherwise it returns the first one @return [Array<GoodData::Model::BlueprintField>] matching fields
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 310 def find_columns_by_type(*types) columns.select { |c| types.any? { |t| t.downcase.to_sym == c.type } } end
Returns labels facts of a dataset
@param dataset [Hash] Dataset
blueprint @return [Array<Hash>] returns the label or an empty array
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 330 def labels(id = :all) return id if id.is_a?(LabelBlueprintField) labs = find_columns_by_type(:label) id == :all ? labs : labs.find { |l| l.id == id } end
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 341 def labels_for_attribute(attribute) a = attributes(attribute) labels.select { |l| l.reference == a.id } end
Merges two schemas together. This method changes the blueprint in place. If you would prefer the method that generates a new blueprint use merge method
@param a_blueprint [GoodData::Model::DatasetBlueprint] Dataset
blueprint to be merged @return [GoodData::Model::DatasetBlueprint] returns itself changed
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 352 def merge!(a_blueprint) new_blueprint = GoodData::Model.merge_dataset_columns(self, a_blueprint) @data = new_blueprint self end
Returns references of a dataset
@return [Array<Hash>] returns the references or an empty array
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 361 def references find_columns_by_type(:reference, :date) end
Removes column from from the blueprint
@param id [String] Id of the column to be removed @return [GoodData::Model::ProjectBlueprint] Returns changed blueprint
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 376 def remove_column!(id) @project_blueprint.remove_column!(self, id) end
Removes all the labels from the anchor. This is a typical operation that people want to perform
@return [GoodData::Model::ProjectBlueprint] Returns changed blueprint
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 383 def strip_anchor! @project_blueprint.strip_anchor!(self) end
Method for suggest a couple of metrics that might get you started Idea is that we will provide couple of strategies. Currently the metrics are created in the random way but they should work.
@return [Array<GoodData::Metric>] matching fields
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 392 def suggest_metrics identifiers = facts.map { |f| identifier_for(f) } identifiers.zip(facts).map do |id, fact| Metric.xcreate( :title => GoodData::Helpers.titleize(fact[:name]), :expression => "SELECT SUM(![#{id}])" ) end end
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 404 def to_blueprint GoodData::Model::ProjectBlueprint.new(datasets: [to_hash]) end
Validate the blueprint return array of errors that are found.
@return [Array] array of errors
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 411 def validate errors = [] errors.concat(validate_more_anchors) errors.concat(validate_some_anchors) errors.concat(validate_label_references) errors.concat(validate_gd_data_type_errors) errors.concat(fields.flat_map(&:validate)) errors.concat(validate_attribute_has_one_label) errors end
Validate if the attribute does have at least one label
@return [Array] array of errors
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 439 def validate_attribute_has_one_label find_columns_by_type(:attribute) .select { |a| a.labels.empty? } .map { |e| { type: :attribute_without_label, attribute: e.id } } end
Validate the the used gd_data_types are one of the allowed types. The data types are checked on lables and facts.
@return [Array] array of errors
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 456 def validate_gd_data_type_errors (labels + facts) .select { |x| x.gd_data_type && !GoodData::Model.check_gd_data_type(x.gd_data_type) } .map { |e| { :error => :invalid_gd_data_type_specified, :column => e.id } } end
Validate the that any labels are pointing to the existing attribute. If not returns the list of errors. Currently just violating labels.
@return [Array] array of errors
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 448 def validate_label_references labels.select { |r| r.attribute.nil? } .map { |er_ref| { type: :wrong_label_reference, label: er_ref.id, wrong_reference: er_ref.data[:reference] } } end
Validate if the dataset does not have more than one anchor defined.
@return [Array] array of errors
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 432 def validate_more_anchors find_columns_by_type(:anchor).count > 1 ? [{ type: :more_than_on_anchor, dataset: id }] : [] end
Validate if the dataset has more than zero anchors defined.
@return [Array] array of errors
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 425 def validate_some_anchors find_columns_by_type(:anchor).count.zero? ? [{ type: :no_anchor, dataset: id }] : [] end
Helper methods to decide wheather the dataset is considered wide. Currently the wider datasets have both performance and usability penalty
@return [Boolean] matching fields
# File lib/gooddata/models/blueprint/dataset_blueprint.rb, line 467 def wide? fields.count > 32 end