diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2023-11-13 17:35:16 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2023-11-13 18:07:57 +0100 |
commit | 1d1fd6c794c8ca8e758fc416b43e0f881136057f (patch) | |
tree | de87e065216f4d1302893c9bd8c2e8b1430b1eea | |
parent | c5b1321e8c1960e75ecf309a8df471dbe53548a2 (diff) | |
download | Ishtar-1d1fd6c794c8ca8e758fc416b43e0f881136057f.tar.bz2 Ishtar-1d1fd6c794c8ca8e758fc416b43e0f881136057f.zip |
⚡️ remove bulk update: should not be useful anymore and can severely degrade performance
-rw-r--r-- | archaeological_context_records/models.py | 92 | ||||
-rw-r--r-- | archaeological_finds/models_finds.py | 271 |
2 files changed, 3 insertions, 360 deletions
diff --git a/archaeological_context_records/models.py b/archaeological_context_records/models.py index eb786db74..985390861 100644 --- a/archaeological_context_records/models.py +++ b/archaeological_context_records/models.py @@ -999,94 +999,6 @@ class ContextRecord( } return dct - @classmethod - def cached_label_bulk_update( - cls, operation_id=None, parcel_id=None, transaction_id=None - ): - profile = get_current_profile() - if profile.contextrecord_cached_label: - # no bulk possible - q = cls.objects - if operation_id: - q = q.filter(operation_id=operation_id) - elif parcel_id: - q = q.filter(parcel_id=parcel_id) - else: - return - for cr in q.all(): - cr.skip_history_when_saving = True - cr._no_move = True - cr.save() - cls._meta.get_field("base_finds").related_model.cached_label_bulk_update( - operation_id=operation_id, parcel_id=parcel_id, - transaction_id=transaction_id - ) - return - transaction_id, is_recursion = cls.bulk_recursion( - transaction_id, [operation_id, parcel_id] - ) - if is_recursion: - return - - if operation_id: - where = "operation_id = %s" - args = [int(operation_id)] - kwargs = {"operation_id": operation_id} - elif parcel_id: - where = "parcel_id = %s" - args = [int(parcel_id)] - kwargs = {"parcel_id": parcel_id} - else: - return - kwargs["transaction_id"] = transaction_id - - profile = get_current_profile() - - sql = """ - UPDATE "archaeological_context_records_contextrecord" AS cr - SET cached_label = - CASE - WHEN context_records_cached_label_bulk_update.main_code - IS NULL - THEN - CASE - WHEN context_records_cached_label_bulk_update.year - IS NOT NULL - AND context_records_cached_label_bulk_update.ope_code - IS NOT NULL - THEN - '{ope_prefix}' || - context_records_cached_label_bulk_update.year || - '-' || - context_records_cached_label_bulk_update.ope_code - ELSE '' - END - ELSE - '{main_ope_prefix}' || - context_records_cached_label_bulk_update.main_code - END - || '{join}' || - context_records_cached_label_bulk_update.section || '{join}' || - context_records_cached_label_bulk_update.number || '{join}' || - context_records_cached_label_bulk_update.label - FROM context_records_cached_label_bulk_update - WHERE cr.id = context_records_cached_label_bulk_update.id - AND cr.id IN ( - SELECT id FROM archaeological_context_records_contextrecord - WHERE {where} - ); - """.format( - main_ope_prefix=(profile.operation_prefix or ""), - ope_prefix=(profile.default_operation_prefix or ""), - join=settings.JOINT, - where=where, - ) - with connection.cursor() as c: - c.execute(sql, args) - cls._meta.get_field("base_finds").related_model.cached_label_bulk_update( - **kwargs - ) - @property def short_label(self): return settings.JOINT.join( @@ -1230,10 +1142,6 @@ class ContextRecord( BaseFind.objects.filter(context_record=self).all() ) - def _cached_labels_bulk_update(self): - self.base_finds.model.cached_label_bulk_update(context_record_id=self.pk) - return True - def _get_base_image_path(self): return self.operation._get_base_image_path() + "/{}/{}".format( self.SLUG, slugify(self.label or "00") diff --git a/archaeological_finds/models_finds.py b/archaeological_finds/models_finds.py index 5eb204949..455570523 100644 --- a/archaeological_finds/models_finds.py +++ b/archaeological_finds/models_finds.py @@ -734,151 +734,6 @@ class BaseFind( def name(self): return self.label - @classmethod - def cached_label_bulk_update( - cls, - operation_id=None, - parcel_id=None, - context_record_id=None, - transaction_id=None, - ): - profile = get_current_profile() - if profile.basefind_cached_label: - # no bulk possible - q = cls.objects - if context_record_id: - q = q.filter(context_record_id=context_record_id) - elif operation_id: - q = q.filter(context_record__operation_id=operation_id) - elif parcel_id: - q = q.filter(context_record__parcel_id=parcel_id) - else: - return - for bf in q.all(): - bf.skip_history_when_saving = True - bf._no_move = True - bf.save() - Find.cached_label_bulk_update( - context_record_id=context_record_id, - operation_id=operation_id, parcel_id=parcel_id, - transaction_id=transaction_id - ) - return - transaction_id, is_recursion = cls.bulk_recursion( - transaction_id, [operation_id, parcel_id, context_record_id] - ) - if is_recursion: - return - - if operation_id: - filters = """ - INNER JOIN archaeological_context_records_contextrecord acr - ON acr.operation_id = %s AND acr.id = mybf.context_record_id - """ - args = [int(operation_id)] - kwargs = {"operation_id": operation_id} - elif parcel_id: - filters = """ - INNER JOIN archaeological_context_records_contextrecord acr - ON acr.parcel_id = %s AND acr.id = mybf.context_record_id - """ - args = [int(parcel_id)] - kwargs = {"parcel_id": parcel_id} - elif context_record_id: - filters = """ - WHERE mybf.context_record_id = %s - """ - args = [int(context_record_id)] - kwargs = {"context_record_id": context_record_id} - else: - return - kwargs["transaction_id"] = transaction_id - - profile = get_current_profile() - - sql = """ - UPDATE "archaeological_finds_basefind" AS bf - SET cache_short_id = - CASE - WHEN basefind_cached_bulk_update.main_ope_code is NULL - THEN - CASE - WHEN basefind_cached_bulk_update.year - IS NOT NULL - AND basefind_cached_bulk_update.ope_code - IS NOT NULL - THEN - '{ope_prefix}' || - basefind_cached_bulk_update.year || - '-' || - basefind_cached_bulk_update.ope_code - ELSE '' - END - ELSE - '{main_ope_prefix}' || - basefind_cached_bulk_update.main_ope_code - END - || '{join}' || - to_char(basefind_cached_bulk_update.index, 'fm{zeros}'), - - cache_complete_id = - CASE - WHEN basefind_cached_bulk_update.main_ope_code IS NULL - THEN - CASE - WHEN basefind_cached_bulk_update.year - IS NOT NULL - AND basefind_cached_bulk_update.ope_code - IS NOT NULL - THEN - '{ope_prefix}' || - basefind_cached_bulk_update.year || - '-' || - basefind_cached_bulk_update.ope_code - ELSE '' - END - ELSE - '{main_ope_prefix}' || - basefind_cached_bulk_update.main_ope_code - END - || '{join}' || - - COALESCE( - (SELECT string_agg(code, '-') FROM - (SELECT DISTINCT mt.code AS code FROM - archaeological_finds_find_material_types fmt - INNER JOIN archaeological_finds_find f - ON f.id=fmt.find_id AND f.downstream_treatment_id IS NULL - INNER JOIN find_first_base_find fbf - ON fbf.find_id = f.id AND - basefind_cached_bulk_update.id = fbf.basefind_id - INNER JOIN archaeological_finds_materialtype mt - ON mt.id = fmt.materialtype_id ORDER BY mt.code) - as ag), - '-') - - || '{join}' || - basefind_cached_bulk_update.cr_label - || '{join}' || - to_char(basefind_cached_bulk_update.index, 'fm{zeros}') - - FROM basefind_cached_bulk_update - WHERE bf.id = basefind_cached_bulk_update.id - AND bf.id IN ( - SELECT mybf.id FROM archaeological_finds_basefind mybf - {filters} - ); - """.format( - main_ope_prefix=(profile.operation_prefix or ""), - ope_prefix=(profile.default_operation_prefix or ""), - join=settings.JOINT, - filters=filters, - zeros=settings.ISHTAR_FINDS_INDEX_ZERO_LEN * "0", - ) - with connection.cursor() as c: - c.execute(sql, args) - Find.cached_label_bulk_update(**kwargs) - def post_save_basefind(sender, **kwargs): cached_label_changed(sender, **kwargs) @@ -2721,8 +2576,9 @@ class Find( label = self._profile_generate_cached_label() if label: return label - self.cached_label_bulk_update(find_id=self.pk) - return Find.objects.filter(pk=self.pk).values("cached_label")[0]["cached_label"] + if not self.base_finds.count(): + return "-" + return self.base_finds.all()[0].cached_label def _generate_cached_periods(self): return " & ".join([dating.period.label for dating in self.datings.all()]) @@ -2733,127 +2589,6 @@ class Find( def _generate_cached_materials(self): return " & ".join([str(mat) for mat in self.material_types.all()]) - @classmethod - def cached_label_bulk_update( - cls, - operation_id=None, - parcel_id=None, - context_record_id=None, - find_id=None, - transaction_id=None, - ): - profile = get_current_profile() - if profile.find_cached_label: - # no bulk possible - q = cls.objects - if context_record_id: - q = q.filter(base_finds__context_record_id=context_record_id) - elif operation_id: - q = q.filter(base_finds__context_record__operation_id=operation_id) - elif parcel_id: - q = q.filter(base_finds__context_record__parcel_id=parcel_id) - else: - return - for f in q.all(): - f.skip_history_when_saving = True - f._no_move = True - f.save() - return - transaction_id, is_recursion = cls.bulk_recursion( - transaction_id, [operation_id, parcel_id, context_record_id, find_id] - ) - if is_recursion: - return - - if operation_id: - filters = """ - INNER JOIN find_first_base_find myfbf - ON myfbf.find_id = myf.id - INNER JOIN archaeological_finds_basefind mybf - ON myfbf.basefind_id = mybf.id - INNER JOIN archaeological_context_records_contextrecord acr - ON acr.operation_id = %s AND acr.id = mybf.context_record_id - """ - args = [int(operation_id)] - elif parcel_id: - filters = """ - INNER JOIN find_first_base_find myfbf - ON myfbf.find_id = myf.id - INNER JOIN archaeological_finds_basefind mybf - ON myfbf.basefind_id = mybf.id - INNER JOIN archaeological_context_records_contextrecord acr - ON acr.parcel_id = %s AND acr.id = mybf.context_record_id - """ - args = [int(parcel_id)] - elif context_record_id: - filters = """ - INNER JOIN find_first_base_find myfbf - ON myfbf.find_id = myf.id - INNER JOIN archaeological_finds_basefind mybf - ON myfbf.basefind_id = mybf.id AND mybf.context_record_id = %s - """ - args = [int(context_record_id)] - elif find_id: - filters = """ - where myf.id = %s - """ - args = [int(find_id)] - else: - return - - profile = get_current_profile() - - index = "" - if profile.find_use_index: - index = """|| '-' || - to_char(find_cached_bulk_update.index, 'fm{zeros}') - """.format( - zeros=settings.ISHTAR_FINDS_INDEX_ZERO_LEN * "0" - ) - - sql = """ - UPDATE "archaeological_finds_find" AS f - SET cached_label = - CASE - WHEN find_cached_bulk_update.main_ope_code is NULL - THEN - CASE - WHEN find_cached_bulk_update.year - IS NOT NULL - AND find_cached_bulk_update.ope_code - IS NOT NULL - THEN - '{ope_prefix}' || - find_cached_bulk_update.year || - '-' || - find_cached_bulk_update.ope_code - ELSE '' - END - ELSE - '{main_ope_prefix}' || - find_cached_bulk_update.main_ope_code - END - {index} - || '{join}' || - find_cached_bulk_update.label - - - FROM find_cached_bulk_update - WHERE f.id = find_cached_bulk_update.id - AND f.id IN ( - SELECT myf.id FROM archaeological_finds_find myf - {filters} - ); - """.format( - main_ope_prefix=(profile.operation_prefix or ""), - ope_prefix=(profile.default_operation_prefix or ""), - join=settings.JOINT, - filters=filters, - index=index, - ) - with connection.cursor() as c: - c.execute(sql, args) - def get_localisation(self, place, is_ref=False): """ Get localisation reference in the warehouse |