task.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. from time import sleep
  5. from datetime import datetime, time, timedelta
  6. from random import randint
  7. from module.config.config import Config
  8. from tasks.Component.config_base import TimeDelta
  9. class Task:
  10. def __init__(self,
  11. next_run: datetime,
  12. limit_time: time,
  13. target_run: datetime = None,
  14. team_task: bool = False,
  15. role: str = 'leader',
  16. limit_count: int = 0,
  17. ):
  18. self.next_run = next_run
  19. self.target_run = target_run
  20. self.limit_time = limit_time
  21. self.team_task = team_task
  22. self.role = role
  23. self.limit_count = limit_count
  24. def update_info(self,
  25. next_run: datetime,
  26. limit_time: time,
  27. limit_count: int,
  28. role: str):
  29. # next_run
  30. if isinstance(next_run, datetime):
  31. self.next_run = next_run
  32. elif isinstance(next_run, str):
  33. self.next_run = datetime.fromisoformat(next_run)
  34. else:
  35. raise TypeError(f'next_run must be datetime or str, not {type(next_run)}')
  36. # limit_count
  37. if isinstance(limit_count, int):
  38. self.limit_count = limit_count
  39. elif isinstance(limit_count, str):
  40. self.limit_count = int(limit_count)
  41. else:
  42. raise TypeError(f'limit_count must be int or str, not {type(limit_count)}')
  43. # limit_time
  44. if isinstance(limit_time, time):
  45. self.limit_time = limit_time
  46. elif isinstance(limit_time, str):
  47. self.limit_time = time.fromisoformat(limit_time)
  48. else:
  49. raise TypeError(f'limit_time must be time or str, not {type(limit_time)}')
  50. # role
  51. if isinstance(role, str):
  52. self.role = role
  53. else:
  54. raise TypeError(f'role must be str, not {type(role)}')
  55. # ----------------------------------------------------------------------------------------------------------------------
  56. # 对于host来进行操作的函数
  57. def u(self) -> float:
  58. """
  59. 效用函数
  60. :return:
  61. """
  62. if self.role == 'leader':
  63. return self.r_leader() - self.r_leader()
  64. elif self.role == 'member':
  65. return self.r_member() - self.c_member()
  66. def r_leader(self, tasks: list['Task']) -> float:
  67. """
  68. 收益函数
  69. :param tasks: 在小组内的启用的同一任务列表
  70. :return:
  71. """
  72. result = 0
  73. for task in tasks:
  74. if task.role == 'leader':
  75. pass
  76. return result
  77. def c_leader(self) -> float:
  78. """
  79. 成本函数
  80. :return:
  81. """
  82. def r_member(self) -> float:
  83. """
  84. 收益函数
  85. :return:
  86. """
  87. def c_member(self) -> float:
  88. """
  89. 成本函数
  90. :return:
  91. """
  92. def t_diff(before: Task, after: Task) -> timedelta:
  93. """
  94. 计算两个任务之间的时间差
  95. :param before:
  96. :param after:
  97. :return: 单位是秒
  98. """
  99. return (after.next_run - before.next_run).total_seconds()
  100. if __name__ == '__main__':
  101. host = Task(next_run=datetime.fromisoformat('2023-09-13 18:46:23'),
  102. limit_time=time.fromisoformat('00:30:00'),
  103. target_run=datetime.fromisoformat('2023-09-13 18:46:23'),
  104. team_task=True, role='leader', limit_count=0)
  105. test1 = Task(next_run=datetime.fromisoformat('2023-09-13 18:44:23'),
  106. limit_time=time.fromisoformat('00:30:00'),
  107. target_run=datetime.fromisoformat('2023-09-13 18:46:23'),
  108. team_task=True, role='leader', limit_count=0)
  109. print(t_diff(host, test1))