Skip to content

Commit

Permalink
chore: opt db query
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 committed Jan 26, 2024
1 parent c4ba0e2 commit b367fca
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions apps/proxy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ def calc_total_traffic(cls):
def get_by_ip(clc, ip: str):
return clc.objects.filter(server=ip).first()

@cached_property
def have_oc_users(self) -> bool:
occupancies_query = UserProxyNodeOccupancy.get_node_occupancies(self)
return occupancies_query.count() > 0

def get_node_users(self):
# 1. if node is not enable, return empty list
if not self.enable:
Expand Down Expand Up @@ -546,9 +551,10 @@ def to_user_config(self, node: ProxyNode, user: User):
user.download_traffic + user.upload_traffic
)
have_oc_traffic = False
oc = UserProxyNodeOccupancy.get_by_proxy_node_and_user(node, user)
if oc:
have_oc_traffic = not oc.out_of_usage()
if node.have_oc_users:
oc = UserProxyNodeOccupancy.get_by_proxy_node_and_user(node, user)
if oc:
have_oc_traffic = not oc.out_of_usage()
enable = node.enable and (have_shared_traffic or have_oc_traffic)
return {
"user_id": user.id,
Expand Down Expand Up @@ -605,9 +611,10 @@ def to_user_config(self, node: ProxyNode, user: User):
user.download_traffic + user.upload_traffic
)
have_oc_traffic = False
oc = UserProxyNodeOccupancy.get_by_proxy_node_and_user(node, user)
if oc:
have_oc_traffic = not oc.out_of_usage()
if node.have_oc_users:
oc = UserProxyNodeOccupancy.get_by_proxy_node_and_user(node, user)
if oc:
have_oc_traffic = not oc.out_of_usage()
enable = node.enable and (have_shared_traffic or have_oc_traffic)

return {
Expand Down

0 comments on commit b367fca

Please sign in to comment.