More improvements on getServices

This commit is contained in:
Adolfo Gómez García 2020-05-22 12:43:41 +02:00
parent fcea278d2f
commit e0a512eefb
7 changed files with 33 additions and 30 deletions

View File

@ -304,7 +304,6 @@ class UserServiceManager:
return None return None
def getAssignationForUser(self, servicePool: ServicePool, user: User) -> typing.Optional[UserService]: # pylint: disable=too-many-branches def getAssignationForUser(self, servicePool: ServicePool, user: User) -> typing.Optional[UserService]: # pylint: disable=too-many-branches
if servicePool.service.getInstance().spawnsNew is False: if servicePool.service.getInstance().spawnsNew is False:
assignedUserService = self.getExistingAssignationForUser(servicePool, user) assignedUserService = self.getExistingAssignationForUser(servicePool, user)
else: else:

View File

@ -30,13 +30,11 @@
""" """
@author: Adolfo Gómez, dkmaster at dkmon dot com @author: Adolfo Gómez, dkmaster at dkmon dot com
""" """
from __future__ import unicode_literals
from . import action from . import action
from . import common from . import common
from . import group from . import group
from . import process from . import process
from . import publication from . import publication
from . import userService from . import user_service as userService
from . import servicePool from . import service_pool as servicePool
from . import task from . import task

View File

@ -32,5 +32,5 @@
""" """
# pylint: disable=unused-import # pylint: disable=unused-import
from .common import ACTIVE, REMOVABLE, REMOVING, REMOVED, INFO_STATES, VALID_STATES # @UnusedImport from .common import ACTIVE, REMOVABLE, REMOVING, REMOVED # @UnusedImport

View File

@ -32,4 +32,4 @@
""" """
# pylint: disable=unused-import # pylint: disable=unused-import
from .common import ERROR, USABLE, PREPARING, REMOVABLE, REMOVING, REMOVED # @UnusedImport from .common import ERROR, USABLE, PREPARING, REMOVABLE, REMOVING, REMOVED, INFO_STATES, VALID_STATES # @UnusedImport

View File

@ -169,11 +169,12 @@ class MetaPool(UUIDModel, TaggingMixin): # type: ignore
) )
if user: if user:
meta = meta.annotate( meta = meta.annotate(
number_assignations=models.Count( number_in_use=models.Count(
'pools__userServices', 'pools__userServices',
filter=models.Q( filter=models.Q(
pools__userServices__user=user, pools__userServices__user=user,
pools__userServices__in_use=True pools__userServices__in_use=True,
pools__userServices__state__in=states.userService.USABLE
) )
) )
) )

View File

@ -490,16 +490,20 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
) )
if user: # Optimize loading if there is some assgned service.. if user: # Optimize loading if there is some assgned service..
list1 = list1.annotate( list1 = list1.annotate(
number_assignations=models.Count( number_in_use=models.Count(
'userServices', filter=models.Q( 'userServices', filter=models.Q(
userServices__user=user, userServices__in_use=True userServices__user=user,
userServices__in_use=True,
userServices__state__in=states.userService.USABLE
) )
) )
) )
list2 = list2.annotate( list2 = list2.annotate(
number_assignations=models.Count( number_in_use=models.Count(
'userServices', filter=models.Q( 'userServices', filter=models.Q(
userServices__user=user, userServices__in_use=True userServices__user=user,
userServices__in_use=True,
userServices__state__in=states.userService.USABLE
) )
) )
) )

View File

@ -97,7 +97,7 @@ def getServicesData(request: 'HttpRequest') -> typing.Dict[str, typing.Any]: #
for meta in availMetaPools: for meta in availMetaPools:
# Check that we have access to at least one transport on some of its children # Check that we have access to at least one transport on some of its children
hasUsablePools = False hasUsablePools = False
in_use = False in_use = meta.number_in_use > 0 # False
for pool in meta.pools.all(): for pool in meta.pools.all():
# if pool.isInMaintenance(): # if pool.isInMaintenance():
# continue # continue
@ -107,10 +107,10 @@ def getServicesData(request: 'HttpRequest') -> typing.Dict[str, typing.Any]: #
hasUsablePools = True hasUsablePools = True
break break
if not in_use and meta.number_assignations: # Only look for assignation on possible used # if not in_use and meta.number_in_use: # Only look for assignation on possible used
assignedUserService = userServiceManager().getExistingAssignationForUser(pool, request.user) # assignedUserService = userServiceManager().getExistingAssignationForUser(pool, request.user)
if assignedUserService: # if assignedUserService:
in_use = assignedUserService.in_use # in_use = assignedUserService.in_use
# Stop when 1 usable pool is found # Stop when 1 usable pool is found
if hasUsablePools: if hasUsablePools:
@ -180,20 +180,21 @@ def getServicesData(request: 'HttpRequest') -> typing.Dict[str, typing.Any]: #
imageId = 'x' imageId = 'x'
# Locate if user service has any already assigned user service for this. Use "pre cached" number of assignations in this pool to optimize # 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 = False in_use = svr.number_in_use > 0
if svr.number_assignations: # Anotated value got from getDeployedServicesForGroups(...). If 0, no assignation for this user # if svr.number_in_use: # Anotated value got from getDeployedServicesForGroups(...). If 0, no assignation for this user
ads = userServiceManager().getExistingAssignationForUser(svr, request.user) # ads = userServiceManager().getExistingAssignationForUser(svr, request.user)
if ads: # if ads:
in_use = ads.in_use # in_use = ads.in_use
group = svr.servicesPoolGroup.as_dict if svr.servicesPoolGroup else ServicePoolGroup.default().as_dict group = svr.servicesPoolGroup.as_dict if svr.servicesPoolGroup else ServicePoolGroup.default().as_dict
tbr = svr.toBeReplaced(request.user) toBeReplaced = svr.toBeReplaced(request.user)
if tbr: # tbr = False
tbr = formats.date_format(tbr, "SHORT_DATETIME_FORMAT") if toBeReplaced:
tbrt = ugettext('This service is about to be replaced by a new version. Please, close the session before {} and save all your work to avoid loosing it.').format(tbr) toBeReplaced = formats.date_format(toBeReplaced, "SHORT_DATETIME_FORMAT")
toBeReplacedTxt = ugettext('This service is about to be replaced by a new version. Please, close the session before {} and save all your work to avoid loosing it.').format(toBeReplaced)
else: else:
tbrt = '' toBeReplacedTxt = ''
services.append({ services.append({
'id': 'F' + svr.uuid, 'id': 'F' + svr.uuid,
@ -209,8 +210,8 @@ def getServicesData(request: 'HttpRequest') -> typing.Dict[str, typing.Any]: #
'maintenance': svr.isInMaintenance(), 'maintenance': svr.isInMaintenance(),
'not_accesible': not svr.isAccessAllowed(now), 'not_accesible': not svr.isAccessAllowed(now),
'in_use': in_use, 'in_use': in_use,
'to_be_replaced': tbr, 'to_be_replaced': toBeReplaced,
'to_be_replaced_text': tbrt, 'to_be_replaced_text': toBeReplacedTxt,
'custom_calendar_text': svr.calendar_message, 'custom_calendar_text': svr.calendar_message,
}) })