Skip to content

Commit

Permalink
reformatted with black
Browse files Browse the repository at this point in the history
  • Loading branch information
yurnov committed Apr 2, 2024
1 parent 8d0b368 commit faef29b
Show file tree
Hide file tree
Showing 11 changed files with 554 additions and 525 deletions.
55 changes: 23 additions & 32 deletions deploy/alerts/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@
alarm_url = "https://api.ukrainealarm.com/api/v3/alerts"
region_url = "https://api.ukrainealarm.com/api/v3/regions"

alert_token = os.environ.get('ALERT_TOKEN') or 'token'
memcached_host = os.environ.get('MEMCACHED_HOST') or 'localhost'
alert_token = os.environ.get("ALERT_TOKEN") or "token"
memcached_host = os.environ.get("MEMCACHED_HOST") or "localhost"

alert_loop_time = int(os.environ.get('ALERT_PERIOD', 3))
alert_loop_time = int(os.environ.get("ALERT_PERIOD", 3))

# Authorization header
headers = {
"Authorization": "%s" % alert_token
}
headers = {"Authorization": "%s" % alert_token}

regions = {
"Закарпатська область": {"id": 1},
Expand Down Expand Up @@ -73,38 +71,33 @@ async def alarm_data(mc):
alerts_cached = await mc.get(b"alerts")

if alerts_cached:
alerts_cached_data = json.loads(alerts_cached.decode('utf-8'))
alerts_cached_data = json.loads(alerts_cached.decode("utf-8"))
else:
alerts_cached_data = {}

empty_data = {
'alertnow': False,
'changed': None,
"alertnow": False,
"changed": None,
}

current_datetime = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
if alerts_cached_data.get('info', {}).get('is_started', False) is False \
or alerts_cached_data.get('version', 0) != version:
if (
alerts_cached_data.get("info", {}).get("is_started", False) is False
or alerts_cached_data.get("version", 0) != version
):
logging.debug("fill empty fields")
alerts_cached_data = {
"version": version,
"states": {},
"info": {
"last_update": None,
"is_started": False
}
}
alerts_cached_data = {"version": version, "states": {}, "info": {"last_update": None, "is_started": False}}

logging.debug("fill start data")
async with aiohttp.ClientSession() as session:
response = await session.get(region_url, headers=headers) # Replace with your URL
new_data = await response.text()
data = json.loads(new_data)
for item in data['states']:
for item in data["states"]:
region_name = item["regionName"]
alerts_cached_data["states"][region_name] = copy(empty_data)

region_alert_url = "%s/%s" % (alarm_url, item['regionId'])
region_alert_url = "%s/%s" % (alarm_url, item["regionId"])
async with aiohttp.ClientSession() as session:
response = await session.get(region_alert_url, headers=headers) # Replace with your URL
new_data = await response.text()
Expand All @@ -121,26 +114,24 @@ async def alarm_data(mc):
alert_region_names = []
for item in data:
for alert in item["activeAlerts"]:
if (alert["regionId"] == item["regionId"]
and alert["regionType"] == "State"
and alert["type"] == "AIR"):
if alert["regionId"] == item["regionId"] and alert["regionType"] == "State" and alert["type"] == "AIR":
region_name = item["regionName"]
region_data = alerts_cached_data['states'].get(region_name, empty_data)
region_data = alerts_cached_data["states"].get(region_name, empty_data)
alert_region_names.append(region_name)
region_data["alertnow"] = True
region_data["changed"] = alert['lastUpdate']
region_data["changed"] = alert["lastUpdate"]
alerts_cached_data["states"][region_name] = region_data

logging.debug("parse states")
for region_name, data in regions.items():
if region_name not in alert_region_names and alerts_cached_data['states'][region_name]['alertnow'] is True:
alerts_cached_data['states'][region_name]['alertnow'] = False
alerts_cached_data['states'][region_name]['changed'] = current_datetime
if region_name not in alert_region_names and alerts_cached_data["states"][region_name]["alertnow"] is True:
alerts_cached_data["states"][region_name]["alertnow"] = False
alerts_cached_data["states"][region_name]["changed"] = current_datetime

alerts_cached_data['info']['is_started'] = True
alerts_cached_data['info']['last_update'] = current_datetime
alerts_cached_data["info"]["is_started"] = True
alerts_cached_data["info"]["last_update"] = current_datetime
logging.debug("store alerts data: %s" % current_datetime)
await mc.set(b"alerts", json.dumps(alerts_cached_data).encode('utf-8'))
await mc.set(b"alerts", json.dumps(alerts_cached_data).encode("utf-8"))
logging.debug("alerts data stored")

except Exception as e:
Expand Down
11 changes: 6 additions & 5 deletions deploy/check/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

memcached_host = os.environ.get('MEMCACHED_HOST') or 'localhost'
memcached_host = os.environ.get("MEMCACHED_HOST") or "localhost"

alert_loop_time = os.environ.get('ALERT_PERIOD') or 3
alert_loop_time = os.environ.get("ALERT_PERIOD") or 3


async def alarm_data(mc, alerts_cached_data):
Expand All @@ -19,8 +19,8 @@ async def alarm_data(mc, alerts_cached_data):

if task1_result:
logger.debug("result")
encoded_result = json.loads(task1_result.decode('utf-8'))
logging.info(task1_result.decode('utf-8'))
encoded_result = json.loads(task1_result.decode("utf-8"))
logging.info(task1_result.decode("utf-8"))

await asyncio.sleep(alert_loop_time)

Expand Down Expand Up @@ -48,9 +48,10 @@ async def main():
finally:
asyncio.sleep(1)


if __name__ == "__main__":
try:
logger.debug("Task 1: Doing some cool async stuff...")
asyncio.run(main())
except KeyboardInterrupt:
pass
pass
1 change: 1 addition & 0 deletions deploy/explosions/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM python:3.10-slim
# FROM python:3.10.6
ADD explosions.py .
ADD requirements.txt .
RUN pip install -r requirements.txt --no-cache-dir && rm -f requirements.txt
Expand Down
89 changes: 41 additions & 48 deletions deploy/explosions/explosions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,40 @@
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

etryvoga_url = os.environ.get('ETRYVOGA_HOST') or 'localhost'
etryvoga_url = os.environ.get("ETRYVOGA_HOST") or "localhost"

memcached_host = os.environ.get('MEMCACHED_HOST') or 'localhost'
memcached_host = os.environ.get("MEMCACHED_HOST") or "localhost"

etryvoga_loop_time = int(os.environ.get('ETRYVOGA_PERIOD', 30))
etryvoga_loop_time = int(os.environ.get("ETRYVOGA_PERIOD", 30))


regions = {
'ZAKARPATSKA': {"name": "Закарпатська область"},
'IVANOFRANKIWSKA': {"name": "Івано-Франківська область"},
'TERNOPILSKA': {"name": "Тернопільська область"},
'LVIVKA': {"name": "Львівська область"},
'VOLYNSKA': {"name": "Волинська область"},
'RIVENSKA': {"name": "Рівненська область"},
'JITOMIRSKAYA': {"name": "Житомирська область"},
'KIYEWSKAYA': {"name": "Київська область"},
'CHERNIGIWSKA': {"name": "Чернігівська область"},
'SUMSKA': {"name": "Сумська область"},
'HARKIVSKA': {"name": "Харківська область"},
'LUGANSKA': {"name": "Луганська область"},
'DONETSKAYA': {"name": "Донецька область"},
'ZAPORIZKA': {"name": "Запорізька область"},
'HERSONSKA': {"name": "Херсонська область"},
'KRIMEA': {"name": "Автономна Республіка Крим"},
'ODESKA': {"name": "Одеська область"},
'MYKOLAYIV': {"name": "Миколаївська область"},
'DNIPROPETROVSKAYA': {"name": "Дніпропетровська область"},
'POLTASKA': {"name": "Полтавська область"},
'CHERKASKA': {"name": "Черкаська область"},
'KIROWOGRADSKA': {"name": "Кіровоградська область"},
'VINNYTSA': {"name": "Вінницька область"},
'HMELNYCKA': {"name": "Хмельницька область"},
'CHERNIVETSKA': {"name": "Чернівецька область"},
'KIYEW': {"name": "м. Київ"}
"ZAKARPATSKA": {"name": "Закарпатська область"},
"IVANOFRANKIWSKA": {"name": "Івано-Франківська область"},
"TERNOPILSKA": {"name": "Тернопільська область"},
"LVIVKA": {"name": "Львівська область"},
"VOLYNSKA": {"name": "Волинська область"},
"RIVENSKA": {"name": "Рівненська область"},
"JITOMIRSKAYA": {"name": "Житомирська область"},
"KIYEWSKAYA": {"name": "Київська область"},
"CHERNIGIWSKA": {"name": "Чернігівська область"},
"SUMSKA": {"name": "Сумська область"},
"HARKIVSKA": {"name": "Харківська область"},
"LUGANSKA": {"name": "Луганська область"},
"DONETSKAYA": {"name": "Донецька область"},
"ZAPORIZKA": {"name": "Запорізька область"},
"HERSONSKA": {"name": "Херсонська область"},
"KRIMEA": {"name": "Автономна Республіка Крим"},
"ODESKA": {"name": "Одеська область"},
"MYKOLAYIV": {"name": "Миколаївська область"},
"DNIPROPETROVSKAYA": {"name": "Дніпропетровська область"},
"POLTASKA": {"name": "Полтавська область"},
"CHERKASKA": {"name": "Черкаська область"},
"KIROWOGRADSKA": {"name": "Кіровоградська область"},
"VINNYTSA": {"name": "Вінницька область"},
"HMELNYCKA": {"name": "Хмельницька область"},
"CHERNIVETSKA": {"name": "Чернівецька область"},
"KIYEW": {"name": "м. Київ"},
}


Expand All @@ -57,18 +57,11 @@ async def explosions_data(mc):
etryvoga_cached = await mc.get(b"explosions")

if etryvoga_cached:
etryvoga_cached_data = json.loads(etryvoga_cached.decode('utf-8'))
etryvoga_cached_data = json.loads(etryvoga_cached.decode("utf-8"))
else:
etryvoga_cached_data = {
"version": 1,
"states": {},
"info": {
"last_update": None,
"last_id": 0
}
}

last_id_cached = int(etryvoga_cached_data['info']['last_id'])
etryvoga_cached_data = {"version": 1, "states": {}, "info": {"last_update": None, "last_id": 0}}

last_id_cached = int(etryvoga_cached_data["info"]["last_id"])
last_id = None

async with aiohttp.ClientSession() as session:
Expand All @@ -77,20 +70,20 @@ async def explosions_data(mc):
new_data = await response.text()
data = json.loads(new_data)
for message in data[::-1]:
if int(message['id']) > last_id_cached:
if int(message["id"]) > last_id_cached:

if message['type'] == 'INFO':
region_name = regions[message['region']]['name']
if message["type"] == "INFO":
region_name = regions[message["region"]]["name"]
region_data = {
'changed': message['createdAt'],
"changed": message["createdAt"],
}
etryvoga_cached_data["states"][region_name] = region_data
last_id = int(message['id'])
last_id = int(message["id"])

etryvoga_cached_data['info']['last_id'] = last_id
etryvoga_cached_data['info']['last_update'] = current_datetime
etryvoga_cached_data["info"]["last_id"] = last_id
etryvoga_cached_data["info"]["last_update"] = current_datetime
logging.debug("store explosions data: %s" % current_datetime)
await mc.set(b"explosions", json.dumps(etryvoga_cached_data).encode('utf-8'))
await mc.set(b"explosions", json.dumps(etryvoga_cached_data).encode("utf-8"))
logging.debug("explosions data stored")
else:
logging.error(f"Request failed with status code: {response.status_code}")
Expand Down
Loading

0 comments on commit faef29b

Please sign in to comment.