sapiopycommons 2024.8.26a309__py3-none-any.whl → 2024.8.27a312__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of sapiopycommons might be problematic. Click here for more details.

Files changed (33) hide show
  1. sapiopycommons/callbacks/callback_util.py +37 -133
  2. sapiopycommons/datatype/attachment_util.py +10 -11
  3. sapiopycommons/eln/experiment_handler.py +48 -209
  4. sapiopycommons/eln/experiment_report_util.py +129 -33
  5. sapiopycommons/files/complex_data_loader.py +4 -5
  6. sapiopycommons/files/file_bridge.py +14 -15
  7. sapiopycommons/files/file_bridge_handler.py +5 -27
  8. sapiopycommons/files/file_data_handler.py +5 -2
  9. sapiopycommons/files/file_util.py +5 -38
  10. sapiopycommons/files/file_validator.py +11 -26
  11. sapiopycommons/files/file_writer.py +15 -44
  12. sapiopycommons/general/aliases.py +3 -147
  13. sapiopycommons/general/custom_report_util.py +32 -34
  14. sapiopycommons/general/popup_util.py +0 -17
  15. sapiopycommons/general/time_util.py +0 -40
  16. sapiopycommons/multimodal/multimodal_data.py +1 -0
  17. sapiopycommons/processtracking/endpoints.py +22 -22
  18. sapiopycommons/recordmodel/record_handler.py +77 -228
  19. sapiopycommons/rules/eln_rule_handler.py +25 -34
  20. sapiopycommons/rules/on_save_rule_handler.py +31 -34
  21. sapiopycommons/webhook/webhook_handlers.py +26 -90
  22. {sapiopycommons-2024.8.26a309.dist-info → sapiopycommons-2024.8.27a312.dist-info}/METADATA +1 -1
  23. sapiopycommons-2024.8.27a312.dist-info/RECORD +43 -0
  24. sapiopycommons/customreport/__init__.py +0 -0
  25. sapiopycommons/customreport/column_builder.py +0 -60
  26. sapiopycommons/customreport/custom_report_builder.py +0 -125
  27. sapiopycommons/customreport/term_builder.py +0 -299
  28. sapiopycommons/general/audit_log.py +0 -196
  29. sapiopycommons/general/sapio_links.py +0 -50
  30. sapiopycommons/webhook/webservice_handlers.py +0 -67
  31. sapiopycommons-2024.8.26a309.dist-info/RECORD +0 -50
  32. {sapiopycommons-2024.8.26a309.dist-info → sapiopycommons-2024.8.27a312.dist-info}/WHEEL +0 -0
  33. {sapiopycommons-2024.8.26a309.dist-info → sapiopycommons-2024.8.27a312.dist-info}/licenses/LICENSE +0 -0
@@ -1,12 +1,7 @@
1
- from __future__ import annotations
2
-
3
1
  import time
4
2
  from collections.abc import Mapping, Iterable
5
- from weakref import WeakValueDictionary
6
3
 
7
- from sapiopylib.rest.DataMgmtService import DataMgmtServer
8
4
  from sapiopylib.rest.ELNService import ElnManager
9
- from sapiopylib.rest.User import SapioUser
10
5
  from sapiopylib.rest.pojo.DataRecord import DataRecord
11
6
  from sapiopylib.rest.pojo.eln.ElnExperiment import ElnExperiment, TemplateExperimentQueryPojo, ElnTemplate, \
12
7
  InitializeNotebookExperimentPojo, ElnExperimentUpdateCriteria
@@ -21,10 +16,8 @@ from sapiopylib.rest.utils.Protocols import ElnEntryStep, ElnExperimentProtocol
21
16
  from sapiopylib.rest.utils.recordmodel.PyRecordModel import PyRecordModel
22
17
  from sapiopylib.rest.utils.recordmodel.RecordModelManager import RecordModelInstanceManager, RecordModelManager
23
18
  from sapiopylib.rest.utils.recordmodel.RecordModelWrapper import WrappedType
24
- from sapiopylib.rest.utils.recordmodel.properties import Child
25
19
 
26
- from sapiopycommons.general.aliases import AliasUtil, SapioRecord, ExperimentIdentifier, UserIdentifier, \
27
- DataTypeIdentifier, RecordModel
20
+ from sapiopycommons.general.aliases import AliasUtil, SapioRecord
28
21
  from sapiopycommons.general.exceptions import SapioException
29
22
 
30
23
  Step = str | ElnEntryStep
@@ -34,8 +27,7 @@ itself."""
34
27
 
35
28
  # FR-46064 - Initial port of PyWebhookUtils to sapiopycommons.
36
29
  class ExperimentHandler:
37
- user: SapioUser
38
- context: SapioWebhookContext | None
30
+ context: SapioWebhookContext
39
31
  """The context that this handler is working from."""
40
32
 
41
33
  # Basic experiment info from the context.
@@ -85,103 +77,44 @@ class ExperimentHandler:
85
77
  ElnExperimentStatus.Canceled]
86
78
  """The set of statuses that an ELN experiment could have and be considered locked."""
87
79
 
88
- __instances: WeakValueDictionary[str, ExperimentHandler] = WeakValueDictionary()
89
- __initialized: bool
90
-
91
- def __new__(cls, context: UserIdentifier, experiment: ExperimentIdentifier | SapioRecord | None = None):
92
- """
93
- :param context: The current webhook context or a user object to send requests from.
94
- :param experiment: If an experiment is provided that is separate from the experiment that is in the context,
95
- that experiment will be used by this ExperimentHandler instead. An experiment can be provided in various
96
- forms, including an ElnExperiment, ElnExperimentProtocol, an experiment record, or a notebook experiment ID.
97
- """
98
- param_results = cls.__parse_params(context, experiment)
99
- user = param_results[0]
100
- experiment = param_results[2]
101
- key = f"{user.__hash__()}:{experiment.notebook_experiment_id}"
102
- obj = cls.__instances.get(key)
103
- if not obj:
104
- obj = object.__new__(cls)
105
- obj.__initialized = False
106
- cls.__instances[key] = obj
107
- return obj
108
-
109
- def __init__(self, context: UserIdentifier, experiment: ExperimentIdentifier | SapioRecord | None = None):
80
+ def __init__(self, context: SapioWebhookContext, experiment: ElnExperiment | None = None):
110
81
  """
111
82
  Initialization will throw an exception if there is no ELN Experiment in the provided context and no experiment
112
83
  is provided.
113
84
 
114
- :param context: The current webhook context or a user object to send requests from.
85
+ :param context: The current webhook context.
115
86
  :param experiment: If an experiment is provided that is separate from the experiment that is in the context,
116
- that experiment will be used by this ExperimentHandler instead. An experiment can be provided in various
117
- forms, including an ElnExperiment, ElnExperimentProtocol, an experiment record, or a notebook experiment ID.
87
+ that experiment will be used by this ExperimentHandler instead.
118
88
  """
119
- param_results = self.__parse_params(context, experiment)
120
- self.user = param_results[0]
121
- self.context = param_results[1]
122
- experiment = param_results[2]
89
+ # FR-46495 - Allow the init function of ExperimentHandler to take in an ElnExperiment that is separate from the
90
+ # context.
91
+ if context.eln_experiment is None and experiment is None:
92
+ raise SapioException("Cannot initialize ExperimentHandler. No ELN Experiment in the context.")
93
+ if context.eln_experiment == experiment:
94
+ experiment = None
95
+ self.context = context
123
96
 
124
97
  # Get the basic information about this experiment that already exists in the context and is often used.
125
- self.__eln_exp = experiment
126
- self.__protocol = ElnExperimentProtocol(experiment, self.user)
98
+ self.__eln_exp = experiment if experiment else context.eln_experiment
99
+ self.__protocol = ElnExperimentProtocol(experiment, context.user) if experiment else context.active_protocol
127
100
  self.__exp_id = self.__protocol.get_id()
128
101
 
129
102
  # Grab various managers that may be used.
130
- self.__eln_man = DataMgmtServer.get_eln_manager(self.user)
131
- self.__inst_man = RecordModelManager(self.user).instance_manager
103
+ self.__eln_man = context.eln_manager
104
+ self.__inst_man = RecordModelManager(context.user).instance_manager
132
105
 
133
106
  # Create empty caches to fill when necessary.
134
107
  self.__steps = {}
135
108
  self.__step_options = {}
136
109
  # CR-46330: Cache any experiment entry information that might already exist in the context.
137
110
  self.__queried_all_steps = False
138
- # We can only trust the entries in the context if the experiment that this handler is for is the same as the
139
- # one from the context.
140
- if self.context is not None and self.context.eln_experiment == experiment:
141
- if self.context.experiment_entry is not None:
142
- self.__steps.update({self.context.active_step.get_name(): self.context.active_step})
143
- if self.context.experiment_entry_list is not None:
144
- for entry in self.context.experiment_entry_list:
145
- self.__steps.update({entry.entry_name: ElnEntryStep(self.__protocol, entry)})
146
-
147
- @staticmethod
148
- def __parse_params(context: UserIdentifier, experiment: ExperimentIdentifier | SapioRecord | None = None) \
149
- -> tuple[SapioUser, SapioWebhookContext | None, ElnExperiment]:
150
- if isinstance(context, SapioWebhookContext):
151
- user = context.user
152
- context = context
153
- else:
154
- user = context
155
- context = None
156
- if context is not None and context.eln_experiment is not None and experiment is None:
157
- experiment = context.eln_experiment
158
- # FR-46495 - Allow the init function of ExperimentHandler to take in an ElnExperiment that is separate from the
159
- # context.
160
- # CR-37038 - Allow other experiment object types to be provided. Convert them all down to ElnExperiment.
161
- if (context is None or context.eln_experiment is None) and experiment is not None:
162
- eln_manager = DataMgmtServer.get_eln_manager(user)
163
- # If this object is already an ElnExperiment, do nothing.
164
- if isinstance(experiment, ElnExperiment):
165
- pass
166
- # If this object is an ElnExperimentProtocol, then we can get the ElnExperiment from the object.
167
- elif isinstance(experiment, ElnExperimentProtocol):
168
- experiment: ElnExperiment = experiment.eln_experiment
169
- # If this object is an integer, assume it is a notebook ID that we can query the system with.
170
- elif isinstance(experiment, int):
171
- notebook_id: int = experiment
172
- experiment: ElnExperiment = eln_manager.get_eln_experiment_by_id(notebook_id)
173
- if not experiment:
174
- raise SapioException(f"No experiment with notebook ID {notebook_id} located in the system.")
175
- # If this object is a record, assume it is an experiment record that we can query the system with.
176
- else:
177
- record_id: int = AliasUtil.to_record_ids([experiment])[0]
178
- experiment: ElnExperiment = eln_manager.get_eln_experiment_by_record_id(record_id)
179
- if not experiment:
180
- raise SapioException(f"No experiment with record ID {record_id} located in the system.")
111
+ # We can only trust the entries in the context if no experiment was given as an input parameter.
181
112
  if experiment is None:
182
- raise SapioException("Cannot initialize ExperimentHandler. No ELN Experiment found in the provided parameters.")
183
-
184
- return user, context, experiment
113
+ if context.experiment_entry is not None:
114
+ self.__steps.update({context.active_step.get_name(): context.active_step})
115
+ if context.experiment_entry_list is not None:
116
+ for entry in context.experiment_entry_list:
117
+ self.__steps.update({entry.entry_name: ElnEntryStep(self.__protocol, entry)})
185
118
 
186
119
  # FR-46495: Split the creation of the experiment in launch_experiment into a create_experiment function.
187
120
  @staticmethod
@@ -330,7 +263,11 @@ class ExperimentHandler:
330
263
  :return: The data record for this experiment. None if it has no record.
331
264
  """
