|
20 | 20 | WriteableCogniteResourceList, |
21 | 21 | ) |
22 | 22 | from cognite.client.data_classes.data_modeling.query import Query, ResultSetExpression, Select |
| 23 | +from cognite.client.data_classes.data_modeling.records import RecordContainerId |
| 24 | +from cognite.client.data_classes.filters import Filter |
23 | 25 | from cognite.client.data_classes.simulators.runs import ( |
24 | 26 | SimulationInputOverride, |
25 | 27 | ) |
@@ -1561,6 +1563,88 @@ def dump(self, camel_case: bool = True) -> dict[str, Any]: |
1561 | 1563 | return item |
1562 | 1564 |
|
1563 | 1565 |
|
| 1566 | +class WorkflowRecordStreamSourceSelector(CogniteResource): |
| 1567 | + """Selects which container properties to include in a record stream trigger's workflow input. |
| 1568 | +
|
| 1569 | + Args: |
| 1570 | + source (RecordContainerId): The container to select properties from. |
| 1571 | + properties (list[str]): Property identifiers to return; use ``["*"]`` to return all. |
| 1572 | + """ |
| 1573 | + |
| 1574 | + def __init__(self, source: RecordContainerId, properties: list[str]) -> None: |
| 1575 | + self.source = source |
| 1576 | + self.properties = properties |
| 1577 | + |
| 1578 | + @classmethod |
| 1579 | + def _load(cls, resource: dict[str, Any]) -> WorkflowRecordStreamSourceSelector: |
| 1580 | + return cls(source=RecordContainerId.load(resource["source"]), properties=resource["properties"]) |
| 1581 | + |
| 1582 | + def dump(self, camel_case: bool = True) -> dict[str, Any]: |
| 1583 | + return {"source": self.source.dump(camel_case=camel_case), "properties": self.properties} |
| 1584 | + |
| 1585 | + |
| 1586 | +class WorkflowRecordStreamTriggerRule(WorkflowTriggerRule): |
| 1587 | + """ |
| 1588 | + This class represents a record stream trigger rule. |
| 1589 | +
|
| 1590 | + Args: |
| 1591 | + stream_external_id (str): The external ID of the stream to subscribe to for record changes. |
| 1592 | + batch_size (int): The maximum number of records to pass to a workflow execution. |
| 1593 | + batch_timeout (int): The maximum time in seconds to wait for the batch to be filled. |
| 1594 | + filter (Filter | None): Optional filter to limit which records trigger the workflow. |
| 1595 | + sources (list[WorkflowRecordStreamSourceSelector] | None): Optional containers and properties to include in |
| 1596 | + the workflow input. |
| 1597 | + initialize_cursor (str | None): Where record stream syncing starts when no cursor exists yet, |
| 1598 | + as a relative duration like ``"6h-ago"``. If omitted, syncing starts from the current time (``"0d-ago"``). |
| 1599 | + """ |
| 1600 | + |
| 1601 | + _trigger_type = "recordStream" |
| 1602 | + |
| 1603 | + def __init__( |
| 1604 | + self, |
| 1605 | + stream_external_id: str, |
| 1606 | + batch_size: int, |
| 1607 | + batch_timeout: int, |
| 1608 | + filter: Filter | None = None, |
| 1609 | + sources: list[WorkflowRecordStreamSourceSelector] | None = None, |
| 1610 | + initialize_cursor: str | None = None, |
| 1611 | + ) -> None: |
| 1612 | + self.stream_external_id = stream_external_id |
| 1613 | + self.batch_size = batch_size |
| 1614 | + self.batch_timeout = batch_timeout |
| 1615 | + self.filter = filter |
| 1616 | + self.sources = sources |
| 1617 | + self.initialize_cursor = initialize_cursor |
| 1618 | + |
| 1619 | + @classmethod |
| 1620 | + def _load_trigger(cls, data: dict[str, Any]) -> WorkflowRecordStreamTriggerRule: |
| 1621 | + return cls( |
| 1622 | + stream_external_id=data["streamExternalId"], |
| 1623 | + batch_size=data["batchSize"], |
| 1624 | + batch_timeout=data["batchTimeout"], |
| 1625 | + filter=Filter._load_if(data.get("filter")), |
| 1626 | + sources=[WorkflowRecordStreamSourceSelector._load(source) for source in (data.get("sources") or [])], |
| 1627 | + initialize_cursor=data.get("initializeCursor"), |
| 1628 | + ) |
| 1629 | + |
| 1630 | + def dump(self, camel_case: bool = True) -> dict[str, Any]: |
| 1631 | + item: dict[str, Any] = { |
| 1632 | + "trigger_type": self.trigger_type, |
| 1633 | + "stream_external_id": self.stream_external_id, |
| 1634 | + "batch_size": self.batch_size, |
| 1635 | + "batch_timeout": self.batch_timeout, |
| 1636 | + } |
| 1637 | + if self.filter is not None: |
| 1638 | + item["filter"] = self.filter.dump() |
| 1639 | + if self.sources: |
| 1640 | + item["sources"] = [source.dump(camel_case=camel_case) for source in self.sources] |
| 1641 | + if self.initialize_cursor is not None: |
| 1642 | + item["initialize_cursor"] = self.initialize_cursor |
| 1643 | + if camel_case: |
| 1644 | + return convert_all_keys_to_camel_case(item) |
| 1645 | + return item |
| 1646 | + |
| 1647 | + |
1564 | 1648 | _TRIGGER_RULE_BY_TYPE: dict[str, type[WorkflowTriggerRule]] = { |
1565 | 1649 | subclass._trigger_type: subclass # type: ignore |
1566 | 1650 | for subclass in WorkflowTriggerRule.__subclasses__() |
|
0 commit comments