Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
{
"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": [
"<table>\n",
" <thead>\n",
" <tr><th colspan=3>2023-02-24 16:23:34.373384</th></tr>\n",
" <tr><th>KPI ID</th><th>Timestamp</th><th>Value</th></tr>\n",
" <thead>\n",
" <tbody><tr><td>0 - 1</td><td>2023-02-23T13:55:09Z</td><td>floatVal: 1868.0\n",
"</td></tr><tr><td>1 - 1</td><td>2023-02-23T13:55:07Z</td><td>floatVal: 1878.0\n",
"</td></tr><tr><td>2 - 1</td><td>2023-02-23T13:55:05Z</td><td>floatVal: 2065.0\n",
"</td></tr><tr><td>3 - 1</td><td>2023-02-23T13:55:03Z</td><td>floatVal: 1993.0\n",
"</td></tr><tr><td>4 - 1</td><td>2023-02-23T13:55:01Z</td><td>floatVal: 2006.0\n",
"</td></tr><tr><td>5 - 1</td><td>2023-02-23T13:54:59Z</td><td>floatVal: 1938.0\n",
"</td></tr><tr><td>6 - 1</td><td>2023-02-23T13:54:57Z</td><td>floatVal: 1920.0\n",
"</td></tr><tr><td>7 - 1</td><td>2023-02-23T13:54:55Z</td><td>floatVal: 1984.0\n",
"</td></tr><tr><td>8 - 1</td><td>2023-02-23T13:54:53Z</td><td>floatVal: 1883.0\n",
"</td></tr><tr><td>9 - 1</td><td>2023-02-23T13:54:51Z</td><td>floatVal: 1948.0\n",
"</td></tr></tbody></table>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"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</tbody></table>\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\"\"\"<table>\n",
" <thead>\n",
" <tr><th colspan=3>{datetime.datetime.now()}</th></tr>\n",
" <tr><th>KPI ID</th><th>Timestamp</th><th>Value</th></tr>\n",
" <thead>\n",
" <tbody>\"\"\"\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 += \"<tr><td>{} - {}</td><td>{}</td><td>{}</td></tr>\".format(\n",
" i, cur_kpi_id, timestamp_float_to_string(raw_kpi.timestamp.timestamp), raw_kpi.kpi_value\n",
" )\n",
" table += \"</tbody></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
}