From 6f254f87633731505a83f6e573a9f2589c56cda5 Mon Sep 17 00:00:00 2001
From: Anders Albert <60234212+doctrino@users.noreply.github.com>
Date: Sat, 9 Nov 2024 22:09:14 +0100
Subject: [PATCH] [PYG-274] Tutorial (#351)
* docs: include in pygen docs
* build: bump
---
cognite/pygen/_version.py | 2 +-
docs-dev/docs_query.ipynb | 3901 -------------
docs/docs-dev/docs_query.ipynb | 5022 +++++++++++++++++
docs/quickstart/cdf_streamlit.md | 2 +-
examples/cognite_core/_api_client.py | 4 +-
examples/equipment_unit/_api_client.py | 4 +-
examples/omni/_api_client.py | 4 +-
examples/omni_multi/_api_client.py | 4 +-
examples/omni_sub/_api_client.py | 4 +-
.../scenario_instance/client/_api_client.py | 4 +-
examples/windmill/_api_client.py | 4 +-
mkdocs.yml | 3 +-
pyproject.toml | 2 +-
13 files changed, 5041 insertions(+), 3919 deletions(-)
delete mode 100644 docs-dev/docs_query.ipynb
create mode 100644 docs/docs-dev/docs_query.ipynb
diff --git a/cognite/pygen/_version.py b/cognite/pygen/_version.py
index 113b560e7..085bda964 100644
--- a/cognite/pygen/_version.py
+++ b/cognite/pygen/_version.py
@@ -1 +1 @@
-__version__ = "0.99.44"
+__version__ = "0.99.45"
diff --git a/docs-dev/docs_query.ipynb b/docs-dev/docs_query.ipynb
deleted file mode 100644
index d96be302e..000000000
--- a/docs-dev/docs_query.ipynb
+++ /dev/null
@@ -1,3901 +0,0 @@
-{
- "cells": [
- {
- "cell_type": "markdown",
- "id": "97698baf-35d1-422f-a962-1e28ad07bdb6",
- "metadata": {},
- "source": [
- "# Querying Data Modeling\n",
- "\n",
- "This tutorial demonstrates how to query a data model in Cognite Data Fuction (CDF) with Python \n",
- "\n",
- "**Prerequisites**\n",
- "\n",
- "* Installed `cognite-sdk`\n",
- "* Setup a service principal to connect to CDF.\n",
- "* Use `cognite-toolkit` quickstart to setup configuration and populated the `CogniteCore` Model [Toolkit quickstart](https://docs.cognite.com/cdf/deploy/cdf_toolkit/references/packages/quickstart)"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6268a78f-e8de-482f-832b-20d28333a3b1",
- "metadata": {},
- "source": [
- "## Setting up a cognite client"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "id": "3d0b28eb-87fe-453e-9cad-a7f5d8c3fb90",
- "metadata": {
- "ExecuteTime": {
- "end_time": "2024-11-09T13:13:17.005946Z",
- "start_time": "2024-11-09T13:13:16.981218Z"
- }
- },
- "outputs": [],
- "source": [
- "import os\n",
- "\n",
- "from cognite.client import CogniteClient\n",
- "from dotenv import load_dotenv\n",
- "\n",
- "load_dotenv(\"../.env\")\n",
- "\n",
- "client = CogniteClient.default_oauth_client_credentials(\n",
- " project=os.environ[\"CDF_PROJECT\"],\n",
- " cdf_cluster=os.environ[\"CDF_CLUSTER\"],\n",
- " tenant_id=os.environ[\"IDP_TENANT_ID\"],\n",
- " client_id=os.environ[\"IDP_CLIENT_ID\"],\n",
- " client_secret=os.environ[\"IDP_CLIENT_SECRET\"],\n",
- ")"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "308cb4ec-826a-496e-ae32-2762658ace18",
- "metadata": {
- "jp-MarkdownHeadingCollapsed": true
- },
- "source": [
- "## Querying across a direct relation"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6af59c70-5e20-4563-bda5-f1d0eb62ca2d",
- "metadata": {},
- "source": [
- "First, we will query across a direct relation. For example, we can get the `parent` starting from `CogniteAsset`"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 27,
- "id": "2db7c273-c079-4ce7-8ae1-08dbb9f334ac",
- "metadata": {},
- "outputs": [],
- "source": [
- "from cognite.client import data_modeling as dm\n",
- "from cognite.client.data_classes import filters\n",
- "\n",
- "asset_view = dm.ViewId(\"cdf_cdm\", \"CogniteAsset\", \"v1\")\n",
- "# The instance space with the instance demo data\n",
- "data_space = \"springfield_instances\""
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 28,
- "id": "4829284b-429c-4bf2-b4ce-f64d7e848bc1",
- "metadata": {
- "ExecuteTime": {
- "end_time": "2024-11-09T13:23:47.333397Z",
- "start_time": "2024-11-09T13:23:47.167740Z"
- }
- },
- "outputs": [],
- "source": [
- "query = dm.query.Query(\n",
- " with_={\n",
- " \"asset\": dm.query.NodeResultSetExpression(\n",
- " limit=10,\n",
- " filter=filters.And(filters.HasData(views=[asset_view]), filters.Equals([\"node\", \"space\"], data_space)),\n",
- " ),\n",
- " \"parent\": dm.query.NodeResultSetExpression(\n",
- " from_=\"asset\", through=dm.PropertyId(source=asset_view, property=\"parent\")\n",
- " ),\n",
- " },\n",
- " select={\n",
- " \"asset\": dm.query.Select(sources=[dm.query.SourceSelector(source=asset_view, properties=[\"*\"])]),\n",
- " \"parent\": dm.query.Select(sources=[dm.query.SourceSelector(source=asset_view, properties=[\"*\"])]),\n",
- " },\n",
- ")\n",
- "\n",
- "result = client.data_modeling.instances.query(query)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 25,
- "id": "38fb49fa-3cff-4fa8-9797-3b134e0f60e3",
- "metadata": {
- "ExecuteTime": {
- "end_time": "2024-11-09T13:23:47.767980Z",
- "start_time": "2024-11-09T13:23:47.739654Z"
- }
- },
- "outputs": [
- {
- "data": {
- "text/html": [
- "
\n",
- "\n",
- "
\n",
- " \n",
- " \n",
- " | \n",
- " space | \n",
- " external_id | \n",
- " version | \n",
- " last_updated_time | \n",
- " created_time | \n",
- " instance_type | \n",
- " path | \n",
- " root | \n",
- " pathLastUpdatedTime | \n",
- " name | \n",
- " description | \n",
- " sourceId | \n",
- " sourceUpdatedUser | \n",
- " parent | \n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " 0 | \n",
- " springfield_instances | \n",
- " WMT:23-2ND STAGE COMP DRY GAS SEAL SYS-PH | \n",
- " 2 | \n",
- " 2024-11-08 14:20:21.633 | \n",
- " 2024-11-08 14:20:20.316 | \n",
- " node | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- " 2024-11-08T14:20:21.633609+00:00 | \n",
- " 23-2ND STAGE COMP DRY GAS SEAL SYS-PH | \n",
- " 2ND STAGE COMP DRY GAS SEAL SYS ON PH | \n",
- " 682546 | \n",
- " 8137 | \n",
- " NaN | \n",
- "
\n",
- " \n",
- " 1 | \n",
- " springfield_instances | \n",
- " WMT:230900 | \n",
- " 3 | \n",
- " 2024-11-09 13:17:39.283 | \n",
- " 2024-11-08 14:20:20.316 | \n",
- " node | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- " 2024-11-09T13:17:39.28327+00:00 | \n",
- " 230900 | \n",
- " GAS COMPRESSION AND RE-INJECTION ON PH | \n",
- " 681760 | \n",
- " 8137 | \n",
- " NaN | \n",
- "
\n",
- " \n",
- " 2 | \n",
- " springfield_instances | \n",
- " WMT:23-1ST STAGE COMP DRY GAS SEAL SYS-PH | \n",
- " 2 | \n",
- " 2024-11-08 14:20:28.094 | \n",
- " 2024-11-08 14:20:27.925 | \n",
- " node | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- " 2024-11-08T14:20:28.094598+00:00 | \n",
- " 23-1ST STAGE COMP DRY GAS SEAL SYS-PH | \n",
- " 1ST STAGE COMP DRY GAS SEAL SYS ON PH | \n",
- " 682540 | \n",
- " 8137 | \n",
- " NaN | \n",
- "
\n",
- " \n",
- " 3 | \n",
- " springfield_instances | \n",
- " WMT:23-2ND STAGE COMPRESSION-PH | \n",
- " 4 | \n",
- " 2024-11-09 13:17:39.283 | \n",
- " 2024-11-08 14:20:27.925 | \n",
- " node | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- " 2024-11-09T13:17:39.28327+00:00 | \n",
- " 23-2ND STAGE COMPRESSION-PH | \n",
- " 2ND STAGE COMPRESSION ON PH | \n",
- " 681794 | \n",
- " 8137 | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- "
\n",
- " \n",
- " 4 | \n",
- " springfield_instances | \n",
- " WMT:23-2ND STAGE COMPRESSOR-PH | \n",
- " 4 | \n",
- " 2024-11-09 13:17:39.283 | \n",
- " 2024-11-08 14:20:28.103 | \n",
- " node | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- " 2024-11-09T13:17:39.28327+00:00 | \n",
- " 23-2ND STAGE COMPRESSOR-PH | \n",
- " 2ND STAGE COMPRESSOR ON PH | \n",
- " 681825 | \n",
- " 8137 | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- "
\n",
- " \n",
- " 5 | \n",
- " springfield_instances | \n",
- " WMT:23-1ST STAGE COMPRESSOR-PH | \n",
- " 4 | \n",
- " 2024-11-09 13:17:39.283 | \n",
- " 2024-11-08 14:21:06.876 | \n",
- " node | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- " 2024-11-09T13:17:39.28327+00:00 | \n",
- " 23-1ST STAGE COMPRESSOR-PH | \n",
- " 1ST STAGE COMPRESSOR ON PH | \n",
- " 681824 | \n",
- " 8137 | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- "
\n",
- " \n",
- " 6 | \n",
- " springfield_instances | \n",
- " WMT:23-1ST STAGE COMP LUBE OIL SYS-PH | \n",
- " 2 | \n",
- " 2024-11-08 14:21:07.507 | \n",
- " 2024-11-08 14:21:06.876 | \n",
- " node | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- " 2024-11-08T14:21:07.507048+00:00 | \n",
- " 23-1ST STAGE COMP LUBE OIL SYS-PH | \n",
- " 1ST STAGE COMP LUBE OIL SYS ON PH | \n",
- " 682544 | \n",
- " 8137 | \n",
- " NaN | \n",
- "
\n",
- " \n",
- " 7 | \n",
- " springfield_instances | \n",
- " WMT:23-1ST STAGE COMP ENCLOSURE-PH | \n",
- " 2 | \n",
- " 2024-11-08 14:21:07.507 | \n",
- " 2024-11-08 14:21:06.876 | \n",
- " node | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- " 2024-11-08T14:21:07.507048+00:00 | \n",
- " 23-1ST STAGE COMP ENCLOSURE-PH | \n",
- " 1ST STAGE COMP ENCLOSURE ON PH | \n",
- " 682542 | \n",
- " 8137 | \n",
- " NaN | \n",
- "
\n",
- " \n",
- " 8 | \n",
- " springfield_instances | \n",
- " WMT:23-1ST STAGE COMPRESSION-PH | \n",
- " 4 | \n",
- " 2024-11-09 13:17:39.283 | \n",
- " 2024-11-08 14:21:06.876 | \n",
- " node | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- " 2024-11-09T13:17:39.28327+00:00 | \n",
- " 23-1ST STAGE COMPRESSION-PH | \n",
- " 1ST STAGE COMPRESSION ON PH | \n",
- " 681792 | \n",
- " 8137 | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- "
\n",
- " \n",
- "
\n",
- "
"
- ],
- "text/plain": [
- " space external_id version \\\n",
- "0 springfield_instances WMT:23-2ND STAGE COMP DRY GAS SEAL SYS-PH 2 \n",
- "1 springfield_instances WMT:230900 3 \n",
- "2 springfield_instances WMT:23-1ST STAGE COMP DRY GAS SEAL SYS-PH 2 \n",
- "3 springfield_instances WMT:23-2ND STAGE COMPRESSION-PH 4 \n",
- "4 springfield_instances WMT:23-2ND STAGE COMPRESSOR-PH 4 \n",
- "5 springfield_instances WMT:23-1ST STAGE COMPRESSOR-PH 4 \n",
- "6 springfield_instances WMT:23-1ST STAGE COMP LUBE OIL SYS-PH 2 \n",
- "7 springfield_instances WMT:23-1ST STAGE COMP ENCLOSURE-PH 2 \n",
- "8 springfield_instances WMT:23-1ST STAGE COMPRESSION-PH 4 \n",
- "\n",
- " last_updated_time created_time instance_type \\\n",
- "0 2024-11-08 14:20:21.633 2024-11-08 14:20:20.316 node \n",
- "1 2024-11-09 13:17:39.283 2024-11-08 14:20:20.316 node \n",
- "2 2024-11-08 14:20:28.094 2024-11-08 14:20:27.925 node \n",
- "3 2024-11-09 13:17:39.283 2024-11-08 14:20:27.925 node \n",
- "4 2024-11-09 13:17:39.283 2024-11-08 14:20:28.103 node \n",
- "5 2024-11-09 13:17:39.283 2024-11-08 14:21:06.876 node \n",
- "6 2024-11-08 14:21:07.507 2024-11-08 14:21:06.876 node \n",
- "7 2024-11-08 14:21:07.507 2024-11-08 14:21:06.876 node \n",
- "8 2024-11-09 13:17:39.283 2024-11-08 14:21:06.876 node \n",
- "\n",
- " path \\\n",
- "0 [{'space': 'springfield_instances', 'externalI... \n",
- "1 [{'space': 'springfield_instances', 'externalI... \n",
- "2 [{'space': 'springfield_instances', 'externalI... \n",
- "3 [{'space': 'springfield_instances', 'externalI... \n",
- "4 [{'space': 'springfield_instances', 'externalI... \n",
- "5 [{'space': 'springfield_instances', 'externalI... \n",
- "6 [{'space': 'springfield_instances', 'externalI... \n",
- "7 [{'space': 'springfield_instances', 'externalI... \n",
- "8 [{'space': 'springfield_instances', 'externalI... \n",
- "\n",
- " root \\\n",
- "0 {'space': 'springfield_instances', 'externalId... \n",
- "1 {'space': 'springfield_instances', 'externalId... \n",
- "2 {'space': 'springfield_instances', 'externalId... \n",
- "3 {'space': 'springfield_instances', 'externalId... \n",
- "4 {'space': 'springfield_instances', 'externalId... \n",
- "5 {'space': 'springfield_instances', 'externalId... \n",
- "6 {'space': 'springfield_instances', 'externalId... \n",
- "7 {'space': 'springfield_instances', 'externalId... \n",
- "8 {'space': 'springfield_instances', 'externalId... \n",
- "\n",
- " pathLastUpdatedTime name \\\n",
- "0 2024-11-08T14:20:21.633609+00:00 23-2ND STAGE COMP DRY GAS SEAL SYS-PH \n",
- "1 2024-11-09T13:17:39.28327+00:00 230900 \n",
- "2 2024-11-08T14:20:28.094598+00:00 23-1ST STAGE COMP DRY GAS SEAL SYS-PH \n",
- "3 2024-11-09T13:17:39.28327+00:00 23-2ND STAGE COMPRESSION-PH \n",
- "4 2024-11-09T13:17:39.28327+00:00 23-2ND STAGE COMPRESSOR-PH \n",
- "5 2024-11-09T13:17:39.28327+00:00 23-1ST STAGE COMPRESSOR-PH \n",
- "6 2024-11-08T14:21:07.507048+00:00 23-1ST STAGE COMP LUBE OIL SYS-PH \n",
- "7 2024-11-08T14:21:07.507048+00:00 23-1ST STAGE COMP ENCLOSURE-PH \n",
- "8 2024-11-09T13:17:39.28327+00:00 23-1ST STAGE COMPRESSION-PH \n",
- "\n",
- " description sourceId sourceUpdatedUser \\\n",
- "0 2ND STAGE COMP DRY GAS SEAL SYS ON PH 682546 8137 \n",
- "1 GAS COMPRESSION AND RE-INJECTION ON PH 681760 8137 \n",
- "2 1ST STAGE COMP DRY GAS SEAL SYS ON PH 682540 8137 \n",
- "3 2ND STAGE COMPRESSION ON PH 681794 8137 \n",
- "4 2ND STAGE COMPRESSOR ON PH 681825 8137 \n",
- "5 1ST STAGE COMPRESSOR ON PH 681824 8137 \n",
- "6 1ST STAGE COMP LUBE OIL SYS ON PH 682544 8137 \n",
- "7 1ST STAGE COMP ENCLOSURE ON PH 682542 8137 \n",
- "8 1ST STAGE COMPRESSION ON PH 681792 8137 \n",
- "\n",
- " parent \n",
- "0 NaN \n",
- "1 NaN \n",
- "2 NaN \n",
- "3 {'space': 'springfield_instances', 'externalId... \n",
- "4 {'space': 'springfield_instances', 'externalId... \n",
- "5 {'space': 'springfield_instances', 'externalId... \n",
- "6 NaN \n",
- "7 NaN \n",
- "8 {'space': 'springfield_instances', 'externalId... "
- ]
- },
- "execution_count": 25,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df = result[\"asset\"].to_pandas(expand_properties=True)\n",
- "df"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 26,
- "id": "400c57d2-754e-4c52-92ec-03dc0e9123d7",
- "metadata": {
- "ExecuteTime": {
- "end_time": "2024-11-09T13:23:48.546916Z",
- "start_time": "2024-11-09T13:23:48.536611Z"
- }
- },
- "outputs": [
- {
- "data": {
- "text/html": [
- "\n",
- "\n",
- "
\n",
- " \n",
- " \n",
- " | \n",
- " space | \n",
- " external_id | \n",
- " version | \n",
- " last_updated_time | \n",
- " created_time | \n",
- " instance_type | \n",
- " path | \n",
- " root | \n",
- " pathLastUpdatedTime | \n",
- " name | \n",
- " description | \n",
- " sourceId | \n",
- " sourceUpdatedUser | \n",
- " parent | \n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " 0 | \n",
- " springfield_instances | \n",
- " WMT:230900 | \n",
- " 3 | \n",
- " 2024-11-09 13:17:39.283 | \n",
- " 2024-11-08 14:20:20.316 | \n",
- " node | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- " 2024-11-09T13:17:39.28327+00:00 | \n",
- " 230900 | \n",
- " GAS COMPRESSION AND RE-INJECTION ON PH | \n",
- " 681760 | \n",
- " 8137 | \n",
- " NaN | \n",
- "
\n",
- " \n",
- " 1 | \n",
- " springfield_instances | \n",
- " WMT:23-2ND STAGE COMPRESSION-PH | \n",
- " 4 | \n",
- " 2024-11-09 13:17:39.283 | \n",
- " 2024-11-08 14:20:27.925 | \n",
- " node | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- " 2024-11-09T13:17:39.28327+00:00 | \n",
- " 23-2ND STAGE COMPRESSION-PH | \n",
- " 2ND STAGE COMPRESSION ON PH | \n",
- " 681794 | \n",
- " 8137 | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- "
\n",
- " \n",
- " 2 | \n",
- " springfield_instances | \n",
- " WMT:23-1ST STAGE COMPRESSION-PH | \n",
- " 4 | \n",
- " 2024-11-09 13:17:39.283 | \n",
- " 2024-11-08 14:21:06.876 | \n",
- " node | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- " 2024-11-09T13:17:39.28327+00:00 | \n",
- " 23-1ST STAGE COMPRESSION-PH | \n",
- " 1ST STAGE COMPRESSION ON PH | \n",
- " 681792 | \n",
- " 8137 | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- "
\n",
- " \n",
- "
\n",
- "
"
- ],
- "text/plain": [
- "NodeListWithCursor([,\n",
- " ,\n",
- " ])"
- ]
- },
- "execution_count": 26,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "result[\"parent\"]"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "1ef1f8db-e525-4b2f-bf47-dabe93abaf2a",
- "metadata": {},
- "source": [
- "## Quering across reverse direct relation of single relation"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "c8d8bf72-4907-4064-a509-a9437f9f82f4",
- "metadata": {},
- "source": [
- "We can for example query across the `children` in `CogniteAsset`. The `children` property is a reverse direct relation of `parent`, so we query in `inwards` direction towards parent"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 31,
- "id": "75db7a18-99fe-4656-86a3-d33c9c4acb75",
- "metadata": {},
- "outputs": [],
- "source": [
- "query = dm.query.Query(\n",
- " with_={\n",
- " \"asset\": dm.query.NodeResultSetExpression(\n",
- " limit=10,\n",
- " filter=filters.And(filters.HasData(views=[asset_view]), filters.Equals([\"node\", \"space\"], data_space)),\n",
- " ),\n",
- " \"children\": dm.query.NodeResultSetExpression(\n",
- " from_=\"asset\", through=dm.PropertyId(source=asset_view, property=\"parent\"), direction=\"inwards\"\n",
- " ),\n",
- " },\n",
- " select={\n",
- " \"asset\": dm.query.Select(sources=[dm.query.SourceSelector(source=asset_view, properties=[\"*\"])]),\n",
- " \"children\": dm.query.Select(sources=[dm.query.SourceSelector(source=asset_view, properties=[\"*\"])]),\n",
- " },\n",
- ")\n",
- "\n",
- "result = client.data_modeling.instances.query(query)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 34,
- "id": "b53e0aff-ac57-4c02-a9a7-b40f1a844dff",
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "\n",
- "\n",
- "
\n",
- " \n",
- " \n",
- " | \n",
- " space | \n",
- " external_id | \n",
- " version | \n",
- " last_updated_time | \n",
- " created_time | \n",
- " instance_type | \n",
- " path | \n",
- " root | \n",
- " pathLastUpdatedTime | \n",
- " name | \n",
- " description | \n",
- " sourceId | \n",
- " sourceUpdatedUser | \n",
- " parent | \n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " 0 | \n",
- " springfield_instances | \n",
- " WMT:23-2ND STAGE COMP DRY GAS SEAL SYS-PH | \n",
- " 2 | \n",
- " 2024-11-08 14:20:21.633 | \n",
- " 2024-11-08 14:20:20.316 | \n",
- " node | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- " 2024-11-08T14:20:21.633609+00:00 | \n",
- " 23-2ND STAGE COMP DRY GAS SEAL SYS-PH | \n",
- " 2ND STAGE COMP DRY GAS SEAL SYS ON PH | \n",
- " 682546 | \n",
- " 8137 | \n",
- " NaN | \n",
- "
\n",
- " \n",
- " 1 | \n",
- " springfield_instances | \n",
- " WMT:230900 | \n",
- " 3 | \n",
- " 2024-11-09 13:17:39.283 | \n",
- " 2024-11-08 14:20:20.316 | \n",
- " node | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- " 2024-11-09T13:17:39.28327+00:00 | \n",
- " 230900 | \n",
- " GAS COMPRESSION AND RE-INJECTION ON PH | \n",
- " 681760 | \n",
- " 8137 | \n",
- " NaN | \n",
- "
\n",
- " \n",
- " 2 | \n",
- " springfield_instances | \n",
- " WMT:23-1ST STAGE COMP DRY GAS SEAL SYS-PH | \n",
- " 2 | \n",
- " 2024-11-08 14:20:28.094 | \n",
- " 2024-11-08 14:20:27.925 | \n",
- " node | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- " 2024-11-08T14:20:28.094598+00:00 | \n",
- " 23-1ST STAGE COMP DRY GAS SEAL SYS-PH | \n",
- " 1ST STAGE COMP DRY GAS SEAL SYS ON PH | \n",
- " 682540 | \n",
- " 8137 | \n",
- " NaN | \n",
- "
\n",
- " \n",
- " 3 | \n",
- " springfield_instances | \n",
- " WMT:23-2ND STAGE COMPRESSION-PH | \n",
- " 4 | \n",
- " 2024-11-09 13:17:39.283 | \n",
- " 2024-11-08 14:20:27.925 | \n",
- " node | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- " 2024-11-09T13:17:39.28327+00:00 | \n",
- " 23-2ND STAGE COMPRESSION-PH | \n",
- " 2ND STAGE COMPRESSION ON PH | \n",
- " 681794 | \n",
- " 8137 | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- "
\n",
- " \n",
- " 4 | \n",
- " springfield_instances | \n",
- " WMT:23-2ND STAGE COMPRESSOR-PH | \n",
- " 4 | \n",
- " 2024-11-09 13:17:39.283 | \n",
- " 2024-11-08 14:20:28.103 | \n",
- " node | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- " 2024-11-09T13:17:39.28327+00:00 | \n",
- " 23-2ND STAGE COMPRESSOR-PH | \n",
- " 2ND STAGE COMPRESSOR ON PH | \n",
- " 681825 | \n",
- " 8137 | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- "
\n",
- " \n",
- " 5 | \n",
- " springfield_instances | \n",
- " WMT:23-1ST STAGE COMPRESSOR-PH | \n",
- " 4 | \n",
- " 2024-11-09 13:17:39.283 | \n",
- " 2024-11-08 14:21:06.876 | \n",
- " node | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- " 2024-11-09T13:17:39.28327+00:00 | \n",
- " 23-1ST STAGE COMPRESSOR-PH | \n",
- " 1ST STAGE COMPRESSOR ON PH | \n",
- " 681824 | \n",
- " 8137 | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- "
\n",
- " \n",
- " 6 | \n",
- " springfield_instances | \n",
- " WMT:23-1ST STAGE COMP LUBE OIL SYS-PH | \n",
- " 2 | \n",
- " 2024-11-08 14:21:07.507 | \n",
- " 2024-11-08 14:21:06.876 | \n",
- " node | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- " 2024-11-08T14:21:07.507048+00:00 | \n",
- " 23-1ST STAGE COMP LUBE OIL SYS-PH | \n",
- " 1ST STAGE COMP LUBE OIL SYS ON PH | \n",
- " 682544 | \n",
- " 8137 | \n",
- " NaN | \n",
- "
\n",
- " \n",
- " 7 | \n",
- " springfield_instances | \n",
- " WMT:23-1ST STAGE COMP ENCLOSURE-PH | \n",
- " 2 | \n",
- " 2024-11-08 14:21:07.507 | \n",
- " 2024-11-08 14:21:06.876 | \n",
- " node | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- " 2024-11-08T14:21:07.507048+00:00 | \n",
- " 23-1ST STAGE COMP ENCLOSURE-PH | \n",
- " 1ST STAGE COMP ENCLOSURE ON PH | \n",
- " 682542 | \n",
- " 8137 | \n",
- " NaN | \n",
- "
\n",
- " \n",
- " 8 | \n",
- " springfield_instances | \n",
- " WMT:23-1ST STAGE COMPRESSION-PH | \n",
- " 4 | \n",
- " 2024-11-09 13:17:39.283 | \n",
- " 2024-11-08 14:21:06.876 | \n",
- " node | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- " 2024-11-09T13:17:39.28327+00:00 | \n",
- " 23-1ST STAGE COMPRESSION-PH | \n",
- " 1ST STAGE COMPRESSION ON PH | \n",
- " 681792 | \n",
- " 8137 | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- "
\n",
- " \n",
- "
\n",
- "
"
- ],
- "text/plain": [
- "NodeListWithCursor([,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ])"
- ]
- },
- "execution_count": 34,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "result[\"asset\"]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 35,
- "id": "b16d7ff4-9f74-475d-981c-8b197f182878",
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "\n",
- "\n",
- "
\n",
- " \n",
- " \n",
- " | \n",
- " space | \n",
- " external_id | \n",
- " version | \n",
- " last_updated_time | \n",
- " created_time | \n",
- " instance_type | \n",
- " path | \n",
- " root | \n",
- " parent | \n",
- " pathLastUpdatedTime | \n",
- " name | \n",
- " description | \n",
- " sourceId | \n",
- " sourceUpdatedUser | \n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " 0 | \n",
- " springfield_instances | \n",
- " WMT:23-2ND STAGE COMPRESSION-PH | \n",
- " 4 | \n",
- " 2024-11-09 13:17:39.283 | \n",
- " 2024-11-08 14:20:27.925 | \n",
- " node | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- " 2024-11-09T13:17:39.28327+00:00 | \n",
- " 23-2ND STAGE COMPRESSION-PH | \n",
- " 2ND STAGE COMPRESSION ON PH | \n",
- " 681794 | \n",
- " 8137 | \n",
- "
\n",
- " \n",
- " 1 | \n",
- " springfield_instances | \n",
- " WMT:23-2ND STAGE COMPRESSOR-PH | \n",
- " 4 | \n",
- " 2024-11-09 13:17:39.283 | \n",
- " 2024-11-08 14:20:28.103 | \n",
- " node | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- " 2024-11-09T13:17:39.28327+00:00 | \n",
- " 23-2ND STAGE COMPRESSOR-PH | \n",
- " 2ND STAGE COMPRESSOR ON PH | \n",
- " 681825 | \n",
- " 8137 | \n",
- "
\n",
- " \n",
- " 2 | \n",
- " springfield_instances | \n",
- " WMT:23-1ST STAGE COMPRESSOR-PH | \n",
- " 4 | \n",
- " 2024-11-09 13:17:39.283 | \n",
- " 2024-11-08 14:21:06.876 | \n",
- " node | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- " 2024-11-09T13:17:39.28327+00:00 | \n",
- " 23-1ST STAGE COMPRESSOR-PH | \n",
- " 1ST STAGE COMPRESSOR ON PH | \n",
- " 681824 | \n",
- " 8137 | \n",
- "
\n",
- " \n",
- " 3 | \n",
- " springfield_instances | \n",
- " WMT:23-1ST STAGE COMPRESSION-PH | \n",
- " 4 | \n",
- " 2024-11-09 13:17:39.283 | \n",
- " 2024-11-08 14:21:06.876 | \n",
- " node | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- " {'space': 'springfield_instances', 'externalId... | \n",
- " 2024-11-09T13:17:39.28327+00:00 | \n",
- " 23-1ST STAGE COMPRESSION-PH | \n",
- " 1ST STAGE COMPRESSION ON PH | \n",
- " 681792 | \n",
- " 8137 | \n",
- "
\n",
- " \n",
- "
\n",
- "
"
- ],
- "text/plain": [
- "NodeListWithCursor([,\n",
- " ,\n",
- " ,\n",
- " ])"
- ]
- },
- "execution_count": 35,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "result[\"children\"]"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "bd779189-a9ee-4a54-8a3e-c9b0634929b2",
- "metadata": {},
- "source": [
- "## Querying across reverse direct relation of listable relation"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "c8b73be4-04f0-4b82-a9f8-364de5e087b4",
- "metadata": {},
- "source": [
- "We want all activities connected to equipment. The property `activities` in `CogniteEquipment` is a reverse direct relation of `equipment` in `CogniteActivity`. \n",
- "The `/query` enpoint does not support querying accross a reverse direct relation of listable relation. Instead, we will first lookup `CogniteEquipment` and the after we have retrieved the equipment items,\n",
- "we will use `/search` to lookup activities that are connected to the equipment we found."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 42,
- "id": "a2a567c8-e21b-4dec-8fb8-1782f0b9d470",
- "metadata": {},
- "outputs": [],
- "source": [
- "equipment_view = dm.ViewId(\"cdf_cdm\", \"CogniteEquipment\", \"v1\")\n",
- "activity_view = dm.ViewId(\"cdf_cdm\", \"CogniteActivity\", \"v1\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 58,
- "id": "c3d22491-d4a0-4d57-9bb2-03c4434b6daa",
- "metadata": {},
- "outputs": [],
- "source": [
- "equipment = client.data_modeling.instances.list(limit=100, sources=equipment_view, space=data_space)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 59,
- "id": "7e8712b9-930e-4732-8df5-a9e113962452",
- "metadata": {},
- "outputs": [],
- "source": [
- "is_equipment = filters.In(activity_view.as_property_ref(\"equipment\"), [item.as_id() for item in equipment])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 60,
- "id": "4070ecce-adcc-4204-8523-74ab36357fd2",
- "metadata": {},
- "outputs": [],
- "source": [
- "activities = client.data_modeling.instances.search(activity_view, filter=is_equipment, limit=1_000)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 61,
- "id": "8be52c76-3fcc-4d4e-ba65-f20df5e7ed9c",
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "\n",
- "\n",
- "
\n",
- " \n",
- " \n",
- " | \n",
- " space | \n",
- " external_id | \n",
- " version | \n",
- " last_updated_time | \n",
- " created_time | \n",
- " instance_type | \n",
- " assets | \n",
- " equipment | \n",
- " timeSeries | \n",
- " name | \n",
- " tags | \n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " 0 | \n",
- " springfield_instances | \n",
- " 6186996212022693 | \n",
- " 3 | \n",
- " 2024-11-09 04:00:55.328 | \n",
- " 2024-11-08 14:20:32.388 | \n",
- " node | \n",
- " [] | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " [] | \n",
- " ***-årlig visuell inspeksjon | \n",
- " [23-LTX-92529] | \n",
- "
\n",
- " \n",
- " 1 | \n",
- " springfield_instances | \n",
- " 226284304135285 | \n",
- " 3 | \n",
- " 2024-11-09 04:00:55.328 | \n",
- " 2024-11-08 14:20:32.968 | \n",
- " node | \n",
- " [] | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " [] | \n",
- " Årlig sampling assessment | \n",
- " [23-LTX-92529] | \n",
- "
\n",
- " \n",
- " 2 | \n",
- " springfield_instances | \n",
- " 8413402637731963 | \n",
- " 3 | \n",
- " 2024-11-09 04:00:55.328 | \n",
- " 2024-11-08 14:20:37.196 | \n",
- " node | \n",
- " [] | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " [] | \n",
- " Årlig sampling assessment | \n",
- " [23-LTX-92529] | \n",
- "
\n",
- " \n",
- " 3 | \n",
- " springfield_instances | \n",
- " 7022411329171792 | \n",
- " 3 | \n",
- " 2024-11-09 04:00:55.328 | \n",
- " 2024-11-08 14:20:48.208 | \n",
- " node | \n",
- " [] | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " [] | \n",
- " Årlig sampling assessment | \n",
- " [23-LTX-92529] | \n",
- "
\n",
- " \n",
- " 4 | \n",
- " springfield_instances | \n",
- " 1641843051187709 | \n",
- " 3 | \n",
- " 2024-11-09 04:00:55.328 | \n",
- " 2024-11-08 14:20:53.101 | \n",
- " node | \n",
- " [] | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " [] | \n",
- " Årlig sampling assessment | \n",
- " [23-LTX-92529] | \n",
- "
\n",
- " \n",
- " ... | \n",
- " ... | \n",
- " ... | \n",
- " ... | \n",
- " ... | \n",
- " ... | \n",
- " ... | \n",
- " ... | \n",
- " ... | \n",
- " ... | \n",
- " ... | \n",
- " ... | \n",
- "
\n",
- " \n",
- " 995 | \n",
- " springfield_instances | \n",
- " 6697040221840231 | \n",
- " 5 | \n",
- " 2024-11-09 13:18:44.093 | \n",
- " 2024-11-08 14:21:02.745 | \n",
- " node | \n",
- " [] | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " | \n",
- " [23-PDT-96167] | \n",
- "
\n",
- " \n",
- " 996 | \n",
- " springfield_instances | \n",
- " 7977648973841643 | \n",
- " 5 | \n",
- " 2024-11-09 13:18:44.093 | \n",
- " 2024-11-08 14:21:02.745 | \n",
- " node | \n",
- " [] | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " *** Monthly monitoring | \n",
- " [23-TT-96181-03] | \n",
- "
\n",
- " \n",
- " 997 | \n",
- " springfield_instances | \n",
- " 1483812283636412 | \n",
- " 5 | \n",
- " 2024-11-09 13:18:44.093 | \n",
- " 2024-11-08 14:21:02.745 | \n",
- " node | \n",
- " [] | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " ***-årlig visuell inspeksjon | \n",
- " [23-VA-9110-M01] | \n",
- "
\n",
- " \n",
- " 998 | \n",
- " springfield_instances | \n",
- " 6512662427125816 | \n",
- " 5 | \n",
- " 2024-11-09 13:18:44.176 | \n",
- " 2024-11-08 14:20:36.244 | \n",
- " node | \n",
- " [] | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " IPL/SIF - Funksjonstest av Sikkerhetskritisk I... | \n",
- " [23-TT-96181-03] | \n",
- "
\n",
- " \n",
- " 999 | \n",
- " springfield_instances | \n",
- " 840223490524167 | \n",
- " 5 | \n",
- " 2024-11-09 13:18:44.176 | \n",
- " 2024-11-08 14:20:36.244 | \n",
- " node | \n",
- " [] | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " [{'space': 'springfield_instances', 'externalI... | \n",
- " Ettersyn av instrumentering | \n",
- " [23-PT-96186-03] | \n",
- "
\n",
- " \n",
- "
\n",
- "
1000 rows × 11 columns
\n",
- "
"
- ],
- "text/plain": [
- "NodeList([,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- " ,\n",
- "