userIdc2024 commited on
Commit
3189c1a
·
verified ·
1 Parent(s): bde447e

Update database/operations.py

Browse files
Files changed (1) hide show
  1. database/operations.py +91 -39
database/operations.py CHANGED
@@ -4,22 +4,23 @@ from typing import Optional, List, Dict, Any, Tuple
4
  from datetime import datetime
5
  from bson import ObjectId
6
  from pymongo import ASCENDING, DESCENDING
7
- from pymongo import MongoClient
8
-
9
-
10
- from database.connections import get_video_collection, get_script_collection, get_results_collection
11
 
 
 
 
 
 
 
12
 
13
  log = logging.getLogger(__name__)
14
 
15
-
16
  def _ensure_results_indexes(col):
17
  try:
18
  col.create_index([("created_at", DESCENDING)])
19
  col.create_index([("type", ASCENDING), ("created_at", DESCENDING)])
20
  col.create_index([("category", ASCENDING), ("created_at", DESCENDING)])
21
  col.create_index([("created_by", ASCENDING), ("created_at", DESCENDING)])
22
-
23
  except Exception:
24
  pass
25
 
@@ -31,7 +32,6 @@ def _ensure_video_indexes(col):
31
  except Exception:
32
  pass
33
 
34
-
35
  _rc = get_results_collection()
36
  if _rc is not None:
37
  _ensure_results_indexes(_rc)
@@ -44,9 +44,11 @@ _sg = get_script_collection()
44
  if _sg is not None:
45
  _ensure_video_indexes(_sg)
46
 
 
 
 
47
 
