From f98589290b27c09df9a760ec995568ea25c6ae4c Mon Sep 17 00:00:00 2001 From: spencer <swilbur@usgs.gov> Date: Thu, 26 Sep 2024 09:02:57 -0600 Subject: [PATCH] I removed an unecessary call to FilterApiQuery from within Data.py. I also added validation to the FilterApiQuery.py. Rather than using super I opted to add the validation to this file beacuse sampling_period was ignored from the base class i.e. DataApiQuery and this is why the REQUEST_LIMIT was being ignored. --- geomagio/api/ws/DataApiQuery.py | 12 +----------- geomagio/api/ws/FilterApiQuery.py | 26 +++++++++++++++++++++++++- geomagio/api/ws/data.py | 1 - 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/geomagio/api/ws/DataApiQuery.py b/geomagio/api/ws/DataApiQuery.py index a1716691..6cba83df 100644 --- a/geomagio/api/ws/DataApiQuery.py +++ b/geomagio/api/ws/DataApiQuery.py @@ -12,7 +12,7 @@ from .Observatory import OBSERVATORY_INDEX, ASL_OBSERVATORY_INDEX DEFAULT_ELEMENTS = ["X", "Y", "Z", "F"] -REQUEST_LIMIT = 345600 +REQUEST_LIMIT = 3456000 # Increased the request limit by 10x what was decided by Jeremy VALID_ELEMENTS = [e.id for e in ELEMENTS] @@ -149,13 +149,3 @@ class DataApiQuery(BaseModel): raise ValueError(f"Request exceeds limit ({samples} > {REQUEST_LIMIT})") # otherwise okay return values - - -# The new class inheriting everything except input/output_sampling period from DataApiQuery -class FilterDataApiQuery(DataApiQuery): - input_sampling_period: SamplingPeriod = SamplingPeriod.SECOND - output_sampling_period: SamplingPeriod = SamplingPeriod.MINUTE - - # Remove inherited fields that we don't need - class Config: - fields = {"sampling_period": {"exclude": True}, "data_host": {"exclude": True}} diff --git a/geomagio/api/ws/FilterApiQuery.py b/geomagio/api/ws/FilterApiQuery.py index 2eb960ac..dcae69ac 100644 --- a/geomagio/api/ws/FilterApiQuery.py +++ b/geomagio/api/ws/FilterApiQuery.py @@ -1,4 +1,5 @@ -from .DataApiQuery import DataApiQuery, SamplingPeriod +from .DataApiQuery import DataApiQuery, SamplingPeriod, REQUEST_LIMIT +from pydantic import root_validator """This script contains the class inheriting everything except input/output_sampling period from the DataApiQuery class. This is where more specific functionailty @@ -12,3 +13,26 @@ class FilterApiQuery(DataApiQuery): # Remove inherited fields that we don't need for this specific endpoint class Config: fields = {"sampling_period": {"exclude": True}} + + @root_validator + def validate_combinations(cls, values): + starttime, endtime, elements, format, input_sampling_period = ( + values.get("starttime"), + values.get("endtime"), + values.get("elements"), + values.get("format"), + values.get("input_sampling_period"), + ) + if len(elements) > 4 and format == "iaga2002": + raise ValueError("No more than four elements allowed for iaga2002 format.") + if starttime > endtime: + raise ValueError("Starttime must be before endtime.") + + # Calculate the number of samples based on the input sampling period + samples = int(len(elements) * (endtime - starttime) / input_sampling_period) + + # Validate the request size + if samples > REQUEST_LIMIT: + raise ValueError(f"Request exceeds limit ({samples} > {REQUEST_LIMIT})") + + return values diff --git a/geomagio/api/ws/data.py b/geomagio/api/ws/data.py index 3cde0746..40a09da6 100644 --- a/geomagio/api/ws/data.py +++ b/geomagio/api/ws/data.py @@ -13,7 +13,6 @@ from .Observatory import ASL_OBSERVATORY_INDEX from .DataApiQuery import ( DEFAULT_ELEMENTS, DataApiQuery, - FilterDataApiQuery, DataType, OutputFormat, SamplingPeriod, -- GitLab