Skip to content

Commit d78cc77

Browse files
committed
add log
1 parent cff5e4b commit d78cc77

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

scripts/iib/db/datamodel.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,40 +279,65 @@ def find_by_substring(
279279

280280
@classmethod
281281
def get_random_images(cls, conn: Connection, size: int) -> List["Image"]:
282+
from scripts.iib.logger import logger
283+
logger.info(f"Starting to get random images, requested size: {size}")
282284
images = []
283285
max_cyc = 10
284286
curr_cyc = 0
285287
with closing(conn.cursor()) as cur:
286288
while len(images) < size and curr_cyc < max_cyc:
287289
curr_cyc += 1
290+
logger.info(f"Starting attempt {curr_cyc} to get random images")
288291
cur.execute("SELECT COUNT(*) FROM image")
289292
total_count = cur.fetchone()[0]
293+
logger.info(f"Total images in database: {total_count}")
290294

291295
if total_count == 0 or size <= 0:
296+
logger.warning(f"Cannot get random images: total_count={total_count}, requested_size={size}")
292297
return []
293298

294299
step = max(1, total_count // size)
300+
logger.info(f"Calculated step size: {step}")
295301

296302
start_indices = []
297303
for i in range(size):
298304
min_val = i * step
299305
max_val = min((i + 1) * step - 1, total_count - 1)
300-
# 确保 max_val 不小于 min_val
306+
# Ensure max_val is not less than min_val
301307
if max_val < min_val:
302308
max_val = min_val
303-
# 确保索引在有效范围内 (1 total_count)
309+
# Ensure indices are within valid range (1 to total_count)
304310
min_val = max(1, min(min_val, total_count))
305311
max_val = max(1, min(max_val, total_count))
306312
if min_val <= max_val:
307-
start_indices.append(random.randint(min_val, max_val))
313+
idx = random.randint(min_val, max_val)
314+
start_indices.append(idx)
315+
logger.debug(f"Generated random index [{i}]: range {min_val}-{max_val}, selected {idx}")
316+
317+
logger.info(f"Generated random index list: {start_indices}")
308318

309319
if start_indices:
310320
placeholders = ",".join("?" * len(start_indices))
311-
cur.execute(f"SELECT * FROM image WHERE id IN ({placeholders})", start_indices)
321+
query = f"SELECT * FROM image WHERE id IN ({placeholders})"
322+
logger.debug(f"Executing SQL query: {query}, parameters: {start_indices}")
323+
cur.execute(query, start_indices)
312324
rows = cur.fetchall()
313-
curr_images = [cls.from_row(row) for row in rows if os.path.exists(row[1])]
325+
logger.info(f"Query returned {len(rows)} records")
326+
327+
curr_images = []
328+
for row in rows:
329+
path = row[1]
330+
if os.path.exists(path):
331+
curr_images.append(cls.from_row(row))
332+
else:
333+
logger.warning(f"Image file does not exist: {path}")
334+
335+
logger.info(f"Valid images found in this cycle: {len(curr_images)}")
314336
images.extend(curr_images)
315337
images = unique_by(images, lambda x: x.path)
338+
logger.info(f"Total unique images after deduplication: {len(images)}")
339+
340+
logger.info(f"Random image retrieval completed, final image count: {len(images)}")
316341
return images
317342

318343

0 commit comments

Comments
 (0)