1+ import time
12from typing import List , Tuple
23from controller .tokenization .tokenization_service import request_tokenize_project
34from submodules .model .business_objects import attribute , record , tokenization
@@ -21,6 +22,14 @@ def get_all_attributes_by_names(
2122def get_all_attributes (
2223 project_id : str , state_filter : List [str ] = None
2324) -> List [Attribute ]:
25+ if not state_filter :
26+ state_filter = [
27+ AttributeState .UPLOADED .value ,
28+ AttributeState .USABLE .value ,
29+ AttributeState .AUTOMATICALLY_CREATED .value ,
30+ ]
31+ if len (state_filter ) == 1 and state_filter [0 ].upper () == "ALL" :
32+ state_filter = [e .value for e in AttributeState ]
2433 return attribute .get_all_ordered (project_id , True , state_filter )
2534
2635
@@ -64,7 +73,7 @@ def create_user_attribute(project_id: str) -> Attribute:
6473 with_commit = True ,
6574 )
6675 notification .send_organization_update (
67- project_id = project_id , message = "calculate_attribute:created:{attribute_id }"
76+ project_id = project_id , message = f "calculate_attribute:created:{ str ( attribute_item . id ) } "
6877 )
6978
7079 return attribute_item
@@ -89,6 +98,7 @@ def update_attribute(
8998 )
9099 if attribute .get (project_id , attribute_id ).state in [
91100 AttributeState .UPLOADED .value ,
101+ AttributeState .AUTOMATICALLY_CREATED .value ,
92102 AttributeState .USABLE .value ,
93103 ]:
94104 notification .send_organization_update (project_id , "attributes_updated" )
@@ -104,7 +114,7 @@ def delete_attribute(project_id: str, attribute_id: str) -> None:
104114 )
105115 attribute .delete (project_id , attribute_id , with_commit = True )
106116 notification .send_organization_update (
107- project_id = project_id , message = "calculate_attribute:deleted:{attribute_id}"
117+ project_id = project_id , message = f "calculate_attribute:deleted:{ attribute_id } "
108118 )
109119 if is_usable :
110120 notification .send_organization_update (
@@ -155,14 +165,28 @@ def calculate_user_attribute_all_records(
155165 )
156166 return
157167
168+ attribute_item = attribute .get (project_id , attribute_id )
169+ equally_named_attributes = attribute .get_all_by_names (
170+ project_id , [attribute_item .name ]
171+ )
172+ usable_attributes = attribute .get_all (project_id )
173+ if len (set (equally_named_attributes ) & set (usable_attributes )) > 1 :
174+ __notify_attribute_calculation_failed (
175+ project_id = project_id ,
176+ attribute_id = attribute_id ,
177+ log = "Calculation of attribute failed. Another attribute with the same name is already in state usable or uploaded." ,
178+ append_to_logs = False ,
179+ )
180+ return
181+
158182 attribute .update (
159183 project_id = project_id ,
160184 attribute_id = attribute_id ,
161185 state = AttributeState .RUNNING .value ,
162186 with_commit = True ,
163187 )
164188 notification .send_organization_update (
165- project_id = project_id , message = "calculate_attribute:started:{attribute_id}"
189+ project_id = project_id , message = f "calculate_attribute:started:{ attribute_id } "
166190 )
167191 daemon .run (
168192 __calculate_user_attribute_all_records ,
@@ -180,6 +204,13 @@ def __calculate_user_attribute_all_records(
180204 calculated_attributes = util .run_attribute_calculation_exec_env (
181205 attribute_id = attribute_id , project_id = project_id , doc_bin = "docbin_full"
182206 )
207+ if not calculated_attributes :
208+ __notify_attribute_calculation_failed (
209+ project_id = project_id ,
210+ attribute_id = attribute_id ,
211+ log = "Calculation of attribute failed." ,
212+ )
213+ return
183214 except Exception :
184215 __notify_attribute_calculation_failed (
185216 project_id = project_id ,
@@ -216,7 +247,10 @@ def __calculate_user_attribute_all_records(
216247 util .add_log_to_attribute_logs (project_id , attribute_id , "Triggering tokenization." )
217248 tokenization .delete_docbins (project_id , with_commit = True )
218249 tokenization .delete_token_statistics_for_project (project_id , with_commit = True )
219- tokenization .delete_tokenization_tasks (project_id , with_commit = True )
250+
251+ while record .count_tokenized_records (project_id ) > 0 :
252+ time .sleep (2 )
253+
220254 request_tokenize_project (project_id , user_id )
221255
222256 attribute .update (
@@ -242,7 +276,7 @@ def __notify_attribute_calculation_failed(
242276 with_commit = True ,
243277 )
244278 notification .send_organization_update (
245- project_id = project_id , message = "calculate_attribute:error:{attribute_id}"
279+ project_id = project_id , message = f "calculate_attribute:error:{ attribute_id } "
246280 )
247281
248282
0 commit comments