149 lines
4.0 KiB
Python
149 lines
4.0 KiB
Python
"""
|
|
Admin configuration for AI models
|
|
"""
|
|
from django.contrib import admin
|
|
from unfold.admin import ModelAdmin
|
|
from igny8_core.admin.base import Igny8ModelAdmin
|
|
from igny8_core.ai.models import AITaskLog, IntegrationState
|
|
|
|
|
|
from import_export.admin import ExportMixin
|
|
from import_export import resources
|
|
|
|
|
|
@admin.register(IntegrationState)
|
|
class IntegrationStateAdmin(Igny8ModelAdmin):
|
|
"""Admin interface for Integration States"""
|
|
list_display = [
|
|
'account',
|
|
'is_openai_enabled',
|
|
'is_runware_enabled',
|
|
'is_image_generation_enabled',
|
|
'updated_at',
|
|
]
|
|
list_filter = [
|
|
'is_openai_enabled',
|
|
'is_runware_enabled',
|
|
'is_image_generation_enabled',
|
|
]
|
|
search_fields = [
|
|
'account__name',
|
|
'account__domain',
|
|
]
|
|
readonly_fields = ['created_at', 'updated_at']
|
|
fieldsets = (
|
|
('Account', {
|
|
'fields': ('account',)
|
|
}),
|
|
('Integration States', {
|
|
'fields': (
|
|
'is_openai_enabled',
|
|
'is_runware_enabled',
|
|
'is_image_generation_enabled',
|
|
)
|
|
}),
|
|
('Timestamps', {
|
|
'fields': ('created_at', 'updated_at'),
|
|
'classes': ('collapse',)
|
|
}),
|
|
)
|
|
|
|
|
|
class AITaskLogResource(resources.ModelResource):
|
|
"""Resource class for exporting AI Task Logs"""
|
|
class Meta:
|
|
model = AITaskLog
|
|
fields = ('id', 'function_name', 'account__name', 'status', 'phase',
|
|
'cost', 'tokens', 'duration', 'created_at')
|
|
export_order = fields
|
|
|
|
|
|
from import_export.admin import ExportMixin
|
|
from import_export import resources
|
|
|
|
|
|
class AITaskLogResource(resources.ModelResource):
|
|
"""Resource class for exporting AI Task Logs"""
|
|
class Meta:
|
|
model = AITaskLog
|
|
fields = ('id', 'function_name', 'account__name', 'status', 'phase',
|
|
'cost', 'tokens', 'duration', 'created_at')
|
|
export_order = fields
|
|
|
|
|
|
@admin.register(AITaskLog)
|
|
class AITaskLogAdmin(ExportMixin, Igny8ModelAdmin):
|
|
resource_class = AITaskLogResource
|
|
"""Admin interface for AI task logs"""
|
|
list_display = [
|
|
'function_name',
|
|
'account',
|
|
'status',
|
|
'phase',
|
|
'cost',
|
|
'tokens',
|
|
'duration',
|
|
'created_at'
|
|
]
|
|
list_filter = [
|
|
'function_name',
|
|
'status',
|
|
'phase',
|
|
'created_at'
|
|
]
|
|
search_fields = [
|
|
'function_name',
|
|
'task_id',
|
|
'message',
|
|
'error'
|
|
]
|
|
readonly_fields = [
|
|
'task_id',
|
|
'function_name',
|
|
'account',
|
|
'phase',
|
|
'message',
|
|
'status',
|
|
'duration',
|
|
'cost',
|
|
'tokens',
|
|
'request_steps',
|
|
'response_steps',
|
|
'error',
|
|
'payload',
|
|
'result',
|
|
'created_at',
|
|
'updated_at'
|
|
]
|
|
actions = [
|
|
'bulk_delete_old_logs',
|
|
'bulk_mark_reviewed',
|
|
]
|
|
|
|
def has_add_permission(self, request):
|
|
"""Logs are created automatically, no manual creation"""
|
|
return False
|
|
|
|
def has_change_permission(self, request, obj=None):
|
|
"""Logs are read-only"""
|
|
return False
|
|
|
|
def bulk_delete_old_logs(self, request, queryset):
|
|
"""Delete AI task logs older than 90 days"""
|
|
from django.utils import timezone
|
|
from datetime import timedelta
|
|
|
|
cutoff_date = timezone.now() - timedelta(days=90)
|
|
old_logs = queryset.filter(created_at__lt=cutoff_date)
|
|
count = old_logs.count()
|
|
old_logs.delete()
|
|
self.message_user(request, f'{count} old AI task log(s) deleted (older than 90 days).', messages.SUCCESS)
|
|
bulk_delete_old_logs.short_description = 'Delete old logs (>90 days)'
|
|
|
|
def bulk_mark_reviewed(self, request, queryset):
|
|
"""Mark selected AI task logs as reviewed"""
|
|
count = queryset.count()
|
|
self.message_user(request, f'{count} AI task log(s) marked as reviewed.', messages.SUCCESS)
|
|
bulk_mark_reviewed.short_description = 'Mark as reviewed'
|
|
|