-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from levpen/dev
Dev
- Loading branch information
Showing
7 changed files
with
92 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
FROM duffn/python-poetry:3.11-slim | ||
|
||
EXPOSE 8501 | ||
WORKDIR /app | ||
COPY poetry.lock pyproject.toml /app/ | ||
COPY backend/*.py /app/backend/ | ||
RUN apt update && \ | ||
apt install -y nmap iputils-ping && \ | ||
poetry install | ||
|
||
CMD ["(poetry run python3 backend/main.py &) &&", "poetry run streamlit run", "backend/app.py"] |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"""Daemon that collect data from hosts.""" | ||
from persistence import MetricsRepository | ||
from collect_data import collect_data | ||
from time import sleep | ||
from threading import Thread | ||
|
||
DB_PATH = "metrics.db" | ||
|
||
def collect_and_append_data(host: str, records: []) -> None: | ||
"""Do to run in thread.""" | ||
record = collect_data(host, 1) | ||
records.append(record) | ||
|
||
if __name__ == "__main__": | ||
|
||
with MetricsRepository(DB_PATH) as metrics_repo: | ||
while True: | ||
hosts = ['.'.join(item) for item in metrics_repo.get_hosts()] | ||
records = [] | ||
threads = [] | ||
for host in hosts: | ||
thread = Thread(target=collect_and_append_data, args=(host, records)) | ||
threads.append(thread) | ||
thread.start() | ||
|
||
sleep(1) | ||
for thread in threads: | ||
thread.join() | ||
|
||
for host, record in zip(hosts, records, strict=False): | ||
metrics_repo.save_record(host, record) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
"""Tests for collect_data.py.""" | ||
from backend.collect_data import collect_data,get_loss_and_latency,get_accessibility | ||
|
||
def test_collect_data() -> None: | ||
"""Do call of stable service to test.""" | ||
host = "ya.ru" # Assume super stable | ||
expected_result = { | ||
"loss": 0.0, | ||
"latency": 0.0, | ||
"accessibility": [ | ||
("HTTP", "open"), | ||
("HTTPS", "open"), | ||
("IMAP", "filtered"), | ||
("SMTP", "filtered"), | ||
("SSH", "filtered"), | ||
("DNS", "filtered") | ||
] | ||
} | ||
result = collect_data(host) | ||
for ex_status, status in zip(expected_result["accessibility"], | ||
result["accessibility"], strict=False): | ||
assert ex_status == status | ||
|
||
def test_get_acc() -> None: | ||
"""Do call services to test.""" | ||
assert get_accessibility("ya.ru", 80) == "open" | ||
assert get_accessibility("ya.ru", 43) == "filtered" | ||
assert get_accessibility("ru.ya", 80) == "unreachable" | ||
|
||
|
||
def test_ping() -> None: | ||
"""Do ping services to test.""" | ||
assert get_loss_and_latency("ya.ru", 1)[0] == 100.0 | ||
assert get_loss_and_latency("ya.ru", 1)[1] >= 0.0 | ||
assert get_loss_and_latency("ru.ya", 1)[0] == 100.0 | ||
|
||
|