File size: 5,714 Bytes
35c63fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
"""
系统监控Admin界面
"""
from django.contrib import admin
from django.utils.html import format_html
from django.utils import timezone
from .models import SystemConfig, TaskMonitor


@admin.register(SystemConfig)
class SystemConfigAdmin(admin.ModelAdmin):
    """系统配置管理"""

    list_display = [
        'id', 'batch_translate_request_interval', 'max_concurrent_translate_tasks',
        'task_timeout_minutes', 'enable_detailed_logging', 'updated_at', 'edit_button'
    ]

    fieldsets = (
        ('API并发控制', {
            'fields': (
                'batch_translate_request_interval',
                'max_concurrent_translate_tasks',
                'task_timeout_minutes'
            ),
            'description': '控制批量翻译任务的执行频率和并发数量'
        }),
        ('监控和清理', {
            'fields': (
                'enable_detailed_logging',
                'auto_cleanup_completed_tasks',
                'cleanup_interval_hours'
            ),
            'description': '任务监控和自动清理设置'
        }),
        ('时间信息', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        })
    )

    readonly_fields = ('created_at', 'updated_at')

    def has_add_permission(self, request):
        # 只允许一个配置实例
        return not SystemConfig.objects.exists()

    def has_delete_permission(self, request, obj=None):
        # 不允许删除配置
        return False

    def edit_button(self, obj):
        """添加修改按钮"""
        return format_html(
            '<a class="button" href="/admin/system_monitor/systemconfig/{}/change/">修改配置</a>',
            obj.pk
        )
    edit_button.short_description = "操作"


@admin.register(TaskMonitor)
class TaskMonitorAdmin(admin.ModelAdmin):
    """任务监控管理"""

    list_display = [
        'task_id', 'task_type', 'project_name', 'status_display',
        'progress_display', 'duration_display', 'created_at'
    ]

    list_filter = [
        'status', 'task_type', 'created_at'
    ]

    search_fields = [
        'task_id', 'project_name', 'current_segment_text'
    ]

    readonly_fields = [
        'task_id', 'task_type', 'project_id', 'project_name',
        'total_segments', 'completed_segments', 'failed_segments',
        'start_time', 'end_time', 'current_segment_text',
        'created_at', 'updated_at', 'progress_percentage', 'duration_seconds'
    ]

    fieldsets = (
        ('任务信息', {
            'fields': (
                'task_id', 'task_type', 'project_id', 'project_name', 'status'
            )
        }),
        ('执行进度', {
            'fields': (
                'total_segments', 'completed_segments', 'failed_segments',
                'progress_percentage', 'current_segment_text'
            )
        }),
        ('时间信息', {
            'fields': (
                'start_time', 'end_time', 'duration_seconds',
                'created_at', 'updated_at'
            )
        }),
        ('错误信息', {
            'fields': ('error_message',),
            'classes': ('collapse',)
        })
    )

    def status_display(self, obj):
        """状态显示"""
        colors = {
            'pending': '#f39c12',
            'running': '#3498db',
            'completed': '#27ae60',
            'failed': '#e74c3c',
            'cancelled': '#95a5a6',
            'timeout': '#e67e22'
        }
        color = colors.get(obj.status, '#000000')
        return format_html(
            '<span style="color: {}; font-weight: bold;">{}</span>',
            color, obj.get_status_display()
        )
    status_display.short_description = '状态'

    def progress_display(self, obj):
        """进度显示"""
        if obj.status == 'pending':
            return '等待中'

        progress = obj.progress_percentage
        if progress == 100:
            color = '#27ae60'
        elif progress >= 50:
            color = '#3498db'
        else:
            color = '#f39c12'

        return format_html(
            '<div style="width: 100px; background-color: #ecf0f1; border-radius: 3px; overflow: hidden;">'
            '<div style="width: {}%; height: 20px; background-color: {}; text-align: center; line-height: 20px; color: white; font-size: 12px;">'
            '{}%</div></div>',
            progress, color, progress
        )
    progress_display.short_description = '进度'

    def duration_display(self, obj):
        """执行时长显示"""
        if not obj.start_time:
            return '-'

        duration = obj.duration_seconds
        if duration < 60:
            return f'{duration}秒'
        elif duration < 3600:
            return f'{duration // 60}{duration % 60}秒'
        else:
            hours = duration // 3600
            minutes = (duration % 3600) // 60
            return f'{hours}{minutes}分'
    duration_display.short_description = '执行时长'

    def has_add_permission(self, request):
        # 任务监控记录不允许手动添加
        return False

    def has_change_permission(self, request, obj=None):
        # 只允许查看,不允许修改(除了状态)
        return True

    def get_readonly_fields(self, request, obj=None):
        if obj and obj.status in ['running']:
            # 运行中的任务只允许修改状态(用于手动取消)
            return [f for f in self.readonly_fields if f != 'status']
        return self.readonly_fields


# 自定义Admin站点标题
admin.site.site_header = "翻译系统管理后台"
admin.site.site_title = "翻译系统"
admin.site.index_title = "系统监控和配置"