-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
142 lines (121 loc) · 4.11 KB
/
main.py
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
#!/usr/bin/env python
import asyncio
import logging
import pathlib
import sys
from json import load
from logging.handlers import TimedRotatingFileHandler
from typing import List
from database import Database
from dtos import (
SlackConnectorConfigDTO,
HealthCheckConfigDTO,
MonthlySummaryConfigDTO,
HealthCheckDTO,
)
from health_check import HealthCheck
from monthly_summary import MonthlySummary
from slack_connector import SlackConnector
import to_checks_types as types
config_file_name = "configuration.json"
logs_file_name = "logs"
current_path = pathlib.Path(__file__).parent.resolve()
class Main:
def __init__(
self,
*,
repository: Database,
health_check: HealthCheck,
slack_connector: SlackConnector,
):
self.repository = repository
self.slack_connector = slack_connector
self.health_check = health_check
async def execute(self, *, to_checks: List[types.ToChecksTypedDict]) -> None:
health_check_dto = HealthCheckDTO(
healthy=[],
new_unhealthy=[],
back_to_healthy=[],
still_unhealthy=[],
)
current_unhealthy_urls = self.repository.current_unhealthy_urls
await self.health_check.execute(
to_checks=to_checks,
current_unhealthy_urls=current_unhealthy_urls,
health_check_dto=health_check_dto,
)
self.slack_connector.send_health_check_report(health_check_dto=health_check_dto)
if not any([health_check_dto.new_unhealthy, health_check_dto.still_unhealthy]):
self.slack_connector.send_if_there_no_unhealthy()
self.repository.add_unhealthy(new_unhealthy=health_check_dto.new_unhealthy)
self.repository.update_still_unhealthy_last_send(
still_unhealthy=health_check_dto.still_unhealthy
)
self.repository.update_monthly_summary(
back_to_healthy=health_check_dto.back_to_healthy
)
self.repository.remove_unhealthy(
back_to_healthy=health_check_dto.back_to_healthy
)
def test(self):
self.slack_connector.hello_message()
if __name__ == "__main__":
try:
param = sys.argv[1]
except IndexError:
param = ""
handler = TimedRotatingFileHandler(
filename=f"{current_path}/{logs_file_name}",
when="W0",
interval=2,
backupCount=2,
)
logging.basicConfig(
encoding="utf-8",
level=logging.INFO,
handlers=[handler],
)
logger = logging.getLogger()
repository = Database(current_path=current_path)
with open(f"{current_path}/{config_file_name}", "r") as config_file:
config = load(config_file)
try:
to_checks: List[types.ToChecksTypedDict] = config["to_checks"]
except KeyError:
raise Exception("Missing 'to_check' list. Check configuration_example.")
try:
slack_webhook_url = config["slack_webhook_url"]
except KeyError:
raise Exception("Missing 'slack_webhook_url'. Check configuration_example.")
slack_connector_config = SlackConnectorConfigDTO(
**config.get("slack_connector_config", {})
)
health_check_config = HealthCheckConfigDTO(**config.get("health_check_config", {}))
monthly_summary_config = MonthlySummaryConfigDTO(
**config.get("monthly_summary_config", {})
)
slack_connector = SlackConnector(
repository=repository,
slack_webhook_url=slack_webhook_url,
slack_connector_config=slack_connector_config,
)
health_check = HealthCheck(
health_check_config=health_check_config,
)
main = Main(
repository=repository,
slack_connector=slack_connector,
health_check=health_check,
)
send_monthly_summary = MonthlySummary(
config=monthly_summary_config,
repository=repository,
connector=slack_connector,
)
if param == "--test":
main.test()
else:
asyncio.run(main.execute(to_checks=to_checks))
send_monthly_summary.execute()
logging.info("-----------------------------------------------")
repository.commit()