Skip to content

Commit

Permalink
QA on cognite functions (cognitedata#4)
Browse files Browse the repository at this point in the history
* update cognite-sdk-experimental to latest version

* Reorganize cells in example 1
- Moved verification of login-attempt right below initiation of CogniteClient
- Added cell for displaying available assets in the cluster

* Formatted code in example 2

* Formatted code in example 3
  • Loading branch information
simenkristoff authored Jun 29, 2022
1 parent 38864a9 commit 8e46826
Show file tree
Hide file tree
Showing 8 changed files with 296 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,45 @@
"from getpass import getpass\n",
"from cognite.experimental import CogniteClient\n",
"\n",
"project = \"<CDF project>\" # Fill in your project here\n",
"cdf_cluster = \"<CDF cluster>\" # Fill in the cluster your project is running in (for instance 'api'/'westeurope-1')\n",
"project = \"<CDF project>\" # Fill in your project here\n",
"cdf_cluster = \"<CDF cluster>\" # Fill in the cluster your project is running in (for instance 'api'/'westeurope-1')\n",
"\n",
"tenant_id = \"<Tenant ID>\" # Fill in your Azure AD tenant ID here\n",
"token_client_id = \"<Token Client ID>\" # Fill in your Client ID here\n",
"tenant_id = \"<Tenant ID>\" # Fill in your Azure AD tenant ID here\n",
"token_client_id = \"<Token Client ID>\" # Fill in your Client ID here\n",
"token_client_secret = getpass(\"Paste your `token_client_secret` in here: \")\n",
"\n",
"token_url = f\"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token\"\n",
"token_scopes = [f\"https://{cdf_cluster}.cognitedata.com/.default\"]\n",
"\n",
"client = CogniteClient(\n",
" project = project,\n",
" server = cdf_cluster,\n",
" client_name = \"my first Cognite Function\",\n",
" token_url = token_url,\n",
" token_scopes = token_scopes,\n",
" token_client_id = token_client_id,\n",
" token_client_secret = token_client_secret,\n",
" project=project,\n",
" server=cdf_cluster,\n",
" client_name=\"my first Cognite Function\",\n",
" token_url=token_url,\n",
" token_scopes=token_scopes,\n",
" token_client_id=token_client_id,\n",
" token_client_secret=token_client_secret,\n",
")"
]
},
{
"cell_type": "markdown",
"id": "6c667444",
"metadata": {},
"source": [
"If we want to verify whether we have logged in successfully, and what capabilites our credentials has, we can call `client.iam.token.inspect()`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b4c12d0b",
"metadata": {},
"outputs": [],
"source": [
"client.iam.token.inspect()"
]
},
{
"cell_type": "markdown",
"id": "1b839790-c15f-4b62-ab75-6db36cf8c2a1",
Expand All @@ -95,16 +113,28 @@
"# If you have no assets in your CDF project but would like to add some for this example, run this cell.\n",
"if len(client.assets.list()) == 0:\n",
" from cognite.client.data_classes import Asset\n",
" \n",
"\n",
" client.assets.create([Asset(name=\"my_first_asset\"), Asset(name=\"my_second_asset\")])"
]
},
{
"cell_type": "markdown",
"id": "8f1b4ff6-1d63-4baa-b7b3-c0937f02f20e",
"id": "475c1c3b",
"metadata": {},
"source": [
"If we want to verify whether we have logged in successfully, and what capabilites our credentials has, we can call `client.iam.token.inspect()`."
"We use the code-snippet below to verify that our project contains assets."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d6638687",
"metadata": {},
"outputs": [],
"source": [
"# Print the first 5 assets in your CDF project\n",
"for asset in client.assets.list()[:5]:\n",
" print(asset)"
]
},
{
Expand Down Expand Up @@ -147,9 +177,7 @@
" all_assets = client.assets.list()\n",
" # We finally print the name of the selected asset, and return the name.\n",
" print(all_assets[asset_number].name)\n",
" return {\n",
" \"assetName\": all_assets[asset_number].name\n",
" }"
" return {\"assetName\": all_assets[asset_number].name}"
]
},
{
Expand All @@ -169,7 +197,7 @@
"metadata": {},
"outputs": [],
"source": [
"handle(client, {\"assetNo\" : 1})"
"handle(client, {\"assetNo\": 1})"
]
},
{
Expand All @@ -193,9 +221,7 @@
"external_id = \"my_first_cognite_function\"\n",
"\n",
"function = client.functions.create(\n",
" name = fn_name,\n",
" external_id = external_id,\n",
" function_handle = handle\n",
" name=fn_name, external_id=external_id, function_handle=handle\n",
")"
]
},
Expand All @@ -219,8 +245,8 @@
"# Repeat until status is ready\n",
"while function.status != \"Ready\":\n",
" function.update()\n",
" \n",
" if function.status == \"Failed\": \n",
"\n",
" if function.status == \"Failed\":\n",
" print(\"Failed to deploy function\")\n",
" break\n",
"else:\n",
Expand Down Expand Up @@ -277,19 +303,11 @@
"source": [
"call.get_logs()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "24aeacfd-7484-4299-8266-a74aed906d5a",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3.10.5 64-bit",
"language": "python",
"name": "python3"
},
Expand All @@ -303,7 +321,12 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
"version": "3.10.5"
},
"vscode": {
"interpreter": {
"hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
}
}
},
"nbformat": 4,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,24 @@
"from getpass import getpass\n",
"from cognite.experimental import CogniteClient\n",
"\n",
"project = \"<CDF project>\" # Fill in your project here\n",
"cdf_cluster = \"<CDF cluster>\" # Fill in the cluster your project is running in (for instance 'api'/'westeurope-1')\n",
"project = \"<CDF project>\" # Fill in your project here\n",
"cdf_cluster = \"<CDF cluster>\" # Fill in the cluster your project is running in (for instance 'api'/'westeurope-1')\n",
"\n",
"tenant_id = \"<Tenant ID>\" # Fill in your Azure AD tenant ID here\n",
"token_client_id = \"<Token Client ID>\" # Fill in your Client ID here\n",
"tenant_id = \"<Tenant ID>\" # Fill in your Azure AD tenant ID here\n",
"token_client_id = \"<Token Client ID>\" # Fill in your Client ID here\n",
"token_client_secret = getpass(\"Paste your `token_client_secret` in here: \")\n",
"\n",
"token_url = f\"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token\"\n",
"token_scopes = [f\"https://{cdf_cluster}.cognitedata.com/.default\"]\n",
"\n",
"client = CogniteClient(\n",
" project = project,\n",
" server = cdf_cluster,\n",
" client_name = \"my second Cognite Function\",\n",
" token_url = token_url,\n",
" token_scopes = token_scopes,\n",
" token_client_id = token_client_id,\n",
" token_client_secret = token_client_secret,\n",
" project=project,\n",
" server=cdf_cluster,\n",
" client_name=\"my second Cognite Function\",\n",
" token_url=token_url,\n",
" token_scopes=token_scopes,\n",
" token_client_id=token_client_id,\n",
" token_client_secret=token_client_secret,\n",
")"
]
},
Expand Down Expand Up @@ -95,6 +95,7 @@
"outputs": [],
"source": [
"import handler\n",
"\n",
"handler.handle(data={\"value\": 2.0})"
]
},
Expand All @@ -121,7 +122,7 @@
},
"outputs": [],
"source": [
"fn_name = \"my-transformer-function\" \n",
"fn_name = \"my-transformer-function\"\n",
"external_id = \"my-transformer-function\""
]
},
Expand All @@ -141,11 +142,11 @@
"outputs": [],
"source": [
"function = client.functions.create(\n",
" name=fn_name, \n",
" name=fn_name,\n",
" external_id=external_id,\n",
" folder='.', # We will upload this directory\n",
" description=\"Square an input value\", \n",
" owner=\"Ola Normann\"\n",
" folder=\".\", # We will upload this directory\n",
" description=\"Square an input value\",\n",
" owner=\"Ola Normann\",\n",
")"
]
},
Expand All @@ -167,8 +168,8 @@
"# Repeat until status is ready\n",
"while function.status != \"Ready\":\n",
" function.update()\n",
" \n",
" if function.status == \"Failed\": \n",
"\n",
" if function.status == \"Failed\":\n",
" print(\"Failed to deploy function\")\n",
" break\n",
"else:\n",
Expand Down Expand Up @@ -246,7 +247,7 @@
"metadata": {},
"outputs": [],
"source": [
"call_bad.get_logs()[-2:] # Here we see the error"
"call_bad.get_logs()[-2:] # Here we see the error"
]
},
{
Expand All @@ -273,16 +274,17 @@
"metadata": {},
"outputs": [],
"source": [
"client_credentials = {'client_id': token_client_id, 'client_secret': token_client_secret}\n",
"client_credentials = {\n",
" \"client_id\": token_client_id,\n",
" \"client_secret\": token_client_secret,\n",
"}\n",
"schedule = client.functions.schedules.create(\n",
" name = \"run-function-every-minute\",\n",
" cron_expression = \"* * * * *\", # the cron expression runs every minutes\n",
" function_id = function.id, # we specify the ID of the function we want to schedule\n",
" client_credentials = client_credentials, # this is a dictionary with 'client_secret' and 'client_id'\n",
" data = { # this is the data we wish to call the function with\n",
" \"value\": 2\n",
" },\n",
" description = \"This schedule runs the function every minute\"\n",
" name=\"run-function-every-minute\",\n",
" cron_expression=\"* * * * *\", # the cron expression runs every minutes\n",
" function_id=function.id, # we specify the ID of the function we want to schedule\n",
" client_credentials=client_credentials, # this is a dictionary with 'client_secret' and 'client_id'\n",
" data={\"value\": 2}, # this is the data we wish to call the function with\n",
" description=\"This schedule runs the function every minute\",\n",
")"
]
},
Expand Down Expand Up @@ -312,6 +314,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Get the response of the last function call\n",
"calls[-1].get_response()"
]
},
Expand All @@ -335,7 +338,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3.10.5 64-bit",
"language": "python",
"name": "python3"
},
Expand All @@ -349,7 +352,12 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
"version": "3.10.5"
},
"vscode": {
"interpreter": {
"hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
}
}
},
"nbformat": 4,
Expand Down
10 changes: 4 additions & 6 deletions examples/02-creating-a-function-from-a-folder/handler.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from helper import square


def foo():
return "bar"


def handle(data):
# You can refer to functions outside the handle function
bar = foo()
Expand All @@ -11,9 +13,5 @@ def handle(data):
value = data["value"]
squared_value = square(value)
print(f"Square of {value} is {squared_value}")

return {
"squaredValue": squared_value,
"value": value,
"foo": bar
}

return {"squaredValue": squared_value, "value": value, "foo": bar}
Loading

0 comments on commit 8e46826

Please sign in to comment.