48
-
49
- # ---------- Image jobs (generation / variation) ----------
50
  def start_job(
51
  col,
52
  *,
@@ -70,14 +72,12 @@ def start_job(
70
  "created_by": created_by,
71
  "created_at": now,
72
  }
73
-
74
  if "file_name" in (inputs or {}):
75
  doc["file_name"] = inputs["file_name"]
76
 
77
  res = col.insert_one(doc)
78
  return str(res.inserted_id)
79
 
80
-
81
  def finish_job(
82
  col,
83
  job_id: str,
@@ -164,7 +164,85 @@ def find_video_analyses(
164
  out.append(d)
165
  return out
166
 
167
- # ------------------- Image/Text Generations -------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
  def find_generation_jobs(
169
  *,
170
  category: Optional[str] = None,
@@ -177,7 +255,6 @@ def find_generation_jobs(
177
  col = get_results_collection()
178
  if col is None:
179
  return [], 0
180
-
181
  q: Dict[str, Any] = {"type": "generation", "source": "text"}
182
  if category:
183
  q["category"] = category
@@ -188,7 +265,6 @@ def find_generation_jobs(
188
  if start_date: rng["$gte"] = start_date
189
  if end_date: rng["$lt"] = end_date
190
  q["created_at"] = rng
191
-
192
  total = col.count_documents(q)
193
  cur = (
194
  col.find(q)
@@ -196,7 +272,6 @@ def find_generation_jobs(
196
  .skip(page * page_size)
197
  .limit(page_size)
198
  )
199
-
200
  out: List[Dict[str, Any]] = []
201
  for d in cur:
202
  d["_id"] = str(d.get("_id"))
@@ -219,8 +294,7 @@ def insert_script_result(
219
  ) -> None:
220
  col = get_script_collection()
221
  if col is None:
222
- raise ValueError("Results collection is not available.")
223
-
224
  doc: Dict[str, Any] = {
225
  "type": "script",
226
  "source": "script_generator",
@@ -236,13 +310,11 @@ def insert_script_result(
236
  "num_scripts": num_scripts,
237
  "created_at": datetime.utcnow(),
238
  }
239
-
240
  try:
241
  col.insert_one(doc)
242
  except Exception as e:
243
  raise ValueError(f"Failed to insert script result: {e}")
244
 
245
-
246
  def find_script_results(
247
  *,
248
  start_date: Optional[datetime] = None,
@@ -255,20 +327,16 @@ def find_script_results(
255
  col = get_script_collection()
256
  if col is None:
257
  return [], 0
258
-
259
  q: Dict[str, Any] = {"type": "script", "source": "script_generator"}
260
  if start_date or end_date:
261
  rng: Dict[str, Any] = {}
262
  if start_date: rng["$gte"] = start_date
263
  if end_date: rng["$lt"] = end_date
264
  q["created_at"] = rng
265
-
266
  if created_by:
267
  q["created_by"] = created_by
268
-
269
  if video_name_query:
270
  q["video_name"] = {"$regex": video_name_query, "$options": "i"}
271
-
272
  total = col.count_documents(q)
273
  cur = (
274
  col.find(q)
@@ -276,25 +344,9 @@ def find_script_results(
276
  .skip(page * page_size)
277
  .limit(page_size)
278
  )
279
-
280
  out: List[Dict[str, Any]] = []
281
  for d in cur:
282
  d["_id"] = str(d.get("_id"))
283
  out.append(d)
284
  return out, int(total)
285
 
286
-
287
- def get_all_scripts(start_date: Optional[datetime] = None, end_date: Optional[datetime] = None, limit: int = 20) -> \
288
- List[Dict[str, Any]]:
289
- col = get_script_collection()
290
- if col is None:
291
- return []
292
- query = {"type": "generation", "source": "text"}
293
-
294
- if start_date:
295
- query["created_at"] = {"$gte": start_date}
296
- if end_date:
297
- query["created_at"]["$lt"] = end_date
298
-
299
- cursor = col.find(query).sort("created_at", DESCENDING).limit(limit)
300
- return list(cursor)
 
4
  from datetime import datetime
5
  from bson import ObjectId
6
  from pymongo import ASCENDING, DESCENDING
 
 
 
 
7
 
8
+ from database.connections import (
9
+ get_video_collection,
10
+ get_script_collection,
11
+ get_results_collection,
12
+ get_image_collection,
13
+ )
14
 
15
  log = logging.getLogger(__name__)
16
 
17
+ # ------------------- Index Setup -------------------
18
  def _ensure_results_indexes(col):
19
  try:
20
  col.create_index([("created_at", DESCENDING)])
21
  col.create_index([("type", ASCENDING), ("created_at", DESCENDING)])
22
  col.create_index([("category", ASCENDING), ("created_at", DESCENDING)])
23
  col.create_index([("created_by", ASCENDING), ("created_at", DESCENDING)])
 
24
  except Exception:
25
  pass
26
 
 
32
  except Exception:
33
  pass
34
 
 
35
  _rc = get_results_collection()
36
  if _rc is not None:
37
  _ensure_results_indexes(_rc)
 
44
  if _sg is not None:
45
  _ensure_video_indexes(_sg)
46
 
47
+ _ia = get_image_collection()
48
+ if _ia is not None:
49
+ _ensure_results_indexes(_ia)
50
 
51
+ # ------------------- Image Jobs -------------------
 
52
  def start_job(
53
  col,
54
  *,
 
72
  "created_by": created_by,
73
  "created_at": now,
74
  }
 
75
  if "file_name" in (inputs or {}):
76
  doc["file_name"] = inputs["file_name"]
77
 
78
  res = col.insert_one(doc)
79
  return str(res.inserted_id)
80
 
 
81
  def finish_job(
82
  col,
83
  job_id: str,
 
164
  out.append(d)
165
  return out
166
 
167
+
168
+ def insert_image_analysis(
169
+ *,
170
+ image_name: str,
171
+ response: Dict[str, Any],
172
+ category: Optional[str] = None,
173
+ created_by: Optional[str] = None,
174
+ analyzer_model: Optional[str] = None,
175
+ image_meta: Optional[Dict[str, Any]] = None,
176
+ thumbnail: str = "",
177
+ ) -> Optional[str]:
178
+ col = get_image_collection()
179
+ if col is None:
180
+ raise ValueError("Image collection not available")
181
+
182
+ doc: Dict[str, Any] = {
183
+ "type": "image_analysis",
184
+ "source": "image_analyzer",
185
+ "category": category or "general",
186
+ "image": {"name": image_name, **(image_meta or {})},
187
+ "analyzer_model": analyzer_model or "gpt-4o",
188
+ "results": response or {},
189
+ "created_by": created_by,
190
+ "created_at": datetime.utcnow(),
191
+ "thumbnail": thumbnail or "",
192
+ }
193
+
194
+ res = col.insert_one(doc)
195
+ return str(res.inserted_id)
196
+
197
+
198
+
199
+ def list_image_categories(created_by: Optional[str] = None) -> List[str]:
200
+ col = get_image_collection()
201
+ if col is None:
202
+ return []
203
+ try:
204
+ q = {"type": "image_analysis"}
205
+ if created_by:
206
+ q["created_by"] = created_by
207
+ vals = col.distinct("category", q)
208
+ return sorted({v for v in vals if v not in (None, "", [])})
209
+ except Exception:
210
+ return []
211
+
212
+
213
+ def find_image_analyses(
214
+ *,
215
+ category: Optional[str] = None,
216
+ start_date: Optional[datetime] = None,
217
+ end_date: Optional[datetime] = None,
218
+ limit: int = 200,
219
+ created_by: Optional[str] = None,
220
+ ) -> List[Dict[str, Any]]:
221
+ col = get_image_collection()
222
+ if col is None:
223
+ return []
224
+ q: Dict[str, Any] = {"type": "image_analysis"}
225
+ if category:
226
+ q["category"] = category
227
+ if created_by:
228
+ q["created_by"] = created_by
229
+ if start_date or end_date:
230
+ rng: Dict[str, Any] = {}
231
+ if start_date:
232
+ rng["$gte"] = start_date
233
+ if end_date:
234
+ rng["$lt"] = end_date
235
+ q["created_at"] = rng
236
+
237
+ cur = col.find(q).sort("created_at", DESCENDING).limit(max(1, int(limit)))
238
+ out: List[Dict[str, Any]] = []
239
+ for d in cur:
240
+ d["_id"] = str(d.get("_id"))
241
+ out.append(d)
242
+ return out
243
+
244
+
245
+ # ------------------- Generation Jobs -------------------
246
  def find_generation_jobs(
247
  *,
248
  category: Optional[str] = None,
 
255
  col = get_results_collection()
256
  if col is None:
257
  return [], 0
 
258
  q: Dict[str, Any] = {"type": "generation", "source": "text"}
259
  if category:
260
  q["category"] = category
 
265
  if start_date: rng["$gte"] = start_date
266
  if end_date: rng["$lt"] = end_date
267
  q["created_at"] = rng
 
268
  total = col.count_documents(q)
269
  cur = (
270
  col.find(q)
 
272
  .skip(page * page_size)
273
  .limit(page_size)
274
  )
 
275
  out: List[Dict[str, Any]] = []
276
  for d in cur:
277
  d["_id"] = str(d.get("_id"))
 
294
  ) -> None:
295
  col = get_script_collection()
296
  if col is None:
297
+ raise ValueError("Script collection not available.")
 
298
  doc: Dict[str, Any] = {
299
  "type": "script",
300
  "source": "script_generator",
 
310
  "num_scripts": num_scripts,
311
  "created_at": datetime.utcnow(),
312
  }
 
313
  try:
314
  col.insert_one(doc)
315
  except Exception as e:
316
  raise ValueError(f"Failed to insert script result: {e}")
317
 
 
318
  def find_script_results(
319
  *,
320
  start_date: Optional[datetime] = None,
 
327
  col = get_script_collection()
328
  if col is None:
329
  return [], 0
 
330
  q: Dict[str, Any] = {"type": "script", "source": "script_generator"}
331
  if start_date or end_date:
332
  rng: Dict[str, Any] = {}
333
  if start_date: rng["$gte"] = start_date
334
  if end_date: rng["$lt"] = end_date
335
  q["created_at"] = rng
 
336
  if created_by:
337
  q["created_by"] = created_by
 
338
  if video_name_query:
339
  q["video_name"] = {"$regex": video_name_query, "$options": "i"}
 
340
  total = col.count_documents(q)
341
  cur = (
342
  col.find(q)
 
344
  .skip(page * page_size)
345
  .limit(page_size)
346
  )
 
347
  out: List[Dict[str, Any]] = []
348
  for d in cur:
349
  d["_id"] = str(d.get("_id"))
350
  out.append(d)
351
  return out, int(total)
352