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

Asset Tracking and Monitoring Enhancements #1454

Merged
merged 17 commits into from
Jul 18, 2023
Merged

Asset Tracking and Monitoring Enhancements #1454

merged 17 commits into from
Jul 18, 2023

Conversation

Ashesh3
Copy link
Member

@Ashesh3 Ashesh3 commented Jul 14, 2023

This PR introduces several changes aimed at improving asset tracking and monitoring.

  • AssetAvailabilityRecord Model: Introduced a new model AssetAvailabilityRecord in models/asset.py and the corresponding migration file. This model will keep track of the availability status of each asset, which includes not monitored, operational, down, and under maintenance states.

  • Asset Monitoring Task: Added a new Celery task in tasks/asset_monitor.py for checking the status of each asset periodically. The task fetches the status from the middleware and updates the AssetAvailabilityRecord accordingly.

  • Asset Status Enum: Created a new Enum class AvailabilityStatus in utils/assetintegration/asset_statuses.py to represent the availability status of assets.

  • Task Scheduling: Added the new asset monitoring task to the period tasks list in tasks/init.py. This task will be executed every 30 minutes.

  • Asset Model Update: Updated the Asset model in models/asset.py to include a reference to the new AssetAvailabilityRecord model.

Celery Task Pseudocode:

The Celery task check_asset_status is set to run every 30 minutes and is responsible for checking and updating the status of all assets.

  1. Fetch All Assets: The task begins by fetching all assets from the database.

  2. Iterate Over Assets: For each asset, it checks if the asset class is defined and if the local IP address is available in the asset's metadata. If either of these conditions is not met, it continues to the next asset.

  3. Get Asset Class: The task then determines the asset class using the AssetClasses enumeration and the asset's metadata, which includes the middleware hostname.

  4. Check Cached Results: Before making a request to the middleware API, the task checks if the status of assets for the given hostname is already cached. If it is, the task uses the cached results instead of making a new API call.

  5. API Call: If the results are not cached, the task makes a GET request to the middleware API to fetch the status of all devices.

  6. Determine New Status: The task then determines the new status of the asset based on the response from the API. If the asset's local IP address is not in the status records returned by the API, the asset's status is set to "not monitored".

  7. Get Last Record: The task fetches the most recent status record for the asset.

  8. Create New Record: If there is no previous record or if the asset's status has changed, the task creates a new AssetAvailabilityRecord with the new status.

  9. Error Handling: If there is any exception during the process, it is caught and printed to the console. The task then proceeds to the next asset.

The purpose of this task is to ensure that the asset status is updated regularly, allowing for efficient management and monitoring of assets.

@coronasafe/care-backend-maintainers @coronasafe/care-backend-admins

care/facility/models/asset.py Outdated Show resolved Hide resolved
care/utils/assetintegration/asset_statuses.py Outdated Show resolved Hide resolved
care/facility/tasks/asset_monitor.py Outdated Show resolved Hide resolved
Ashesh3 and others added 3 commits July 17, 2023 21:42
Co-authored-by: Aakash Singh <mail@singhaakash.dev>
Co-authored-by: Aakash Singh <mail@singhaakash.dev>
@sainak
Copy link
Member

sainak commented Jul 17, 2023

@Ashesh3 add tests for api endpoints

Copy link
Member

@vigneshhari vigneshhari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge all the migrations into a single one

care/facility/api/viewsets/asset.py Show resolved Hide resolved
@Ashesh3
Copy link
Member Author

Ashesh3 commented Jul 18, 2023

@Ashesh3 add tests for api endpoints

Tests added.

@vigneshhari
Copy link
Member

Looks good to me, two things to note, maybe these can be made as issues,

  • Make sure that the request to any external API's have a timeout and its reasonable
  • Instead of one long celery job to monitor each asset, the master job could spin off multiple jobs per asset that are performed asynchronously, the checks themselves need not be serial.

@vigneshhari vigneshhari merged commit 9f84bd4 into master Jul 18, 2023
2 checks passed
@vigneshhari vigneshhari deleted the asset-uptime-2 branch July 18, 2023 14:07
@sentry-io
Copy link

sentry-io bot commented Jul 18, 2023

Suspect Issues

This pull request has been deployed and Sentry has observed the following issues:

  • ‼️ APIException: care.facility.tasks.asset_monitor.check_asset_s... View Issue

Did you find this useful? React with a 👍 or 👎

@Ashesh3
Copy link
Member Author

Ashesh3 commented Jul 18, 2023

Suspect Issues

This pull request has been deployed and Sentry has observed the following issues:

  • ‼️ **APIException: ** care.facility.tasks.asset_monitor.check_asset_s... View Issue

Did you find this useful? React with a 👍 or 👎

This is expected. Some middleware endpoints are non functional.

@sainak sainak mentioned this pull request Jul 21, 2023
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants