Spaces:
Running
Running
Update database/operations.py
Browse files- database/operations.py +25 -59
database/operations.py
CHANGED
|
@@ -57,31 +57,6 @@ def start_job(
|
|
| 57 |
settings: Dict[str, Any],
|
| 58 |
user_prompt: Optional[str] = None,
|
| 59 |
) -> str:
|
| 60 |
-
"""
|
| 61 |
-
Variation schema (AI Library compatible):
|
| 62 |
-
{
|
| 63 |
-
type: "variation",
|
| 64 |
-
source: "variation",
|
| 65 |
-
created_by, category, file_name, prompt, lob,
|
| 66 |
-
status: "in_progress",
|
| 67 |
-
urls: [],
|
| 68 |
-
created_at,
|
| 69 |
-
settings, inputs
|
| 70 |
-
}
|
| 71 |
-
|
| 72 |
-
Generation schema:
|
| 73 |
-
{
|
| 74 |
-
type: "generation",
|
| 75 |
-
source: "text",
|
| 76 |
-
category, prompt,
|
| 77 |
-
status: "in_progress",
|
| 78 |
-
urls: [],
|
| 79 |
-
settings,
|
| 80 |
-
inputs,
|
| 81 |
-
created_by,
|
| 82 |
-
created_at,
|
| 83 |
-
}
|
| 84 |
-
"""
|
| 85 |
now = datetime.utcnow()
|
| 86 |
doc: Dict[str, Any] = {
|
| 87 |
"type": type,
|
|
@@ -111,9 +86,6 @@ def finish_job(
|
|
| 111 |
outputs_urls: Optional[List[str]] = None,
|
| 112 |
provider_update: Optional[Dict[str, Any]] = None,
|
| 113 |
) -> None:
|
| 114 |
-
"""
|
| 115 |
-
Finalizes a job. Writes top-level `urls` and `status`.
|
| 116 |
-
"""
|
| 117 |
set_fields: Dict[str, Any] = {"status": status}
|
| 118 |
if outputs_urls is not None:
|
| 119 |
set_fields["urls"] = list(dict.fromkeys(outputs_urls or []))
|
|
@@ -125,8 +97,7 @@ def finish_job(
|
|
| 125 |
except Exception as e:
|
| 126 |
log.error(f"finish_job update failed: {e}")
|
| 127 |
|
| 128 |
-
|
| 129 |
-
# ---------- Video analyses ----------
|
| 130 |
def insert_video_analysis(
|
| 131 |
*,
|
| 132 |
video_name: str,
|
|
@@ -152,30 +123,35 @@ def insert_video_analysis(
|
|
| 152 |
res = col.insert_one(doc)
|
| 153 |
return str(res.inserted_id)
|
| 154 |
|
| 155 |
-
|
| 156 |
-
def list_video_categories() -> List[str]:
|
| 157 |
col = get_video_collection()
|
| 158 |
if col is None:
|
| 159 |
return []
|
| 160 |
try:
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
| 162 |
return sorted({v for v in vals if v not in (None, "", [])})
|
| 163 |
except Exception:
|
| 164 |
return []
|
| 165 |
|
| 166 |
-
|
| 167 |
def find_video_analyses(
|
| 168 |
*,
|
| 169 |
category: Optional[str] = None,
|
| 170 |
start_date: Optional[datetime] = None,
|
| 171 |
end_date: Optional[datetime] = None,
|
| 172 |
limit: int = 200,
|
|
|
|
| 173 |
) -> List[Dict[str, Any]]:
|
| 174 |
col = get_video_collection()
|
| 175 |
if col is None:
|
| 176 |
return []
|
| 177 |
q: Dict[str, Any] = {}
|
| 178 |
-
if category:
|
|
|
|
|
|
|
|
|
|
| 179 |
if start_date or end_date:
|
| 180 |
rng: Dict[str, Any] = {}
|
| 181 |
if start_date: rng["$gte"] = start_date
|
|
@@ -188,8 +164,7 @@ def find_video_analyses(
|
|
| 188 |
out.append(d)
|
| 189 |
return out
|
| 190 |
|
| 191 |
-
|
| 192 |
-
|
| 193 |
def find_generation_jobs(
|
| 194 |
*,
|
| 195 |
category: Optional[str] = None,
|
|
@@ -197,6 +172,7 @@ def find_generation_jobs(
|
|
| 197 |
end_date: Optional[datetime] = None,
|
| 198 |
page: int = 0,
|
| 199 |
page_size: int = 20,
|
|
|
|
| 200 |
) -> Tuple[List[Dict[str, Any]], int]:
|
| 201 |
col = get_results_collection()
|
| 202 |
if col is None:
|
|
@@ -205,10 +181,12 @@ def find_generation_jobs(
|
|
| 205 |
q: Dict[str, Any] = {"type": "generation", "source": "text"}
|
| 206 |
if category:
|
| 207 |
q["category"] = category
|
|
|
|
|
|
|
| 208 |
if start_date or end_date:
|
| 209 |
rng: Dict[str, Any] = {}
|
| 210 |
if start_date: rng["$gte"] = start_date
|
| 211 |
-
if end_date: rng["$lt"]
|
| 212 |
q["created_at"] = rng
|
| 213 |
|
| 214 |
total = col.count_documents(q)
|
|
@@ -225,11 +203,7 @@ def find_generation_jobs(
|
|
| 225 |
out.append(d)
|
| 226 |
return out, int(total)
|
| 227 |
|
| 228 |
-
|
| 229 |
-
script_collection = get_script_collection()
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
def insert_script_result(
|
| 234 |
*,
|
| 235 |
video_name: str,
|
|
@@ -243,10 +217,6 @@ def insert_script_result(
|
|
| 243 |
num_scripts: Optional[int] = None,
|
| 244 |
category: Optional[str] = None,
|
| 245 |
) -> None:
|
| 246 |
-
"""
|
| 247 |
-
Save a script generation run into the results collection.
|
| 248 |
-
Stored with type/source so the library can query it.
|
| 249 |
-
"""
|
| 250 |
col = get_script_collection()
|
| 251 |
if col is None:
|
| 252 |
raise ValueError("Results collection is not available.")
|
|
@@ -286,13 +256,7 @@ def find_script_results(
|
|
| 286 |
if col is None:
|
| 287 |
return [], 0
|
| 288 |
|
| 289 |
-
q: Dict[str, Any] = {
|
| 290 |
-
"$or": [
|
| 291 |
-
{"type": "script", "source": "script_generator"},
|
| 292 |
-
{"type": {"$exists": False}, "response": {"$exists": True}},
|
| 293 |
-
]
|
| 294 |
-
}
|
| 295 |
-
|
| 296 |
if start_date or end_date:
|
| 297 |
rng: Dict[str, Any] = {}
|
| 298 |
if start_date: rng["$gte"] = start_date
|
|
@@ -306,10 +270,12 @@ def find_script_results(
|
|
| 306 |
q["video_name"] = {"$regex": video_name_query, "$options": "i"}
|
| 307 |
|
| 308 |
total = col.count_documents(q)
|
| 309 |
-
cur = (
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
|
|
|
|
|
|
| 313 |
|
| 314 |
out: List[Dict[str, Any]] = []
|
| 315 |
for d in cur:
|
|
@@ -331,4 +297,4 @@ List[Dict[str, Any]]:
|
|
| 331 |
query["created_at"]["$lt"] = end_date
|
| 332 |
|
| 333 |
cursor = col.find(query).sort("created_at", DESCENDING).limit(limit)
|
| 334 |
-
return list(cursor)
|
|
|
|
| 57 |
settings: Dict[str, Any],
|
| 58 |
user_prompt: Optional[str] = None,
|
| 59 |
) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
now = datetime.utcnow()
|
| 61 |
doc: Dict[str, Any] = {
|
| 62 |
"type": type,
|
|
|
|
| 86 |
outputs_urls: Optional[List[str]] = None,
|
| 87 |
provider_update: Optional[Dict[str, Any]] = None,
|
| 88 |
) -> None:
|
|
|
|
|
|
|
|
|
|
| 89 |
set_fields: Dict[str, Any] = {"status": status}
|
| 90 |
if outputs_urls is not None:
|
| 91 |
set_fields["urls"] = list(dict.fromkeys(outputs_urls or []))
|
|
|
|
| 97 |
except Exception as e:
|
| 98 |
log.error(f"finish_job update failed: {e}")
|
| 99 |
|
| 100 |
+
# ------------------- Video Analyses -------------------
|
|
|
|
| 101 |
def insert_video_analysis(
|
| 102 |
*,
|
| 103 |
video_name: str,
|
|
|
|
| 123 |
res = col.insert_one(doc)
|
| 124 |
return str(res.inserted_id)
|
| 125 |
|
| 126 |
+
def list_video_categories(created_by: Optional[str] = None) -> List[str]:
|
|
|
|
| 127 |
col = get_video_collection()
|
| 128 |
if col is None:
|
| 129 |
return []
|
| 130 |
try:
|
| 131 |
+
q = {}
|
| 132 |
+
if created_by:
|
| 133 |
+
q["created_by"] = created_by
|
| 134 |
+
vals = col.distinct("category", q)
|
| 135 |
return sorted({v for v in vals if v not in (None, "", [])})
|
| 136 |
except Exception:
|
| 137 |
return []
|
| 138 |
|
|
|
|
| 139 |
def find_video_analyses(
|
| 140 |
*,
|
| 141 |
category: Optional[str] = None,
|
| 142 |
start_date: Optional[datetime] = None,
|
| 143 |
end_date: Optional[datetime] = None,
|
| 144 |
limit: int = 200,
|
| 145 |
+
created_by: Optional[str] = None,
|
| 146 |
) -> List[Dict[str, Any]]:
|
| 147 |
col = get_video_collection()
|
| 148 |
if col is None:
|
| 149 |
return []
|
| 150 |
q: Dict[str, Any] = {}
|
| 151 |
+
if category:
|
| 152 |
+
q["category"] = category
|
| 153 |
+
if created_by:
|
| 154 |
+
q["created_by"] = created_by
|
| 155 |
if start_date or end_date:
|
| 156 |
rng: Dict[str, Any] = {}
|
| 157 |
if start_date: rng["$gte"] = start_date
|
|
|
|
| 164 |
out.append(d)
|
| 165 |
return out
|
| 166 |
|
| 167 |
+
# ------------------- Image/Text Generations -------------------
|
|
|
|
| 168 |
def find_generation_jobs(
|
| 169 |
*,
|
| 170 |
category: Optional[str] = None,
|
|
|
|
| 172 |
end_date: Optional[datetime] = None,
|
| 173 |
page: int = 0,
|
| 174 |
page_size: int = 20,
|
| 175 |
+
created_by: Optional[str] = None,
|
| 176 |
) -> Tuple[List[Dict[str, Any]], int]:
|
| 177 |
col = get_results_collection()
|
| 178 |
if col is None:
|
|
|
|
| 181 |
q: Dict[str, Any] = {"type": "generation", "source": "text"}
|
| 182 |
if category:
|
| 183 |
q["category"] = category
|
| 184 |
+
if created_by:
|
| 185 |
+
q["created_by"] = created_by
|
| 186 |
if start_date or end_date:
|
| 187 |
rng: Dict[str, Any] = {}
|
| 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)
|
|
|
|
| 203 |
out.append(d)
|
| 204 |
return out, int(total)
|
| 205 |
|
| 206 |
+
# ------------------- Script Results -------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
def insert_script_result(
|
| 208 |
*,
|
| 209 |
video_name: str,
|
|
|
|
| 217 |
num_scripts: Optional[int] = None,
|
| 218 |
category: Optional[str] = None,
|
| 219 |
) -> None:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
col = get_script_collection()
|
| 221 |
if col is None:
|
| 222 |
raise ValueError("Results collection is not available.")
|
|
|
|
| 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
|
|
|
|
| 270 |
q["video_name"] = {"$regex": video_name_query, "$options": "i"}
|
| 271 |
|
| 272 |
total = col.count_documents(q)
|
| 273 |
+
cur = (
|
| 274 |
+
col.find(q)
|
| 275 |
+
.sort("created_at", DESCENDING)
|
| 276 |
+
.skip(page * page_size)
|
| 277 |
+
.limit(page_size)
|
| 278 |
+
)
|
| 279 |
|
| 280 |
out: List[Dict[str, Any]] = []
|
| 281 |
for d in cur:
|
|
|
|
| 297 |
query["created_at"]["$lt"] = end_date
|
| 298 |
|
| 299 |
cursor = col.find(query).sort("created_at", DESCENDING).limit(limit)
|
| 300 |
+
return list(cursor)
|