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

Нові сповіщення: Ракетна небезпека, Загроза БПЛА #249

Merged
merged 10 commits into from
Dec 21, 2024
25 changes: 19 additions & 6 deletions deploy/svg_generator/svg_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ async def svg_generator(mc, shared_data):
weather_svg_data = {}

if cached_data != shared_data.data:
alerts_data, weather_data, explosions_data = cached_data.split(":")
alerts_data, weather_data, explosions_data, rockets_data, drones_data = cached_data.split(":")
alerts = [int(alert) for alert in alerts_data.split(",")]
weathers = [float(weather) for weather in weather_data.split(",")]
explosions = [int(explosion) for explosion in explosions_data.split(",")]
rockets = [int(rocket) for rocket in rockets_data.split(",")]
drones = [int(drone) for drone in drones_data.split(",")]

position = 0
for alert in alerts:
Expand All @@ -125,8 +127,15 @@ async def svg_generator(mc, shared_data):
case 3:
alerts_svg_data[regions[position]] = "#FFA533"

if drones[position] == 1:
alerts_svg_data[regions[position]] = "#FF00FF"

if rockets[position] == 1:
alerts_svg_data[regions[position]] = "#9D00FF"

if explosions[position] == 1:
alerts_svg_data[regions[position]] = "#00FFFF"