332
265
  if not hasattr(self, "_ExperimentHandler__exp_record"):
333
- self.__exp_record = self.__protocol.get_record()
266
+ drm = self.context.data_record_manager
267
+ dt = self.__eln_exp.experiment_data_type_name
268
+ results = drm.query_data_records_by_id(dt, [self.__eln_exp.experiment_record_id]).result_list
269
+ # PR-46504: Set the exp_record to None if there are no results.
270
+ self.__exp_record = results[0] if results else None
334
271
  if self.__exp_record is None and exception_on_none:
335
272
  raise SapioException(f"Experiment record not found for experiment with ID {self.__exp_id}.")
336
273
  return self.__exp_record
@@ -530,7 +467,7 @@ class ExperimentHandler:
530
467
  ret_list.append(step)
531
468
  return ret_list
532
469
 
533
- def get_all_steps(self, data_type: DataTypeIdentifier | None = None) -> list[ElnEntryStep]:
470
+ def get_all_steps(self, data_type: str | type[WrappedType] | None = None) -> list[ElnEntryStep]:
534
471
  """
535
472
  Get a list of every entry in the experiment. Optionally filter the returned entries by a data type.
536
473
 
@@ -546,7 +483,8 @@ class ExperimentHandler:
546
483
  all_steps: list[ElnEntryStep] = self.__protocol.get_sorted_step_list()
547
484
  if data_type is None:
548
485
  return all_steps
549
- data_type: str = AliasUtil.to_data_type_name(data_type)
486
+ if not isinstance(data_type, str):
487
+ data_type: str = data_type.get_wrapper_data_type_name()
550
488
  return [x for x in all_steps if data_type in x.get_data_type_names()]
551
489
 
552
490
  def get_step_records(self, step: Step) -> list[DataRecord]:
@@ -598,10 +536,6 @@ class ExperimentHandler:
598
536
  The records may be provided as either DataRecords, PyRecordModels, or WrappedRecordModels.
599
537
  """
600
538
  step = self.__to_eln_step(step)
601
- dt: str = AliasUtil.to_singular_data_type_name(records)
602
- if dt != step.get_data_type_names()[0]:
603
- raise SapioException(f"Cannot add {dt} records to entry {step.get_name()} of type "
604
- f"{step.get_data_type_names()[0]}.")
605
539
  step.add_records(AliasUtil.to_data_records(records))
606
540
 
607
541
  def remove_step_records(self, step: Step, records: Iterable[SapioRecord]) -> None:
