{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a0ddeac4",
   "metadata": {},
   "source": [
    "# Creating basic threshold rules\n",
    "\n",
    "This notebook provides a step-by-step guide to set up, test, and deploy Rule models using Falkonry APIs. Follow each section to create a workspace, define rules, evaluate them, set up assessments, and monitor live outputs.\n",
    "\n",
    "### 7 Steps to going live with your basic threshold rule\n",
    "1. Set Up Falkonry API Access\n",
    "2. Create Workspace\n",
    "3. Create a New Rule\n",
    "4. Run RULEEVAL for Testing (Optional)\n",
    "5. Create Assessment\n",
    "6. Get Assessment Info and Extract Output Signals\n",
    "7. Run RULEEVAL for Live Assessment Output"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "821e05fa",
   "metadata": {},
   "source": [
    "COPYRIGHT (c) 2026 FALKONRY, INC. ALL RIGHTS RESERVED.\n",
    "\n",
    "FOR FALKONRY's CUSTOMER USE ONLY.\n",
    "\n",
    "THIS SAMPLE CODE FROM FALKONRY IS PROVIDED \"AS IS\" AND WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL FALKONRY INC. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) SUSTAINED BY YOU OR A THIRD PARTY, HOWEVER CAUSED AND UNDER ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ARISING IN ANY WAY OUT OF THE USE OF OR INABILITY TO USE THIS SAMPLE CODE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a862f9a9",
   "metadata": {},
   "source": [
    "## 1. Set Up Falkonry API Access\n",
    "\n",
    "Configure authentication and base URL for Falkonry API requests. Set your API key, account_id, and other required variables."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b17f99d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Testing API connection...\n",
      "https:///api/1.3/accounts/\n",
      "Connection error: Invalid URL 'https:///api/1.3/accounts/': No host supplied\n",
      "STOP! Check your API connection parameters and try again.\n"
     ]
    }
   ],
   "source": [
    "# Set up Falkonry API access\n",
    "import requests\n",
    "\n",
    "# Set your API connection and account ID here\n",
    "BASE_SERVER = \"\" # e.g., app3.falkonry.ai\n",
    "API_TOKEN   = \"\" # <YOUR_API_TOKEN>\n",
    "ACCOUNT_ID  = \"\" # <YOUR_ACCOUNT_ID>\n",
    "BASE_URL    = f\"https://{BASE_SERVER}/api/1.3\"\n",
    "HEADERS     = {\n",
    "    \"Authorization\": f\"Bearer {API_TOKEN}\",\n",
    "    \"Content-Type\": \"application/json\"\n",
    "}\n",
    "\n",
    "# Set your workspace, rule and assessment parameters here\n",
    "WORKSPACE_ID = \"\" # e.g., \"1387690529049334272\"\n",
    "WORKSPACE_NAME = \"\" # e.g., \"Machine 2 Model Development\"\n",
    "RULE_NAME = \"\" # e.g., \"Machine2_High_Current\"\n",
    "ASSESSMENT_NAME = RULE_NAME\n",
    "DESCRIPTION = \"\"# Description of the Flow e.g., \"High Current rule on machine 2\".\n",
    "NAME = \"\" # Name of the Flow e.g., \"High Current\".\n",
    "INPUT_SIGNALS = [ # e.g.,\n",
    "            # {\"name\": \"plant1/line1/machine2/current1\", \"valueType\": \"Numeric\"},\n",
    "            # {\"name\": \"plant1/line1/machine2/current2\", \"valueType\": \"Numeric\"},\n",
    "            # {\"name\": \"plant1/line1/machine2/current3\", \"valueType\": \"Numeric\"},\n",
    "        ]\n",
    "INPUT_SIGNALS_WITH_IDS = [ # e.g.,\n",
    "            # {\"name\": \"plant1/line1/machine2/current1\", \"signal\": \"1387690534045331456\"},\n",
    "            # {\"name\": \"plant1/line1/machine2/current2\", \"signal\": \"1387690531629412352\"},\n",
    "            # {\"name\": \"plant1/line1/machine2/current3\", \"signal\": \"1387690534695448576\"}\n",
    "        ]\n",
    "OUTPUT_SIGNAL_PREFIX = \"\" # e.g., \"test01\"\n",
    "\n",
    "RULE_PARAM_ALERT_FREQUENCY = \"\" # e.g., \"PT1H\"\n",
    "RULE_PARAM_COVERAGE = 20 # e.g., 20\n",
    "RULE_PARAM_DENSITY = 12 # e.g., 12\n",
    "RULE_PARAM_EVALUATION_WINDOW = \"PT5M\" # e.g., \"PT5M\"\n",
    "RULE_PARAM_EXPRESSION_CONDITION = \">\" # e.g., \">\" {\"<\", \">\", \">=\", \"<=\"}\n",
    "RULE_PARAM_EXPRESSION_VALUE = 15 # e.g., 15\n",
    "RULE_PARAM_STATISTIC = \"max\" # e.g., \"max\" {\"max, \"min\", \"mean\", \"count\"}\n",
    "RULE_PARAM_VALUE_TYPE = \"Numeric\" # e.g., \"Numeric\" {\"Numeric\", \"Categorical\"}\n",
    "\n",
    "EVAL_TIME_RANGE = {\n",
    "            \"startTime\": \"\", # e.g., \"2024-07-22T07:00:00.00Z\",\n",
    "            \"endTime\": \"\" # e.g., \"2024-10-10T23:59:59.00Z\"\n",
    "        }\n",
    "\n",
    "# Test the API connection before proceeding with the rest of the notebook\n",
    "print(\"Testing API connection...\")\n",
    "try:\n",
    "    test_url = f\"{BASE_URL}/accounts/{ACCOUNT_ID}\"\n",
    "    print (test_url)\n",
    "    response = requests.get(test_url, headers=HEADERS, timeout=10)\n",
    "    print(f\"Status code: {response.status_code}\")\n",
    "    if response.status_code == 200:\n",
    "        print(\"API connection successful!\")\n",
    "        print(\"Proceed with the rest of the notebook...\")\n",
    "    else:\n",
    "        print(f\"API error: {response.text[:100]}\")\n",
    "        print(\"STOP! Check your API connection parameters and try again.\")\n",
    "except Exception as e:\n",
    "    print(f\"Connection error: {str(e)}\")\n",
    "    print(\"STOP! Check your API connection parameters and try again.\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ecfa1fa5",
   "metadata": {},
   "source": [
    "## 2. Create Workspace\n",
    "\n",
    "Send a POST request to `/api/1.3/accounts/{{account_id}}/workspace` to create a new workspace for development. It is recommended that you have 1 workspace for ech asset you want to model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "65cfb96a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Status: 201\n",
      "Response: {'id': '1511839170830970880', 'tenant': '1356886815915696128', 'type': 'entities.workspace', 'name': 'Machine 2 Model Development', 'description': 'Created by notebook', 'createTime': 1780520946939, 'updateTime': 1780520946939, 'createdBy': '596582102439518208', 'updatedBy': '596582102439518208', 'archived': False, 'links': []}\n",
      "Created Workspace ID: 1511839170830970880\n"
     ]
    }
   ],
   "source": [
    "if WORKSPACE_ID:\n",
    "    workspace_id = WORKSPACE_ID\n",
    "    print(\"Using the Existing Workspace ID:\", workspace_id)\n",
    "else:\n",
    "    # Create a new workspace\n",
    "    workspace_payload = {\n",
    "        \"name\": WORKSPACE_NAME,\n",
    "        \"description\": \"Created by notebook\"\n",
    "    }\n",
    "\n",
    "    response = requests.post(\n",
    "        f\"{BASE_URL}/accounts/{ACCOUNT_ID}/workspace\",\n",
    "        headers=HEADERS,\n",
    "        json=workspace_payload\n",
    "    )\n",
    "\n",
    "    print(\"Status:\", response.status_code)\n",
    "    print(\"Response:\", response.json())\n",
    "\n",
    "    workspace_id = response.json().get(\"id\")\n",
    "    print(\"Created Workspace ID:\", workspace_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95b6ce38",
   "metadata": {},
   "source": [
    "## 3. Create a New Rule\n",
    "\n",
    "Send a POST request to `/api/1.3/accounts/{{account_id}}/flows` to create a new rule in the development workspace. Use the payload specifying rule details, input schema, and model configuration."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b352912",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Status: 201\n",
      "Response: {'id': '1511839186873843712', 'type': 'entities.flow', 'tenant': '1356886815915696128', 'flowType': 'MODELSETUP', 'status': 'CREATED', 'name': 'New rule for High Current', 'description': 'A rule for High Current rule on machine 2', 'spec': {'modelName': 'Machine2_High_Current', 'modelType': 'RULE', 'modelDetails': {'statistic': 'max', 'expression': {'condition': '>', 'value': 15}, 'valueType': 'Numeric', 'alertFrequency': 'PT1H', 'evaluationWindow': 'PT5M', 'coverage': 20, 'density': 12, 'clockSignals': [], 'assessmentRate': None, 'delayTolerance': None}, 'inputschema': [{'name': 'plant1/line1/machine2/current1', 'valueType': 'Numeric'}, {'name': 'plant1/line1/machine2/current2', 'valueType': 'Numeric'}, {'name': 'plant1/line1/machine2/current3', 'valueType': 'Numeric'}], 'workspace': '1511839170830970880', 'config': {}}, 'outputs': [], 'messages': [], 'createTime': 1780520950764, 'updateTime': 1780520950764, 'createdBy': '596582102439518208', 'updatedBy': '596582102439518208', 'archived': False, 'transitions': [{'id': '1', 'status': 'CREATED', 'createTime': '2026-06-03T21:09:10.764000Z'}], 'links': []}\n",
      "MODELSETUP Flow ID: 1511839186873843712\n"
     ]
    }
   ],
   "source": [
    "# Create a new rule\n",
    "rule_payload = {\n",
    "    \"description\": \"A rule for \" + DESCRIPTION,\n",
    "    \"flowType\": \"MODELSETUP\",\n",
    "    \"name\": \"New rule for \" + NAME,\n",
    "    \"spec\": {\n",
    "        \"inputschema\": INPUT_SIGNALS,\n",
    "        \"modelDetails\": {\n",
    "            \"alertFrequency\": RULE_PARAM_ALERT_FREQUENCY,\n",
    "            \"coverage\": RULE_PARAM_COVERAGE,\n",
    "            \"density\": RULE_PARAM_DENSITY,\n",
    "            \"evaluationWindow\": RULE_PARAM_EVALUATION_WINDOW,\n",
    "            \"expression\": {\"condition\": RULE_PARAM_EXPRESSION_CONDITION, \"value\": RULE_PARAM_EXPRESSION_VALUE},\n",
    "            \"statistic\": RULE_PARAM_STATISTIC,\n",
    "            \"valueType\": RULE_PARAM_VALUE_TYPE\n",
    "        },\n",
    "        \"modelName\": RULE_NAME,\n",
    "        \"modelType\": \"RULE\",\n",
    "        \"workspace\": workspace_id \n",
    "    }\n",
    "}\n",
    "\n",
    "response = requests.post(\n",
    "    f\"{BASE_URL}/accounts/{ACCOUNT_ID}/flows\",\n",
    "    headers=HEADERS,\n",
    "    json=rule_payload\n",
    ")\n",
    "\n",
    "print(\"Status:\", response.status_code)\n",
    "print(\"Response:\", response.json())\n",
    "flow_id = response.json().get(\"id\")\n",
    "print(\"MODELSETUP Flow ID:\", flow_id)\n",
    "\n",
    "#get the model ID from the created flow\n",
    "response = requests.get(\n",
    "    f\"{BASE_URL}/accounts/{ACCOUNT_ID}/flows/{flow_id}\",\n",
    "    headers=HEADERS)\n",
    "\n",
    "print(response.json())\n",
    "model_id = response.json()[\"outputs\"][0][\"model\"]\n",
    "print(\"Model ID:\", model_id)\n",
    "\n",
    "# Send a GET request to `/models/{model_id}?denorm=inputschema&denorm=outputschema` to get\n",
    "# info on the input and output schema for the model. This is useful in debugging failed \n",
    "# RULEEVAL flows due to mismatch in the input schema and output schema.\n",
    "#\n",
    "# Get the model details including input and output schema\n",
    "response = requests.get(\n",
    "    f\"{BASE_URL}/accounts/{ACCOUNT_ID}/models/{model_id}?denorm=inputschema&denorm=outputschema\",\n",
    "    headers=HEADERS)\n",
    "\n",
    "print(response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "647a445f",
   "metadata": {},
   "source": [
    "## 4. Run RULEEVAL for Testing (Optional)\n",
    "\n",
    "Send a POST request to `/api/1.3/accounts/{{account_id}}/flows` with `flowType` 'RULEEVAL' and an `outputsignalPrefix` to test the rule on historical data without affecting assessment outputs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "450cbb03",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Status: 201\n",
      "Response: {'id': '1511825223016951808', 'type': 'entities.flow', 'tenant': '1356886815915696128', 'flowType': 'RULEEVAL', 'status': 'CREATED', 'name': 'Rule Eval for High Current', 'description': 'Evaluating the High Current rule on machine 1', 'spec': {'model': '1511797497673121792', 'modelType': 'RULE', 'timeRange': {'startTime': '2024-07-22T07:00:00.000000Z', 'endTime': '2024-10-10T23:59:59.000000Z'}, 'modelDetails': {'statistic': 'max', 'expression': {'condition': '>', 'value': 15}, 'valueType': 'Numeric', 'alertFrequency': 'PT1H', 'evaluationWindow': 'PT5M', 'level': 10, 'coverage': 20, 'density': 12, 'clockSignals': [], 'assessmentRate': 'PT10S', 'delayTolerance': 'PT10M'}, 'inputsignals': [{'signal': '1387690535492366336', 'name': 'plant1/line1/machine1/current1'}, {'signal': '1387690533311328256', 'name': 'plant1/line1/machine1/current2'}, {'signal': '1387690534225686528', 'name': 'plant1/line1/machine1/current3'}], 'outputsignalPrefix': 'test01', 'outputsignals': [], 'assessmentRate': None, 'workspace': '1511797484784365568', 'config': {'flow.ephemeral.workspace': 's3a://falkonry-customer-1356886815915696128/backend/workspace/1356886815915696128/FLOWDATA/1511825223016951808'}}, 'outputs': [], 'messages': [], 'createTime': 1780517621521, 'updateTime': 1780517621521, 'createdBy': '596582102439518208', 'updatedBy': '596582102439518208', 'archived': False, 'transitions': [{'id': '1', 'status': 'CREATED', 'createTime': '2026-06-03T20:13:41.521000Z'}], 'links': []}\n"
     ]
    }
   ],
   "source": [
    "ruleeval_payload = {\n",
    "    \"description\": \"Evaluating the \" + DESCRIPTION,\n",
    "    \"flowType\": \"RULEEVAL\",\n",
    "    \"name\": \"Rule Eval for \" + NAME,\n",
    "    \"spec\": {\n",
    "        \"inputsignals\": INPUT_SIGNALS_WITH_IDS,\n",
    "        \"model\": model_id,\n",
    "        \"outputsignalPrefix\": OUTPUT_SIGNAL_PREFIX,\n",
    "        \"timeRange\": EVAL_TIME_RANGE,\n",
    "        \"workspace\": workspace_id\n",
    "    }\n",
    "}\n",
    "\n",
    "response = requests.post(\n",
    "    f\"{BASE_URL}/accounts/{ACCOUNT_ID}/flows\",\n",
    "    headers=HEADERS,\n",
    "    json=ruleeval_payload\n",
    ")\n",
    "\n",
    "print(\"Status:\", response.status_code)\n",
    "print(\"Response:\", response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46c2e9e2",
   "metadata": {},
   "source": [
    "## 5. Create Assessment\n",
    "\n",
    "Send a POST request to `/api/1.3/accounts/{{account_id}}/flows` with `flowType` 'ASSESSMENTSETUP' to create a live assessment for the rule. Provide the model, assessment name, and input signals."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3b3cdf16",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Status: 201\n",
      "Response: {'id': '1511839618924904448', 'type': 'entities.flow', 'tenant': '1356886815915696128', 'flowType': 'ASSESSMENTSETUP', 'status': 'CREATED', 'name': 'Create assessment High Current', 'description': 'Created from notebook High Current rule on machine 2', 'spec': {'assessmentName': 'Machine2_High_Current', 'model': '1511839187989528576', 'assessmentRate': None, 'clockSignals': [], 'assessmentState': 'MONITORING', 'inputsignals': [{'signal': '1387690534045331456', 'name': 'plant1/line1/machine2/current1'}, {'signal': '1387690531629412352', 'name': 'plant1/line1/machine2/current2'}, {'signal': '1387690534695448576', 'name': 'plant1/line1/machine2/current3'}], 'outputsignals': [], 'config': {}}, 'outputs': [], 'messages': [], 'createTime': 1780521053773, 'updateTime': 1780521053773, 'createdBy': '596582102439518208', 'updatedBy': '596582102439518208', 'archived': False, 'transitions': [{'id': '1', 'status': 'CREATED', 'createTime': '2026-06-03T21:10:53.773000Z'}], 'links': []}\n",
      "Created Assessment: Machine2_High_Current\n"
     ]
    }
   ],
   "source": [
    "# Create a live assessment\n",
    "assessment_payload = {\n",
    "    \"description\": \"Created from notebook \" + DESCRIPTION,\n",
    "    \"flowType\": \"ASSESSMENTSETUP\",\n",
    "    \"name\": \"Create assessment \" + NAME,\n",
    "    \"spec\": {\n",
    "        \"assessmentName\": ASSESSMENT_NAME,\n",
    "        \"assessmentState\": \"MONITORING\",\n",
    "        \"inputsignals\": INPUT_SIGNALS_WITH_IDS,\n",
    "        \"model\": model_id \n",
    "    }\n",
    "}\n",
    "\n",
    "response = requests.post(\n",
    "    f\"{BASE_URL}/accounts/{ACCOUNT_ID}/flows\",\n",
    "    headers=HEADERS,\n",
    "    json=assessment_payload\n",
    ")\n",
    "\n",
    "print(\"Status:\", response.status_code)\n",
    "print(\"Response:\", response.json())\n",
    "assessment_name = response.json()[\"spec\"][\"assessmentName\"]\n",
    "print(\"Created Assessment:\", assessment_name)\n",
    "\n",
    "response = requests.get(\n",
    "    f\"{BASE_URL}/accounts/{ACCOUNT_ID}/assessments\",\n",
    "    headers=HEADERS)\n",
    "\n",
    "count = response.json()[0][\"count\"]\n",
    "\n",
    "response = requests.get(\n",
    "    f\"{BASE_URL}/accounts/{ACCOUNT_ID}/assessments?limit={count}\",\n",
    "    headers=HEADERS)\n",
    "\n",
    "response = response.json()\n",
    "for assessment in response:\n",
    "    if assessment[\"name\"] == ASSESSMENT_NAME:\n",
    "        assessment_id = assessment[\"id\"]\n",
    "        print(\"Assessment ID:\", assessment_id)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d0a84f0",
   "metadata": {},
   "source": [
    "## 6. Get Assessment Info and Extract Output Signals\n",
    "\n",
    "Send a GET request to `/api/1.3/accounts/{{account_id}}/assessments/{{assessment_id}}?denorm=inputSignalset&denorm=outputSignalset` to retrieve assessment info. Extract the `outputsignals` array for use in live rule evaluation.\n",
    "\n",
    "Wait for the above flow to complete before running the next cell."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7e0f007c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracted output signals:\n",
      "{'name': 'explanations-plant1/line1/machine2/current1', 'signal': '1511839620745232384'}\n",
      "{'name': 'explanations-plant1/line1/machine2/current2', 'signal': '1511839620921393152'}\n",
      "{'name': 'explanations-plant1/line1/machine2/current3', 'signal': '1511839621080776704'}\n",
      "{'name': 'rule', 'signal': '1511839621235965952'}\n",
      "{'name': 'alert', 'signal': '1511839619843457024'}\n"
     ]
    }
   ],
   "source": [
    "# Get assessment info and extract output signals    \n",
    "response = requests.get(\n",
    "    f\"{BASE_URL}/accounts/{ACCOUNT_ID}/assessments/{assessment_id}?denorm=inputSignalset&denorm=outputSignalset\",\n",
    "    headers=HEADERS\n",
    ")\n",
    "\n",
    "links = response.json()[\"links\"]\n",
    "for link in links:\n",
    "    if link[\"name\"] == \"outputSignalset\":\n",
    "        outputsignalset = link[\"object\"][\"signals\"]\n",
    "        outputsignals = [\n",
    "            {\"name\": sig[\"name\"], \"signal\": sig[\"signal\"]}\n",
    "            for sig in outputsignalset\n",
    "        ]\n",
    "\n",
    "print(\"Extracted output signals:\")\n",
    "for sig in outputsignals:\n",
    "    print(sig)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b587ed64",
   "metadata": {},
   "source": [
    "## 7. Run RULEEVAL for Live Assessment Output\n",
    "\n",
    "Send a POST request to `/api/1.3/accounts/{{account_id}}/flows` with `flowType` 'RULEEVAL', providing the extracted `outputsignals` to route rule evaluation results to assessment output signals."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "521bb549",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Status: 201\n",
      "Response: {'id': '1511839831563534336', 'type': 'entities.flow', 'tenant': '1356886815915696128', 'flowType': 'RULEEVAL', 'status': 'CREATED', 'name': 'Rule Eval for High Current', 'description': 'Evaluating the High Current rule on machine 2', 'spec': {'model': '1511839187989528576', 'modelType': 'RULE', 'timeRange': {'startTime': '2024-07-22T07:00:00.000000Z', 'endTime': '2024-10-10T23:59:59.000000Z'}, 'modelDetails': {'statistic': 'max', 'expression': {'condition': '>', 'value': 15}, 'valueType': 'Numeric', 'alertFrequency': 'PT1H', 'evaluationWindow': 'PT5M', 'level': 10, 'coverage': 20, 'density': 12, 'clockSignals': [], 'assessmentRate': 'PT10S', 'delayTolerance': 'PT10M'}, 'inputsignals': [{'signal': '1387690534045331456', 'name': 'plant1/line1/machine2/current1'}, {'signal': '1387690531629412352', 'name': 'plant1/line1/machine2/current2'}, {'signal': '1387690534695448576', 'name': 'plant1/line1/machine2/current3'}], 'outputsignals': [{'signal': '1511839620745232384', 'name': 'explanations-plant1/line1/machine2/current1'}, {'signal': '1511839620921393152', 'name': 'explanations-plant1/line1/machine2/current2'}, {'signal': '1511839621080776704', 'name': 'explanations-plant1/line1/machine2/current3'}, {'signal': '1511839621235965952', 'name': 'rule'}, {'signal': '1511839619843457024', 'name': 'alert'}], 'assessmentRate': None, 'workspace': '1511839170830970880', 'config': {'flow.ephemeral.workspace': 's3a://falkonry-customer-1356886815915696128/backend/workspace/1356886815915696128/FLOWDATA/1511839831563534336'}}, 'outputs': [], 'messages': [], 'createTime': 1780521104470, 'updateTime': 1780521104470, 'createdBy': '596582102439518208', 'updatedBy': '596582102439518208', 'archived': False, 'transitions': [{'id': '1', 'status': 'CREATED', 'createTime': '2026-06-03T21:11:44.470000Z'}], 'links': []}\n"
     ]
    }
   ],
   "source": [
    "# Run RULEEVAL for live assessment output\n",
    "ruleeval_live_payload = {\n",
    "    \"description\": \"Evaluating the \" + DESCRIPTION,\n",
    "    \"flowType\": \"RULEEVAL\",\n",
    "    \"name\": \"Rule Eval for \" + NAME,\n",
    "    \"spec\": {\n",
    "        \"inputsignals\": INPUT_SIGNALS_WITH_IDS,\n",
    "        \"model\": model_id,\n",
    "        \"outputsignals\": outputsignals,  # Use extracted outputsignals\n",
    "        \"timeRange\": EVAL_TIME_RANGE,\n",
    "        \"workspace\": workspace_id\n",
    "    }\n",
    "}\n",
    "\n",
    "response = requests.post(\n",
    "    f\"{BASE_URL}/accounts/{ACCOUNT_ID}/flows\",\n",
    "    headers=HEADERS,\n",
    "    json=ruleeval_live_payload\n",
    ")\n",
    "\n",
    "print(\"Status:\", response.status_code)\n",
    "print(\"Response:\", response.json())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
