django-nativemojo 0.1.10__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (194) hide show
  1. django_nativemojo-0.1.10.dist-info/LICENSE +19 -0
  2. django_nativemojo-0.1.10.dist-info/METADATA +96 -0
  3. django_nativemojo-0.1.10.dist-info/NOTICE +8 -0
  4. django_nativemojo-0.1.10.dist-info/RECORD +194 -0
  5. django_nativemojo-0.1.10.dist-info/WHEEL +4 -0
  6. mojo/__init__.py +3 -0
  7. mojo/apps/account/__init__.py +1 -0
  8. mojo/apps/account/admin.py +91 -0
  9. mojo/apps/account/apps.py +16 -0
  10. mojo/apps/account/migrations/0001_initial.py +77 -0
  11. mojo/apps/account/migrations/0002_user_is_email_verified_user_is_phone_verified.py +23 -0
  12. mojo/apps/account/migrations/0003_group_mojo_secrets_user_mojo_secrets.py +23 -0
  13. mojo/apps/account/migrations/__init__.py +0 -0
  14. mojo/apps/account/models/__init__.py +3 -0
  15. mojo/apps/account/models/group.py +98 -0
  16. mojo/apps/account/models/member.py +95 -0
  17. mojo/apps/account/models/pkey.py +18 -0
  18. mojo/apps/account/models/user.py +211 -0
  19. mojo/apps/account/rest/__init__.py +3 -0
  20. mojo/apps/account/rest/group.py +25 -0
  21. mojo/apps/account/rest/user.py +47 -0
  22. mojo/apps/account/utils/__init__.py +0 -0
  23. mojo/apps/account/utils/jwtoken.py +72 -0
  24. mojo/apps/account/utils/passkeys.py +54 -0
  25. mojo/apps/fileman/README.md +549 -0
  26. mojo/apps/fileman/__init__.py +0 -0
  27. mojo/apps/fileman/apps.py +15 -0
  28. mojo/apps/fileman/backends/__init__.py +117 -0
  29. mojo/apps/fileman/backends/base.py +319 -0
  30. mojo/apps/fileman/backends/filesystem.py +397 -0
  31. mojo/apps/fileman/backends/s3.py +398 -0
  32. mojo/apps/fileman/examples/configurations.py +378 -0
  33. mojo/apps/fileman/examples/usage_example.py +665 -0
  34. mojo/apps/fileman/management/__init__.py +1 -0
  35. mojo/apps/fileman/management/commands/__init__.py +1 -0
  36. mojo/apps/fileman/management/commands/cleanup_expired_uploads.py +222 -0
  37. mojo/apps/fileman/models/__init__.py +7 -0
  38. mojo/apps/fileman/models/file.py +292 -0
  39. mojo/apps/fileman/models/manager.py +227 -0
  40. mojo/apps/fileman/models/render.py +0 -0
  41. mojo/apps/fileman/rest/__init__ +0 -0
  42. mojo/apps/fileman/rest/__init__.py +23 -0
  43. mojo/apps/fileman/rest/fileman.py +13 -0
  44. mojo/apps/fileman/rest/upload.py +92 -0
  45. mojo/apps/fileman/utils/__init__.py +19 -0
  46. mojo/apps/fileman/utils/upload.py +616 -0
  47. mojo/apps/incident/__init__.py +1 -0
  48. mojo/apps/incident/handlers/__init__.py +3 -0
  49. mojo/apps/incident/handlers/event_handlers.py +142 -0
  50. mojo/apps/incident/migrations/0001_initial.py +83 -0
  51. mojo/apps/incident/migrations/0002_rename_bundle_ruleset_bundle_minutes_event_hostname_and_more.py +44 -0
  52. mojo/apps/incident/migrations/0003_alter_event_model_id.py +18 -0
  53. mojo/apps/incident/migrations/0004_alter_incident_model_id.py +18 -0
  54. mojo/apps/incident/migrations/__init__.py +0 -0
  55. mojo/apps/incident/models/__init__.py +3 -0
  56. mojo/apps/incident/models/event.py +135 -0
  57. mojo/apps/incident/models/incident.py +33 -0
  58. mojo/apps/incident/models/rule.py +247 -0
  59. mojo/apps/incident/parsers/__init__.py +0 -0
  60. mojo/apps/incident/parsers/ossec/__init__.py +1 -0
  61. mojo/apps/incident/parsers/ossec/core.py +82 -0
  62. mojo/apps/incident/parsers/ossec/parsed.py +23 -0
  63. mojo/apps/incident/parsers/ossec/rules.py +124 -0
  64. mojo/apps/incident/parsers/ossec/utils.py +169 -0
  65. mojo/apps/incident/reporter.py +42 -0
  66. mojo/apps/incident/rest/__init__.py +2 -0
  67. mojo/apps/incident/rest/event.py +23 -0
  68. mojo/apps/incident/rest/ossec.py +22 -0
  69. mojo/apps/logit/__init__.py +0 -0
  70. mojo/apps/logit/admin.py +37 -0
  71. mojo/apps/logit/migrations/0001_initial.py +32 -0
  72. mojo/apps/logit/migrations/0002_log_duid_log_payload_log_username.py +28 -0
  73. mojo/apps/logit/migrations/0003_log_level.py +18 -0
  74. mojo/apps/logit/migrations/__init__.py +0 -0
  75. mojo/apps/logit/models/__init__.py +1 -0
  76. mojo/apps/logit/models/log.py +57 -0
  77. mojo/apps/logit/rest.py +9 -0
  78. mojo/apps/metrics/README.md +79 -0
  79. mojo/apps/metrics/__init__.py +12 -0
  80. mojo/apps/metrics/redis_metrics.py +331 -0
  81. mojo/apps/metrics/rest/__init__.py +1 -0
  82. mojo/apps/metrics/rest/base.py +152 -0
  83. mojo/apps/metrics/rest/db.py +0 -0
  84. mojo/apps/metrics/utils.py +227 -0
  85. mojo/apps/notify/README.md +91 -0
  86. mojo/apps/notify/README_NOTIFICATIONS.md +566 -0
  87. mojo/apps/notify/__init__.py +0 -0
  88. mojo/apps/notify/admin.py +52 -0
  89. mojo/apps/notify/handlers/__init__.py +0 -0
  90. mojo/apps/notify/handlers/example_handlers.py +516 -0
  91. mojo/apps/notify/handlers/ses/__init__.py +25 -0
  92. mojo/apps/notify/handlers/ses/bounce.py +0 -0
  93. mojo/apps/notify/handlers/ses/complaint.py +25 -0
  94. mojo/apps/notify/handlers/ses/message.py +86 -0
  95. mojo/apps/notify/management/__init__.py +0 -0
  96. mojo/apps/notify/management/commands/__init__.py +1 -0
  97. mojo/apps/notify/management/commands/process_notifications.py +370 -0
  98. mojo/apps/notify/mod +0 -0
  99. mojo/apps/notify/models/__init__.py +12 -0
  100. mojo/apps/notify/models/account.py +128 -0
  101. mojo/apps/notify/models/attachment.py +24 -0
  102. mojo/apps/notify/models/bounce.py +68 -0
  103. mojo/apps/notify/models/complaint.py +40 -0
  104. mojo/apps/notify/models/inbox.py +113 -0
  105. mojo/apps/notify/models/inbox_message.py +173 -0
  106. mojo/apps/notify/models/outbox.py +129 -0
  107. mojo/apps/notify/models/outbox_message.py +288 -0
  108. mojo/apps/notify/models/template.py +30 -0
  109. mojo/apps/notify/providers/__init__.py +0 -0
  110. mojo/apps/notify/providers/aws.py +73 -0
  111. mojo/apps/notify/rest/__init__.py +0 -0
  112. mojo/apps/notify/rest/ses.py +0 -0
  113. mojo/apps/notify/utils/__init__.py +2 -0
  114. mojo/apps/notify/utils/notifications.py +404 -0
  115. mojo/apps/notify/utils/parsing.py +202 -0
  116. mojo/apps/notify/utils/render.py +144 -0
  117. mojo/apps/tasks/README.md +118 -0
  118. mojo/apps/tasks/__init__.py +11 -0
  119. mojo/apps/tasks/manager.py +489 -0
  120. mojo/apps/tasks/rest/__init__.py +2 -0
  121. mojo/apps/tasks/rest/hooks.py +0 -0
  122. mojo/apps/tasks/rest/tasks.py +62 -0
  123. mojo/apps/tasks/runner.py +174 -0
  124. mojo/apps/tasks/tq_handlers.py +14 -0
  125. mojo/decorators/__init__.py +3 -0
  126. mojo/decorators/auth.py +25 -0
  127. mojo/decorators/cron.py +31 -0
  128. mojo/decorators/http.py +132 -0
  129. mojo/decorators/validate.py +14 -0
  130. mojo/errors.py +88 -0
  131. mojo/helpers/__init__.py +0 -0
  132. mojo/helpers/aws/__init__.py +0 -0
  133. mojo/helpers/aws/client.py +8 -0
  134. mojo/helpers/aws/s3.py +268 -0
  135. mojo/helpers/aws/setup_email.py +0 -0
  136. mojo/helpers/cron.py +79 -0
  137. mojo/helpers/crypto/__init__.py +4 -0
  138. mojo/helpers/crypto/aes.py +60 -0
  139. mojo/helpers/crypto/hash.py +59 -0
  140. mojo/helpers/crypto/privpub/__init__.py +1 -0
  141. mojo/helpers/crypto/privpub/hybrid.py +97 -0
  142. mojo/helpers/crypto/privpub/rsa.py +104 -0
  143. mojo/helpers/crypto/sign.py +36 -0
  144. mojo/helpers/crypto/too.l.py +25 -0
  145. mojo/helpers/crypto/utils.py +26 -0
  146. mojo/helpers/daemon.py +94 -0
  147. mojo/helpers/dates.py +69 -0
  148. mojo/helpers/dns/__init__.py +0 -0
  149. mojo/helpers/dns/godaddy.py +62 -0
  150. mojo/helpers/filetypes.py +128 -0
  151. mojo/helpers/logit.py +310 -0
  152. mojo/helpers/modules.py +95 -0
  153. mojo/helpers/paths.py +63 -0
  154. mojo/helpers/redis.py +10 -0
  155. mojo/helpers/request.py +89 -0
  156. mojo/helpers/request_parser.py +269 -0
  157. mojo/helpers/response.py +14 -0
  158. mojo/helpers/settings.py +146 -0
  159. mojo/helpers/sysinfo.py +140 -0
  160. mojo/helpers/ua.py +0 -0
  161. mojo/middleware/__init__.py +0 -0
  162. mojo/middleware/auth.py +26 -0
  163. mojo/middleware/logging.py +55 -0
  164. mojo/middleware/mojo.py +21 -0
  165. mojo/migrations/0001_initial.py +32 -0
  166. mojo/migrations/__init__.py +0 -0
  167. mojo/models/__init__.py +2 -0
  168. mojo/models/meta.py +262 -0
  169. mojo/models/rest.py +538 -0
  170. mojo/models/secrets.py +59 -0
  171. mojo/rest/__init__.py +1 -0
  172. mojo/rest/info.py +26 -0
  173. mojo/serializers/__init__.py +0 -0
  174. mojo/serializers/models.py +165 -0
  175. mojo/serializers/openapi.py +188 -0
  176. mojo/urls.py +38 -0
  177. mojo/ws4redis/README.md +174 -0
  178. mojo/ws4redis/__init__.py +2 -0
  179. mojo/ws4redis/client.py +283 -0
  180. mojo/ws4redis/connection.py +327 -0
  181. mojo/ws4redis/exceptions.py +32 -0
  182. mojo/ws4redis/redis.py +183 -0
  183. mojo/ws4redis/servers/__init__.py +0 -0
  184. mojo/ws4redis/servers/base.py +86 -0
  185. mojo/ws4redis/servers/django.py +171 -0
  186. mojo/ws4redis/servers/uwsgi.py +63 -0
  187. mojo/ws4redis/settings.py +45 -0
  188. mojo/ws4redis/utf8validator.py +128 -0
  189. mojo/ws4redis/websocket.py +403 -0
  190. testit/__init__.py +0 -0
  191. testit/client.py +147 -0
  192. testit/faker.py +20 -0
  193. testit/helpers.py +198 -0
  194. testit/runner.py +262 -0
