From 2aa285e97573bfc418564875a0c792894247dd8b Mon Sep 17 00:00:00 2001 From: Travis Rivers <trivers@contractor.usgs.gov> Date: Tue, 3 Nov 2020 14:47:38 -0700 Subject: [PATCH 01/10] update query --- geomagio/api/db/metadata_table.py | 9 +++++++++ geomagio/api/secure/MetadataQuery.py | 4 +++- geomagio/api/secure/metadata.py | 4 ++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/geomagio/api/db/metadata_table.py b/geomagio/api/db/metadata_table.py index 9b1571a2..a605012d 100644 --- a/geomagio/api/db/metadata_table.py +++ b/geomagio/api/db/metadata_table.py @@ -102,6 +102,8 @@ async def get_metadata( category: MetadataCategory = None, starttime: datetime = None, endtime: datetime = None, + created_after: datetime = None, + created_before: datetime = None, data_valid: bool = None, metadata_valid: bool = None, ): @@ -110,6 +112,7 @@ async def get_metadata( query = query.where(metadata.c.id == id) if category: query = query.where(metadata.c.category == category) + if network or station or channel or location: query = ( query.where(metadata.c.network.like(network or "%")) @@ -125,6 +128,12 @@ async def get_metadata( query = query.where( or_(metadata.c.starttime == None, metadata.c.starttime < endtime) ) + + if created_before and created_after: + query = query.where( + or_(metadata.c.created_time.between(created_after, created_before)) + ) + if data_valid is not None: query = query.where(metadata.c.data_valid == data_valid) if metadata_valid is not None: diff --git a/geomagio/api/secure/MetadataQuery.py b/geomagio/api/secure/MetadataQuery.py index 629ad014..b2f902e1 100644 --- a/geomagio/api/secure/MetadataQuery.py +++ b/geomagio/api/secure/MetadataQuery.py @@ -12,6 +12,8 @@ class MetadataQuery(BaseModel): category: MetadataCategory = None starttime: UTCDateTime = None endtime: UTCDateTime = None + created_after: UTCDateTime = None + created_before: UTCDateTime = None network: str = None station: str = None channel: str = None @@ -21,7 +23,7 @@ class MetadataQuery(BaseModel): def datetime_dict(self, **kwargs): values = self.dict(**kwargs) - for key in ["starttime", "endtime"]: + for key in ["starttime", "endtime", "created_after", "created_before"]: if key in values and values[key] is not None: values[key] = values[key].datetime.replace(tzinfo=timezone.utc) return values diff --git a/geomagio/api/secure/metadata.py b/geomagio/api/secure/metadata.py index fb0dfc4c..875aad44 100644 --- a/geomagio/api/secure/metadata.py +++ b/geomagio/api/secure/metadata.py @@ -51,6 +51,8 @@ async def get_metadata( category: MetadataCategory = None, starttime: UTCDateTime = None, endtime: UTCDateTime = None, + created_after: UTCDateTime = None, + created_before: UTCDateTime = None, network: str = None, station: str = None, channel: str = None, @@ -62,6 +64,8 @@ async def get_metadata( category=category, starttime=starttime, endtime=endtime, + created_after=created_after, + created_before=created_before, network=network, station=station, channel=channel, -- GitLab From 915b3c32fcfa13c35655c0b2c7252fa5cbc6c2bc Mon Sep 17 00:00:00 2001 From: Travis Rivers <trivers@contractor.usgs.gov> Date: Thu, 5 Nov 2020 18:57:54 -0700 Subject: [PATCH 02/10] change created parameter behavior --- geomagio/api/db/metadata_table.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/geomagio/api/db/metadata_table.py b/geomagio/api/db/metadata_table.py index a605012d..116b77ca 100644 --- a/geomagio/api/db/metadata_table.py +++ b/geomagio/api/db/metadata_table.py @@ -112,7 +112,6 @@ async def get_metadata( query = query.where(metadata.c.id == id) if category: query = query.where(metadata.c.category == category) - if network or station or channel or location: query = ( query.where(metadata.c.network.like(network or "%")) @@ -128,12 +127,10 @@ async def get_metadata( query = query.where( or_(metadata.c.starttime == None, metadata.c.starttime < endtime) ) - - if created_before and created_after: - query = query.where( - or_(metadata.c.created_time.between(created_after, created_before)) - ) - + if created_after: + query = query.where(metadata.c.starttime > created_after) + if created_before: + query = query = query.where(metadata.c.endtime < created_before) if data_valid is not None: query = query.where(metadata.c.data_valid == data_valid) if metadata_valid is not None: -- GitLab From ea7c6ad1abdd8f8cd5ea710c0b33f71368ed13a1 Mon Sep 17 00:00:00 2001 From: Travis Rivers <trivers@contractor.usgs.gov> Date: Fri, 6 Nov 2020 16:51:09 -0700 Subject: [PATCH 03/10] fix created time --- geomagio/api/db/metadata_table.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/geomagio/api/db/metadata_table.py b/geomagio/api/db/metadata_table.py index 116b77ca..9d32384b 100644 --- a/geomagio/api/db/metadata_table.py +++ b/geomagio/api/db/metadata_table.py @@ -128,9 +128,9 @@ async def get_metadata( or_(metadata.c.starttime == None, metadata.c.starttime < endtime) ) if created_after: - query = query.where(metadata.c.starttime > created_after) + query = query.where(metadata.c.created_time > created_after) if created_before: - query = query = query.where(metadata.c.endtime < created_before) + query = query = query.where(metadata.c.created_time < created_before) if data_valid is not None: query = query.where(metadata.c.data_valid == data_valid) if metadata_valid is not None: -- GitLab From d34db5ee93ce7b68f0ab47ef9190847233dfab43 Mon Sep 17 00:00:00 2001 From: Travis Rivers <trivers@contractor.usgs.gov> Date: Mon, 9 Nov 2020 17:08:52 -0700 Subject: [PATCH 04/10] separate sncl query parameters --- geomagio/api/db/metadata_table.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/geomagio/api/db/metadata_table.py b/geomagio/api/db/metadata_table.py index 9d32384b..4ac0454f 100644 --- a/geomagio/api/db/metadata_table.py +++ b/geomagio/api/db/metadata_table.py @@ -108,17 +108,18 @@ async def get_metadata( metadata_valid: bool = None, ): query = metadata.select() - if id is not None: + if id: query = query.where(metadata.c.id == id) if category: query = query.where(metadata.c.category == category) - if network or station or channel or location: - query = ( - query.where(metadata.c.network.like(network or "%")) - .where(metadata.c.station.like(station or "%")) - .where(metadata.c.channel.like(channel or "%")) - .where(metadata.c.location.like(location or "%")) - ) + if network: + query = query.where(metadata.c.network == network) + if station: + query = query.where(metadata.c.station.like(station or "%")) + if channel: + query = query.where(metadata.c.channel.like(channel or "%")) + if location: + query = query.where(metadata.c.location.like(location or "%")) if starttime: query = query.where( or_(metadata.c.endtime == None, metadata.c.endtime > starttime) @@ -135,6 +136,8 @@ async def get_metadata( query = query.where(metadata.c.data_valid == data_valid) if metadata_valid is not None: query = query.where(metadata.c.metadata_valid == metadata_valid) + print(query) + print(network) rows = await database.fetch_all(query) return [Metadata(**row) for row in rows] -- GitLab From af6620919f0970b6e005ddfdde29c27192abec82 Mon Sep 17 00:00:00 2001 From: Travis Rivers <trivers@contractor.usgs.gov> Date: Tue, 17 Nov 2020 09:49:03 -0700 Subject: [PATCH 05/10] remove print statements --- geomagio/api/db/metadata_table.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/geomagio/api/db/metadata_table.py b/geomagio/api/db/metadata_table.py index 4ac0454f..05e5cfe4 100644 --- a/geomagio/api/db/metadata_table.py +++ b/geomagio/api/db/metadata_table.py @@ -136,8 +136,7 @@ async def get_metadata( query = query.where(metadata.c.data_valid == data_valid) if metadata_valid is not None: query = query.where(metadata.c.metadata_valid == metadata_valid) - print(query) - print(network) + rows = await database.fetch_all(query) return [Metadata(**row) for row in rows] -- GitLab From 06facc37b898e3aea386890c8dfa8b1f050ef3e7 Mon Sep 17 00:00:00 2001 From: Travis Rivers <trivers@contractor.usgs.gov> Date: Tue, 17 Nov 2020 12:09:53 -0700 Subject: [PATCH 06/10] add to json method for Metadata class --- geomagio/api/secure/metadata.py | 5 +++-- geomagio/metadata/Metadata.py | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/geomagio/api/secure/metadata.py b/geomagio/api/secure/metadata.py index 875aad44..3592e48b 100644 --- a/geomagio/api/secure/metadata.py +++ b/geomagio/api/secure/metadata.py @@ -35,8 +35,9 @@ async def create_metadata( metadata: Metadata, user: User = Depends(require_user()), ): - metadata = await metadata_table.create_metadata(metadata) - return Response(metadata, status_code=201, media_type="application/json") + created_metadata = await metadata_table.create_metadata(metadata) + print(metadata.to_json()) + return Response(metadata.to_json(), status_code=201, media_type="application/json") @router.delete("/metadata/{id}") diff --git a/geomagio/metadata/Metadata.py b/geomagio/metadata/Metadata.py index ce2ad521..eb8f5b8f 100644 --- a/geomagio/metadata/Metadata.py +++ b/geomagio/metadata/Metadata.py @@ -1,4 +1,5 @@ from datetime import timezone +import json from typing import Dict from obspy import UTCDateTime @@ -87,3 +88,6 @@ class Metadata(BaseModel): @validator("created_time") def set_default_created_time(cls, created_time: UTCDateTime = None) -> UTCDateTime: return created_time or UTCDateTime() + + def to_json(self): + return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4) -- GitLab From 242c49bc6c6aafdc263fa8ef2c8d325f361bf457 Mon Sep 17 00:00:00 2001 From: Travis Rivers <trivers@contractor.usgs.gov> Date: Tue, 17 Nov 2020 12:24:01 -0700 Subject: [PATCH 07/10] remove custom to_json method, use builtin json --- geomagio/api/db/metadata_table.py | 2 +- geomagio/api/secure/metadata.py | 3 +-- geomagio/metadata/Metadata.py | 4 ---- 3 files changed, 2 insertions(+), 7 deletions(-) diff --git a/geomagio/api/db/metadata_table.py b/geomagio/api/db/metadata_table.py index 05e5cfe4..f1286420 100644 --- a/geomagio/api/db/metadata_table.py +++ b/geomagio/api/db/metadata_table.py @@ -131,7 +131,7 @@ async def get_metadata( if created_after: query = query.where(metadata.c.created_time > created_after) if created_before: - query = query = query.where(metadata.c.created_time < created_before) + query = query.where(metadata.c.created_time < created_before) if data_valid is not None: query = query.where(metadata.c.data_valid == data_valid) if metadata_valid is not None: diff --git a/geomagio/api/secure/metadata.py b/geomagio/api/secure/metadata.py index 3592e48b..d6912104 100644 --- a/geomagio/api/secure/metadata.py +++ b/geomagio/api/secure/metadata.py @@ -36,8 +36,7 @@ async def create_metadata( user: User = Depends(require_user()), ): created_metadata = await metadata_table.create_metadata(metadata) - print(metadata.to_json()) - return Response(metadata.to_json(), status_code=201, media_type="application/json") + return Response(metadata.json(), status_code=201, media_type="application/json") @router.delete("/metadata/{id}") diff --git a/geomagio/metadata/Metadata.py b/geomagio/metadata/Metadata.py index eb8f5b8f..ce2ad521 100644 --- a/geomagio/metadata/Metadata.py +++ b/geomagio/metadata/Metadata.py @@ -1,5 +1,4 @@ from datetime import timezone -import json from typing import Dict from obspy import UTCDateTime @@ -88,6 +87,3 @@ class Metadata(BaseModel): @validator("created_time") def set_default_created_time(cls, created_time: UTCDateTime = None) -> UTCDateTime: return created_time or UTCDateTime() - - def to_json(self): - return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4) -- GitLab From 445af4a2d7b69cbc4d8221014f4a13c96273a1ad Mon Sep 17 00:00:00 2001 From: Travis Rivers <trivers@contractor.usgs.gov> Date: Tue, 17 Nov 2020 15:14:28 -0700 Subject: [PATCH 08/10] fix create_metadata method, clean up --- geomagio/api/db/metadata_table.py | 5 ++--- geomagio/api/secure/MetadataQuery.py | 2 +- geomagio/api/secure/metadata.py | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/geomagio/api/db/metadata_table.py b/geomagio/api/db/metadata_table.py index f1286420..6a7c1f17 100644 --- a/geomagio/api/db/metadata_table.py +++ b/geomagio/api/db/metadata_table.py @@ -83,8 +83,8 @@ async def create_metadata(meta: Metadata) -> Metadata: query = metadata.insert() values = meta.datetime_dict(exclude={"id"}, exclude_none=True) query = query.values(**values) - metadata.id = await database.execute(query) - return metadata + meta.id = await database.execute(query) + return meta async def delete_metadata(id: int) -> None: @@ -136,7 +136,6 @@ async def get_metadata( query = query.where(metadata.c.data_valid == data_valid) if metadata_valid is not None: query = query.where(metadata.c.metadata_valid == metadata_valid) - rows = await database.fetch_all(query) return [Metadata(**row) for row in rows] diff --git a/geomagio/api/secure/MetadataQuery.py b/geomagio/api/secure/MetadataQuery.py index b2f902e1..72955653 100644 --- a/geomagio/api/secure/MetadataQuery.py +++ b/geomagio/api/secure/MetadataQuery.py @@ -23,7 +23,7 @@ class MetadataQuery(BaseModel): def datetime_dict(self, **kwargs): values = self.dict(**kwargs) - for key in ["starttime", "endtime", "created_after", "created_before"]: + for key in ["endtime", "created_after", "created_before"]: if key in values and values[key] is not None: values[key] = values[key].datetime.replace(tzinfo=timezone.utc) return values diff --git a/geomagio/api/secure/metadata.py b/geomagio/api/secure/metadata.py index d6912104..7126a1ac 100644 --- a/geomagio/api/secure/metadata.py +++ b/geomagio/api/secure/metadata.py @@ -35,7 +35,7 @@ async def create_metadata( metadata: Metadata, user: User = Depends(require_user()), ): - created_metadata = await metadata_table.create_metadata(metadata) + metadata = await metadata_table.create_metadata(metadata) return Response(metadata.json(), status_code=201, media_type="application/json") -- GitLab From 3ecbaefbb192ddfdb4c701983616e046712ea157 Mon Sep 17 00:00:00 2001 From: Travis Rivers <trivers@contractor.usgs.gov> Date: Tue, 17 Nov 2020 15:33:40 -0700 Subject: [PATCH 09/10] remove default starttime --- geomagio/api/secure/MetadataQuery.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/geomagio/api/secure/MetadataQuery.py b/geomagio/api/secure/MetadataQuery.py index 72955653..1d92c67d 100644 --- a/geomagio/api/secure/MetadataQuery.py +++ b/geomagio/api/secure/MetadataQuery.py @@ -23,11 +23,7 @@ class MetadataQuery(BaseModel): def datetime_dict(self, **kwargs): values = self.dict(**kwargs) - for key in ["endtime", "created_after", "created_before"]: + for key in ["starttime", "endtime", "created_after", "created_before"]: if key in values and values[key] is not None: values[key] = values[key].datetime.replace(tzinfo=timezone.utc) return values - - @validator("starttime") - def set_default_starttime(cls, starttime: UTCDateTime = None) -> UTCDateTime: - return starttime or UTCDateTime() - 30 * 86400 -- GitLab From c32bb4b69a2015376d514e75dcee2860aac96874 Mon Sep 17 00:00:00 2001 From: Jeremy M Fee <jmfee@usgs.gov> Date: Wed, 25 Nov 2020 17:17:05 +0000 Subject: [PATCH 10/10] Remove default values for station/channel/location They are only used if truthy. --- geomagio/api/db/metadata_table.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/geomagio/api/db/metadata_table.py b/geomagio/api/db/metadata_table.py index 6a7c1f17..367e5936 100644 --- a/geomagio/api/db/metadata_table.py +++ b/geomagio/api/db/metadata_table.py @@ -115,11 +115,11 @@ async def get_metadata( if network: query = query.where(metadata.c.network == network) if station: - query = query.where(metadata.c.station.like(station or "%")) + query = query.where(metadata.c.station == station) if channel: - query = query.where(metadata.c.channel.like(channel or "%")) + query = query.where(metadata.c.channel.like(channel)) if location: - query = query.where(metadata.c.location.like(location or "%")) + query = query.where(metadata.c.location.like(location)) if starttime: query = query.where( or_(metadata.c.endtime == None, metadata.c.endtime > starttime) -- GitLab