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

Constructor Details

#initialize(**args) ⇒ WalkModelTreeWorkflow

Returns a new instance of WalkModelTreeWorkflow.



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

def initialize(**args)
  super(**({}.update(args)))
  @template_for = {}
end

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

#all_attributes(template:) ⇒ Object



15
16
17
# File 'lib/etna/clients/magma/workflows/walk_model_tree_workflow.rb', line 15

def all_attributes(template:)
  template.attributes.attribute_keys
end

#attribute_included?(mask, attribute_name) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
# File 'lib/etna/clients/magma/workflows/walk_model_tree_workflow.rb', line 44

def attribute_included?(mask, attribute_name)
  return true if mask.nil?
  mask.include?(attribute_name)
end

#masked_attributes(template:, model_attributes_mask:, model_name:) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/etna/clients/magma/workflows/walk_model_tree_workflow.rb', line 30

def masked_attributes(template:, model_attributes_mask:, model_name:)
  attributes_mask = model_attributes_mask[model_name]

  if attributes_mask.nil?
    return [
      safe_group_attributes(template: template, attributes_mask: attributes_mask),
      all_attributes(template: template)
    ]
  end

  [(safe_group_attributes(template: template, attributes_mask: attributes_mask) + [template.identifier, 'parent']).uniq,
    attributes_mask]
end

#safe_group_attributes(template:, attributes_mask:) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/etna/clients/magma/workflows/walk_model_tree_workflow.rb', line 19

def safe_group_attributes(template:, attributes_mask:)
  result = []

  template.attributes.attribute_keys.each do |attribute_name|
    next if template.attributes.attribute(attribute_name).attribute_type == Etna::Clients::Magma::AttributeType::TABLE
    result << attribute_name if attribute_included?(attributes_mask, attribute_name)
  end

  result
end

#template_for(model_name) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/etna/clients/magma/workflows/walk_model_tree_workflow.rb', line 49

def template_for(model_name)
  @template_for[model_name] ||= magma_crud.magma_client.retrieve(RetrievalRequest.new(
      project_name: magma_crud.project_name,
      model_name: model_name,
      record_names: [],
      attribute_names: [],
  )).models.model(model_name).template
end

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



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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/etna/clients/magma/workflows/walk_model_tree_workflow.rb', line 58

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])

    template = template_for(model_name)
    query_attributes, walk_attributes = masked_attributes(
      template: template,
      model_attributes_mask: model_attributes_mask,
      model_name: 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],
        attribute_names: query_attributes,
        page_size: page_size, page: 1
    )

    related_models = {}

    magma_crud.page_records(model_name, request) do |response|
      logger&.info("Fetched page of #{model_name}")
      tables = []
      collections = []
      links = []
      attributes = []
      model = response.models.model(model_name)

      template.attributes.attribute_keys.each do |attr_name|
        attr = template.attributes.attribute(attr_name)
        if attr.attribute_type == AttributeType::TABLE && attribute_included?(walk_attributes, attr_name)
          tables << attr_name
        end

        next unless attribute_included?(query_attributes, attr_name)
        attributes << attr_name

        if 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 && attribute_included?(walk_attributes, attr_name)
          related_models[attr.link_model_name] ||= Set.new
          links << attr_name
        end
      end

      table_data = {}
      # Request tables in an inner chunk.
      tables.each do |table_attr|
        request = RetrievalRequest.new(
          project_name: magma_crud.project_name,
          model_name: model_name,
          record_names: model.documents.document_keys,
          attribute_names: [table_attr],
        )

        logger&.info("Fetching inner table #{table_attr}...")

        table_response = magma_crud.magma_client.retrieve(request)
        d = table_response.models.model(model_name).documents
        table_d = table_response.models.model(table_attr).documents
        table_data[table_attr] = d.document_keys.map do |id|
          [id, d.document(id)[table_attr].map { |tid| table_d.document(tid) }]
        end.to_h
      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] = table_data[table_attr][key] unless table_data[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