Skip to content

Commit

Permalink
Merge pull request swanchain#5 from filswan/feature/remove-unnecessar…
Browse files Browse the repository at this point in the history
…y-api-and-stats-general-create

Feature/remove unnecessary api and stats general create
  • Loading branch information
PlutoNbai authored Dec 5, 2023
2 parents 0539399 + 353272d commit 426f6de
Showing 3 changed files with 82 additions and 228 deletions.
49 changes: 27 additions & 22 deletions backend/router/computing_provider.py
Original file line number Diff line number Diff line change
@@ -46,6 +46,7 @@ def get_provider_info():
response_object.message = 'Provider not found'
return jsonify(response_object.to_dict()), 200


# connect the cp by sending a get request
@cp_bp.route('/cp/connect', methods=['GET'])
@jwt_or_api_token_required
@@ -58,15 +59,15 @@ def get_provider_connection():
if not node_id:
response.message = "node_id can not be empty."
return jsonify(response.to_dict()), http.client.BAD_REQUEST

provider = ComputingProvider.get_provider_by_node_id(node_id)
if not provider or provider.user_id != user.id:
response.message = "Provider is NOT found with the given node_id."
return jsonify(response.to_dict()), http.client.NOT_FOUND

try:
url = multiaddress_to_url(provider.multi_address, "api/v1/computing/host/info")
res = requests.get(url, timeout=15, verify=False) #15s timeout
res = requests.get(url, timeout=15, verify=False) # 15s timeout
if res.status_code == 200:
response.message = "successfully get response 200 from provider."
response.status = constant.STATUS_SUCCESS
@@ -79,6 +80,7 @@ def get_provider_connection():
logging.info(f"Error sending request to provider with node_id: {node_id}. Error {str(e)}]")
return jsonify(response.to_dict()), http.client.BAD_REQUEST


# register new cp
@cp_bp.route('/cp', methods=['POST'])
@api_token_required
@@ -122,6 +124,7 @@ def create_provider_info():
response.message = "CP with node_id already created."
return jsonify(response.to_dict()), http.client.OK


# update existing cp
@cp_bp.route("/cp/update", methods=["POST"])
@api_token_required
@@ -173,6 +176,7 @@ def update_provider_info():

return jsonify(response.to_dict()), http.client.OK


@cp_bp.route('/cp/summary', methods=['POST'])
@jwt_or_api_token_required
def update_provider_resources():
@@ -300,7 +304,7 @@ def get_provider_dashboard():
"total_used_vcpu": 0,
}

for provider in providers_query.all():
for provider in providers_query.all():
machine_data = machine_service.get_cp_machines_data(provider.id)
if machine_data:
all_machines_info["total_gpu"] += machine_data["gpu"]["total"]
@@ -337,7 +341,7 @@ def get_provider_dashboard():

# get map info from that provider.
cp_map_info = computing_provider_service.get_cp_map_info(provider.node_id)
city = cp_map_info["city"]
city = cp_map_info["city"]
if city and city not in city_set:
city_set.add(city)
map_info.append(cp_map_info)
@@ -392,7 +396,7 @@ def get_provider_dashboard():
if doc["_id"] in provider_data:
provider_data[doc["_id"]]["uptime"] = doc["success"] / doc["total_requests"]

