Skip to content

Commit

Permalink
Expand use of list query parameters for endpoint keys (#499)
Browse files Browse the repository at this point in the history
* Add MPID list query parameters to endpoints

* Add tests + lint
  • Loading branch information
Jason Munro authored Aug 11, 2022
1 parent e5cd614 commit f84f446
Show file tree
Hide file tree
Showing 14 changed files with 180 additions and 211 deletions.
6 changes: 3 additions & 3 deletions emmet-api/emmet/api/routes/bonds/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
BondLengthQuery,
CoordinationEnvsQuery,
)
from emmet.api.routes.materials.query_operators import MultiMaterialIDQuery
from emmet.api.core.settings import MAPISettings
from emmet.api.core.global_header import GlobalHeaderProcessor

Expand All @@ -16,13 +17,12 @@ def bonds_resource(bonds_store):
bonds_store,
BondingDoc,
query_operators=[
MultiMaterialIDQuery(),
BondLengthQuery(),
CoordinationEnvsQuery(),
SortQuery(),
PaginationQuery(),
SparseFieldsQuery(
BondingDoc, default_fields=["material_id", "last_updated"],
),
SparseFieldsQuery(BondingDoc, default_fields=["material_id", "last_updated"],),
],
header_processor=GlobalHeaderProcessor(),
tags=["Bonds"],
Expand Down
6 changes: 3 additions & 3 deletions emmet-api/emmet/api/routes/dielectric/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from maggma.api.query_operator import PaginationQuery, SortQuery, SparseFieldsQuery
from emmet.api.routes.dielectric.query_operators import DielectricQuery
from emmet.api.routes.materials.query_operators import MultiMaterialIDQuery
from emmet.api.core.global_header import GlobalHeaderProcessor
from emmet.api.core.settings import MAPISettings

Expand All @@ -12,12 +13,11 @@ def dielectric_resource(dielectric_store):
dielectric_store,
DielectricDoc,
query_operators=[
MultiMaterialIDQuery(),
DielectricQuery(),
SortQuery(),
PaginationQuery(),
SparseFieldsQuery(
DielectricDoc, default_fields=["material_id", "last_updated"]
),
SparseFieldsQuery(DielectricDoc, default_fields=["material_id", "last_updated"]),
],
header_processor=GlobalHeaderProcessor(),
tags=["Dielectric"],
Expand Down
125 changes: 69 additions & 56 deletions emmet-api/emmet/api/routes/electrodes/query_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,10 @@ class ElectrodeElementsQuery(QueryOperator):
def query(
self,
elements: Optional[str] = Query(
None,
description="Query by elements in the material composition as a comma-separated list",
None, description="Query by elements in the material composition as a comma-separated list",
),
exclude_elements: Optional[str] = Query(
None,
description="Query by excluded elements in the material composition as a comma-separated list",
None, description="Query by excluded elements in the material composition as a comma-separated list",
),
) -> STORE_PARAMS:

Expand All @@ -98,24 +96,18 @@ def query(
element_list = [Element(e) for e in elements.strip().split(",")]
except ValueError:
raise HTTPException(
status_code=400,
detail="Problem processing one or more provided elements.",
status_code=400, detail="Problem processing one or more provided elements.",
)
crit["entries_composition_summary.all_elements"]["$all"] = [
str(el) for el in element_list
]
crit["entries_composition_summary.all_elements"]["$all"] = [str(el) for el in element_list]

if exclude_elements:
try:
element_list = [Element(e) for e in exclude_elements.strip().split(",")]
except ValueError:
raise HTTPException(
status_code=400,
detail="Problem processing one or more provided elements.",
status_code=400, detail="Problem processing one or more provided elements.",
)
crit["entries_composition_summary.all_elements"]["$nin"] = [
str(el) for el in element_list
]
crit["entries_composition_summary.all_elements"]["$nin"] = [str(el) for el in element_list]

return {"criteria": crit}

Expand All @@ -128,36 +120,28 @@ class VoltageStepQuery(QueryOperator):
def query(
self,
delta_volume_max: Optional[float] = Query(
None,
description="Maximum value for the max volume change in percent for a particular voltage step.",
None, description="Maximum value for the max volume change in percent for a particular voltage step.",
),
delta_volume_min: Optional[float] = Query(
None,
description="Minimum value for the max volume change in percent for a particular voltage step.",
None, description="Minimum value for the max volume change in percent for a particular voltage step.",
),
average_voltage_max: Optional[float] = Query(
None,
description="Maximum value for the average voltage for a particular voltage step in V.",
None, description="Maximum value for the average voltage for a particular voltage step in V.",
),
average_voltage_min: Optional[float] = Query(
None,
description="Minimum value for the average voltage for a particular voltage step in V.",
None, description="Minimum value for the average voltage for a particular voltage step in V.",
),
max_voltage_max: Optional[float] = Query(
None,
description="Maximum value for the maximum voltage for a particular voltage step in V.",
None, description="Maximum value for the maximum voltage for a particular voltage step in V.",
),
max_voltage_min: Optional[float] = Query(
None,
description="Minimum value for the maximum voltage for a particular voltage step in V.",
None, description="Minimum value for the maximum voltage for a particular voltage step in V.",
),
min_voltage_max: Optional[float] = Query(
None,
description="Maximum value for the minimum voltage for a particular voltage step in V.",
None, description="Maximum value for the minimum voltage for a particular voltage step in V.",
),
min_voltage_min: Optional[float] = Query(
None,
description="Minimum value for the minimum voltage for a particular voltage step in V.",
None, description="Minimum value for the minimum voltage for a particular voltage step in V.",
),
capacity_grav_max: Optional[float] = Query(
None, description="Maximum value for the gravimetric capacity in maH/g.",
Expand All @@ -172,36 +156,28 @@ def query(
None, description="Minimum value for the volumetric capacity in maH/cc.",
),
energy_grav_max: Optional[float] = Query(
None,
description="Maximum value for the gravimetric energy (specific energy) in Wh/kg.",
None, description="Maximum value for the gravimetric energy (specific energy) in Wh/kg.",
),
energy_grav_min: Optional[float] = Query(
None,
description="Minimum value for the gravimetric energy (specific energy) in Wh/kg.",
None, description="Minimum value for the gravimetric energy (specific energy) in Wh/kg.",
),
energy_vol_max: Optional[float] = Query(
None,
description="Maximum value for the volumetric energy (energy_density) in Wh/l.",
None, description="Maximum value for the volumetric energy (energy_density) in Wh/l.",
),
energy_vol_min: Optional[float] = Query(
None,
description="Minimum value for the volumetric energy (energy_density) in Wh/l.",
None, description="Minimum value for the volumetric energy (energy_density) in Wh/l.",
),
fracA_charge_max: Optional[float] = Query(
None,
description="Maximum value for the atomic fraction of the working ion in the charged state.",
None, description="Maximum value for the atomic fraction of the working ion in the charged state.",
),
fracA_charge_min: Optional[float] = Query(
None,
description="Minimum value for the atomic fraction of the working ion in the charged state.",
None, description="Minimum value for the atomic fraction of the working ion in the charged state.",
),
fracA_discharge_max: Optional[float] = Query(
None,
description="Maximum value for the atomic fraction of the working ion in the discharged state.",
None, description="Maximum value for the atomic fraction of the working ion in the discharged state.",
),
fracA_discharge_min: Optional[float] = Query(
None,
description="Minimum value for the atomic fraction of the working ion in the discharged state.",
None, description="Minimum value for the atomic fraction of the working ion in the discharged state.",
),
) -> STORE_PARAMS:

Expand Down Expand Up @@ -248,20 +224,16 @@ class InsertionVoltageStepQuery(QueryOperator):
def query(
self,
stability_charge_max: Optional[float] = Query(
None,
description="The maximum value of the energy above hull of the charged material.",
None, description="The maximum value of the energy above hull of the charged material.",
),
stability_charge_min: Optional[float] = Query(
None,
description="The minimum value of the energy above hull of the charged material.",
None, description="The minimum value of the energy above hull of the charged material.",
),
stability_discharge_max: Optional[float] = Query(
None,
description="The maximum value of the energy above hull of the discharged material.",
None, description="The maximum value of the energy above hull of the discharged material.",
),
stability_discharge_min: Optional[float] = Query(
None,
description="The minimum value of the energy above hull of the discharged material.",
None, description="The minimum value of the energy above hull of the discharged material.",
),
) -> STORE_PARAMS:

Expand Down Expand Up @@ -300,8 +272,7 @@ class WorkingIonQuery(QueryOperator):
def query(
self,
working_ion: Optional[str] = Query(
None,
title="Element of the working ion, or comma-delimited string list of working ion elements.",
None, title="Element of the working ion, or comma-delimited string list of working ion elements.",
),
) -> STORE_PARAMS:

Expand All @@ -318,3 +289,45 @@ def query(

def ensure_indexes(self): # pragma: no cover
return [("working_ion", False)]


class ElectrodeMultiMaterialIDQuery(QueryOperator):
"""
Method to generate a query for different root-level material_id values
"""

def query(
self,
material_ids: Optional[str] = Query(None, description="Comma-separated list of material_id values to query on"),
) -> STORE_PARAMS:

crit = {} # type: dict

if material_ids:
crit.update({"material_ids": {"$in": [material_id.strip() for material_id in material_ids.split(",")]}})

return {"criteria": crit}


class MultiBatteryIDQuery(QueryOperator):
"""
Method to generate a query for different root-level battery_id values
"""

def query(
self,
battery_ids: Optional[str] = Query(None, description="Comma-separated list of battery_id values to query on"),
) -> STORE_PARAMS:

crit = {} # type: dict

if battery_ids:

battery_id_list = [material_id.strip() for material_id in battery_ids.split(",")]

if len(battery_id_list) == 1:
crit.update({"battery_id": battery_id_list[0]})
else:
crit.update({"battery_id": {"$in": battery_id_list}})

return {"criteria": crit}
4 changes: 4 additions & 0 deletions emmet-api/emmet/api/routes/electrodes/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
ElectrodeElementsQuery,
ElectrodesChemsysQuery,
WorkingIonQuery,
ElectrodeMultiMaterialIDQuery,
MultiBatteryIDQuery
)

from emmet.api.core.settings import MAPISettings
Expand All @@ -19,6 +21,8 @@ def insertion_electrodes_resource(insertion_electrodes_store):
insertion_electrodes_store,
InsertionElectrodeDoc,
query_operators=[
MultiBatteryIDQuery(),
ElectrodeMultiMaterialIDQuery(),
ElectrodeFormulaQuery(),
ElectrodesChemsysQuery(),
WorkingIonQuery(),
Expand Down
2 changes: 2 additions & 0 deletions emmet-api/emmet/api/routes/electronic_structure/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
FormulaQuery,
ChemsysQuery,
DeprecationQuery,
MultiMaterialIDQuery
)

from emmet.api.routes.electronic_structure.query_operators import (
Expand All @@ -28,6 +29,7 @@ def es_resource(es_store):
es_store,
ElectronicStructureDoc,
query_operators=[
MultiMaterialIDQuery(),
ESSummaryDataQuery(),
FormulaQuery(),
ChemsysQuery(),
Expand Down
8 changes: 4 additions & 4 deletions emmet-api/emmet/api/routes/magnetism/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from maggma.api.query_operator import PaginationQuery, SortQuery, SparseFieldsQuery
from emmet.api.routes.magnetism.query_operators import MagneticQuery
from emmet.api.routes.materials.query_operators import MultiMaterialIDQuery
from emmet.api.core.global_header import GlobalHeaderProcessor
from emmet.api.core.settings import MAPISettings

Expand All @@ -12,17 +13,16 @@ def magnetism_resource(magnetism_store):
magnetism_store,
MagnetismDoc,
query_operators=[
MultiMaterialIDQuery(),
MagneticQuery(),
SortQuery(),
PaginationQuery(),
SparseFieldsQuery(
MagnetismDoc, default_fields=["material_id", "last_updated"]
),
SparseFieldsQuery(MagnetismDoc, default_fields=["material_id", "last_updated"]),
],
header_processor=GlobalHeaderProcessor(),
tags=["Magnetism"],
disable_validation=True,
timeout=MAPISettings().TIMEOUT
timeout=MAPISettings().TIMEOUT,
)

return resource
Loading

0 comments on commit f84f446

Please sign in to comment.