1
0
mirror of https://github.com/dkmstr/openuds.git synced 2025-08-29 01:50:00 +03:00

Updated usageInfo to be more coherent

This commit is contained in:
Adolfo Gómez García
2023-11-13 18:31:19 +01:00
parent cc3e5acd52
commit f3ddb68381
4 changed files with 46 additions and 33 deletions

View File

@ -82,3 +82,12 @@ class HighAvailabilityPolicy(enum.IntEnum):
(HighAvailabilityPolicy.DISABLED, _('Disabled')),
(HighAvailabilityPolicy.ENABLED, _('Enabled')),
]
class UsageInfo(typing.NamedTuple):
used: int
total: int
@property
def percent(self) -> int:
return (self.used * 100 // self.total) if self.total > 0 else 0

View File

@ -155,16 +155,16 @@ class MetaPool(UUIDModel, TaggingMixin): # type: ignore
return access == states.action.ALLOW
def usage(self, cachedValue=-1) -> typing.Tuple[int, int, int]:
def usage(self, cachedValue=-1) -> types.pools.UsageInfo:
"""
Returns the % used services, then count and the max related to "maximum" user services
If no "maximum" number of services, will return 0% ofc
cachedValue is used to optimize (if known the number of assigned services, we can avoid to query the db)
"""
# If no pools, return 0, 0, 0
# If no pools, return 0%
if self.members.count() == 0:
return (0, 0, 0)
return types.pools.UsageInfo(0, 0)
query = (
ServicePool.objects.filter(
memberOfMeta__meta_pool=self,
@ -185,22 +185,22 @@ class MetaPool(UUIDModel, TaggingMixin): # type: ignore
'service__provider',
)
)
usage_count = 0
max_count = 0
for pool in query:
p, u, m = pool.usage(pool.usage_count) # type:ignore # Anotated field
usage_count += u
poolInfo = pool.usage(pool.usage_count) # type:ignore # Anotated field
usage_count += poolInfo.used
# If any of the pools has no max, then max is -1
if max_count == consts.UNLIMITED or m == consts.UNLIMITED:
if max_count == consts.UNLIMITED or poolInfo.total == consts.UNLIMITED:
max_count = consts.UNLIMITED
else:
max_count += m
max_count += poolInfo.total
if max_count == 0 or max_count == consts.UNLIMITED:
return (0, usage_count, consts.UNLIMITED)
return types.pools.UsageInfo(usage_count, consts.UNLIMITED)
return (usage_count * 100 // max_count, usage_count, max_count)
return types.pools.UsageInfo(usage_count, max_count)
@property
def visual_name(self) -> str:

View File

@ -36,7 +36,7 @@ import operator
from datetime import datetime, timedelta
from django.db import models, transaction
from uds.core import exceptions
from uds.core import exceptions, types
from uds.core.environment import Environment
from uds.core.util import log, states, calendar, serializer
@ -631,7 +631,7 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
"""
return self.userServices.filter(cache_level=0, user=None)
def usage(self, cachedValue=-1) -> typing.Tuple[int, int, int]:
def usage(self, cachedValue=-1) -> types.pools.UsageInfo:
"""
Returns the % used services, then count and the max related to "maximum" user services
If no "maximum" number of services, will return 0% ofc
@ -645,9 +645,9 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
cachedValue = self.assignedUserServices().filter(state__in=states.userService.VALID_STATES).count()
if maxs == 0 or max == consts.UNLIMITED:
return 0, cachedValue, consts.UNLIMITED
return types.pools.UsageInfo(cachedValue, consts.UNLIMITED)
return 100 * cachedValue // maxs, cachedValue, maxs
return types.pools.UsageInfo(cachedValue, maxs)
def testServer(self, host: str, port: typing.Union[str, int], timeout: float = 4) -> bool:
return bool(self.service) and self.service.testServer(host, port, timeout)

View File

@ -187,14 +187,14 @@ def getServicesData(
inAll: typing.Optional[typing.Set[str]] = None
tmpSet: typing.Set[str]
# If no macro on names, skip calculation (and set to empty)
if '{' in meta.name or '{' in meta.visual_name:
up, uc, max_s = meta.usage()
use_percent = str(up) + '%'
use_count = str(uc)
left_count = str(max_s - uc)
max_srvs = str(max_s)
poolUsageInfo = meta.usage()
use_percent = str(poolUsageInfo.percent) + '%'
use_count = str(poolUsageInfo.used)
left_count = str(poolUsageInfo.total - poolUsageInfo.used)
max_srvs = str(poolUsageInfo.total)
else:
max_srvs = ''
use_percent = ''
@ -209,7 +209,7 @@ def getServicesData(
.replace('{usec}', use_count)
.replace('{left}', left_count)
)
if meta.transport_grouping == types.pools.TransportSelectionPolicy.COMMON:
# only keep transports that are in ALL members
for member in meta.members.all().order_by('priority'):
@ -310,14 +310,16 @@ def getServicesData(
# Skip pools that are part of meta pools
if sPool.owned_by_meta:
continue
# If no macro on names, skip calculation
if '{' in sPool.name or '{' in sPool.visual_name:
up, uc, max_s = sPool.usage(sPool.usage_count) # type: ignore # anotated value
use_percent = str(up) + '%' # type: ignore # anotated value
use_count = str(uc) # type: ignore # anotated value
left_count = str(max_s - uc) # type: ignore # anotated value
max_srvs = str(max_s)
poolUsageInfo = sPool.usage(
sPool.usage_count, # type: ignore # anotated value
)
use_percent = str(poolUsageInfo.percent) + '%'
use_count = str(poolUsageInfo.used)
left_count = str(poolUsageInfo.total - poolUsageInfo.used)
max_srvs = str(poolUsageInfo.total)
else:
max_srvs = ''
use_percent = ''
@ -338,7 +340,12 @@ def getServicesData(
sPool.transports.all(), key=lambda x: x.priority
): # In memory sort, allows reuse prefetched and not too big array
typeTrans = t.getType()
if typeTrans and t.isValidForIp(request.ip) and typeTrans.supportsOs(osType) and t.isValidForOs(osType):
if (
typeTrans
and t.isValidForIp(request.ip)
and typeTrans.supportsOs(osType)
and t.isValidForOs(osType)
):
if typeTrans.ownLink:
link = reverse('TransportOwnLink', args=('F' + sPool.uuid, t.uuid)) # type: ignore
else:
@ -349,7 +356,6 @@ def getServicesData(
if not trans:
continue
# Locate if user service has any already assigned user service for this. Use "pre cached" number of assignations in this pool to optimize
in_use = typing.cast(typing.Any, sPool).number_in_use > 0
# if svr.number_in_use: # Anotated value got from getDeployedServicesForGroups(...). If 0, no assignation for this user
@ -383,9 +389,7 @@ def getServicesData(
uuid=sPool.uuid,
is_meta=False,
name=macro_info(sPool.name),
visual_name=macro_info(
sPool.visual_name
),
visual_name=macro_info(sPool.visual_name),
description=sPool.comments,
group=group,
transports=trans,