@@ -620,10 +554,6 @@ class ExperimentHandler:
620
554
  The records may be provided as either DataRecords, PyRecordModels, or WrappedRecordModels.
621
555
  """
622
556
  step = self.__to_eln_step(step)
623
- dt: str = AliasUtil.to_singular_data_type_name(records)
624
- if dt != step.get_data_type_names()[0]:
625
- raise SapioException(f"Cannot remove {dt} records from entry {step.get_name()} of type "
626
- f"{step.get_data_type_names()[0]}.")
627
557
  step.remove_records(AliasUtil.to_data_records(records))
628
558
 
629
559
  def set_step_records(self, step: Step, records: Iterable[SapioRecord]) -> None:
@@ -647,13 +577,11 @@ class ExperimentHandler:
647
577
  The records may be provided as either DataRecords, PyRecordModels, or WrappedRecordModels.
648
578
  """
649
579
  step = self.__to_eln_step(step)
650
- dt: str = AliasUtil.to_singular_data_type_name(records)
651
- if dt != step.get_data_type_names()[0]:
652
- raise SapioException(f"Cannot set {dt} records for entry {step.get_name()} of type "
653
- f"{step.get_data_type_names()[0]}.")
654
580
  step.set_records(AliasUtil.to_data_records(records))
655
581
 
656
582
  # FR-46496 - Provide alias of set_step_records for use with form entries.
583
+ # TODO: Provide a similar aliased function for attachment entries once sapiopylib allows setting multiple
584
+ # attachments to an attachment step.
657
585
  def set_form_record(self, step: Step, record: SapioRecord) -> None:
658
586
  """
659
587
  Sets the record for a form entry.
@@ -671,8 +599,7 @@ class ExperimentHandler:
671
599
  self.set_step_records(step, [record])
672
600
 
673
601
  # FR-46496 - Provide functions for adding and removing rows from an ELN data type entry.
674
- def add_eln_rows(self, step: Step, count: int, wrapper_type: type[WrappedType] | None = None) \
675
- -> list[PyRecordModel | WrappedType]:
602
+ def add_eln_rows(self, step: Step, count: int) -> list[PyRecordModel]:
676
603
  """
677
604
  Add rows to an ELNExperimentDetail or ELNSampleDetail table entry. The rows will not appear in the system
678
605
  until a record manager store and commit has occurred.
@@ -684,37 +611,15 @@ class ExperimentHandler:
684
611
  The step may be provided as either a string for the name of the step or an ElnEntryStep.
685
612
  If given a name, throws an exception if no step of the given name exists in the experiment.
686
613
  :param count: The number of new rows to add to the entry.
687
- :param wrapper_type: Optionally wrap the ELN data type in a record model wrapper. If not provided, returns
688
- an unwrapped PyRecordModel.
689
614
  :return: A list of the newly created rows.
690
615
  """
691
616
  step = self.__to_eln_step(step)
692
617
  if step.eln_entry.entry_type != ElnEntryType.Table:
693
618
  raise SapioException("The provided step is not a table entry.")
694
619
  dt: str = step.get_data_type_names()[0]
695
- if not ElnBaseDataType.is_eln_type(dt):
620
+ if not self.__is_eln_type(dt):
696
621
  raise SapioException("The provided step is not an ELN data type entry.")
697
- records: list[PyRecordModel] = self.__inst_man.add_new_records(dt, count)
698
- if wrapper_type:
699
- return self.__inst_man.wrap_list(records, wrapper_type)
700
- return records
701
-
702
- def add_eln_row(self, step: Step, wrapper_type: type[WrappedType] | None = None) -> PyRecordModel | WrappedType:
703
- """
704
- Add a row to an ELNExperimentDetail or ELNSampleDetail table entry. The row will not appear in the system
705
- until a record manager store and commit has occurred.
706
-
707
- If no step functions have been called before and a step is being searched for by name, queries for the
708
- list of steps in the experiment and caches them.
709
-
710
- :param step:
711
- The step may be provided as either a string for the name of the step or an ElnEntryStep.
712
- If given a name, throws an exception if no step of the given name exists in the experiment.
713
- :param wrapper_type: Optionally wrap the ELN data type in a record model wrapper. If not provided, returns
714
- an unwrapped PyRecordModel.
715
- :return: The newly created row.
716
- """
717
- return self.add_eln_rows(step, 1, wrapper_type)[0]
622
+ return self.__inst_man.add_new_records(dt, count)
718
623
 
719
624
  def remove_eln_rows(self, step: Step, records: list[SapioRecord]) -> None:
720
625
  """
