-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcrud.py
236 lines (204 loc) · 6.43 KB
/
crud.py
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import json
from typing import Optional
from uuid import uuid4
from lnbits.db import Database, Filters, Page
from .logger import logger
from .models import CreateJobData, HeaderItems, Job, JobFilters, LogEntry
db = Database("ext_scheduler")
async def create_scheduler_jobs(admin_id: str, data: CreateJobData) -> Job:
# Convert headers to JSON string if present
headers_json = (
json.dumps([h.dict() for h in data.headers]) if data.headers else "[]"
)
# Convert extra to JSON string if present
extra_json = json.dumps(data.extra) if data.extra else "{}"
# Parse headers from JSON string to list of HeaderItems
headers_list = (
[HeaderItems(**h) for h in json.loads(headers_json)]
if headers_json != "[]"
else None
)
job = Job(
id=uuid4().hex,
name=data.name or f"Job-{uuid4().hex}",
admin=admin_id,
status=data.status,
schedule=data.schedule,
selectedverb=data.selectedverb,
url=data.url,
headers=headers_list, # Pass the parsed list of HeaderItems
body=data.body or "{}",
extra=(
json.loads(extra_json) if extra_json != "{}" else None
), # Parse extra as well
)
await db.execute(
"""
INSERT INTO scheduler.jobs (
id, name, admin, status, schedule, selectedverb, url, headers, body, extra
) VALUES (
:id, :name, :admin, :status, :schedule, :selectedverb,
:url, :headers, :body, :extra
)
""",
{
"id": job.id,
"name": job.name,
"admin": job.admin,
"status": job.status,
"schedule": job.schedule,
"selectedverb": job.selectedverb,
"url": job.url,
"headers": headers_json,
"body": job.body,
"extra": extra_json,
},
)
logger.info(f"Scheduler job created: {job.id}")
return job
async def get_scheduler_job(job_id: str) -> Optional[Job]:
row = await db.fetchone(
"SELECT * FROM scheduler.jobs WHERE id = :id",
{"id": job_id},
)
if not row:
return None
# Parse JSON strings back to Python objects
headers = [HeaderItems(**h) for h in json.loads(row.headers)] if row.headers else []
extra = json.loads(row.extra) if row.extra else {}
return Job(
id=row.id,
name=row.name,
admin=row.admin,
status=row.status,
schedule=row.schedule,
selectedverb=row.selectedverb,
url=row.url,
headers=headers,
body=row.body,
extra=extra,
)
async def get_scheduler_jobs(admin: str, filters: Filters[JobFilters]) -> Page[Job]:
rows = await db.fetch_page(
"SELECT * FROM scheduler.jobs",
["admin = :admin"],
{"admin": admin},
filters,
)
jobs = []
for row in rows.data:
# Parse JSON strings back to Python objects
headers = (
[HeaderItems(**h) for h in json.loads(row.headers)] if row.headers else []
)
extra = json.loads(row.extra) if row.extra else {}
jobs.append(
Job(
id=row.id,
name=row.name,
admin=row.admin,
status=row.status,
schedule=row.schedule,
selectedverb=row.selectedverb,
url=row.url,
headers=headers,
body=row.body,
extra=extra,
)
)
return Page(data=jobs, total=rows.total)
async def delete_scheduler_jobs(job_id: str) -> None:
await db.execute("DELETE FROM scheduler.jobs WHERE id = :id", {"id": job_id})
logger.info(f"Deleted scheduler job: {job_id}")
async def update_scheduler_job(job: Job) -> Job:
# Convert headers and extra to JSON strings
headers_json = (
json.dumps([h.dict() if hasattr(h, "dict") else h for h in job.headers])
if job.headers
else "[]"
)
extra_json = json.dumps(job.extra) if job.extra else "{}"
await db.execute(
"""
UPDATE scheduler.jobs
SET name = :name,
status = :status,
schedule = :schedule,
selectedverb = :selectedverb,
url = :url,
headers = :headers,
body = :body,
extra = :extra
WHERE id = :id
""",
{
"id": job.id,
"name": job.name,
"status": job.status,
"schedule": job.schedule,
"selectedverb": job.selectedverb,
"url": job.url,
"headers": headers_json,
"body": job.body or "{}",
"extra": extra_json,
},
)
# Return a new Job instance with the updated data
return Job(
id=job.id,
name=job.name,
admin=job.admin,
status=job.status,
schedule=job.schedule,
selectedverb=job.selectedverb,
url=job.url,
headers=(
[HeaderItems(**h) for h in json.loads(headers_json)]
if headers_json != "[]"
else None
),
body=job.body,
extra=json.loads(extra_json) if extra_json != "{}" else None,
)
async def create_log_entry(data: LogEntry) -> LogEntry:
"""
create log entry in database
"""
entry = LogEntry(
id=uuid4().hex,
job_id=data.job_id,
status=data.status,
response=data.response,
)
await db.insert("scheduler.logs", entry)
return entry
async def get_log_entry(log_id: str) -> LogEntry:
"""
get a single log entry based on primary key Unique ID
"""
return await db.fetchone(
"SELECT * FROM scheduler.logs WHERE id = :id",
{"id": log_id},
LogEntry,
)
async def get_log_entries(job_id: str) -> str:
"""
get all log entries from data base for particular job
"""
log_entries = await db.fetchall(
"SELECT * FROM scheduler.logs WHERE job_id = :id",
{"id": job_id},
LogEntry,
)
all_entries = ""
for entry in log_entries:
all_entries += (
f"[{entry.timestamp}]: JobID: {entry.job_id} "
f"Status: {entry.status} Response: {entry.response}\n\n"
)
return all_entries
async def delete_log_entries(job_id: str) -> None:
"""
delete all log entries from data base for particular job
"""
await db.execute("DELETE FROM scheduler.logs WHERE job_id = :id", {"id": job_id})