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

Refactor asset status check logic #1750

Merged
merged 1 commit into from
Dec 7, 2023
Merged
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
99 changes: 34 additions & 65 deletions care/facility/tasks/asset_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from typing import Any

from celery import shared_task
from django.db.models import Q
from django.utils import timezone

from care.facility.models.asset import (
Expand All @@ -22,92 +21,63 @@
logger.info(f"Checking Asset Status: {timezone.now()}")

assets = Asset.objects.all()
middleware_status_cache = {}
middleware_camera_status_cache = {}

for asset in assets:
# Skipping if asset class or local IP address is not present
if not asset.asset_class or not asset.meta.get("local_ip_address", None):
continue
try:
# Fetching middleware hostname
hostname = (
resolved_middleware = (

Check warning on line 31 in care/facility/tasks/asset_monitor.py

View check run for this annotation

Codecov / codecov/patch

care/facility/tasks/asset_monitor.py#L31

Added line #L31 was not covered by tests
asset.meta.get(
"middleware_hostname",
)
or asset.current_location.middleware_address
or asset.current_location.facility.middleware_address
) # From asset configuration
or asset.current_location.middleware_address # From location configuration
or asset.current_location.facility.middleware_address # From facility configuration
)
if not hostname:

if not resolved_middleware:
logger.warn(
f"Asset {asset.external_id} does not have a middleware hostname"
)
continue

result: Any = None

# Checking if middleware status is already cached
if (
hostname in middleware_camera_status_cache
and asset.asset_class == "ONVIF"
):
result = middleware_camera_status_cache[hostname]
elif hostname in middleware_status_cache and asset.asset_class != "ONVIF":
result = middleware_status_cache[hostname]
else:
try:
# Creating an instance of the asset class
asset_class: BaseAssetIntegration = AssetClasses[
asset.asset_class
].value(
try:

Check warning on line 47 in care/facility/tasks/asset_monitor.py

View check run for this annotation

Codecov / codecov/patch

care/facility/tasks/asset_monitor.py#L47

Added line #L47 was not covered by tests
# Creating an instance of the asset class
asset_class: BaseAssetIntegration = AssetClasses[

Check warning on line 49 in care/facility/tasks/asset_monitor.py

View check run for this annotation

Codecov / codecov/patch

care/facility/tasks/asset_monitor.py#L49

Added line #L49 was not covered by tests
asset.asset_class
].value(
{
**asset.meta,
"middleware_hostname": resolved_middleware,
}
)
# Fetching the status of the device
if asset.asset_class == "ONVIF":
asset_config = asset.meta["camera_access_key"].split(":")
assets_config = [

Check warning on line 60 in care/facility/tasks/asset_monitor.py

View check run for this annotation

Codecov / codecov/patch

care/facility/tasks/asset_monitor.py#L59-L60

Added lines #L59 - L60 were not covered by tests
{
**asset.meta,
"middleware_hostname": hostname,
"hostname": asset.meta.get("local_ip_address"),
"port": 80,
"username": asset_config[0],
"password": asset_config[1],
}
]

result = asset_class.api_post(

Check warning on line 69 in care/facility/tasks/asset_monitor.py

View check run for this annotation

Codecov / codecov/patch

care/facility/tasks/asset_monitor.py#L69

Added line #L69 was not covered by tests
asset_class.get_url("cameras/status"), data=assets_config
)
# Fetching the status of the device
if asset.asset_class == "ONVIF":
similar_assets = Asset.objects.filter(
asset_class="ONVIF"
).filter(
Q(meta__middleware_hostname=hostname)
| Q(current_location__middleware_address=hostname)
| Q(current_location__facility__middleware_address=hostname)
)
assets_config = []
for asset in similar_assets:
try:
asset_config = asset.meta["camera_access_key"].split(
":"
)
assets_config.append(
{
"hostname": asset.meta.get("local_ip_address"),
"port": 80,
"username": asset_config[0],
"password": asset_config[1],
}
)
except Exception:
pass
result = asset_class.api_post(
asset_class.get_url("cameras/status"), data=assets_config
)
else:
result = asset_class.api_get(
asset_class.get_url("devices/status")
)
except Exception:
logger.warn(f"Middleware {hostname} is down", exc_info=True)
else:
result = asset_class.api_get(asset_class.get_url("devices/status"))
except Exception:
logger.warn(f"Middleware {resolved_middleware} is down", exc_info=True)

Check warning on line 75 in care/facility/tasks/asset_monitor.py

View check run for this annotation

Codecov / codecov/patch

care/facility/tasks/asset_monitor.py#L73-L75

Added lines #L73 - L75 were not covered by tests

# If no status is returned, setting default status as down
if not result:
if not result or "error" in result:
result = [{"time": timezone.now().isoformat(), "status": []}]

if asset.asset_class == "ONVIF":
middleware_camera_status_cache[hostname] = result
else:
middleware_status_cache[hostname] = result

# Setting new status as down by default
new_status = AvailabilityStatus.DOWN
for status_record in result:
Expand Down Expand Up @@ -144,6 +114,5 @@
status=new_status.value,
timestamp=status_record.get("time", timezone.now()),
)

except Exception:
logger.error("Error in Asset Status Check", exc_info=True)
Loading