@@ -736,12 +641,10 @@ class ExperimentHandler:
736
641
  """
737
642
  step = self.__to_eln_step(step)
738
643
  dt: str = step.get_data_type_names()[0]
739
- if not ElnBaseDataType.is_eln_type(dt):
644
+ if not self.__is_eln_type(dt):
740
645
  raise SapioException("The provided step is not an ELN data type entry.")
741
- record_dt: str = AliasUtil.to_singular_data_type_name(records)
742
- if record_dt != dt:
743
- raise SapioException(f"Cannot remove {dt} records from entry {step.get_name()} of type "
744
- f"{step.get_data_type_names()[0]}.")
646
+ if any([x.data_type_name != dt for x in records]):
647
+ raise SapioException("Not all of the provided records match the data type of the step.")
745
648
  # If any rows were provided as data records, turn them into record models before deleting them, as otherwise
746
649
  # this function would need to make a webservice call to do the deletion.
747
650
  data_records: list[DataRecord] = []
@@ -755,59 +658,16 @@ class ExperimentHandler:
755
658
  for record in record_models:
756
659
  record.delete()
757
660
 
758
- def remove_eln_row(self, step: Step, record: SapioRecord) -> None:
759
- """
760
- Remove a row from an ELNExperimentDetail or ELNSampleDetail table entry. ELN data type table entries display all
761
- records in the system that match the entry's data type. This means that removing rows from an ELN data type
762
- table entry is equivalent to deleting the records for the rows.
763
-
764
- The row will not be deleted in the system until a record manager store and commit has occurred.
765
-
766
- If no step functions have been called before and a step is being searched for by name, queries for the
767
- list of steps in the experiment and caches them.
768
-
769
- :param step:
770
- The step may be provided as either a string for the name of the step or an ElnEntryStep.
771
- If given a name, throws an exception if no step of the given name exists in the experiment.
772
- :param record:
773
- The record to remove from the given step.
774
- The record may be provided as either a DataRecord, PyRecordModel, or WrappedRecordModel.
775
- """
776
- self.remove_eln_row(step, [record])
777
-
778
- def add_sample_details(self, step: Step, samples: list[RecordModel], wrapper_type: type[WrappedType]) \
779
- -> list[PyRecordModel | WrappedType]:
780
- """
781
- Add sample details to a sample details entry while relating them to the input sample records.
782
-
783
- :param step:
784
- The step may be provided as either a string for the name of the step or an ElnEntryStep.
785
- If given a name, throws an exception if no step of the given name exists in the experiment.
786
- :param samples: The sample records to add the sample details to.
787
- :param wrapper_type: Optionally wrap the sample details in a record model wrapper. If not provided, returns
788
- an unwrapped PyRecordModel.
789
- :return: The newly created sample details. The indices of the samples in the input list match the index of the
790
- sample details in this list that they are related to.
791
- """
792
- step = self.__to_eln_step(step)
793
- if step.eln_entry.entry_type != ElnEntryType.Table:
794
- raise SapioException("The provided step is not a table entry.")
795
- dt: str = step.get_data_type_names()[0]
796
- if not ElnBaseDataType.is_eln_type(dt) or ElnBaseDataType.get_base_type(dt) != ElnBaseDataType.SAMPLE_DETAIL:
797
- raise SapioException("The provided step is not an ELNSampleDetail entry.")
798
- records: list[PyRecordModel] = []
799
- for sample in samples:
800
- if sample.data_type_name != "Sample":
801
- raise SapioException(f"Received a {sample.data_type_name} record when Sample records were expected.")
802
- detail: PyRecordModel = sample.add(Child.of_type_name(dt))
803
- detail.set_field_values({
804
- "SampleId": sample.get_field_value("SampleId"),
805
- "OtherSampleId": sample.get_field_value("OtherSampleId")
806
- })
807
- records.append(detail)
808
- if wrapper_type:
809
- return self.__inst_man.wrap_list(records, wrapper_type)
810
- return records
661
+ # TODO: Remove and use the function of the same name in ElnBaseDataType in the future. Currently this function is
662
+ # bugged in sapiopylib and is comparing against base_type.name instead of base_type.value.
663
+ @staticmethod
664
+ def __is_eln_type(data_type: str):
665
+ if data_type is None or not data_type:
666
+ return False
667
+ for base_type in ElnBaseDataType:
668
+ if data_type.lower().startswith(base_type.value.lower()):
669
+ return True
670
+ return False
811
671
 
812
672
  def update_step(self, step: Step,
813
673
  entry_name: str | None = None,
@@ -1096,27 +956,6 @@ class ExperimentHandler:
1096
956
  step.unlock_step()
1097
957
  step.eln_entry.entry_status = ExperimentEntryStatus.UnlockedChangesRequired
1098
958
 
1099
- def disable_step(self, step: Step) -> None:
1100
- """
1101
- Set the status of the input step to Disabled. This is the state that entries are in when they are waiting for
1102
- entries that they are dependent upon to be submitted before they can be enabled. If you have unsubmitted an
1103
- entry and want its dependent entries to be locked again, then you would use this to set their status to
1104
- disabled.
1105
-
1106
- Makes a webservice call to update the step. Checks if the step is already unlocked, and does nothing if so.
1107
-
1108
- If no step functions have been called before and a step is being searched for by name, queries for the
1109
- list of steps in the experiment and caches them.
1110
-
1111
- :param step:
1112
- The step to disable.
1113
- The step may be provided as either a string for the name of the step or an ElnEntryStep.
1114
- If given a name, throws an exception if no step of the given name exists in the experiment.
1115
- """
1116
- step = self.__to_eln_step(step)
1117
- if step.eln_entry.entry_status in self.__ENTRY_LOCKED_STATUSES:
1118
- self.update_step(step, entry_status=ExperimentEntryStatus.Disabled)
1119
-
1120
959
  def step_is_submitted(self, step: Step) -> bool:
1121
960
  """
