Class: Etna::Clients::Magma::WalkModelTreeWorkflow

Inherits:
Struct
  • Object
show all
Defined in:
lib/etna/clients/magma/workflows/walk_model_tree_workflow.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loggerObject

Returns the value of attribute logger

Returns:

  • (Object)

    the current value of logger



9
10
11
# File 'lib/etna/clients/magma/workflows/walk_model_tree_workflow.rb', line 9

def logger
  @logger
end

#magma_crudObject

Returns the value of attribute magma_crud

Returns:

  • (Object)

    the current value of magma_crud



9
10
11
# File 'lib/etna/clients/magma/workflows/walk_model_tree_workflow.rb', line 9

def magma_crud
  @magma_crud
end

Instance Method Details

#walk_from(model_name, record_names = 'all', model_attributes_mask: {}, model_filters: {}, page_size: 100, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/etna/clients/magma/workflows/walk_model_tree_workflow.rb', line 10

def walk_from(
    model_name,
    record_names = 'all',
    model_attributes_mask: {},
    model_filters: {},
    page_size: 100,
    &block)
  q = [ { model_name: model_name, from: nil, record_names: record_names } ]
  seen = Set.new

  while (path = q.pop)
    model_name = path[:model_name]
    next if seen.include?([path[:from], model_name])
    seen.add([path[:from], model_name])

    request = RetrievalRequest.new(
        project_name: magma_crud.project_name,
        model_name: model_name,
        record_names: path[:record_names],
        filter: model_filters[model_name],
        page_size: page_size, page: 1
    )

    related_models = {}

    magma_crud.page_records(model_name, request) do |response|
      model = response.models.model(model_name)
      template = model.template

      tables = []
      collections = []
      links = []
      attributes = []

      template.attributes.attribute_keys.each do |attr_name|
        attributes_mask = model_attributes_mask[model_name]
        next if !attributes_mask.nil? && !attributes_mask.include?(attr_name) && attr_name != template.identifier
        attributes << attr_name

        attr = template.attributes.attribute(attr_name)
        if attr.attribute_type == AttributeType::TABLE
          tables << attr_name
        elsif attr.attribute_type == AttributeType::COLLECTION
          related_models[attr.link_model_name] ||= Set.new
          collections << attr_name
        elsif attr.attribute_type == AttributeType::LINK
          related_models[attr.link_model_name] ||= Set.new
          links << attr_name
        elsif attr.attribute_type == AttributeType::CHILD
          related_models[attr.link_model_name] ||= Set.new
          links << attr_name
        elsif attr.attribute_type == AttributeType::PARENT
          related_models[attr.link_model_name] ||= Set.new
          links << attr_name
        end
      end

      model.documents.document_keys.each do |key|
        record = model.documents.document(key).slice(*attributes)

        # Inline tables inside the record
        tables.each do |table_attr|
          record[table_attr] = record[table_attr].map do |id|
            response.models.model(template.attributes.attribute(table_attr).link_model_name).documents.document(id)
          end unless record[table_attr].nil?
        end

        collections.each do |collection_attr|
          record[collection_attr].each do |collected_id|
            related_models[template.attributes.attribute(collection_attr).link_model_name].add(collected_id)
          end unless record[collection_attr].nil?
        end

        links.each do |link_attr|
          related_models[template.attributes.attribute(link_attr).link_model_name].add(record[link_attr]) unless record[link_attr].nil?
        end

        yield template, record
      end
    end

    related_models.each do |link_model_name, id_set|
      next if id_set.empty?
      q.push({ model_name: link_model_name, from: model_name, record_names: id_set.to_a })
    end
  end
end