response.data = {
response.data = {
"map_info": map_info,
"total_providers": total_providers_cnt,
"total_deployments": total_deployments,
@@ -414,11 +418,12 @@ def get_provider_dashboard():

return jsonify(response.to_dict()), 200


@cp_bp.route("/cp/machines", methods=["GET"])
def get_all_cp_machines():
response = Response(constant.STATUS_SUCCESS, message=None, data=None)

response.data = {"hardware" : []}
response.data = {"hardware": []}

configs = config_service.get_all_configs()
active_machines = computing_provider_service.get_all_active_machines()
@@ -444,13 +449,13 @@ def get_all_cp_machines():
specs = machine.specs
if not specs:
continue

if machine_service.validate_machine_spec(cfg_dict, specs):
hardware_info["hardware_status"] = "available"
cp = ComputingProvider.get_provider_by_id(machine.provider_id)
if cp.region:
machine_regions.add(cp.region)

except Exception as e:
logging.info(f"Error getting machine data for machine_id {machine.id}. Error {str(e)}]")

@@ -459,7 +464,8 @@ def get_all_cp_machines():

return jsonify(response.to_dict()), http.client.OK

# given a node_id, check if any cp with that node_id is active

# given a node_id, check if any cp with that node_id is active
@cp_bp.route("/cp/active", methods=["GET"])
def get_active_cp():
response_object = Response(constant.STATUS_SUCCESS, message=None, data=None)
@@ -478,6 +484,7 @@ def get_active_cp():
response_object.message = "Can not find any active cp with given node_id."
return jsonify(response_object.to_dict()), 200


@cp_bp.route('/cp/filter/<string:search_string>', methods=['GET'])
def filter_computing_providers_by_name(search_string: str):
response_object = Response(constant.STATUS_SUCCESS, message=None, data=None)
@@ -523,7 +530,7 @@ def get_computing_provider_details(node_id: str):
@api_token_required
def update_job_status():
response = Response(constant.STATUS_FAILED, message=None, data=None)

data = request.get_json()

uuid = data["job_uuid"]
@@ -542,17 +549,17 @@ def update_job_status():
provider = computing_provider_service.get_cp_by_node_id(job.bidder_id)
if provider is None:
response.message = "Provider running the job not found."
logging.error(f'{user_id} : CURRENT USER' )
logging.error(f'{user_id} : CURRENT USER')
return jsonify(response.to_dict()), http.client.NOT_FOUND

if provider.user_id != user_id:
response.message = "Invalid permissions to update the status of this job."
return jsonify(response.to_dict()), http.client.NOT_FOUND

# Check if job is in a status that can be updated
valid_statuses = [
"paid", # This is old status that is replaced by Submitted,
# including for backwards compatibility
"paid", # This is old status that is replaced by Submitted,
# including for backwards compatibility
constant.JOB_SUBMITTED,
constant.JOB_DOWNLOAD_SOURCE,
constant.JOB_UPLOAD_RESULT,
@@ -566,7 +573,7 @@ def update_job_status():
response.message = "Job's status cannot be updated."
return jsonify(response.to_dict()), http.client.BAD_REQUEST

if job.status!=constant.JOB_CANCELLED and job.status!=constant.JOB_CANCEL_FAILED:
if job.status != constant.JOB_CANCELLED and job.status != constant.JOB_CANCEL_FAILED:
job.status = status
update_record(job, db.session)
# sync_service.send_sync_update("job", {"uuid":job.uuid}, {"status":job.status,"updated_at":job.updated_at})
@@ -582,21 +589,21 @@ def update_job_status():
response.message = "Job status updated"
response.status = constant.SUCCESS
return jsonify(response.to_dict()), 200

response.message = "Job already cancelled."
response.status = constant.SUCCESS
return jsonify(response.to_dict()), 200


@cp_bp.route('/cp/<string:node_id>/<string:space_uuid>', methods=['GET'])
def cp_detail_for_space(node_id,space_uuid):
def cp_detail_for_space(node_id, space_uuid):
response_object = Response(constant.STATUS_FAILED, message=None, data=None)
cp = ComputingProvider.query.filter_by(node_id=node_id).first()
space = Space.query.filter_by(uuid=space_uuid).first()
response_object.data = {
"payment_amount": 0,
"running_time": 0,
"remaining_time":0,
"remaining_time": 0,
"space_status": None
}
if cp is None:
@@ -616,13 +623,11 @@ def cp_detail_for_space(node_id,space_uuid):
response_object.message = "Space's task does not have a leading job"
response_object.data["space_status"] = space.status
return jsonify(response_object.to_dict()), 200
payment_amount = computing_provider_service.calculate_job_payout(space,leading_job)
payment_amount = computing_provider_service.calculate_job_payout(space, leading_job)
if type(payment_amount) == str:
response_object.message = payment_amount
else:
response_object.data = payment_amount
response_object.data["space_status"] = space.status
response_object.status = constant.STATUS_SUCCESS
return jsonify(response_object.to_dict()), 200


53 changes: 53 additions & 0 deletions backend/router/stats.py
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
Loading

0 comments on commit 426f6de

Please sign in to comment.