1122
961
  Determine if the input step has already been submitted.
@@ -1,10 +1,19 @@
1
1
  from sapiopylib.rest.User import SapioUser
2
+ from sapiopylib.rest.pojo.CustomReport import (
3
+ CompositeReportTerm,
4
+ CompositeTermOperation,
5
+ CustomReportCriteria,
6
+ ExplicitJoinDefinition,
7
+ FieldCompareReportTerm,
8
+ RawReportTerm,
9
+ RawTermOperation,
10
+ ReportColumn,
11
+ )
2
12
  from sapiopylib.rest.pojo.datatype.FieldDefinition import FieldType
13
+ from sapiopylib.rest.pojo.webhook.WebhookContext import SapioWebhookContext
3
14
  from sapiopylib.rest.utils.recordmodel.RecordModelWrapper import WrappedType
4
15
 
5
- from sapiopycommons.customreport.custom_report_builder import CustomReportBuilder
6
- from sapiopycommons.customreport.term_builder import TermBuilder
7
- from sapiopycommons.general.aliases import SapioRecord, UserIdentifier, AliasUtil
16
+ from sapiopycommons.general.aliases import SapioRecord
8
17
  from sapiopycommons.general.custom_report_util import CustomReportUtil
9
18
  from sapiopycommons.recordmodel.record_handler import RecordHandler
10
19
 
@@ -16,8 +25,10 @@ _RECORD_ID = "RECORDID"
16
25
  # that given records were used in or getting all records of a datatype used in given experiments.
17
26
  class ExperimentReportUtil:
18
27
  @staticmethod
19
- def map_records_to_experiment_ids(context: UserIdentifier, records: list[SapioRecord]) \
20
- -> dict[SapioRecord, list[int]]:
28
+ def map_records_to_experiment_ids(
29
+ context: SapioWebhookContext | SapioUser,
30
+ records: list[SapioRecord],
31
+ ) -> dict[SapioRecord, list[int]]:
21
32
  """
22
33
  Return a dictionary mapping each record to a list of ids of experiments that they were used in.
23
34
  If a record wasn't used in any experiments then it will be mapped to an empty list.
@@ -29,25 +40,38 @@ class ExperimentReportUtil:
29
40
  if not records:
30
41
  return {}
31
42
 
32
- user: SapioUser = AliasUtil.to_sapio_user(context)
33
- data_type_name: str = AliasUtil.to_singular_data_type_name(records)
43
+ user: SapioUser = context if isinstance(context, SapioUser) else context.user
44
+
45
+ data_type_name = records[0].data_type_name
34
46
 
35
47
  record_ids = [record.record_id for record in records]
36
- rows = ExperimentReportUtil.__get_record_experiment_relation_rows(user, data_type_name, record_ids=record_ids)
48
+
49
+ rows = ExperimentReportUtil.__get_record_experiment_relation_rows(
50
+ user, data_type_name, record_ids=record_ids
51
+ )
37
52
 
38
53
  id_to_record: dict[int, SapioRecord] = RecordHandler.map_by_id(records)
39
- record_to_exps: dict[SapioRecord, set[int]] = {record: set() for record in records}
54
+
55
+ record_to_exps: dict[SapioRecord, set[int]] = {
56
+ record: set() for record in records
57
+ }
58
+
40
59
  for row in rows:
41
60
  record_id: int = row[_RECORD_ID]
42
61
  exp_id: int = row[_NOTEBOOK_ID]
62
+
43
63
  record = id_to_record[record_id]
64
+
44
65
  record_to_exps[record].add(exp_id)
45
66
 
46
67
  return {record: list(exps) for record, exps in record_to_exps.items()}
47
68
 
48
69
  @staticmethod
49
- def map_experiments_to_records_of_type(context: UserIdentifier, exp_ids: list[int],
50
- wrapper_type: type[WrappedType]) -> dict[int, list[WrappedType]]:
70
+ def map_experiments_to_records_of_type(
71
+ context: SapioWebhookContext | SapioUser,
72
+ exp_ids: list[int],
73
+ wrapper_type: type[WrappedType],
74
+ ) -> dict[int, list[WrappedType]]:
51
75
  """
