Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker optimization #190

Merged
merged 16 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/compile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@
with:
name: lint-report
path: ${{ github.workspace }}/lint-report.json
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Setup uncompromising Python code formatter
run: |
python -m pip install black==24.3.0
- name: Run Black in the check mode (and ignore output)
run: |
black --check --verbose --line-length 120 --target-version py312 deploy || true
check_flasher:
runs-on: ubuntu-latest
steps:
Expand Down
8 changes: 4 additions & 4 deletions deploy/alerts/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM python:3.10.6
ADD alerts.py .
ADD requirements.txt .
RUN pip install -r requirements.txt --no-cache-dir && rm -f requirements.txt
FROM python:3.12-slim
COPY alerts.py .
RUN --mount=type=bind,source=requirements.txt,target=/tmp/requirements.txt \
python -m pip install --no-cache-dir -r /tmp/requirements.txt
CMD python alerts.py
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
2 changes: 1 addition & 1 deletion deploy/alerts/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
aiohttp==3.9.2
aiohttp==3.9.3
aiomcache==0.8.1
8 changes: 4 additions & 4 deletions deploy/check/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM python:3.10.6
ADD check.py .
ADD requirements.txt .
RUN pip install -r requirements.txt --no-cache-dir && rm -f requirements.txt
FROM python:3.12-slim
COPY check.py .
RUN --mount=type=bind,source=requirements.txt,target=/tmp/requirements.txt \
python -m pip install --no-cache-dir -r /tmp/requirements.txt
CMD python check.py
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
2 changes: 1 addition & 1 deletion deploy/check/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
aiohttp==3.9.2
aiohttp==3.9.3
aiomcache==0.8.1
8 changes: 4 additions & 4 deletions deploy/explosions/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM python:3.10.6
ADD explosions.py .
ADD requirements.txt .
RUN pip install -r requirements.txt --no-cache-dir && rm -f requirements.txt
FROM python:3.12-slim
COPY explosions.py .
RUN --mount=type=bind,source=requirements.txt,target=/tmp/requirements.txt \
python -m pip install --no-cache-dir -r /tmp/requirements.txt
CMD python explosions.py
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
2 changes: 1 addition & 1 deletion deploy/explosions/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
aiohttp==3.9.2
aiohttp==3.9.3
aiomcache==0.8.1
8 changes: 4 additions & 4 deletions deploy/svg_generator/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM python:3.10.6
ADD svg_generator.py .
ADD requirements.txt .
RUN pip install -r requirements.txt --no-cache-dir && rm -f requirements.txt
FROM python:3.12-bullseye
COPY svg_generator.py .
RUN --mount=type=bind,source=requirements.txt,target=/tmp/requirements.txt \
python -m pip install --no-cache-dir -r /tmp/requirements.txt
CMD python svg_generator.py
Loading
Loading