[UNSTABLE] - Update FetchCelery to use next_id (not child_id)

pull/655/head
Rick Carlino 2018-02-02 14:08:42 -06:00
parent 08bd406c03
commit bbbb583c73
2 changed files with 29 additions and 17 deletions

View File

@ -6,17 +6,6 @@ module CeleryScript
class FetchCelery < Mutations::Command
private # = = = = = = =
# Returns an `EdgeNode` or `nil` for the following situations:
# 1. The first element of a node's `body` attribute
# 2. (when already inside of a body array) The next `body` element in the chain.
# 3. (when the node has no body) nil
# 3. (when at the end of the body chain) nil
def get_child(node) # => EdgeNode | nil
children = primary_nodes.by.parent_id[node.id]
return children ?
children.find { |x| !x.parent_arg_name && x.kind != "nothing" } : nil
end
# This class is too CPU intensive to make multiple SQL requests.
# To speed up querying, we create an in-memory index for frequently
# looked up attributes such as :id, :kind, :parent_id, :primary_node_id
@ -29,6 +18,22 @@ module CeleryScript
@primary_nodes ||= Indexer.new(sequence.primary_nodes)
end
def get_first_body_node(node)
return primary_nodes
.by
.id[node.body_id]
.select{|x| x.kind != "nothing"}
.first
end
def get_next_sibling(node) # => EdgeNode | nil
return primary_nodes
.by
.id[node.next_id]
.select{|x| x.kind != "nothing"}
.first
end
# Helper function for frequently references object. All nodes that point to
# this node as their `parent_id` or `body_id` indicate a `nil` condition.
def null_node
@ -56,8 +61,7 @@ module CeleryScript
# Mutate an array to contain all the body items of the `origin` node
# Turns a linked list into a JSON array. Returns Array or nil
def recurse_into_body(origin, output_array = [])
# How do I detect if I should pass `output_array` or instantiate a new copy?
child = get_child(origin)
child = get_next_sibling(origin)
child && output_array.push(recurse_into_node(child))
return output_array.empty? ? nil : output_array
end
@ -74,9 +78,8 @@ module CeleryScript
# Top level function call for converting a single EdgeNode into a JSON
# document. Returns Hash<Symbol, any>
def recurse_into_node(node)
output = { kind: node.kind, args: recurse_into_args(node) }
body = recurse_into_body(node, [])
output[:body] = body if body
output = { kindt_body_node(node)
output[:body] = recurse_into_body(body, []) if body
return output
end
@ -92,7 +95,9 @@ module CeleryScript
updated_at: sequence.updated_at
}
end
public # = = = = = = =
public # = = = = = = =
required do
model :sequence, class: Sequence
end

View File

@ -18,6 +18,13 @@ describe CeleryScript::FetchCelery do
.as_json
.deep_symbolize_keys
.without(:device_id, :migrated_nodes)
expect(actual[:body]).to be_kind_of(Array)
expected[:body]
.each_with_index do |item, index|
x = actual[:body][index]
y = expected[:body][index]
expect(HashDiff.diff(x, y)).to eq([])
end
expect(HashDiff.diff(actual, expected)).to eq([])
end
end