52
76
  Return a dictionary mapping each experiment id to a list of records of the given type that were used in each experiment.
53
77
  If an experiment didn't use any records of the given type then it will be mapped to an empty list.
@@ -60,27 +84,41 @@ class ExperimentReportUtil:
60
84
  if not exp_ids:
61
85
  return {}
62
86
 
63
- user = AliasUtil.to_sapio_user(context)
87
+ user = context if isinstance(context, SapioUser) else context.user
88
+
64
89
  record_handler = RecordHandler(user)
90
+
65
91
  data_type_name: str = wrapper_type.get_wrapper_data_type_name()
66
92
 
67
- rows = ExperimentReportUtil.__get_record_experiment_relation_rows(user, data_type_name, exp_ids=exp_ids)
93
+ rows = ExperimentReportUtil.__get_record_experiment_relation_rows(
94
+ user, data_type_name, exp_ids=exp_ids
95
+ )
96
+
68
97
  record_ids: set[int] = {row[_RECORD_ID] for row in rows}
98
+
69
99
  records = record_handler.query_models_by_id(wrapper_type, record_ids)
70
100
 
71
101
  id_to_record: dict[int, WrappedType] = RecordHandler.map_by_id(records)
102
+
72
103
  exp_to_records: dict[int, set[SapioRecord]] = {exp: set() for exp in exp_ids}
104
+
73
105
  for row in rows:
74
106
  record_id: int = row[_RECORD_ID]
75
107
  exp_id: int = row[_NOTEBOOK_ID]
108
+
76
109
  record = id_to_record[record_id]
110
+
77
111
  exp_to_records[exp_id].add(record)
78
112
 
79
113
  return {exp: list(records) for exp, records in exp_to_records.items()}
80
114
 
81
115
  @staticmethod
