mirror of
https://github.com/KenanZhu/AutoLibrary.git
synced 2026-06-18 15:33:03 +08:00
feat(TimerTaskManageWidget): 实现重复任务执行与历史记录
- onTimerTaskIsExecuted/onTimerTaskIsError 添加历史记录 - 历史记录包含:execute_time、executed_time、result、duration - 重复任务执行后自动计算并更新下次执行时间
This commit is contained in:
@@ -21,11 +21,14 @@ from PySide6.QtWidgets import (
|
|||||||
QDialog, QWidget, QListWidgetItem, QMessageBox,
|
QDialog, QWidget, QListWidgetItem, QMessageBox,
|
||||||
QHBoxLayout, QVBoxLayout, QLabel, QPushButton
|
QHBoxLayout, QVBoxLayout, QLabel, QPushButton
|
||||||
)
|
)
|
||||||
|
|
||||||
|
import gui.ALTimerTaskHistoryDialog as ALTimerTaskHistoryDialog
|
||||||
from PySide6.QtGui import (
|
from PySide6.QtGui import (
|
||||||
QCloseEvent
|
QCloseEvent
|
||||||
)
|
)
|
||||||
|
|
||||||
import utils.ConfigManager as ConfigManager
|
import utils.ConfigManager as ConfigManager
|
||||||
|
import utils.TimerUtils as TimerUtils
|
||||||
|
|
||||||
from gui.resources.ui.Ui_ALTimerTaskManageWidget import Ui_ALTimerTaskManageWidget
|
from gui.resources.ui.Ui_ALTimerTaskManageWidget import Ui_ALTimerTaskManageWidget
|
||||||
from gui.ALTimerTaskAddDialog import ALTimerTaskAddDialog, ALTimerTaskStatus
|
from gui.ALTimerTaskAddDialog import ALTimerTaskAddDialog, ALTimerTaskStatus
|
||||||
@@ -61,8 +64,18 @@ class ALTimerTaskItemWidget(QWidget):
|
|||||||
TaskNameLabel.setFont(TaskNameLabelFont)
|
TaskNameLabel.setFont(TaskNameLabelFont)
|
||||||
TaskNameLabel.setFixedHeight(25)
|
TaskNameLabel.setFixedHeight(25)
|
||||||
self.TaskInfoLayout.addWidget(TaskNameLabel)
|
self.TaskInfoLayout.addWidget(TaskNameLabel)
|
||||||
|
|
||||||
ExecuteTimeStr = self.__timer_task["execute_time"].strftime("%Y-%m-%d %H:%M:%S")
|
ExecuteTimeStr = self.__timer_task["execute_time"].strftime("%Y-%m-%d %H:%M:%S")
|
||||||
|
if self.__timer_task.get("repeat", False):
|
||||||
|
repeat_days = self.__timer_task.get("repeat_days", [])
|
||||||
|
if len(repeat_days) == 7:
|
||||||
|
time_str = f"{self.__timer_task.get('repeat_hour', 0):02d}:{self.__timer_task.get('repeat_minute', 0):02d}:{self.__timer_task.get('repeat_second', 0):02d}"
|
||||||
|
ExecuteTimeLabel = QLabel(f"下次执行时间: {ExecuteTimeStr} (每日 {time_str})")
|
||||||
|
else:
|
||||||
|
day_names = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
|
||||||
|
selected_days = [day_names[d] for d in repeat_days]
|
||||||
|
time_str = f"{self.__timer_task.get('repeat_hour', 0):02d}:{self.__timer_task.get('repeat_minute', 0):02d}:{self.__timer_task.get('repeat_second', 0):02d}"
|
||||||
|
ExecuteTimeLabel = QLabel(f"下次执行时间: {ExecuteTimeStr} (每{','.join(selected_days)} {time_str})")
|
||||||
|
else:
|
||||||
ExecuteTimeLabel = QLabel(f"执行时间: {ExecuteTimeStr}")
|
ExecuteTimeLabel = QLabel(f"执行时间: {ExecuteTimeStr}")
|
||||||
ExecuteTimeLabel.setStyleSheet("color: #969696;")
|
ExecuteTimeLabel.setStyleSheet("color: #969696;")
|
||||||
ExecuteTimeLabel.setFixedHeight(20)
|
ExecuteTimeLabel.setFixedHeight(20)
|
||||||
@@ -124,6 +137,10 @@ class ALTimerTaskItemWidget(QWidget):
|
|||||||
if self.__timer_task["status"] == ALTimerTaskStatus.READY\
|
if self.__timer_task["status"] == ALTimerTaskStatus.READY\
|
||||||
or self.__timer_task["status"] == ALTimerTaskStatus.RUNNING:
|
or self.__timer_task["status"] == ALTimerTaskStatus.RUNNING:
|
||||||
self.DeleteButton.setEnabled(False)
|
self.DeleteButton.setEnabled(False)
|
||||||
|
if self.__timer_task.get("repeat", False):
|
||||||
|
self.HistoryButton = QPushButton("历史")
|
||||||
|
self.HistoryButton.setFixedSize(80, 25)
|
||||||
|
self.ItemWidgetLayout.addWidget(self.HistoryButton)
|
||||||
self.setFixedHeight(55)
|
self.setFixedHeight(55)
|
||||||
|
|
||||||
|
|
||||||
@@ -334,6 +351,10 @@ class ALTimerTaskManageWidget(QWidget, Ui_ALTimerTaskManageWidget):
|
|||||||
widget.DeleteButton.clicked.connect(
|
widget.DeleteButton.clicked.connect(
|
||||||
lambda _, uuid = timer_task["task_uuid"]: self.deleteTask(uuid)
|
lambda _, uuid = timer_task["task_uuid"]: self.deleteTask(uuid)
|
||||||
)
|
)
|
||||||
|
if timer_task.get("repeat", False) and hasattr(widget, "HistoryButton"):
|
||||||
|
widget.HistoryButton.clicked.connect(
|
||||||
|
lambda _, task = timer_task: self.showTaskHistory(task)
|
||||||
|
)
|
||||||
item.setSizeHint(widget.size())
|
item.setSizeHint(widget.size())
|
||||||
self.TimerTasksListWidget.addItem(item)
|
self.TimerTasksListWidget.addItem(item)
|
||||||
self.TimerTasksListWidget.setItemWidget(item, widget)
|
self.TimerTasksListWidget.setItemWidget(item, widget)
|
||||||
@@ -392,6 +413,16 @@ class ALTimerTaskManageWidget(QWidget, Ui_ALTimerTaskManageWidget):
|
|||||||
self.timerTasksChanged.emit()
|
self.timerTasksChanged.emit()
|
||||||
|
|
||||||
|
|
||||||
|
def showTaskHistory(
|
||||||
|
self,
|
||||||
|
task: dict
|
||||||
|
):
|
||||||
|
|
||||||
|
dialog = ALTimerTaskHistoryDialog.ALTimerTaskHistoryDialog(self, task)
|
||||||
|
if dialog.exec() == QDialog.DialogCode.Accepted:
|
||||||
|
self.timerTasksChanged.emit()
|
||||||
|
|
||||||
|
|
||||||
def checkTasks(
|
def checkTasks(
|
||||||
self
|
self
|
||||||
):
|
):
|
||||||
@@ -471,6 +502,31 @@ class ALTimerTaskManageWidget(QWidget, Ui_ALTimerTaskManageWidget):
|
|||||||
|
|
||||||
for task in self.__timer_tasks:
|
for task in self.__timer_tasks:
|
||||||
if task["task_uuid"] == timer_task["task_uuid"]:
|
if task["task_uuid"] == timer_task["task_uuid"]:
|
||||||
|
if task.get("repeat", False):
|
||||||
|
if "history" not in task:
|
||||||
|
task["history"] = []
|
||||||
|
executed_time = datetime.now()
|
||||||
|
duration = (executed_time - task["execute_time"]).total_seconds()
|
||||||
|
task["history"].append({
|
||||||
|
"execute_time": task["execute_time"].strftime("%Y-%m-%d %H:%M:%S"),
|
||||||
|
"executed_time": executed_time.strftime("%Y-%m-%d %H:%M:%S"),
|
||||||
|
"result": "成功",
|
||||||
|
"duration": duration,
|
||||||
|
"uuid": timer_task["task_uuid"]
|
||||||
|
})
|
||||||
|
next_time = TimerUtils.calculateNextRepeatTime(
|
||||||
|
task["repeat_days"],
|
||||||
|
task["repeat_hour"],
|
||||||
|
task["repeat_minute"],
|
||||||
|
task["repeat_second"]
|
||||||
|
)
|
||||||
|
if next_time:
|
||||||
|
task["execute_time"] = next_time
|
||||||
|
task["status"] = ALTimerTaskStatus.PENDING
|
||||||
|
task["executed"] = False
|
||||||
|
else:
|
||||||
|
task["status"] = ALTimerTaskStatus.EXECUTED
|
||||||
|
else:
|
||||||
task["status"] = ALTimerTaskStatus.EXECUTED
|
task["status"] = ALTimerTaskStatus.EXECUTED
|
||||||
self.timerTasksChanged.emit()
|
self.timerTasksChanged.emit()
|
||||||
|
|
||||||
@@ -482,5 +538,17 @@ class ALTimerTaskManageWidget(QWidget, Ui_ALTimerTaskManageWidget):
|
|||||||
|
|
||||||
for task in self.__timer_tasks:
|
for task in self.__timer_tasks:
|
||||||
if task["task_uuid"] == timer_task["task_uuid"]:
|
if task["task_uuid"] == timer_task["task_uuid"]:
|
||||||
|
if task.get("repeat", False):
|
||||||
|
if "history" not in task:
|
||||||
|
task["history"] = []
|
||||||
|
executed_time = datetime.now()
|
||||||
|
duration = (executed_time - task["execute_time"]).total_seconds()
|
||||||
|
task["history"].append({
|
||||||
|
"execute_time": task["execute_time"].strftime("%Y-%m-%d %H:%M:%S"),
|
||||||
|
"executed_time": executed_time.strftime("%Y-%m-%d %H:%M:%S"),
|
||||||
|
"result": "失败",
|
||||||
|
"duration": duration,
|
||||||
|
"uuid": timer_task["task_uuid"]
|
||||||
|
})
|
||||||
task["status"] = ALTimerTaskStatus.ERROR
|
task["status"] = ALTimerTaskStatus.ERROR
|
||||||
self.timerTasksChanged.emit()
|
self.timerTasksChanged.emit()
|
||||||
|
|||||||
@@ -18,7 +18,7 @@
|
|||||||
</property>
|
</property>
|
||||||
<property name="maximumSize">
|
<property name="maximumSize">
|
||||||
<size>
|
<size>
|
||||||
<width>600</width>
|
<width>800</width>
|
||||||
<height>400</height>
|
<height>400</height>
|
||||||
</size>
|
</size>
|
||||||
</property>
|
</property>
|
||||||
|
|||||||
Reference in New Issue
Block a user