forked from swanchain/python-swan-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request swanchain#5 from filswan/feature/remove-unnecessar…
…y-api-and-stats-general-create Feature/remove unnecessary api and stats general create
- Loading branch information
Showing
3 changed files
with
82 additions
and
228 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
""" All codes related to Stats""" | ||
import logging | ||
import traceback | ||
from flask import jsonify, Blueprint | ||
from backend.model.response import Response | ||
from backend.service import job_service, user_service | ||
from backend.utils import constant | ||
|
||
stats_bp = Blueprint("stats_bp", __name__) | ||
|
||
|
||
@stats_bp.route('/stats/general', methods=['GET']) | ||
def get_general_stats(): | ||
""" | ||
Retrieves general statistics about the system. | ||
Returns: | ||
tuple: A tuple containing the JSON response and the HTTP status code. | ||
Raises: | ||
None | ||
""" | ||
# Initialize response data structure | ||
res_data = { | ||
"total_jobs": None, | ||
"total_running_jobs": None, | ||
"total_leading_jobs": None, | ||
"total_leading_job_duration": None, | ||
"total_users": None, | ||
"total_space_builders": None, | ||
} | ||
response = Response(status=constant.STATUS_FAILED, message=None) | ||
|
||
try: | ||
# Fetch statistics from services | ||
res_data["total_jobs"] = job_service.get_all_job() | ||
res_data["total_running_jobs"] = job_service.get_all_running_job() | ||
res_data["total_leading_jobs"] = job_service.get_all_leading_job() | ||
res_data["total_leading_job_duration"] = job_service.get_all_leading_job_duration() | ||
res_data["total_users"] = user_service.get_all_user() | ||
res_data["total_space_builders"] = user_service.get_all_space_builder() | ||
|
||
# Update response object with fetched data | ||
response.data = res_data | ||
response.status = constant.STATUS_SUCCESS | ||
response.message = "General stats found." | ||
|
||
return jsonify(response.to_dict()), 200 | ||
except Exception as e: | ||
# Log error and update response object in case of an exception | ||
logging.error(f"Failed to retrieve general stats: {str(e)}\n{traceback.format_exc()}") | ||
response.message = "Failed to retrieve general stats." | ||
return jsonify(response.to_dict()), 500 |
Oops, something went wrong.