82
- def __get_record_experiment_relation_rows(user: SapioUser, data_type_name: str, record_ids: list[int] | None = None,
83
- exp_ids: list[int] | None = None) -> list[dict[str, int]]:
116
+ def __get_record_experiment_relation_rows(
117
+ user: SapioUser,
118
+ data_type_name: str,
119
+ record_ids: list[int] | None = None,
120
+ exp_ids: list[int] | None = None,
121
+ ) -> list[dict[str, int]]:
84
122
  """
85
123
  Return a list of dicts mapping \"RECORDID\" to the record id and \"EXPERIMENTID\" to the experiment id.
86
124
  At least one of record_ids and exp_ids should be provided.
@@ -88,31 +126,89 @@ class ExperimentReportUtil:
88
126
  assert (record_ids or exp_ids)
89
127
 
90
128
  if record_ids:
91
- records_term = TermBuilder.is_term(data_type_name, "RECORDID", record_ids)
129
+ rec_ids = [str(record_id) for record_id in record_ids]
130
+
131
+ ids_str = "{" + ", ".join(rec_ids) + "}"
132
+
133
+ records_term = RawReportTerm(
134
+ data_type_name, "RECORDID", RawTermOperation.EQUAL_TO_OPERATOR, ids_str
135
+ )
136
+
92
137
  else:
93
138
  # Get all records of the given type
94
- records_term = TermBuilder.all_records_term(data_type_name)
139
+ records_term = RawReportTerm(
140
+ data_type_name,
141
+ "RECORDID",
142
+ RawTermOperation.GREATER_THAN_OR_EQUAL_OPERATOR,
143
+ "0",
144
+ )
95
145
 
96
146
  if exp_ids:
97
- exp_term = TermBuilder.is_term("NOTEBOOKEXPERIMENT", "EXPERIMENTID", exp_ids)
147
+ exp_ids = [str(exp_id) for exp_id in exp_ids]
148
+
149
+ ids_str = "{" + ", ".join(exp_ids) + "}"
150
+
151
+ exp_term = RawReportTerm(
152
+ "NOTEBOOKEXPERIMENT",
153
+ "EXPERIMENTID",
154
+ RawTermOperation.EQUAL_TO_OPERATOR,
155
+ ids_str,
156
+ )
157
+
98
158
  else:
99
159
  # Get all experiments
100
- exp_term = TermBuilder.gte_term("NOTEBOOKEXPERIMENT", "EXPERIMENTID", "0")
101
-
102
- root_term = TermBuilder.and_terms(records_term, exp_term)
160
+ exp_term = RawReportTerm(
161
+ "NOTEBOOKEXPERIMENT",
162
+ "EXPERIMENTID",
163
+ RawTermOperation.GREATER_THAN_OR_EQUAL_OPERATOR,
164
+ "0",
165
+ )
166
+
167
+ root_term = CompositeReportTerm(
168
+ records_term, CompositeTermOperation.AND_OPERATOR, exp_term
169
+ )
170
+
171
+ # The columns the resulting dataframe will have
172
+ column_list = [
173
+ ReportColumn(data_type_name, "RECORDID", FieldType.LONG),
174
+ ReportColumn("NOTEBOOKEXPERIMENT", "EXPERIMENTID", FieldType.LONG),
175
+ ]
103
176
 
104
177
  # Join records on the experiment entry records that correspond to them.
105
- records_entry_join = TermBuilder.compare_is_term("EXPERIMENTENTRYRECORD", "RECORDID", data_type_name, "RECORDID")
178
+ records_entry_join = FieldCompareReportTerm(
179
+ data_type_name,
180
+ "RECORDID",
181
+ RawTermOperation.EQUAL_TO_OPERATOR,
182
+ "EXPERIMENTENTRYRECORD",
183
+ "RECORDID",
184
+ )
185
+
106
186
  # Join entry records on the experiment entries they are in.
107
- experiment_entry_enb_entry_join = TermBuilder.compare_is_term("ENBENTRY", "ENTRYID", "EXPERIMENTENTRYRECORD", "ENTRYID")
187
+ experiment_entry_enb_entry_join = FieldCompareReportTerm(
188
+ "EXPERIMENTENTRYRECORD",
189
+ "ENTRYID",
190
+ RawTermOperation.EQUAL_TO_OPERATOR,
191
+ "ENBENTRY",
192
+ "ENTRYID",
193
+ )
194
+
108
195
  # Join entries on the experiments they are in.
109
- enb_entry_experiment_join = TermBuilder.compare_is_term("NOTEBOOKEXPERIMENT", "EXPERIMENTID", "ENBENTRY", "EXPERIMENTID")
110
-
111
- report_builder = CustomReportBuilder(data_type_name)
112
- report_builder.set_root_term(root_term)
113
- report_builder.add_column("RECORDID", FieldType.LONG, data_type=data_type_name)
114
- report_builder.add_column("EXPERIMENTID", FieldType.LONG, data_type="NOTEBOOKEXPERIMENT")
115
- report_builder.add_join(records_entry_join)
116
- report_builder.add_join(experiment_entry_enb_entry_join)
117
- report_builder.add_join(enb_entry_experiment_join)
118
- return CustomReportUtil.run_custom_report(user, report_builder.build_report_criteria())
196
+ enb_entry_experiment_join = FieldCompareReportTerm(
197
+ "ENBENTRY",
198
+ "EXPERIMENTID",
199
+ RawTermOperation.EQUAL_TO_OPERATOR,
200
+ "NOTEBOOKEXPERIMENT",
201
+ "EXPERIMENTID",
202
+ )
203
+
204
+ report_criteria = CustomReportCriteria(
205
+ column_list,
206
+ root_term,
207
+ join_list=[
208
+ ExplicitJoinDefinition("EXPERIMENTENTRYRECORD", records_entry_join),
209
+ ExplicitJoinDefinition("ENBENTRY", experiment_entry_enb_entry_join),
210
+ ExplicitJoinDefinition("NOTEBOOKEXPERIMENT", enb_entry_experiment_join),
211
+ ],
212
+ )
213
+
214
+ return CustomReportUtil.run_custom_report(user, report_criteria)
@@ -1,13 +1,12 @@
1
1
  import io
2
2
 
3
3
  from sapiopylib.rest.User import SapioUser
4
-
5
- from sapiopycommons.general.aliases import UserIdentifier, AliasUtil
4
+ from sapiopylib.rest.pojo.webhook.WebhookContext import SapioWebhookContext
6
5
 
7
6
 
8
7
  class CDL:
9
8
  @staticmethod
10
- def load_cdl(context: UserIdentifier, config_name: str, file_name: str, file_data: bytes | str) \
9
+ def load_cdl(context: SapioWebhookContext | SapioUser, config_name: str, file_name: str, file_data: bytes | str) \
11
10
  -> list[int]:
12
11
  """
13
12
  Create data records from a file using one of the complex data loader (CDL) configurations in the system.
@@ -23,8 +22,8 @@ class CDL:
23
22
  "configName": config_name,
24
23
  "fileName": file_name
25
24
  }
26
- user: SapioUser = AliasUtil.to_sapio_user(context)
27
- with io.BytesIO(file_data.encode() if isinstance(file_data, str) else file_data) as data_stream:
25
+ user: SapioUser = context if isinstance(context, SapioUser) else context.user
26
+ with io.StringIO(file_data) if isinstance(file_data, str) else io.BytesIO(file_data) as data_stream:
28
27
  response = user.post_data_stream(sub_path, params=params, data_stream=data_stream)
29
28
  user.raise_for_status(response)
30
29
  # The response content is returned as bytes for a comma separated string of record IDs.