position += 1
file_path = os.path.join(shared_path, "alerts_map.png")
await generate_map(
Expand Down Expand Up @@ -945,13 +954,17 @@ async def generate_map(time, output_file, show_alert_info=False, show_weather_in
<circle cx="50" cy="630" r="20" fill="#ff5733" id="circle224" />
<text x="75" y="635" font-family="Arial" font-size="22px" fill="#ffffff" id="text226">- тривога</text>
<circle cx="50" cy="680" r="20" fill="#32cd32" id="circle228" />
<text x="75" y="686" font-family="Arial" font-size="22px" fill="#ffffff" id="text230">- немає тривоги</text>
<text x="75" y="685" font-family="Arial" font-size="22px" fill="#ffffff" id="text230">- немає тривоги</text>
<circle cx="50" cy="730" r="20" fill="#ffa533" id="circle232" />
<text x="75" y="736" font-family="Arial" font-size="22px" fill="#ffffff" id="text234">- оголошено тривогу (до 5 хв. тому)</text>
<text x="75" y="735" font-family="Arial" font-size="22px" fill="#ffffff" id="text234">- оголошено тривогу (до 5 хв. тому)</text>
<circle cx="50" cy="780" r="20" fill="#bbff33" id="circle236" />
<text x="75" y="787" font-family="Arial" font-size="22px" fill="#ffffff" id="text238">- оголошено відбій (до 5 хв. тому)</text>
<circle cx="50" cy="830" r="20" fill="#00ffff" id="circle240" />
<text x="75" y="837" font-family="Arial" font-size="22px" fill="#ffffff" id="text242">- ЗМІ повідомляють про вибухи</text>
<text x="75" y="785" font-family="Arial" font-size="22px" fill="#ffffff" id="text238">- оголошено відбій (до 5 хв. тому)</text>
<circle cx="50" cy="830" r="20" fill="#9d00ff" id="circle244" />
<text x="75" y="835" font-family="Arial" font-size="22px" fill="#ffffff" id="text247">- ракетна небезпека (до 5 хв. тому)</text>
<circle cx="50" cy="880" r="20" fill="#ff00ff" id="circle248" />
<text x="75" y="885" font-family="Arial" font-size="22px" fill="#ffffff" id="text249">- загроза БПЛА (до 5 хв. тому)</text>
<circle cx="50" cy="930" r="20" fill="#00ffff" id="circle240" />
<text x="75" y="935" font-family="Arial" font-size="22px" fill="#ffffff" id="text242">- ЗМІ повідомляють про вибухи (до 5 хв. тому)</text>
</g>

<g id="WEATHER_GRADIENT" visibility="{"visible" if show_weather_info else "hidden"}">
Expand Down
90 changes: 85 additions & 5 deletions deploy/updater/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,13 @@ async def update_data(mc):
alerts_cached_v2 = await mc.get(b"alerts_websocket_v2")
weather_cached_v1 = await mc.get(b"weather_websocket_v1")
explosions_cached_v1 = await mc.get(b"explosions_websocket_v1")
rockets_cached_v1 = await mc.get(b"rockets_websocket_v1")
drones_cached_v1 = await mc.get(b"drones_websocket_v1")
alerts_data = await mc.get(b"alerts")
weather_data = await mc.get(b"weather")
explosions_data = await mc.get(b"explosions")
rockets_data = await mc.get(b"rockets")
drones_data = await mc.get(b"drones")

if tcp_cached:
tcp_cached_data = json.loads(tcp_cached.decode("utf-8"))
Expand All @@ -92,12 +96,24 @@ async def update_data(mc):
explosions_cached_data_v1 = json.loads(explosions_cached_v1.decode("utf-8"))
else:
explosions_cached_data_v1 = []
if rockets_cached_v1:
rockets_cached_data_v1 = json.loads(rockets_cached_v1.decode("utf-8"))
else:
rockets_cached_data_v1 = []
if drones_cached_v1:
drones_cached_data_v1 = json.loads(drones_cached_v1.decode("utf-8"))
else:
drones_cached_data_v1 = []

current_datetime = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")

alerts_data = json.loads(alerts_data.decode("utf-8")) if alerts_data else "No data from Memcached"
weather_data = json.loads(weather_data.decode("utf-8")) if weather_data else "No data from Memcached"
explosions_data = json.loads(explosions_data.decode("utf-8")) if explosions_data else "No data from Memcached"
alerts_data = json.loads(alerts_data.decode("utf-8")) if alerts_data else "No alerts data from Memcached"
weather_data = json.loads(weather_data.decode("utf-8")) if weather_data else "No weather data from Memcached"
explosions_data = (
json.loads(explosions_data.decode("utf-8")) if explosions_data else "No explosions data from Memcached"
)
rockets_data = json.loads(rockets_data.decode("utf-8")) if rockets_data else "No missiles data from Memcached"
drones_data = json.loads(drones_data.decode("utf-8")) if drones_data else "No drones data from Memcached"

logger.debug(f"Alerts updated: {alerts_data['info']['last_update']}")
logger.debug(f"Weather updated: {weather_data['info']['last_update']}")
Expand All @@ -110,6 +126,10 @@ async def update_data(mc):
weather_v1 = []
explosions_v1 = []
explosions_svg = []
rockets_v1 = []
rockets_svg = []
drones_v1 = []
drones_svg = []

try:
for region_name in regions:
Expand Down Expand Up @@ -150,7 +170,7 @@ async def update_data(mc):
timestamp = int(datetimeObjUtc.timestamp())
explosions_v1.append(str(timestamp))
time_diff = int(datetime.now().timestamp()) - timestamp
if time_diff > 180:
if time_diff > 300:
exp_mod = 0
else:
exp_mod = 1
Expand All @@ -161,8 +181,54 @@ async def update_data(mc):
except Exception as e:
logger.error(f"Explosions error: {e}")

try:
for region in regions:
if region in rockets_data["states"]:
isoDatetimeStr = rockets_data["states"][region]["changed"]
datetimeObj = datetime.fromisoformat(isoDatetimeStr)
datetimeObjUtc = datetimeObj.replace(tzinfo=timezone.utc)
timestamp = int(datetimeObjUtc.timestamp())
rockets_v1.append(str(timestamp))
time_diff = int(datetime.now().timestamp()) - timestamp
if time_diff > 300:
exp_mod = 0
else:
exp_mod = 1
rockets_svg.append(str(exp_mod))
else:
rockets_v1.append(str(0))
rockets_svg.append(str(0))
except Exception as e:
logger.error(f"Rockets error: {e}")

try:
for region in regions:
if region in drones_data["states"]:
isoDatetimeStr = drones_data["states"][region]["changed"]
datetimeObj = datetime.fromisoformat(isoDatetimeStr)
datetimeObjUtc = datetimeObj.replace(tzinfo=timezone.utc)
timestamp = int(datetimeObjUtc.timestamp())
drones_v1.append(str(timestamp))
time_diff = int(datetime.now().timestamp()) - timestamp
if time_diff > 300:
exp_mod = 0
else:
exp_mod = 1
drones_svg.append(str(exp_mod))
else:
drones_v1.append(str(0))
drones_svg.append(str(0))
except Exception as e:
logger.error(f"Drones error: {e}")

tcp_data = "%s:%s" % (",".join(alerts_v1), ",".join(weather_v1))
svg_data = "%s:%s:%s" % (",".join(alerts_v1), ",".join(weather_v1), ",".join(explosions_svg))
svg_data = "%s:%s:%s:%s:%s" % (
",".join(alerts_v1),
",".join(weather_v1),
",".join(explosions_svg),
",".join(rockets_svg),
",".join(drones_svg),
)

if tcp_cached_data != tcp_data:
logger.debug("store tcp data: %s" % current_datetime)
Expand Down Expand Up @@ -205,6 +271,20 @@ async def update_data(mc):
logger.debug("explosions_v1 stored")
else:
logger.debug("explosions_v1 not changed")

if rockets_cached_data_v1 != rockets_v1:
logger.debug("store rockets_v1: %s" % current_datetime)
await mc.set(b"rockets_websocket_v1", json.dumps(rockets_v1).encode("utf-8"))
logger.debug("rockets_v1 stored")
else:
logger.debug("rockets_v1 not changed")

if drones_cached_data_v1 != drones_v1:
logger.debug("store drones_v1: %s" % current_datetime)
await mc.set(b"drones_websocket_v1", json.dumps(drones_v1).encode("utf-8"))
logger.debug("drones_v1 stored")
else:
logger.debug("drones_v1 not changed")
except Exception as e:
logging.error(f"Error fetching data: {str(e)}")
raise
Expand Down
Loading
Loading