{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Checking the monitoring component"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"import datetime\n",
"import uuid\n",
"import random\n",
"\n",
"from dotenv import load_dotenv\n",
"from IPython.display import clear_output, display, HTML\n",
"\n",
"from common.tools.timestamp.Converters import timestamp_utcnow_to_float, timestamp_float_to_string\n",
"from common.tools.grpc.Tools import grpc_message_to_json_string\n",
"from common.proto.kpi_sample_types_pb2 import KpiSampleType\n",
"from common.proto.monitoring_pb2 import KpiDescriptor, KpiId, KpiQuery, Kpi\n",
"from monitoring.client.MonitoringClient import MonitoringClient"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'0abfb00117d4461b9fa5085bee4be58f'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"load_dotenv()\n",
"\n",
"monitoring_client = MonitoringClient()\n",
"uuid.uuid4().hex"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Created KPI {\"kpi_id\": {\"uuid\": \"1\"}}: \n"
]
}
],
"source": [
"kpi_description: KpiDescriptor = KpiDescriptor()\n",
"kpi_description.kpi_description = \"Security status of service {}\".format(uuid.uuid4().hex)\n",
"kpi_description.service_id.service_uuid.uuid = \"608df176-90b8-5950-b50d-1810c6eaaa5d\"\n",
"kpi_description.kpi_sample_type = KpiSampleType.KPISAMPLETYPE_UNKNOWN\n",
"new_kpi = monitoring_client.SetKpi(kpi_description)\n",
"print(\"Created KPI {}: \".format(grpc_message_to_json_string(new_kpi)))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
" \n",
" | 2023-02-24 16:23:34.373384 |
\n",
" | KPI ID | Timestamp | Value |
\n",
" \n",
" | 0 - 1 | 2023-02-23T13:55:09Z | floatVal: 1868.0\n",
" |
| 1 - 1 | 2023-02-23T13:55:07Z | floatVal: 1878.0\n",
" |
| 2 - 1 | 2023-02-23T13:55:05Z | floatVal: 2065.0\n",
" |
| 3 - 1 | 2023-02-23T13:55:03Z | floatVal: 1993.0\n",
" |
| 4 - 1 | 2023-02-23T13:55:01Z | floatVal: 2006.0\n",
" |
| 5 - 1 | 2023-02-23T13:54:59Z | floatVal: 1938.0\n",
" |
| 6 - 1 | 2023-02-23T13:54:57Z | floatVal: 1920.0\n",
" |
| 7 - 1 | 2023-02-23T13:54:55Z | floatVal: 1984.0\n",
" |
| 8 - 1 | 2023-02-23T13:54:53Z | floatVal: 1883.0\n",
" |
| 9 - 1 | 2023-02-23T13:54:51Z | floatVal: 1948.0\n",
" |
"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn [4], line 31\u001b[0m\n\u001b[1;32m 29\u001b[0m table \u001b[39m+\u001b[39m\u001b[39m=\u001b[39m \u001b[39m\"\u001b[39m\u001b[39m\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m 30\u001b[0m display(HTML(table))\n\u001b[0;32m---> 31\u001b[0m time\u001b[39m.\u001b[39;49msleep(\u001b[39m5\u001b[39;49m)\n\u001b[1;32m 32\u001b[0m clear_output(wait\u001b[39m=\u001b[39m\u001b[39mTrue\u001b[39;00m)\n",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"kpi_id = input(\"What is the KPI ID?\")\n",
"query = KpiQuery()\n",
"query.kpi_ids.append(KpiId(**{\"kpi_id\": {\"uuid\": kpi_id}}))\n",
"query.last_n_samples = 10\n",
"\n",
"while True:\n",
"\n",
" kpi = Kpi()\n",
" kpi.kpi_id.kpi_id.uuid = new_kpi.kpi_id.uuid\n",
" kpi.timestamp.timestamp = timestamp_utcnow_to_float()\n",
" kpi.kpi_value.int32Val = random.randint(10, 4000)\n",
" # monitoring_client.IncludeKpi(kpi)\n",
"\n",
" response = monitoring_client.QueryKpiData(query)\n",
" # print(response)\n",
" table = f\"\"\"\n",
" \n",
" | {datetime.datetime.now()} |
\n",
" | KPI ID | Timestamp | Value |
\n",
" \n",
" \"\"\"\n",
" for kpi in response.raw_kpi_lists:\n",
" cur_kpi_id = kpi.kpi_id.kpi_id.uuid\n",
" for i, raw_kpi in enumerate(kpi.raw_kpis):\n",
" # print(cur_kpi_id, raw_kpi.timestamp.timestamp, raw_kpi.kpi_value)\n",
" table += \"| {} - {} | {} | {} |
\".format(\n",
" i, cur_kpi_id, timestamp_float_to_string(raw_kpi.timestamp.timestamp), raw_kpi.kpi_value\n",
" )\n",
" table += \"
\"\n",
" display(HTML(table))\n",
" time.sleep(5)\n",
" clear_output(wait=True)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "tfs",
"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.9.14"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "7ea5723b29014fc8d8bf1a065f5287f0787f54201758f2b5d4b4b0b2ddc48863"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}