Skip to content

Commit ba911ff

Browse files
author
sreeder
committed
update queries and add equipment queries
1 parent 6047f1b commit ba911ff

File tree

2 files changed

+97
-87
lines changed

2 files changed

+97
-87
lines changed

odm2api/ODM2/services/readService.py

Lines changed: 95 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,19 @@ def getAnnotations(self, type=None):
5555
#TODO What keywords do I use for type
5656
a = Annotations
5757
if type =="action": a=ActionAnnotations
58-
elif type =="categoricalResultValue": a=CategoricalResultValueAnnotations
59-
elif type =="equipmentAnnotation": a=EquipmentAnnotations
60-
elif type =="measurementResultValue": a=MeasurementResultValueAnnotations
58+
elif type =="categoricalresultvalue": a=CategoricalResultValueAnnotations
59+
elif type =="equipmentannotation": a=EquipmentAnnotations
60+
elif type =="measurementresultvalue": a=MeasurementResultValueAnnotations
6161
elif type =="method": a=MethodAnnotations
62-
elif type =="pointCoverageResultValue": a=PointCoverageResultValueAnnotations
63-
elif type =="profileResultValue": a= ProfileResultValueAnnotations
62+
elif type =="pointcoverageresultvalue": a=PointCoverageResultValueAnnotations
63+
elif type =="profileresultvalue": a= ProfileResultValueAnnotations
6464
elif type =="result": a = ResultAnnotations
65-
elif type =="samplingFeature": a=SamplingFeatureAnnotations
66-
elif type =="sectionResultValue": a=SectionResultValueAnnotations
67-
elif type =="spectraResultValue": a=SpectraResultValueAnnotations
68-
elif type =="timeSeriesResultValue": a=TimeSeriesResultValueAnnotations
69-
elif type =="trajectoryResultValue": a= TrajectoryResultValueAnnotations
70-
elif type =="transectResultValue": a= TransectResultValueAnnotations
65+
elif type =="samplingfeature": a=SamplingFeatureAnnotations
66+
elif type =="sectionresultvalue": a=SectionResultValueAnnotations
67+
elif type =="spectraresultvalue": a=SpectraResultValueAnnotations
68+
elif type =="timeSeriesresultvalue": a=TimeSeriesResultValueAnnotations
69+
elif type =="trajectoryresultvalue": a= TrajectoryResultValueAnnotations
70+
elif type =="transectresultvalue": a= TransectResultValueAnnotations
7171
try:
7272
return self._session.query(a).all()
7373
except:
@@ -81,12 +81,12 @@ def getAnnotations(self, type=None):
8181
def getCVs(self, type):
8282

