fixed prefetchs on some methods

This commit is contained in:
Adolfo Gómez García 2020-05-19 18:05:30 +02:00
parent 5ea3949d4c
commit 5b9fe17353
3 changed files with 14 additions and 14 deletions

View File

@ -121,11 +121,12 @@ class ServicesPools(ModelHandler):
def getItems(self, *args, **kwargs):
# Optimized query, due that there is a lot of info needed for theee
d = getSqlDatetime() - datetime.timedelta(seconds=GlobalConfig.RESTRAINT_TIME.getInt())
return super().getItems(overview=kwargs.get('overview', True),
query=(ServicePool.objects.prefetch_related('service', 'service__provider', 'servicesPoolGroup', 'image', 'tags', 'meta')
return super().getItems(overview=kwargs.get('overview', True),
query=(ServicePool.objects.prefetch_related('service', 'service__provider', 'servicesPoolGroup', 'image', 'tags', 'meta', 'account')
.annotate(valid_count=Count('userServices', filter=~Q(userServices__state__in=State.INFO_STATES)))
.annotate(preparing_count=Count('userServices', filter=Q(userServices__state=State.PREPARING)))
.annotate(error_count=Count('userServices', filter=Q(userServices__state=State.ERROR, state_date__gt=d)))
.annotate(usage_count=Count('userServices', filter=Q(userServices__state__in=State.VALID_STATES, userServices__cache_level=0)))
)
)
# return super().getItems(overview=kwargs.get('overview', True), prefetch=['service', 'service__provider', 'servicesPoolGroup', 'image', 'tags'])
@ -151,7 +152,6 @@ class ServicesPools(ModelHandler):
# This needs a lot of queries, and really does not shows anything important i think...
# elif userServiceManager().canInitiateServiceFromDeployedService(item) is False:
# state = State.SLOWED_DOWN
val = {
'id': item.uuid,
'name': item.name,
@ -187,16 +187,12 @@ class ServicesPools(ModelHandler):
valid_count = item.valid_count
preparing_count = item.preparing_count
restrained = item.error_count > GlobalConfig.RESTRAINT_COUNT.getInt()
usage_count = item.usage_count
else:
valid_count = item.userServices.exclude(state__in=State.INFO_STATES).count()
preparing_count = item.userServices.filter(state=State.PREPARING).count()
restrained = item.isRestrained()
state = item.state
if item.isInMaintenance():
state = State.MAINTENANCE
elif userServiceManager().canInitiateServiceFromDeployedService(item) is False:
state = State.SLOWED_DOWN
usage_count = -1
poolGroupId = None
poolGroupName = _('Default')
@ -207,6 +203,7 @@ class ServicesPools(ModelHandler):
if item.servicesPoolGroup.image is not None:
poolGroupThumb = item.servicesPoolGroup.image.thumb64
val['state'] = state
val['thumb'] = item.image.thumb64 if item.image is not None else DEFAULT_THUMB_BASE64
val['user_services_count'] = valid_count
@ -218,7 +215,7 @@ class ServicesPools(ModelHandler):
val['pool_group_id'] = poolGroupId
val['pool_group_name'] = poolGroupName
val['pool_group_thumb'] = poolGroupThumb
val['usage'] = str(item.usage()) + '%'
val['usage'] = str(item.usage(usage_count)) + '%'
if item.osmanager:
val['osmanager_id'] = item.osmanager.uuid

View File

@ -106,7 +106,7 @@ class AssignedService(DetailHandler):
try:
if not item:
return [AssignedService.itemToDict(k) for k in parent.assignedUserServices().all()
.prefetch_related('properties').prefetch_related('deployed_service').prefetch_related('publication')]
.prefetch_related('properties', 'deployed_service', 'publication', 'user')]
return AssignedService.itemToDict(parent.assignedUserServices().get(processUuid(uuid=processUuid(item))))
except Exception:
logger.exception('getItems')
@ -197,7 +197,7 @@ class CachedService(AssignedService):
try:
if not item:
return [AssignedService.itemToDict(k, True) for k in parent.cachedUserServices().all()
.prefetch_related('properties').prefetch_related('deployed_service').prefetch_related('publication')]
.prefetch_related('properties', 'deployed_service', 'publication')]
cachedService: models.UserService = parent.cachedUserServices().get(uuid=processUuid(item))
return AssignedService.itemToDict(cachedService, True)
except Exception:

View File

@ -501,7 +501,7 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
"""
return self.userServices.filter(cache_level=0, user=None)
def usage(self) -> int:
def usage(self, cachedValue=-1) -> int:
"""
Returns the % used services, related to "maximum" user services
If no "maximum" number of services, will return 0% ofc
@ -513,7 +513,10 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
if maxs <= 0:
return 0
return 100 * self.assignedUserServices().filter(state__in=states.servicePool.VALID_STATES).count() // maxs
if cachedValue == -1:
cachedValue = self.assignedUserServices().filter(state__in=states.servicePool.VALID_STATES).count()
return 100 * cachedValue // maxs
def testServer(self, host, port, timeout=4) -> bool:
return self.service.testServer(host, port, timeout)