MCPcopy Index your code
hub / github.com/Tong89/smartNode / _update_resource_utilization

Method _update_resource_utilization

backend/core.py:709–763  ·  view source on GitHub ↗

更新资源利用率统计 - 基于带宽的精确算法

(self)

Source from the content-addressed store, hash-verified

707 self.stats["rejected_requests"] += 1
708
709 def _update_resource_utilization(self):
710 """更新资源利用率统计 - 基于带宽的精确算法"""
711
712 # ⭐ 新算法:
713 # 1. 卫星: 只要有请求分配(包括等待)就算占用
714 # 2. 地面站: 只统计正在传输(transmitting)的请求
715 # 3. 中继: 基于带宽占用率计算,一个中继可以同时服务多个任务
716
717 # 计算卫星利用率 - 包括等待和传输中的请求
718 total_satellites = len(self.leo_satellites)
719 occupied_satellites = sum(1 for sat_id, req_list in self.resource_usage["satellites"].items() if len(req_list) > 0)
720 self.stats["resource_utilization"]["satellites"] = (occupied_satellites / total_satellites) if total_satellites > 0 else 0.0
721
722 # 计算地面站利用率 - 仅统计正在传输的请求
723 total_ground_stations = len(self.ground_stations)
724 transmitting_ground_stations = set()
725 for req in self.transmission_requests:
726 if req.status == "transmitting" and req.selected_ground_station:
727 transmitting_ground_stations.add(req.selected_ground_station)
728 self.stats["resource_utilization"]["ground_stations"] = (len(transmitting_ground_stations) / total_ground_stations) if total_ground_stations > 0 else 0.0
729
730 # ⭐ 计算GEO中继带宽利用率 - 基于实际带宽占用
731 # 每个中继有固定带宽(2000 Mbps),统计所有正在传输的任务占用的带宽
732 relay_bandwidth_usage = {} # {relay_id: used_bandwidth}
733
734 for req in self.transmission_requests:
735 if req.status == "transmitting":
736 # 检查第一跳中继
737 if req.selected_relay:
738 if req.selected_relay not in relay_bandwidth_usage:
739 relay_bandwidth_usage[req.selected_relay] = 0
740 relay_bandwidth_usage[req.selected_relay] += req.transmission_rate
741
742 # 检查第二跳中继
743 if req.selected_relay2:
744 if req.selected_relay2 not in relay_bandwidth_usage:
745 relay_bandwidth_usage[req.selected_relay2] = 0
746 relay_bandwidth_usage[req.selected_relay2] += req.transmission_rate
747
748 # 计算平均带宽利用率
749 total_geo_relays = len(self.geo_relays)
750 if total_geo_relays > 0:
751 total_bandwidth_utilization = 0
752 for geo in self.geo_relays:
753 relay_bandwidth = geo.get("bandwidth", 1600) # 默认1600 Mbps
754 used_bandwidth = relay_bandwidth_usage.get(geo["id"], 0)
755 utilization = min(1.0, used_bandwidth / relay_bandwidth) if relay_bandwidth > 0 else 0
756 total_bandwidth_utilization += utilization
757
758 self.stats["resource_utilization"]["geo_relays"] = total_bandwidth_utilization / total_geo_relays
759 else:
760 self.stats["resource_utilization"]["geo_relays"] = 0.0
761
762 # ⭐ 保存详细的中继带宽使用情况,供前端显示
763 self.stats["relay_bandwidth_usage"] = relay_bandwidth_usage
764
765 def _update_decision_metrics(self):
766 """⭐ 更新决策指标统计"""

Callers 4

_simulation_loopMethod · 0.95
_advanceFunction · 0.80
run_stepsMethod · 0.80
advance_engineFunction · 0.80

Calls 2

addMethod · 0.80
getMethod · 0.45

Tested by 2

_advanceFunction · 0.64
run_stepsMethod · 0.64