Updated admin: better functionality + faster loading for models with many foreign keys

This commit is contained in:
Dana Van Aken 2019-08-27 13:20:49 -04:00
parent e28c979912
commit 7c964758e9
2 changed files with 72 additions and 77 deletions

View File

@ -13,118 +13,111 @@ from .models import (BackupData, DBMSCatalog, KnobCatalog,
SessionKnob) SessionKnob)
class BaseAdmin(admin.ModelAdmin): class DBMSCatalogAdmin(admin.ModelAdmin):
pass
@staticmethod
def dbms_info(obj):
try:
return obj.dbms.full_name
except AttributeError:
return obj.full_name
class DBMSCatalogAdmin(BaseAdmin): class KnobCatalogAdmin(admin.ModelAdmin):
list_display = ['dbms_info'] list_display = ('name', 'dbms', 'tunable')
list_filter = (('dbms', admin.RelatedOnlyFieldListFilter), 'tunable')
ordering = ('dbms', '-tunable', 'name')
class KnobCatalogAdmin(BaseAdmin): class MetricCatalogAdmin(admin.ModelAdmin):
list_display = ['name', 'dbms_info', 'tunable'] list_display = ('name', 'dbms', 'metric_type')
ordering = ['name', 'dbms__type', 'dbms__version'] list_filter = (('dbms', admin.RelatedOnlyFieldListFilter), 'metric_type')
list_filter = ['tunable'] ordering = ('dbms', 'name')
class MetricCatalogAdmin(BaseAdmin):
list_display = ['name', 'dbms_info', 'metric_type']
ordering = ['name', 'dbms__type', 'dbms__version']
list_filter = ['metric_type']
class ProjectAdmin(admin.ModelAdmin): class ProjectAdmin(admin.ModelAdmin):
list_display = ('name', 'user', 'last_update', 'creation_time') list_display = ('name', 'user', 'last_update', 'creation_time')
fields = ['name', 'user', 'last_update', 'creation_time'] list_filter = (('user', admin.RelatedOnlyFieldListFilter),)
ordering = ('name', 'user__username')
class SessionAdmin(admin.ModelAdmin): class SessionAdmin(admin.ModelAdmin):
list_display = ('name', 'user', 'last_update', 'creation_time') list_display = ('name', 'user', 'project', 'last_update', 'creation_time')
list_display_links = ('name',) list_filter = (('user', admin.RelatedOnlyFieldListFilter),
('project', admin.RelatedOnlyFieldListFilter))
ordering = ('name', 'user__username', 'project__name')
class SessionKnobAdmin(admin.ModelAdmin): class SessionKnobAdmin(admin.ModelAdmin):
list_display = ('knob', 'session', 'minval', 'maxval', 'tunable') list_display = ('knob', 'dbms', 'session', 'minval', 'maxval', 'tunable')
list_filter = (('session__dbms', admin.RelatedOnlyFieldListFilter),
('session', admin.RelatedOnlyFieldListFilter), ('tunable'))
ordering = ('session__dbms', 'session__name', '-tunable', 'knob__name')
@staticmethod
def dbms(obj):
return obj.session.dbms
class HardwareAdmin(admin.ModelAdmin): class HardwareAdmin(admin.ModelAdmin):
list_display = ('cpu', 'memory', 'storage') pass
class KnobDataAdmin(BaseAdmin): class KnobDataAdmin(admin.ModelAdmin):
list_display = ['name', 'dbms_info', 'creation_time'] list_display = ('name', 'dbms', 'session', 'creation_time')
fields = ['session', 'name', 'creation_time', list_filter = (('dbms', admin.RelatedOnlyFieldListFilter),
'knobs', 'data', 'dbms'] ('session', admin.RelatedOnlyFieldListFilter))
ordering = ('creation_time',)
class MetricDataAdmin(BaseAdmin): class MetricDataAdmin(admin.ModelAdmin):
list_display = ['name', 'dbms_info', 'creation_time'] list_display = ('name', 'dbms', 'session', 'creation_time')
fields = ['session', 'name', 'creation_time', list_filter = (('dbms', admin.RelatedOnlyFieldListFilter),
'metrics', 'data', 'dbms'] ('session', admin.RelatedOnlyFieldListFilter))
ordering = ('creation_time',)
class TaskMetaAdmin(admin.ModelAdmin): class TaskMetaAdmin(admin.ModelAdmin):
list_display = ['id', 'status', 'date_done'] list_display = ('id', 'status', 'task_result', 'date_done')
readonly_fields = ('id', 'task_id', 'status', 'result', 'date_done',
'traceback', 'hidden', 'meta')
class ResultAdmin(BaseAdmin): fields = readonly_fields
list_display = ['result_id', 'dbms_info', 'workload', 'creation_time', list_filter = ('status',)
'observation_time'] ordering = ('date_done',)
list_filter = ['dbms__type', 'dbms__version']
ordering = ['id']
@staticmethod @staticmethod
def result_id(obj): def task_result(obj, maxlen=300):
return obj.id res = obj.result
if res and len(res) > maxlen:
res = res[:maxlen] + '...'
return res
@staticmethod
def workload(obj): class ResultAdmin(admin.ModelAdmin):
return obj.workload.name readonly_fields = ('dbms', 'knob_data', 'metric_data', 'session', 'workload')
list_display = ('id', 'dbms', 'session', 'workload', 'creation_time')
list_select_related = ('dbms', 'session', 'workload')
list_filter = (('dbms', admin.RelatedOnlyFieldListFilter),
('session', admin.RelatedOnlyFieldListFilter),
('workload', admin.RelatedOnlyFieldListFilter))
ordering = ('creation_time',)
class BackupDataAdmin(admin.ModelAdmin): class BackupDataAdmin(admin.ModelAdmin):
list_display = ['id', 'result_id'] readonly_fields = ('id', 'result')
ordering = ('id',)
@staticmethod
def result_id(obj):
return obj.id
class PipelineDataAdmin(admin.ModelAdmin): class PipelineDataAdmin(admin.ModelAdmin):
list_display = ['id', 'version', 'task_type', 'workload', readonly_fields = ('pipeline_run',)
'creation_time'] list_display = ('id', 'pipeline_run', 'task_type', 'workload', 'creation_time')
ordering = ['-creation_time'] list_filter = ('task_type', ('workload', admin.RelatedOnlyFieldListFilter))
ordering = ('pipeline_run', 'creation_time')
@staticmethod
def version(obj):
return obj.pipeline_run.id
class PipelineRunAdmin(admin.ModelAdmin): class PipelineRunAdmin(admin.ModelAdmin):
list_display = ['id', 'start_time', 'end_time'] list_display = ('id', 'start_time', 'end_time')
ordering = ('id', 'start_time')
class PipelineResultAdmin(BaseAdmin):
list_display = ['task_type', 'dbms_info',
'hardware_info', 'creation_timestamp']
@staticmethod
def hardware_info(obj):
return obj.hardware.name
class WorkloadAdmin(admin.ModelAdmin): class WorkloadAdmin(admin.ModelAdmin):
list_display = ['workload_id', 'name'] list_display = ('name', 'dbms', 'hardware')
list_filter = (('dbms', admin.RelatedOnlyFieldListFilter),
@staticmethod ('hardware', admin.RelatedOnlyFieldListFilter))
def workload_id(obj):
return obj.pk
admin.site.register(DBMSCatalog, DBMSCatalogAdmin) admin.site.register(DBMSCatalog, DBMSCatalogAdmin)

View File

@ -21,7 +21,7 @@ class BaseModel(models.Model):
return self.__unicode__() return self.__unicode__()
def __unicode__(self): def __unicode__(self):
return self.name return getattr(self, 'name', str(self.pk))
@classmethod @classmethod
def get_labels(cls, style=LabelStyleType.DEFAULT_STYLE): def get_labels(cls, style=LabelStyleType.DEFAULT_STYLE):
@ -236,11 +236,13 @@ class SessionKnobManager(models.Manager):
knob_dicts = [knob for knob in knob_dicts if knob["tunable"]] knob_dicts = [knob for knob in knob_dicts if knob["tunable"]]
return knob_dicts return knob_dicts
def __unicode__(self):
return self.session.name + " " + self.knob.name
class SessionKnob(BaseModel): class SessionKnob(BaseModel):
@property
def name(self):
return self.knob.name
objects = SessionKnobManager() objects = SessionKnobManager()
session = models.ForeignKey(Session) session = models.ForeignKey(Session)
knob = models.ForeignKey(KnobCatalog) knob = models.ForeignKey(KnobCatalog)