Skip to content

Commit

Permalink
Add trigger for FunctionXGraph
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls committed Nov 28, 2023
1 parent 4335729 commit 308dc77
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 14 deletions.
24 changes: 20 additions & 4 deletions arches_for_science/models.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,43 @@
import uuid

from arches.app.models.models import IIIFManifest, TileModel
from arches.app.models.models import IIIFManifest, TileModel, FunctionXGraph
from django.db import models
from django.db.models import JSONField
from django.db.models.signals import post_delete, post_save
from django.dispatch import receiver
import pgtrigger

from .trigger_functions import MULTICARD_PRIMARY_DESCRIPTOR_FUNC
from .trigger_functions import CALCULATE_MULTICARD_PRIMARY_DESCRIPTOR_SINGLE, CALCULATE_MULTICARD_PRIMARY_DESCRIPTOR_ALL

class TileModelProxy(TileModel):
class Meta:
proxy = True
triggers = [
pgtrigger.Trigger(
name='calculate_multicard_primary_descriptor',
name='calculate_multicard_primary_descriptor_single',
when=pgtrigger.After,
operation=pgtrigger.Insert | pgtrigger.Update | pgtrigger.Delete,
func=MULTICARD_PRIMARY_DESCRIPTOR_FUNC,
func=CALCULATE_MULTICARD_PRIMARY_DESCRIPTOR_SINGLE,
timing=pgtrigger.Deferred,
),
]

class FunctionXGraphProxy(FunctionXGraph):
class Meta:
proxy = True
triggers = [
pgtrigger.Trigger(
name='calculate_multicard_primary_descriptor_all',
when=pgtrigger.After,
condition=pgtrigger.Q(
new__function_id="00b2d15a-fda0-4578-b79a-784e4138664b",
new__config__isnull=False,
),
operation=pgtrigger.Insert | pgtrigger.Update,
func=CALCULATE_MULTICARD_PRIMARY_DESCRIPTOR_ALL,
),
]


class RendererConfig(models.Model):
configid = models.UUIDField(primary_key=True, unique=True)
Expand Down
71 changes: 61 additions & 10 deletions arches_for_science/trigger_functions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
MULTICARD_PRIMARY_DESCRIPTOR_FUNC = """
CALCULATE_MULTICARD_PRIMARY_DESCRIPTOR_SINGLE = """
DECLARE
fn_config jsonb;
name_template text;
Expand All @@ -10,6 +10,7 @@
descriptor_key text;
graph UUID;
resourceid UUID;
node_alias text;
alias_with_separators text;
node_for_alias UUID;
Expand All @@ -23,10 +24,12 @@
BEGIN
SELECT NEW.resourceinstanceid INTO resourceid;
SELECT config, r.graphid
INTO fn_config, graph
FROM functions_x_graphs
INNER JOIN resource_instances AS r ON r.resourceinstanceid = NEW.resourceinstanceid
INNER JOIN resource_instances AS r ON r.resourceinstanceid = resourceid
WHERE
functionid = '00b2d15a-fda0-4578-b79a-784e4138664b'
AND config IS NOT NULL;
Expand Down Expand Up @@ -54,18 +57,19 @@
FOR alias_with_separators IN SELECT UNNEST(REGEXP_MATCHES(this_template, '<\w+>', 'g'))
LOOP
SELECT TRIM(BOTH '<>' FROM alias_with_separators) INTO node_alias;
SELECT nodeid, nodegroupid INTO node_for_alias, nodegroup_for_alias
FROM nodes
WHERE alias = node_alias
AND graphid = graph;
SELECT tiledata ->> node_for_alias::text INTO localized_string_node_value
FROM tiles
WHERE resourceinstanceid = NEW.resourceinstanceid
WHERE resourceinstanceid = resourceid
AND nodegroupid = nodegroup_for_alias
AND sortorder = 0
LIMIT 1;
-- Replace template with localized string
FOR lang, inner18n_obj_for_lang IN SELECT * FROM jsonb_each(localized_string_node_value)
LOOP
Expand All @@ -86,7 +90,7 @@
ELSE (localized_calculated_result -> lang -> descriptor_key)::text
END
INTO working_string;
SELECT TRIM(
REPLACE(
working_string,
Expand All @@ -95,16 +99,16 @@
)
, '"')
INTO resolved_node_value_for_lang;
-- Update the working value
SELECT jsonb_set(
localized_calculated_result,
ARRAY[lang, descriptor_key],
TO_JSONB(resolved_node_value_for_lang)
) INTO localized_calculated_result;
RAISE NOTICE '%s', localized_calculated_result;
END LOOP;
END LOOP;
Expand All @@ -113,9 +117,56 @@
UPDATE resource_instances
SET descriptors = localized_calculated_result
WHERE resourceinstanceid = NEW.resourceinstanceid;
WHERE resourceinstanceid = resourceid;
END IF;
END;
RETURN NULL;
"""

# Make two adjustments to create a very similar function that works on ALL resource instances.

target_block_1 = """
SELECT NEW.resourceinstanceid INTO resourceid;
SELECT config, r.graphid
INTO fn_config, graph
FROM functions_x_graphs
INNER JOIN resource_instances AS r ON r.resourceinstanceid = resourceid
WHERE
functionid = '00b2d15a-fda0-4578-b79a-784e4138664b'
AND config IS NOT NULL;
IF FOUND THEN
"""
assert target_block_1 in CALCULATE_MULTICARD_PRIMARY_DESCRIPTOR_SINGLE

replacement_block_1 = """
SELECT NEW.config, NEW.graphid
INTO fn_config, graph;
FOR resourceid IN (SELECT resourceinstanceid FROM resource_instances WHERE graphid = graph)
LOOP
"""

target_block_2 = """
END IF;
END;
"""
assert target_block_2 in CALCULATE_MULTICARD_PRIMARY_DESCRIPTOR_SINGLE

replacement_block_2 = """
END LOOP;
END;
"""

# Now we can create the function to loop through all resource instances.
CALCULATE_MULTICARD_PRIMARY_DESCRIPTOR_ALL = (
CALCULATE_MULTICARD_PRIMARY_DESCRIPTOR_SINGLE.replace(
target_block_1,
replacement_block_1
).replace(
target_block_2,
replacement_block_2
)
)

0 comments on commit 308dc77

Please sign in to comment.