@@ -0,0 +1,489 @@
1
+ from venv import create
2
+ from mojo.helpers import redis
3
+ from objict import objict
4
+ import uuid
5
+ import time
6
+
7
+
8
+ class TaskManager:
9
+ def __init__(self, channels, prefix="taskit"):
10
+ """
11
+ Initialize the TaskManager with Redis connection, channels, and a key prefix.
12
+
13
+ :param channels: List of channels for task management.
14
+ :param prefix: Prefix for Redis keys. Default is "taskit".
15
+ """
16
+ self.redis = redis.get_connection()
17
+ self.channels = channels
18
+ self.prefix = prefix
19
+
20
+ def take_out_the_dead(self):
21
+ for channel in self.channels:
22
+ # this will remove all expired tasks by default
23
+ self.get_pending()
24
+ self.get_running()
25
+
26
+ def get_completed_key(self, channel):
27
+ """
28
+ Get the Redis key for completed tasks in a channel.
29
+
30
+ :param channel: Channel name.
31
+ :return: Redis key for completed tasks.
32
+ """
33
+ return f"{self.prefix}:d:{channel}"
34
+
35
+ def get_pending_key(self, channel):
36
+ """
37
+ Get the Redis key for pending tasks in a channel.
38
+
39
+ :param channel: Channel name.
40
+ :return: Redis key for pending tasks.
41
+ """
42
+ return f"{self.prefix}:p:{channel}"
43
+
44
+ def get_error_key(self, channel):
45
+ """
46
+ Get the Redis key for tasks with errors in a channel.
47
+
48
+ :param channel: Channel name.
49
+ :return: Redis key for tasks with errors.
50
+ """
51
+ return f"{self.prefix}:e:{channel}"
52
+
53
+ def get_running_key(self, channel):
54
+ """
55
+ Get the Redis key for running tasks in a channel.
56
+
57
+ :param channel: Channel name.
58
+ :return: Redis key for running tasks.
59
+ """
60
+ return f"{self.prefix}:r:{channel}"
61
+
62
+ def get_task_key(self, task_id):
63
+ """
64
+ Get the Redis key for a specific task.
65
+
66
+ :param task_id: Task ID.
67
+ :return: Redis key for the task.
68
+ """
69
+ return f"{self.prefix}:t:{task_id}"
70
+
71
+ def get_channel_key(self, channel):
72
+ """
73
+ Get the Redis key for a channel.
74
+
75
+ :param channel: Channel name.
76
+ :return: Redis key for the channel.
77
+ """
78
+ return f"{self.prefix}:c:{channel}"
79
+
80
+ def get_task(self, task_id):
81
+ """
82
+ Retrieve a task from Redis using its task ID.
83
+
84
+ :param task_id: Task ID.
85
+ :return: Task data as an objict, or None if not found.
86
+ """
87
+ task_data_raw = self.redis.get(self.get_task_key(task_id))
88
+ return objict.from_json(task_data_raw, ignore_errors=True)
89
+
90
+ def save_task(self, task_data, expires=1800):
91
+ """
92
+ Save a task to Redis with an expiration time.
93
+
94
+ :param task_data: Task data as an objict.
95
+ :param expires: Expiration time in seconds. Default is 1800.
96
+ """
97
+ self.redis.set(self.get_task_key(task_data.id), task_data.to_json(as_string=True), ex=expires)
98
+
99
+ def get_key_expiration(self, task_id):
100
+ """
101
+ Get the expiration time of a task in Redis.
102
+
103
+ :param task_id: Task ID.
104
+ :return: Time to live for the task key in seconds, or None if the key does not exist.
105
+ """
106
+ ttl = self.redis.ttl(self.get_task_key(task_id))
107
+ return ttl if ttl != -2 else None
108
+
109
+ def add_to_pending(self, task_id, channel="default"):
110
+ """
111
+ Add a task ID to the pending set in Redis for a channel.
112
+
113
+ :param task_id: Task ID.
114
+ :param channel: Channel name. Default is "default".
115
+ :return: True if operation is successful.
116
+ """
117
+ self.redis.sadd(self.get_pending_key(channel), task_id)
118
+ return True
119
+
120
+ def add_to_running(self, task_id, channel="default"):
121
+ """
122
+ Add a task ID to the running set in Redis for a channel.
123
+
124
+ :param task_id: Task ID.
125
+ :param channel: Channel name. Default is "default".
126
+ :return: True if operation is successful.
127
+ """
128
+ self.redis.sadd(self.get_running_key(channel), task_id)
129
+ return True
130
+
131
+ def add_to_errors(self, task_data, error_message):
132
+ """
133
+ Add a task to the error set in Redis with an error message.
134
+
135
+ :param task_data: Task data as an objict.
136
+ :param error_message: Error message string.
137
+ :return: True if operation is successful.
138
+ """
139
+ task_data.status = "error"
140
+ task_data.error = error_message
141
+ self.save_task(task_data, expires=86400)
142
+ self.redis.sadd(self.get_error_key(task_data.channel), task_data.id)
143
+ return True
144
+
145
+ def add_to_completed(self, task_data):
146
+ """
147
+ Add a task to the completed set in Redis.
148
+
149
+ :param task_data: Task data as an objict.
150
+ :return: True if operation is successful.
151
+ """
152
+ task_data.status = "completed"
153
+ # save completed tasks for 24 hours
154
+ self.save_task(task_data, expires=86400)
155
+ self.redis.sadd(self.get_completed_key(task_data.channel), task_data.id)
156
+ return True
157
+
158
+ def remove_from_running(self, task_id, channel="default"):
159
+ """
160
+ Remove a task ID from the running set in Redis for a channel.
161
+
162
+ :param task_id: Task ID.
163
+ :param channel: Channel name. Default is "default".
164
+ :return: True if operation is successful.
165
+ """
166
+ self.redis.srem(self.get_running_key(channel), task_id)
167
+ return True
168
+
169
+ def remove_from_pending(self, task_id, channel="default"):
170
+ """
171
+ Remove a task ID from the pending set in Redis for a channel.
172
+
173
+ :param task_id: Task ID.
174
+ :param channel: Channel name. Default is "default".
175
+ :return: True if operation is successful.
176
+ """
177
+ self.redis.srem(self.get_pending_key(channel), task_id)
178
+ return True
179
+
180
+ def remove_from_completed(self, task_id, channel="default"):
181
+ """
182
+ Remove a task ID from the completed set in Redis for a channel.
183
+
184
+ :param task_id: Task ID.
185
+ :param channel: Channel name. Default is "default".
186
+ :return: True if operation is successful.
187
+ """
188
+ self.redis.srem(self.get_completed_key(channel), task_id)
189
+ return True
190
+
191
+ def remove_from_errors(self, task_id, channel="default"):
192
+ """
193
+ Remove a task ID from the error set in Redis for a channel.
194
+
195
+ :param task_id: Task ID.
196
+ :param channel: Channel name. Default is "default".
197
+ :return: True if operation is successful.
198
+ """
199
+ self.redis.srem(self.get_error_key(channel), task_id)
200
+ return True
201
+
202
+ def remove_task(self, task_id):
203
+ """
204
+ Remove a task from all sets and delete it from Redis.
205
+
206
+ :param task_id: Task ID.
207
+ :return: True if task was found and removed, otherwise False.
208
+ """
209
+ task_data = self.get_task(task_id)
210
+ if task_data:
211
+ self.redis.srem(self.get_running_key(task_data.channel), task_data.id)
212
+ self.redis.srem(self.get_pending_key(task_data.channel), task_data.id)
213
+ self.redis.delete(self.get_task_key(task_id))
214
+ return True
215
+ return False
216
+
217
+ def cancel_task(self, task_id):
218
+ """
219
+ Cancel a task by removing it from running and pending sets and deleting it.
220
+
221
+ :param task_id: Task ID.
222
+ """
223
+ task_data_raw = self.redis.get(self.get_task_key(task_id))
224
+ task_data = objict.from_json(task_data_raw, ignore_errors=True)
225
+ if task_data:
226
+ self.redis.srem(self.get_running_key(task_data.channel), task_data.id)
227
+ self.redis.srem(self.get_pending_key(task_data.channel), task_data.id)
228
+ self.redis.delete(self.get_task_key(task_id))
229
+
230
+ def get_running_ids(self, channel="default"):
231
+ """
232
+ Get all running task IDs from Redis for a channel.
233
+
234
+ :param channel: Channel name. Default is "default".
235
+ :return: List of running task IDs.
236
+ """
237
+ return [task_id.decode('utf-8') for task_id in self.redis.smembers(self.get_running_key(channel))]
238
+
239
+ def get_pending_ids(self, channel="default"):
240
+ """
241
+ Get all pending task IDs from Redis for a channel.
242
+
243
+ :param channel: Channel name. Default is "default".
244
+ :return: List of pending task IDs.
245
+ """
246
+ return [task_id.decode('utf-8') for task_id in self.redis.smembers(self.get_pending_key(channel))]
247
+
248
+ def get_completed_ids(self, channel="default"):
249
+ """
250
+ Get all pending task IDs from Redis for a channel.
251
+
252
+ :param channel: Channel name. Default is "default".
253
+ :return: List of pending task IDs.
254
+ """
255
+ return [task_id.decode('utf-8') for task_id in self.redis.smembers(self.get_completed_key(channel))]
256
+
257
+ def get_error_ids(self, channel="default"):
258
+ """
259
+ Get all error task IDs from Redis for a channel.
260
+
261
+ :param channel: Channel name. Default is "default".
262
+ :return: List of error task IDs.
263
+ """
264
+ return [task_id.decode('utf-8') for task_id in self.redis.smembers(self.get_error_key(channel))]
265
+
266
+ def get_pending(self, channel="default"):
267
+ """
268
+ Get all pending tasks as objicts for a channel.
269
+
270
+ :param channel: Channel name. Default is "default".
271
+ :return: List of pending task objicts.
272
+ """
273
+ pending_tasks = []
274
+ for task_id in self.get_pending_ids(channel):
275
+ task = self.get_task(task_id)
276
+ if task:
277
+ pending_tasks.append(task)
278
+ else:
279
+ self.remove_from_pending(task_id, channel)
280
+ return pending_tasks
281
+
282
+ def get_running(self, channel="default"):
283
+ """
284
+ Get all running tasks as objicts for a channel.
285
+
286
+ :param channel: Channel name. Default is "default".
287
+ :return: List of running task objicts.
288
+ """
289
+ running_tasks = []
290
+ for task_id in self.get_running_ids(channel):
291
+ task = self.get_task(task_id)
292
+ if task:
293
+ running_tasks.append(task)
294
+ else:
295
+ self.remove_from_running(task_id, channel)
296
+ return running_tasks
297
+
298
+ def get_completed(self, channel="default"):
299
+ """
300
+ Get all completed tasks as objicts for a channel.
301
+
302
+ :param channel: Channel name. Default is "default".
303
+ :return: List of completed task objicts.
304
+ """
305
+ completed_tasks = []
306
+ for task_id in self.get_completed_ids(channel):
307
+ task = self.get_task(task_id)
308
+ if task:
309
+ completed_tasks.append(task)
310
+ else:
311
+ self.remove_from_completed(task_id, channel)
312
+ return completed_tasks
313
+
314
+ def get_errors(self, channel="default"):
315
+ """
316
+ Get all error tasks as objicts for a channel.
317
+
318
+ :param channel: Channel name. Default is "default".
319
+ :return: List of error task objicts.
320
+ """
321
+ error_tasks = []
322
+ for task_id in self.get_error_ids(channel):
323
+ task = self.get_task(task_id)
324
+ if task:
325
+ error_tasks.append(task)
326
+ else:
327
+ self.remove_from_errors(task_id, channel)
328
+ return error_tasks
329
+
330
+ def publish(self, function, data, channel="default", expires=1800):
331
+ """
332
+ Publish a new task to a channel, save it, and add to pending tasks.
333
+
334
+ :param function: Function associated with the task.
335
+ :param data: Data to be processed by the task.
336
+ :param channel: Channel name. Default is "default".
337
+ :param expires: Expiration time in seconds. Default is 1800.
338
+ :return: Task ID of the published task.
339
+ """
340
+ task_data = objict(channel=channel, function=function, data=objict.from_dict(data))
341
+ task_data.id = str(uuid.uuid4()).replace('-', '')
342
+ task_data.created = time.time()
343
+ task_data.expires = time.time() + expires
344
+ task_data.status = "pending"
345
+ self.add_to_pending(task_data.id, channel)
346
+ self.save_task(task_data, expires)
347
+ self.redis.publish(self.get_channel_key(channel), task_data.id)
348
+ # metrics.record("taskit:p:global", category="taskit")
349
+ # metrics.record(f"taskit:p:{channel}", category="taskit_channels")
350
+ return task_data.id
351
+
352
+ def get_all_pending_ids(self):
353
+ """
354
+ Get all pending task IDs across all channels.
355
+
356
+ :return: List of all pending task IDs.
357
+ """
358
+ pending_ids = []
359
+ for channel in self.channels:
360
+ pending_ids.extend(self.get_pending_ids(channel))
361
+ return pending_ids
362
+
363
+ def get_all_running_ids(self):
364
+ """
365
+ Get all running task IDs across all channels.
366
+
367
+ :return: List of all running task IDs.
368
+ """
369
+ running_ids = []
370
+ for channel in self.channels:
371
+ running_ids.extend(self.get_running_ids(channel))
372
+ return running_ids
373
+
374
+ def get_all_completed_ids(self):
375
+ """
376
+ Get all completed task IDs across all channels.
377
+
378
+ :return: List of all completed task IDs.
379
+ """
380
+ completed_ids = []
381
+ for channel in self.channels:
382
+ completed_ids.extend(self.get_completed_ids(channel))
383
+ return completed_ids
384
+
385
+ def get_all_error_ids(self):
386
+ """
387
+ Get all error task IDs across all channels.
388
+
389
+ :return: List of all error task IDs.
390
+ """
391
+ error_ids = []
392
+ for channel in self.channels:
393
+ error_ids.extend(self.get_error_ids(channel))
394
+ return error_ids
395
+
396
+ def get_all_pending(self, include_data=False):
397
+ """
398
+ Get all pending tasks as objects.
399
+
400
+ :return: List of pending task objects.
401
+ """
402
+ pending_tasks = []
403
+ for task_id in self.get_all_pending_ids():
404
+ task = self.get_task(task_id)
405
+ if task:
406
+ if not include_data:
407
+ del task.data
408
+ pending_tasks.append(task)
409
+ else:
410
+ self.remove_from_pending(task_id)
411
+ return pending_tasks
412
+
413
+ def get_all_running(self, include_data=False):
414
+ """
415
+ Get all running tasks as objects.
416
+
417
+ :return: List of running task objects.
418
+ """
419
+ running_tasks = []
420
+ for task_id in self.get_all_running_ids():
421
+ task = self.get_task(task_id)
422
+ if task:
423
+ if not include_data:
424
+ del task.data
425
+ running_tasks.append(task)
426
+ else:
427
+ self.remove_from_running(task_id)
428
+ return running_tasks
429
+
430
+ def get_all_completed(self, include_data=False):
431
+ """
432
+ Get all completed tasks as objects.
433
+
434
+ :return: List of completed task objects.
435
+ """
436
+ completed_tasks = []
437
+ for task_id in self.get_all_completed_ids():
438
+ task = self.get_task(task_id)
439
+ if task:
440
+ if not include_data:
441
+ del task.data
442
+ completed_tasks.append(task)
443
+ else:
444
+ self.remove_from_completed(task_id)
445
+ # Sort tasks by the created timestamp in descending order
446
+ completed_tasks.sort(key=lambda x: x.created, reverse=True)
447
+ return completed_tasks
448
+
449
+ def get_all_errors(self):
450
+ """
451
+ Get all error tasks as objects.
452
+
453
+ :return: List of error task objects.
454
+ """
455
+ error_tasks = []
456
+ for task_id in self.get_all_error_ids():
457
+ task = self.get_task(task_id)
458
+ if task:
459
+ error_tasks.append(task)
460
+ else:
461
+ self.remove_from_errors(task_id)
462
+ # Sort tasks by the created timestamp in descending order
463
+ error_tasks.sort(key=lambda x: x.created, reverse=True)
464
+ return error_tasks
465
+
466
+ def get_status(self, simple=False):
467
+ """
468
+ Get the status of tasks across all channels, including pending and running tasks.
469
+
470
+ :return: Status object containing counts of pending and running tasks per channel.
471
+ """
472
+ status = objict(pending=0, running=0, completed=0, errors=0, channels=objict())
473
+ for channel in self.channels:
474
+ pending = self.get_pending_ids(channel)
475
+ running = self.get_running_ids(channel)
476
+ completed = self.get_completed_ids(channel)
477
+ errors = self.get_error_ids(channel)
478
+ status.pending += len(pending)
479
+ status.running += len(running)
480
+ status.completed += len(completed)
481
+ status.errors += len(errors)
482
+ if not simple:
483
+ cstats = objict()
484
+ cstats.pending = len(pending)
485
+ cstats.running = len(running)
486
+ cstats.completed = len(completed)
487
+ cstats.errors = len(errors)
488
+ status.channels[channel] = cstats
489
+ return status
@@ -0,0 +1,2 @@
1
+ from .tasks import *
2
+ from .hooks import *
File without changes
@@ -0,0 +1,62 @@
1
+ from mojo import decorators as md
2
+ from mojo.helpers.response import JsonResponse
3
+ # from django.http import JsonResponse
4
+ import mojo.tasks
5
+
6
+ @md.URL('status')
7
+ def api_status(request):
8
+ tman = mojo.tasks.get_manager()
9
+ return JsonResponse(tman.get_status())
10
+
11
+ @md.URL('pending')
12
+ def api_pending(request):
13
+ tman = mojo.tasks.get_manager()
14
+ pending = tman.get_all_pending()
15
+ size = len(pending)
16
+ response = {
17
+ 'count': size,
18
+ 'page': 0,
19
+ 'size': size,
20
+ 'data': pending
21
+ }
22
+ return JsonResponse(response)
23
+
24
+ @md.URL('completed')
25
+ def api_completed(request):
26
+ tman = mojo.tasks.get_manager()
27
+ completed = tman.get_all_completed()
28
+ size = len(completed)
29
+ response = {
30
+ 'count': size,
31
+ 'page': 0,
32
+ 'size': size,
33
+ 'data': completed
34
+ }
35
+ return JsonResponse(response)
36
+
37
+ @md.URL('running')
38
+ def api_running(request):
39
+ tman = mojo.tasks.get_manager()
40
+ running = tman.get_all_running()
41
+ size = len(running)
42
+ response = {
43
+ 'count': size,
44
+ 'page': 0,
45
+ 'size': size,
46
+ 'data': running
47
+ }
48
+ return JsonResponse(response)
49
+
50
+
51
+ @md.URL('errors')
52
+ def api_errors(request):
53
+ tman = mojo.tasks.get_manager()
54
+ errors = tman.get_all_errors()
55
+ size = len(errors)
56
+ response = {
57
+ 'count': size,
58
+ 'page': 0,
59
+ 'size': size,
60
+ 'data': errors
61
+ }
62
+ return JsonResponse(response)