Skip to content

Commit d33c6f5

Browse files
authored
Merge pull request #1899 from MTG/sound-reindex-logging
Add progress indicator and time remaining to index process
2 parents f9afbc8 + 4743c16 commit d33c6f5

3 files changed

Lines changed: 27 additions & 19 deletions

File tree

freesound/logger.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
},
5959
'search': {
6060
'handlers': ['stdout'],
61-
'level': 'INFO',
61+
'level': 'ERROR',
6262
'propagate': False,
6363
},
6464
'file_upload': {

search/management/commands/post_dirty_sounds_to_search_engine.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
# See AUTHORS file.
1919
#
2020

21+
import datetime
2122
import logging
23+
import time
2224

2325
from sounds.models import Sound
2426
from utils.management_commands import LoggingBaseCommand
@@ -28,12 +30,23 @@
2830
console_logger = logging.getLogger("console")
2931

3032

33+
def time_stats(done, total, starttime):
34+
nowtime = time.monotonic()
35+
position = done*1.0 / total
36+
duration = round(nowtime - starttime)
37+
durdelta = datetime.timedelta(seconds=duration)
38+
remaining = round((duration / position) - duration)
39+
remdelta = datetime.timedelta(seconds=remaining)
40+
41+
return str(durdelta), str(remdelta)
42+
43+
3144
def send_sounds_to_search_engine(sounds_to_index_ids, slice_size=4000, delete_if_existing=False):
32-
num_sounds = len(sounds_to_index_ids)
33-
console_logger.info("Starting to post dirty sounds to solr. %i sounds to be added/updated to the search engine"
34-
% num_sounds)
45+
total_sounds = len(sounds_to_index_ids)
46+
console_logger.info(f"Starting to post dirty sounds to solr. {total_sounds} sounds to be added/updated to the search engine")
3547
n_sounds_indexed_correctly = 0
36-
for i in range(0, num_sounds, slice_size):
48+
starttime = time.monotonic()
49+
for i in range(0, total_sounds, slice_size):
3750
sound_ids_slice = sounds_to_index_ids[i:i + slice_size]
3851
if delete_if_existing:
3952
delete_sounds_from_search_engine(sound_ids_slice)
@@ -42,6 +55,8 @@ def send_sounds_to_search_engine(sounds_to_index_ids, slice_size=4000, delete_if
4255
if n_sounds_indexed > 0:
4356
Sound.objects.filter(pk__in=sound_ids_slice).update(is_index_dirty=False)
4457
n_sounds_indexed_correctly += n_sounds_indexed
58+
elapsed, remaining = time_stats(n_sounds_indexed_correctly, total_sounds, starttime)
59+
console_logger.info(f"Added {n_sounds_indexed_correctly}/{total_sounds} sounds. Elapsed: {elapsed}, Remaining: {remaining}")
4560

4661
return n_sounds_indexed_correctly
4762

utils/search/search_sounds.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,11 @@ def perform_search_engine_query(query_params):
7575
return results, paginator
7676

7777

78-
def add_sounds_to_search_engine(sound_objects, fields_to_include=[], update=False):
78+
def add_sounds_to_search_engine(sound_objects: list["sounds.models.Sound"]):
7979
"""Add the Sounds from the queryset to the search engine
8080
8181
Args:
82-
sound_objects (list[sounds.models.Sound]): list (or queryset) of Sound objects to index
83-
fields_to_include (list[str]): use this list to indicate the specific field names of the sounds
84-
that need to be included in the documents that will be indexed. If no fields are specified
85-
(fields_to_update=[]), then all available fields will be included.
86-
update (bool): if True, the sounds' data will be updated in the index, otherwise it will be
87-
replaced by the new generated documents. This is specially useful in combination with
88-
fields_to_include so that different fields of the indexed can be updated separately.
89-
82+
sound_objects: list (or queryset) of Sound objects to index
9083
Returns:
9184
int: number of sounds added to the index
9285
"""
@@ -95,13 +88,13 @@ def add_sounds_to_search_engine(sound_objects, fields_to_include=[], update=Fals
9588
else:
9689
num_sounds = len(sound_objects)
9790
try:
98-
console_logger.info("Adding %d sounds to the search engine" % num_sounds)
99-
search_logger.info("Adding %d sounds to the search engine" % num_sounds)
100-
get_search_engine().add_sounds_to_index(sound_objects, fields_to_include=fields_to_include, update=update)
91+
console_logger.debug(f"Adding {num_sounds} sounds to the search engine")
92+
search_logger.debug(f"Adding {num_sounds} sounds to the search engine")
93+
get_search_engine().add_sounds_to_index(sound_objects, fields_to_include=[], update=False)
10194
return num_sounds
10295
except SearchEngineException as e:
103-
console_logger.info(f"Failed to add sounds to search engine index: {str(e)}")
104-
search_logger.info(f"Failed to add sounds to search engine index: {str(e)}")
96+
console_logger.error(f"Failed to add sounds to search engine index: {str(e)}")
97+
search_logger.error(f"Failed to add sounds to search engine index: {str(e)}")
10598
return 0
10699

107100

0 commit comments

Comments
 (0)