Refactors object_totals to crunch more of the data db-side instead of app-side.
This commit is contained in:
parent
edff348d87
commit
b848d321f9
2 changed files with 31 additions and 13 deletions
|
|
@ -606,9 +606,9 @@ class CmdObjects(COMMAND_DEFAULT_CLASS):
|
||||||
"|wtypeclass|n", "|wcount|n", "|w%|n", border="table", align="l"
|
"|wtypeclass|n", "|wcount|n", "|w%|n", border="table", align="l"
|
||||||
)
|
)
|
||||||
typetable.align = "l"
|
typetable.align = "l"
|
||||||
dbtotals = ObjectDB.objects.object_totals()
|
dbtotals = ObjectDB.objects.get_typeclass_totals()
|
||||||
for path, count in dbtotals.items():
|
for stat in dbtotals:
|
||||||
typetable.add_row(path, count, "%.2f" % ((float(count) / nobjs) * 100))
|
typetable.add_row(stat.get('typeclass', '<error>'), stat.get('count', -1), "%.2f" % stat.get('percent', -1))
|
||||||
|
|
||||||
# last N table
|
# last N table
|
||||||
objs = ObjectDB.objects.all().order_by("db_date_created")[
|
objs = ObjectDB.objects.all().order_by("db_date_created")[
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,8 @@ all Attributes and TypedObjects).
|
||||||
|
|
||||||
"""
|
"""
|
||||||
import shlex
|
import shlex
|
||||||
from django.db.models import Q, Count
|
from django.db.models import F, Q, Count, ExpressionWrapper, FloatField
|
||||||
|
from django.db.models.functions import Cast
|
||||||
from evennia.utils import idmapper
|
from evennia.utils import idmapper
|
||||||
from evennia.utils.utils import make_iter, variable_from_module
|
from evennia.utils.utils import make_iter, variable_from_module
|
||||||
from evennia.typeclasses.attributes import Attribute
|
from evennia.typeclasses.attributes import Attribute
|
||||||
|
|
@ -265,7 +266,7 @@ class TypedObjectManager(idmapper.manager.SharedMemoryManager):
|
||||||
tagtype (str, optional): 'type' of Tag, by default
|
tagtype (str, optional): 'type' of Tag, by default
|
||||||
this is either `None` (a normal Tag), `alias` or
|
this is either `None` (a normal Tag), `alias` or
|
||||||
`permission`. This always apply to all queried tags.
|
`permission`. This always apply to all queried tags.
|
||||||
|
|
||||||
Kwargs:
|
Kwargs:
|
||||||
match (str): ALL or ANY, determines whether the target object must match
|
match (str): ALL or ANY, determines whether the target object must match
|
||||||
ALL of the provided tags/categories or ANY single one. ANY will perform
|
ALL of the provided tags/categories or ANY single one. ANY will perform
|
||||||
|
|
@ -499,7 +500,29 @@ class TypedObjectManager(idmapper.manager.SharedMemoryManager):
|
||||||
if max_dbref is not None:
|
if max_dbref is not None:
|
||||||
retval = retval.filter(id__lte=self.dbref(max_dbref, reqhash=False))
|
retval = retval.filter(id__lte=self.dbref(max_dbref, reqhash=False))
|
||||||
return retval
|
return retval
|
||||||
|
|
||||||
|
def get_typeclass_totals(self, *args, **kwargs) -> object:
|
||||||
|
"""
|
||||||
|
Returns a queryset of typeclass composition statistics.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
qs (Queryset): A queryset of dicts containing the typeclass (name),
|
||||||
|
the count of objects with that typeclass and a float representing
|
||||||
|
the percentage of objects associated with the typeclass.
|
||||||
|
|
||||||
|
"""
|
||||||
|
return self.values('db_typeclass_path').distinct().annotate(
|
||||||
|
# Get count of how many objects for each typeclass exist
|
||||||
|
count=Count('db_typeclass_path')
|
||||||
|
).annotate(
|
||||||
|
# Rename db_typeclass_path field to something more human
|
||||||
|
typeclass=F('db_typeclass_path'),
|
||||||
|
# Calculate this class' percentage of total composition
|
||||||
|
percent=ExpressionWrapper(
|
||||||
|
((F('count') / float(self.count())) * 100.0), output_field=FloatField()
|
||||||
|
),
|
||||||
|
).values('typeclass', 'count', 'percent')
|
||||||
|
|
||||||
def object_totals(self):
|
def object_totals(self):
|
||||||
"""
|
"""
|
||||||
Get info about database statistics.
|
Get info about database statistics.
|
||||||
|
|
@ -511,13 +534,8 @@ class TypedObjectManager(idmapper.manager.SharedMemoryManager):
|
||||||
object having that typeclass set on themselves).
|
object having that typeclass set on themselves).
|
||||||
|
|
||||||
"""
|
"""
|
||||||
dbtotals = {}
|
stats = self.get_typeclass_totals().order_by('typeclass')
|
||||||
typeclass_paths = set(self.values_list("db_typeclass_path", flat=True))
|
return {x.get('typeclass'):x.get('count') for x in stats}
|
||||||
for typeclass_path in typeclass_paths:
|
|
||||||
dbtotals[typeclass_path] = self.filter(
|
|
||||||
db_typeclass_path=typeclass_path
|
|
||||||
).count()
|
|
||||||
return dbtotals
|
|
||||||
|
|
||||||
def typeclass_search(
|
def typeclass_search(
|
||||||
self, typeclass, include_children=False, include_parents=False
|
self, typeclass, include_children=False, include_parents=False
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue