Skip to content

Commit

Permalink
Fix "inputs" cache type (#98)
Browse files Browse the repository at this point in the history
* fix input cache

* improve
  • Loading branch information
mariusandra committed Jun 25, 2024
1 parent 5d2b5c7 commit 57cab2a
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions backend/app/codegen/scene_nim.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ def get_app_node_cacheable_fields_with_types(self, node_id) -> dict[str, str]:

return cache_fields

# case_or_block = 'case' or 'block' or 'vars' or 'blockWithVars'
def process_app_run_lines(self, node, case_or_block = "case"):
node_id = node["id"]
name = node.get("data", {}).get("keyword", f"app_{node_id}")
Expand All @@ -431,7 +432,7 @@ def process_app_run_lines(self, node, case_or_block = "case"):
run_lines += [
f"of {node_integer}.NodeId: # {name}",
]
else:
elif case_or_block == "block" or case_or_block == "blockWithVars" or case_or_block == "vars":
run_lines += [
"block:",
]
Expand All @@ -441,21 +442,34 @@ def process_app_run_lines(self, node, case_or_block = "case"):
source_field_inputs_for_node = self.source_field_inputs.get(node_id, {})

for key, code in field_inputs_for_node.items():
if case_or_block == "vars":
fieldAssignment = f"let {key}"
else:
fieldAssignment = f"self.{app_id}.appConfig.{key}"

if case_or_block == "blockWithVars":
run_lines += [f" {fieldAssignment} = {key}"]
continue

if key in code_fields_for_node:
code_lines = self.get_code_field_value(code_fields_for_node[key])
# Wrap optional images with some()
if not self.required_fields[node_id].get(key, False) and field_types_for_node.get(key, "string") == "image":
code_lines[0] = "some(" + code_lines[0]
code_lines[-1] = code_lines[-1] + ")"
run_lines += [f" self.{app_id}.appConfig.{key} = {code_lines[0]}"]
run_lines += [f" {fieldAssignment} = {code_lines[0]}"]
for line in code_lines[1:]:
run_lines += [f" {line}"]
else:
run_lines += [f" self.{app_id}.appConfig.{key} = {code}"]
run_lines += [f" {fieldAssignment} = {code}"]

for key, (source_id, source_key) in source_field_inputs_for_node.items():
if case_or_block == "vars":
fieldAssignment = "let {key}"
else:
fieldAssignment = f"self.{app_id}.appConfig.{key}"
run_lines += [
f" self.{app_id}.appConfig.{key} = self.node{self.node_id_to_integer(source_id)}.appConfig.{source_key}"
f" {fieldAssignment} = self.node{self.node_id_to_integer(source_id)}.appConfig.{source_key}"
]
next_node_id = self.next_nodes.get(node_id, None)

Expand All @@ -464,10 +478,9 @@ def process_app_run_lines(self, node, case_or_block = "case"):
run_lines += [
f" nextNode = {-1 if next_node_id is None else self.node_id_to_integer(next_node_id)}.NodeId",
]
else:
elif case_or_block == "block" or case_or_block == "blockWithVars":
run_lines += [f" self.{app_id}.get(context)"]


return run_lines

def read_nodes(self):
Expand Down Expand Up @@ -909,9 +922,18 @@ def get_code_field_value(self, node_id, depth = 0) -> list[str]:
cache_enabled = node.get("data", {}).get('cache', {}).get('enabled', False)
if node.get("type") == "app":
self.process_app_run(node)
result = self.process_app_run_lines(node, "block")
if cache_enabled:
result = self.wrap_with_cache(node_id, result, node.get("data", {}))
input_enabled = node.get("data", {}).get('cache', {}).get('inputEnabled', False)
if input_enabled:
vars = self.process_app_run_lines(node, "vars")
result = self.process_app_run_lines(node, "blockWithVars")
if cache_enabled:
result = self.wrap_with_cache(node_id, result, node.get("data", {}))
if len(vars) > 1: # just "block:" doesn't as a line
result = vars + [f" {r}" for r in result]
else:
result = self.process_app_run_lines(node, "block")
if cache_enabled:
result = self.wrap_with_cache(node_id, result, node.get("data", {}))
elif node.get("type") == "code":
code = [node.get("data", {}).get("code", "")]
code_args = node.get("data", {}).get("codeArgs", [])
Expand Down Expand Up @@ -1010,11 +1032,7 @@ def wrap_with_cache(self, node_id: str, value_list: list[str], data: dict):
}

if len(cache_fields) > 0:
app_id = f"node{self.node_id_to_integer(node_id)}"
if node.get("type") == "app":
cache_key = ", ".join(map(lambda x: f"self.{app_id}.appConfig.{x}", cache_fields.keys()))
else:
cache_key = ", ".join(cache_fields.keys())
cache_key = ", ".join(cache_fields.keys())
cache_key_data_type = ", ".join(cache_fields.values())
if len(cache_fields) > 1:
cache_key = f"({cache_key})"
Expand Down

0 comments on commit 57cab2a

Please sign in to comment.