Skip to content

Commit ab2d532

Browse files
authored
Merge pull request #106 from GabrielSalla/format-monitor-name
Format monitor name
2 parents 37ed428 + b66d743 commit ab2d532

File tree

4 files changed

+78
-3
lines changed

4 files changed

+78
-3
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import re
2+
3+
4+
def format_monitor_name(monitor_name: str) -> str:
5+
"""Format the monitor name"""
6+
monitor_name = re.sub(r"[\. ]", "_", monitor_name.lower())
7+
monitor_name = re.sub(r"[^\w_]", "", monitor_name)
8+
return re.sub(r"_{2,}", "_", monitor_name).strip("_")

src/components/http_server/monitor_routes.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
from aiohttp.web_request import Request
88
from aiohttp.web_response import Response
99

10-
import commands as commands
10+
import commands
11+
from components.http_server.format_monitor_name import format_monitor_name
1112
from components.monitors_loader import MonitorValidationError
1213
from models import CodeModule, Monitor
1314

@@ -153,6 +154,16 @@ async def monitor_validate(request: Request) -> Response:
153154
return web.json_response(success_response)
154155

155156

157+
@monitor_routes.post(base_route + "/format_name/{monitor_name}")
158+
@monitor_routes.post(base_route + "/format_name/{monitor_name}/")
159+
async def format_name(request: Request) -> Response:
160+
"""Route to format a monitor name"""
161+
monitor_name = request.match_info["monitor_name"]
162+
return web.json_response(
163+
{"name": monitor_name, "formatted_name": format_monitor_name(monitor_name)}
164+
)
165+
166+
156167
@monitor_routes.post(base_route + "/register/{monitor_name}")
157168
@monitor_routes.post(base_route + "/register/{monitor_name}/")
158169
async def monitor_register(request: Request) -> Response:
@@ -169,8 +180,7 @@ async def monitor_register(request: Request) -> Response:
169180
error_response = {"status": "error", "message": "'monitor_code' parameter is required"}
170181
return web.json_response(error_response, status=400)
171182

172-
# Remove any dots from the monitor name
173-
monitor_name = monitor_name.replace(".", "_")
183+
monitor_name = format_monitor_name(monitor_name)
174184

175185
try:
176186
monitor = await commands.monitor_register(monitor_name, monitor_code, additional_files)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import pytest
2+
3+
import components.http_server.format_monitor_name as format_monitor_name
4+
5+
6+
@pytest.mark.parametrize(
7+
"input_name, expected_output",
8+
[
9+
("simple_monitor", "simple_monitor"),
10+
("UPPERCASE", "uppercase"),
11+
("MixedCase", "mixedcase"),
12+
("monitor.name", "monitor_name"),
13+
("a.b.c.d", "a_b_c_d"),
14+
("monitor name", "monitor_name"),
15+
("monitor._name", "monitor_name"),
16+
("monitor_._name", "monitor_name"),
17+
("monitor____name", "monitor_name"),
18+
("_monitor_", "monitor"),
19+
("__monitor__", "monitor"),
20+
("___monitor___", "monitor"),
21+
("My.Monitor-Name@123", "my_monitorname123"),
22+
("Service.Monitor_v2.1", "service_monitor_v2_1"),
23+
("test...monitor", "test_monitor"),
24+
("Monitor--.--Name", "monitor_name"),
25+
("API_Gateway.Health-Check@v1", "api_gateway_healthcheckv1"),
26+
("", ""),
27+
(".", ""),
28+
("..", ""),
29+
("...", ""),
30+
("___", ""),
31+
("123", "123"),
32+
("monitor123", "monitor123"),
33+
("123monitor", "123monitor"),
34+
],
35+
)
36+
def test_format_monitor_name(input_name, expected_output):
37+
"""'format_monitor_name' should format the monitor name correctly"""
38+
assert format_monitor_name.format_monitor_name(input_name) == expected_output

tests/components/http_server/test_monitor_routes.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,25 @@ async def test_monitor_validate_invalid_monitor_code(mocker, monitor_code, expec
317317
}
318318

319319

320+
@pytest.mark.parametrize(
321+
"monitor_name, expected_formatted_name",
322+
[
323+
("test_monitor_format_name", "test_monitor_format_name"),
324+
("a.b.c", "a_b_c"),
325+
("My.Monitor-Name@123", "my_monitorname123"),
326+
],
327+
)
328+
async def test_format_name(monitor_name, expected_formatted_name):
329+
"""The 'format name' route should return the formatted name of the monitor"""
330+
url = BASE_URL + f"/format_name/{monitor_name}"
331+
async with aiohttp.ClientSession() as session:
332+
async with session.post(url) as response:
333+
assert await response.json() == {
334+
"name": monitor_name,
335+
"formatted_name": expected_formatted_name,
336+
}
337+
338+
320339
@pytest.mark.parametrize(
321340
"monitor_name",
322341
[

0 commit comments

Comments
 (0)