8383
CV = CVActionType
84-
if type == "ActionType": CV = CVActionType
85-
elif type == "Aggregation Statistic": CV = CVAggregationStatistic
86-
elif type == "Annotation Type": CV = CVAnnotationType
87-
elif type == "Censor Code": CV = CVCensorCode
88-
elif type == "Data Quality Type": CV = CVDataQualityType
89-
elif type == "Dataset Type": CV = CVDataSetType
84+
if type == "actiontype": CV = CVActionType
85+
elif type == "aggregationstatistic": CV = CVAggregationStatistic
86+
elif type == "annotationtype": CV = CVAnnotationType
87+
elif type == "censorcode": CV = CVCensorCode
88+
elif type == "dataqualitytype": CV = CVDataQualityType
89+
elif type == "dataset type": CV = CVDataSetType
9090
elif type == "Directive Type": CV = CVDirectiveType
9191
elif type == "Elevation Datum": CV = CVElevationDatum
9292
elif type == "Equipment Type": CV = CVEquipmentType
@@ -228,10 +228,18 @@ def getGeometryTest(self, TestGeom):
228228
Action
229229
"""
230230

231-
def getActions(self, id = None):
231+
def getActions(self, id = None, type=None, sfid=None):
232232

233233
q= self._session.query(Actions)
234234
if id: q= q.filter_by(ActionID=id)
235+
if type: q=q.filter_by(Actions.ActionTypeCV.ilike(type))
236+
if sfid:
237+
q=q.join(FeatureActions).filter(FeatureActions.SamplingFeatureID == sfid)
238+
# return self._session.query(Results) \
239+
# .join(FeatureActions) \
240+
# .join(Actions) \
241+
# .join(Simulations) \
242+
# .filter(Simulations.SimulationID == simulationid).all()
235243
try:
236244
return q.all()
237245
except:
@@ -329,11 +337,11 @@ def getResults(self, id=None, type=None):
329337
if type == "categorical": R = CategoricalResults
330338
# elif "countObservation": R=
331339
elif type == "measurement": R = MeasurementResults
332-
elif type == "pointCoverage":R = PointCoverageResults
340+
elif type == "pointcoverage":R = PointCoverageResults
333341
elif type == "profile": R = ProfileResults
334342
elif type == "section": R = SectionResults
335343
elif type == "spectra": R = SpectraResults
336-
elif type == "timeSeries": R = TimeSeriesResults
344+
elif type == "timeseries": R = TimeSeriesResults
337345
elif type == "trajectory": R = TrajectoryResults
338346
elif type == "transect": R = TransectResults
339347
# elif "truthObservation": R=
@@ -419,8 +427,16 @@ def getResultsDataQuality(self):
419427

420428
#ToDo get list of Equipment queries from Juan
421429
#TODO Equipment Schema Queries
422-
def getEquipment(self):
423-
return self._session.query(Equipment).all()
430+
def getEquipment(self, code=None, type = None , sfid=None, actionid=None):
431+
e = self._session.query(Equipment)
432+
if sfid: e= e.join(EquipmentUsed)\
433+
.join(Actions)\
434+
.join(FeatureActions)\
435+
.filter(FeatureActions.SamplingFeatureID == sfid)
436+
if actionid: e=e.join(EquipmentUsed).join(Actions)\
437+
.filter(Actions.ActionID == actionid)
438+
return e.all()
439+
424440
def CalibrationActions(self):
425441
return self._session.query(CalibrationActions).all()
426442
def CalibrationReferenceEquipment(self):
@@ -437,12 +453,17 @@ def EquipmentModels(self):
437453
return self._session.query(EquipmentModels).all()
438454
def EquipmentUsed(self):
439455
return self._session.query(EquipmentUsed).all()
440-
def InstrumentOutputVariables(self):
441-
return self._session.query(InstrumentOutputVariables).all()
456+
def InstrumentOutputVariables(self, modelid=None, variableid=None):
457+
i=self._session.query(InstrumentOutputVariables)
458+
if modelid: i=i.filter_by(ModelID=modelid)
459+
if variableid: i=i.filter_by(VariableID= variableid)
460+
return i.all()
442461
def MaintenanceActions(self):
443462
return self._session.query(MaintenanceActions).all()
444-
def RelatedEquipment(self):
445-
return self._session.query(RelatedEquipment).all()
463+
def RelatedEquipment(self, code=None):
464+
r=self._session.query(RelatedEquipment)
465+
if code: r=r.filter_by(EquipmentCode=code)
466+
return r.all()
446467

447468
# ################################################################################
448469
# Extension Properties
@@ -451,12 +472,12 @@ def RelatedEquipment(self):
451472
def getExtensionProperties(self, type=None):
452473
#Todo what values to use for extensionproperties type
453474
e = ExtensionProperties
454-
if type =="Action": e = ActionExtensionPropertyValues
455-
elif type =="Citation": e= CitationExtensionPropertyValues
456-
elif type =="Method": e= MethodExtensionPropertyValues
457-
elif type =="Result":e=ResultExtensionPropertyValues
458-
elif type =="SamplingFeature": e= SamplingFeatureExtensionPropertyValues
459-
elif type =="Variable": e=VariableExtensionPropertyValues
475+
if type =="action": e = ActionExtensionPropertyValues
476+
elif type =="citation": e= CitationExtensionPropertyValues
477+
elif type =="method": e= MethodExtensionPropertyValues
478+
elif type =="result":e=ResultExtensionPropertyValues
479+
elif type =="samplingfeature": e= SamplingFeatureExtensionPropertyValues
480+
elif type =="variable": e=VariableExtensionPropertyValues
460481
try:
461482
return self._session.query(e).all()
462483
except:
@@ -468,14 +489,14 @@ def getExtensionProperties(self, type=None):
468489
# ################################################################################
469490
def getExternalIdentifiers(self, type=None):
470491
e = ExternalIdentifierSystems
471-
if type =="Citation": e = CitationExternalIdentifiers
472-
elif type =="Method": e = MethodExternalIdentifiers
473-
elif type =="Person": e = PersonExternalIdentifiers
474-
elif type =="ReferenceMaterial": e = ReferenceMaterialExternalIdentifiers
475-
elif type =="SamplingFeature": e = SamplingFeatureExternalIdentifiers
476-
elif type =="SpatialReference": e = SpatialReferenceExternalIdentifiers
477-
elif type =="TaxonomicClassifier": e = TaxonomicClassifierExternalIdentifiers
478-
elif type =="Variable": e = VariableExternalIdentifiers
492+
if type.lowercase =="citation": e = CitationExternalIdentifiers
493+
elif type =="method": e = MethodExternalIdentifiers
494+
elif type =="person": e = PersonExternalIdentifiers
495+
elif type =="referencematerial": e = ReferenceMaterialExternalIdentifiers
496+
elif type =="samplingfeature": e = SamplingFeatureExternalIdentifiers
497+
elif type =="spatialreference": e = SpatialReferenceExternalIdentifiers
498+
elif type =="taxonomicclassifier": e = TaxonomicClassifierExternalIdentifiers
499+
elif type =="variable": e = VariableExternalIdentifiers
479500
try:
480501
return self._session.query(e).all()
481502
except:
@@ -485,6 +506,7 @@ def getExternalIdentifiers(self, type=None):
485506
# ################################################################################
486507
# Lab Analyses
487508
# ################################################################################
509+
#TODO functions for Lab Analyses
488510
def getDirectives(self):
489511
return self._session.query(Directives).all()
490512
def getActionDirectives(self):
@@ -498,8 +520,6 @@ def getSpecimenBatchPositions(self):
498520
# Provenance
499521
# ################################################################################
500522

501-
502-
503523
#TODO functions for Provenance
504524
def getAuthorLists(self):
505525
self._session.query(AuthorLists).all()
@@ -616,60 +636,59 @@ def getSpatialReferenceByCode(self, srsCode):
616636
return None
617637

618638

619-
# ################################################################################
620-
# Equipment
621-
# ################################################################################
622-
639+
# ################################################################################
640+
# Equipment
641+
# ################################################################################
623642

624643

625644

626-
# ################################################################################
627-
# Simulation
628-
# ################################################################################
629645

630-
def getAllModels(self):
646+
# ################################################################################
647+
# Simulation
648+
# ################################################################################
631649

650+
def getSimulations(self, name=None, actionid=None):
651+
s=self._session.query(Simulations)
652+
if name: s = s.filter(Simulations.SimulationName.ilike(name))
653+
if actionid: s= s.filter_by(ActionID=actionid)
632654
try:
633-
return self._session.query(Models).all()
655+
return s.all()
634656
except:
635657
return None
636658

637-
def getModelByCode(self, modelcode):
659+
def getResultsBySimulationID(self, simulationid):
638660
try:
639-
return self._session.query(Models).filter(Models.ModelCode.ilike(modelcode)).first()
640-
except:
661+
return self._session.query(Results) \
662+
.join(FeatureActions) \
663+
.join(Actions) \
664+
.join(Simulations) \
665+
.filter(Simulations.SimulationID == simulationid).all()
666+
except Exception, e:
667+
print e
641668
return None
642669

643-
def getAllSimulations(self):
644-
670+
def getModels(self, code= None):
671+
m= self._session.query(Models)
672+
if code: m = m.filter(Models.ModelCode.ilike(code))
645673
try:
646-
return self._session.query(Simulations).all()
674+
return m.all()
647675
except:
648676
return None
649677

650-
def getSimulationByName(self, simulationName):
651-
try:
652-
return self._session.query(Simulations).filter(Simulations.SimulationName.ilike(simulationName)).first()
653-
except:
654-
return None
655678

656-
def getSimulationByActionID(self, actionID):
657-
try:
658-
return self._session.query(Simulations).filter_by(ActionID=actionID).first()
659-
except:
660-
return None
679+
def getRelatedModels(self, id = None, code = None):
680+
681+
m=self._session.query(RelatedModels)
682+
if id: m= m.filter_by(RelatedModelID=id)
683+
# if code: m= m.filter_by(RelatedModels.)
661684

662-
def getRelatedModelsByID(self, modelid):
663-
"""
664-
queries the ODM2 for any models that have a relationship with the provided model id
665-
:param modelid: id of the model to search
666-
:return: all models related to the specified id
667-
"""
668685
try:
669-
return self._session.query(RelatedModels).filter_by(RelatedModelID=modelid).all()
686+
return m.all()
670687
except Exception, e:
671688
print e
672-
return None
689+
return None
690+
691+
673692

674693
def getRelatedModelsByCode(self, modelcode):
675694
"""
@@ -684,16 +703,7 @@ def getRelatedModelsByCode(self, modelcode):
684703
print e
685704
return None
686705

687-
def getResultsBySimulationID(self, simulationID):
688-
try:
689-
return self._session.query(Results) \
690-
.join(FeatureActions) \
691-
.join(Actions) \
692-
.join(Simulations) \
693-
.filter(Simulations.SimulationID == simulationID).all()
694-
except Exception, e:
695-
print e
696-
return None
706+
697707

698708

699709
# ################################################################################

tests/test_odm2/test_readservice.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def test_getAllModels(self):
9898
res = self.engine.execute('SELECT * FROM Models').fetchall()
9999

100100
# get all simulations using the api
101-
resapi = self.reader.getAllModels()
101+
resapi = self.reader.getModels()
102102

103103
assert len(res) == len(resapi)
104104

@@ -110,7 +110,7 @@ def test_getModelByCode(self):
110110

111111

112112
# get the model using the api
113-
resapi = self.reader.getModelByCode(modelcode=modelCode)
113+
resapi = self.reader.getModels(code=modelCode)
114114

115115
assert resapi is not None
116116

0 commit comments

Comments
 (0)