From b809f570aad26db7d4c19739884625948be07b8e Mon Sep 17 00:00:00 2001 From: Juan Date: Fri, 26 Jun 2020 14:56:50 -0400 Subject: [PATCH] Changed series ids to be an equivalent of the ODM composite id. Fixed small sorting issue with the resources list. --- src/GAMUTRawData/odmdata/series.py | 17 ++++++++++++----- src/GuiComponents/VisualH2OMainWindow.py | 17 ++++++++--------- src/GuiComponents/WxUtilities.py | 6 +++--- src/Utilities/H2OSeries.py | 9 +++++++-- src/Utilities/H2OServices.py | 2 +- src/utility-scripts/patch_series_id.py | 18 ++++++++++++++++++ 6 files changed, 49 insertions(+), 20 deletions(-) create mode 100644 src/utility-scripts/patch_series_id.py diff --git a/src/GAMUTRawData/odmdata/series.py b/src/GAMUTRawData/odmdata/series.py index d31736f..8061a43 100644 --- a/src/GAMUTRawData/odmdata/series.py +++ b/src/GAMUTRawData/odmdata/series.py @@ -2,6 +2,7 @@ from sqlalchemy import Column, Integer, ForeignKey, String, Float, DateTime from sqlalchemy.orm import relationship +from sqlalchemy.ext.hybrid import hybrid_property from base import Base from site import Site @@ -95,19 +96,24 @@ class Series(Base): source = relationship(Source) quality_control_level = relationship(QualityControlLevel) + @hybrid_property + def odm_id(self): + return '{}_{}_{}_{}_{}'.format(self.site_id, self.variable_id, self.method_id, + self.source_id, self.quality_control_level_id) + def __repr__(self): return "" % (self.id, self.site_name, self.variable_code, self.variable_name) def __eq__(self, other): # return self.__dict__ == other.__dict__ - return [self.id, self.site_id, self.site_code, self.site_name, self.variable_id, self.variable_code, + return [self.site_id, self.site_code, self.site_name, self.variable_id, self.variable_code, self.variable_name, self.speciation, self.variable_units_id, self.variable_units_name, self.sample_medium, self.value_type, self.time_support, self.time_units_id, self.time_units_name, self.data_type, self.general_category, self.method_id, self.method_description, self.source_id, self.source_description, self.organization, self.citation, self.quality_control_level_id, self.quality_control_level_code, self.begin_date_time, self.end_date_time, self.begin_date_time_utc, self.end_date_time_utc, self.value_count] == \ - [other.id, other.site_id, other.site_code, other.site_name, other.variable_id, other.variable_code, + [other.site_id, other.site_code, other.site_name, other.variable_id, other.variable_code, other.variable_name, other.speciation, other.variable_units_id, other.variable_units_name, other.sample_medium, other.value_type, other.time_support, other.time_units_id, other.time_units_name, other.data_type, other.general_category, other.method_id, other.method_description, @@ -122,7 +128,7 @@ def get_table_columns(self): return self.__table__.columns.keys() def list_repr(self): - return [self.id, self.site_id, self.site_code, self.site_name, self.variable_id, self.variable_code, + return [self.site_id, self.site_code, self.site_name, self.variable_id, self.variable_code, self.variable_name, self.speciation, self.variable_units_id, self.variable_units_name, self.sample_medium, self.value_type, self.time_support, self.time_units_id, self.time_units_name, self.data_type, self.general_category, self.method_id, self.method_description, @@ -131,14 +137,14 @@ def list_repr(self): self.end_date_time, self.begin_date_time_utc, self.end_date_time_utc, self.value_count] def dict_repr(self): - keys = ['SeriesID', 'SiteID', 'SiteCode', 'SiteName', 'VariableID', 'VariableCode', 'VariableName', + keys = ['SiteID', 'SiteCode', 'SiteName', 'VariableID', 'VariableCode', 'VariableName', 'Speciation', 'VariableUnitsID', 'VariableUnitsName', 'SampleMedium', 'ValueType', 'TimeSupport', 'TimeUnitsID', 'TimeUnitsName', 'DataType', 'GeneralCategory', 'MethodID', 'MethodDescription', 'SourceID', 'SourceDescription', 'Organization', 'Citation', 'QualityControlLevelID', 'QualityControlLevelCode', 'BeginDateTime', 'EndDateTime', 'BeginDateTimeUTC', 'EndDateTimeUTC', 'ValueCount' ] - values = [self.id, self.site_id, self.site_code, self.site_name, self.variable_id, self.variable_code, + values = [self.site_id, self.site_code, self.site_name, self.variable_id, self.variable_code, self.variable_name, self.speciation, self.variable_units_id, self.variable_units_name, self.sample_medium, self.value_type, self.time_support, self.time_units_id, self.time_units_name, self.data_type, self.general_category, self.method_id, self.method_description, @@ -147,6 +153,7 @@ def dict_repr(self): self.end_date_time, self.begin_date_time_utc, self.end_date_time_utc, self.value_count] return OrderedDict(zip(keys, values)) + def returnDict(): keys = ['SeriesID', 'SiteID', 'SiteCode', 'SiteName', 'VariableID', 'VariableCode', 'VariableName', 'Speciation', 'VariableUnitsID', 'VariableUnitsName', 'SampleMedium', 'ValueType', 'TimeSupport', 'TimeUnitsID', diff --git a/src/GuiComponents/VisualH2OMainWindow.py b/src/GuiComponents/VisualH2OMainWindow.py index 16dbd60..61bdd93 100644 --- a/src/GuiComponents/VisualH2OMainWindow.py +++ b/src/GuiComponents/VisualH2OMainWindow.py @@ -312,8 +312,8 @@ def _get_destination_resource_choices(self): in self.H2OService.ManagedResources.iteritems() if resource.resource is not None] unmanaged_resources = [CHOICE_DEFAULTS.RESOURCE_STR.format(resource.title, hs_id) for hs_id, resource in self._resources.iteritems() if hs_id not in self.H2OService.ManagedResources] - managed_resources.sort(reverse=self.invert_resource_choices_checkbox.IsChecked()) - unmanaged_resources.sort(reverse=self.invert_resource_choices_checkbox.IsChecked()) + managed_resources.sort(reverse=(not self.invert_resource_choices_checkbox.IsChecked())) + unmanaged_resources.sort(reverse=(not self.invert_resource_choices_checkbox.IsChecked())) choices = [CHOICE_DEFAULTS.CREATE_NEW_RESOURCE, CHOICE_DEFAULTS.MANAGED_RESOURCES.format(len(managed_resources), @@ -375,8 +375,8 @@ def set_odm_connection(self, connection): return for series in series_list: - self.h2o_series_dict[series.id] = OdmSeriesHelper.CreateH2OSeriesFromOdmSeries(series) - self.odm_series_dict[series.id] = series + self.h2o_series_dict[series.odm_id] = OdmSeriesHelper.CreateH2OSeriesFromOdmSeries(series) + self.odm_series_dict[series.odm_id] = series self.reset_series_in_grid() # Re-enable controls for ODM series UI elements @@ -470,9 +470,9 @@ def reset_series_grid_with_resource(self, resource, event=None): self.add_to_selected_button.Disable() return - selected_series_ids = [int(series_id) for series_id in resource.selected_series.keys()] + selected_series_ids = [series_odm_id for series_odm_id in resource.selected_series.keys()] for series in self.odm_series_dict.values(): - if series.id in selected_series_ids: + if series.odm_id in selected_series_ids: selected_series.append(series) else: available_series.append(series) @@ -484,9 +484,8 @@ def reset_series_grid_with_resource(self, resource, event=None): self.remove_selected_button.Enable() self.add_to_selected_button.Enable() - def _move_to_selected_series(self, event): - series_list = [self.odm_series_dict[series_id] for series_id in self.available_series_grid.GetSelectedSeries()] + series_list = [self.odm_series_dict[series_odm_id] for series_odm_id in self.available_series_grid.GetSelectedSeries()] self.selected_series_grid.InsertSeriesList(series_list, do_sort=True) self.available_series_grid.RemoveSelectedRows() @@ -495,7 +494,7 @@ def _move_to_selected_series(self, event): mngres.selected_series = self.get_selected_series() def _move_to_available_series(self, event): - series_list = [self.odm_series_dict[series_id] for series_id in self.selected_series_grid.GetSelectedSeries()] + series_list = [self.odm_series_dict[series_omd_id] for series_omd_id in self.selected_series_grid.GetSelectedSeries()] self.available_series_grid.InsertSeriesList(series_list, do_sort=True) self.selected_series_grid.RemoveSelectedRows() diff --git a/src/GuiComponents/WxUtilities.py b/src/GuiComponents/WxUtilities.py index d6ce9b8..5db788f 100644 --- a/src/GuiComponents/WxUtilities.py +++ b/src/GuiComponents/WxUtilities.py @@ -141,7 +141,7 @@ def AddGridRow(self, values): self.SetRowValue(row_pos, values) def AppendSeries(self, series): - values = [series.id, series.site_code, series.site_name, series.variable_name, + values = [series.odm_id, series.site_code, series.site_name, series.variable_name, series.variable_code, series.quality_control_level_code, series.source_description, series.method_description] self.AddGridRow(values) @@ -163,12 +163,12 @@ def RemoveSelectedRows(self): self.DeleteRows(i) def GetSelectedSeries(self): - return [int(self.GetCellValue(row, 0)) for row in self.GetSelectedRows()] + return [self.GetCellValue(row, 0) for row in self.GetSelectedRows()] def GetSeries(self): series = [] for row in range(0, self.NumberRows): - series.append(int(self.GetCellValue(row, 0))) + series.append(self.GetCellValue(row, 0)) return series def Clear(self): diff --git a/src/Utilities/H2OSeries.py b/src/Utilities/H2OSeries.py index 742497c..c673457 100644 --- a/src/Utilities/H2OSeries.py +++ b/src/Utilities/H2OSeries.py @@ -8,7 +8,7 @@ class H2OSeries: def __init__(self, SeriesID=None, SiteID=None, SiteCode=None, VariableID=None, VariableCode=None, MethodID=None, SourceID=None, QualityControlLevelID=None, QualityControlLevelCode=None): - self.SeriesID = SeriesID if SeriesID is not None else -1 # type: int + self.SeriesID = SeriesID if SeriesID is not None else "" # type: str self.SiteID = SiteID if SiteID is not None else -1 # type: int self.SiteCode = SiteCode if SiteCode is not None else "" # type: str self.VariableID = VariableID if VariableID is not None else -1 # type: int @@ -19,6 +19,11 @@ def __init__(self, SeriesID=None, SiteID=None, SiteCode=None, VariableID=None, V self.QualityControlLevelCode = QualityControlLevelCode if QualityControlLevelCode is not None else -1 # # type: float + def get_odm_id(self): + return '{}_{}_{}_{}_{}'.format(self.SiteID, self.VariableID, self.MethodID, + self.SourceID, self.QualityControlLevelID) + odm_id = property(get_odm_id) + def __hash__(self): return hash((self.SiteCode, self.VariableCode, self.MethodID, self.SourceID, self.QualityControlLevelCode)) @@ -94,7 +99,7 @@ def CreateH2OSeriesFromOdmSeries(series): if series is None: return None else: - return H2OSeries(SeriesID=series.id, SiteID=series.site_id, VariableID=series.variable_id, + return H2OSeries(SeriesID=series.odm_id, SiteID=series.site_id, VariableID=series.variable_id, MethodID=series.method_id, SourceID=series.source_id, VariableCode=series.variable_code, QualityControlLevelID=series.quality_control_level_id, SiteCode=series.site_code, QualityControlLevelCode=series.quality_control_level_code) diff --git a/src/Utilities/H2OServices.py b/src/Utilities/H2OServices.py index d34dd93..26732e9 100644 --- a/src/Utilities/H2OServices.py +++ b/src/Utilities/H2OServices.py @@ -125,7 +125,6 @@ def _generate_datasets(self, resource=None): database_resource_dict[rsrc.odm_db_name].append(rsrc) - for db_dame in database_resource_dict.keys(): self._thread_checkpoint() @@ -403,6 +402,7 @@ def CreateResourceFromTemplate(self, template): return self.ActiveHydroshare.createNewResource(template) + class H2OLogger: def __init__(self, logfile_dir=None, log_to_gui=False): if logfile_dir is None: diff --git a/src/utility-scripts/patch_series_id.py b/src/utility-scripts/patch_series_id.py new file mode 100644 index 0000000..9c8cebe --- /dev/null +++ b/src/utility-scripts/patch_series_id.py @@ -0,0 +1,18 @@ +from Utilities.H2OServices import H2OService + +service = H2OService() +service.LoadData() + +print('- Starting operation') +for resource_id, managed_resource in service.ManagedResources.iteritems(): + print('- Working with resource {}'.format(resource_id)) + selected_series = managed_resource.selected_series + managed_resource.selected_series = {} + for old_series_id, series in selected_series.iteritems(): + print('-- Changing series id {} for new series id {}'.format(old_series_id, series.odm_id)) + managed_resource.selected_series[series.odm_id] = series + series.SeriesID = series.odm_id + +print('- Writing new operations file with new series ids') +service.SaveData() +print('DONE!')