Skip to content

Commit

Permalink
Changed series ids to be an equivalent of the ODM composite id. Fixed…
Browse files Browse the repository at this point in the history
… small sorting issue with the resources list.
  • Loading branch information
jcaraballo17 committed Jun 26, 2020
1 parent 7378ff6 commit b809f57
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 20 deletions.
17 changes: 12 additions & 5 deletions src/GAMUTRawData/odmdata/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 "<Series('%s', '%s', '%s', '%s')>" % (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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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',
Expand Down
17 changes: 8 additions & 9 deletions src/GuiComponents/VisualH2OMainWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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()

Expand All @@ -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()

Expand Down
6 changes: 3 additions & 3 deletions src/GuiComponents/WxUtilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand Down
9 changes: 7 additions & 2 deletions src/Utilities/H2OSeries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/Utilities/H2OServices.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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:
Expand Down
18 changes: 18 additions & 0 deletions src/utility-scripts/patch_series_id.py
Original file line number Diff line number Diff line change
@@ -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!')

0 comments on commit b809f57

Please sign in to comment.