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

Mark asset as down when middleware is down #1466

Merged
merged 3 commits into from
Jul 20, 2023
Merged
Changes from 2 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
39 changes: 22 additions & 17 deletions care/facility/tasks/asset_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,23 @@ def check_asset_status():
middleware_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 = asset.meta.get(
"middleware_hostname",
asset.current_location.facility.middleware_address,
)
result: Any = {}
result: Any = None

# Checking if middleware status is already cached
if hostname in middleware_status_cache:
result = middleware_status_cache[hostname]
else:
try:
# Creating an instance of the asset class
asset_class: BaseAssetIntegration = AssetClasses[
asset.asset_class
].value(
Expand All @@ -45,42 +49,43 @@ def check_asset_status():
"middleware_hostname": hostname,
}
)
# Fetching the status of the device
result = asset_class.api_get(asset_class.get_url("devices/status"))
middleware_status_cache[hostname] = result
except Exception:
logger.exception("Error in Asset Status Check - Fetching Status")
middleware_status_cache[hostname] = None
continue
logger.warn(f"Middleware {hostname} is down", exc_info=True)

if not result:
continue
# If no status is returned, setting default status as down
if not result or len(result) == 0:
Ashesh3 marked this conversation as resolved.
Show resolved Hide resolved
result = [{"time": timezone.now().isoformat(), "status": []}]

new_status = None
middleware_status_cache[hostname] = result

# Setting new status as down by default
new_status = AvailabilityStatus.DOWN
for status_record in result:
if asset.meta.get("local_ip_address") in status_record.get(
"status", {}
):
new_status = status_record["status"][
asset_status = status_record["status"][
asset.meta.get("local_ip_address")
]
else:
new_status = "not_monitored"
asset_status = "down"

# Fetching the last record of the asset
last_record = (
AssetAvailabilityRecord.objects.filter(asset=asset)
.order_by("-timestamp")
.first()
)

if new_status == "up":
# Setting new status based on the status returned by the device
if asset_status == "up":
new_status = AvailabilityStatus.OPERATIONAL
elif new_status == "down":
new_status = AvailabilityStatus.DOWN
elif new_status == "maintenance":
elif asset_status == "maintenance":
new_status = AvailabilityStatus.UNDER_MAINTENANCE
else:
new_status = AvailabilityStatus.NOT_MONITORED

# Creating a new record if the status has changed
if not last_record or (
datetime.fromisoformat(status_record.get("time"))
> last_record.timestamp
Expand All @@ -93,4 +98,4 @@ def check_asset_status():
)

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