Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PB-1169 Search forecast properties #500

Merged
merged 8 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions app/stac_api/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
from django.urls import reverse
from django.utils.translation import gettext_lazy as _

from stac_api.models import BBOX_CH
from stac_api.models import Asset
from stac_api.models import AssetUpload
from stac_api.models import Collection
from stac_api.models import CollectionAsset
from stac_api.models import CollectionLink
from stac_api.models import Item
from stac_api.models import ItemLink
from stac_api.models import LandingPage
from stac_api.models import LandingPageLink
from stac_api.models import Provider
from stac_api.models.collection import Collection
from stac_api.models.collection import CollectionAsset
from stac_api.models.collection import CollectionLink
from stac_api.models.general import BBOX_CH
from stac_api.models.general import LandingPage
from stac_api.models.general import LandingPageLink
from stac_api.models.general import Provider
from stac_api.models.item import Asset
from stac_api.models.item import AssetUpload
from stac_api.models.item import Item
from stac_api.models.item import ItemLink
from stac_api.utils import build_asset_href
from stac_api.utils import get_query_params
from stac_api.validators import validate_href_url
Expand Down
2 changes: 1 addition & 1 deletion app/stac_api/management/commands/calculate_extent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.core.management.base import CommandParser
from django.db import connection

from stac_api.models import Collection
from stac_api.models.collection import Collection
from stac_api.utils import CommandHandler
from stac_api.utils import CustomBaseCommand

Expand Down
6 changes: 3 additions & 3 deletions app/stac_api/management/commands/dummy_asset_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from django.conf import settings
from django.core.management.base import BaseCommand

from stac_api.models import Asset
from stac_api.models import AssetUpload
from stac_api.models import BaseAssetUpload
from stac_api.models.general import BaseAssetUpload
from stac_api.models.item import Asset
from stac_api.models.item import AssetUpload
from stac_api.s3_multipart_upload import MultipartUpload
from stac_api.utils import AVAILABLE_S3_BUCKETS
from stac_api.utils import CommandHandler
Expand Down
6 changes: 3 additions & 3 deletions app/stac_api/management/commands/dummy_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.management.base import BaseCommand

from stac_api.models import Asset
from stac_api.models import Collection
from stac_api.models import Item
from stac_api.models.collection import Collection
from stac_api.models.item import Asset
from stac_api.models.item import Item
from stac_api.utils import CommandHandler
from stac_api.validators import MEDIA_TYPES

Expand Down
2 changes: 1 addition & 1 deletion app/stac_api/management/commands/list_asset_uploads.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.core.management.base import BaseCommand
from django.core.serializers.json import DjangoJSONEncoder

from stac_api.models import AssetUpload
from stac_api.models.item import AssetUpload
from stac_api.s3_multipart_upload import MultipartUpload
from stac_api.serializers.upload import AssetUploadSerializer
from stac_api.utils import CommandHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from rest_framework.request import Request
from rest_framework.test import APIRequestFactory

from stac_api.models import Item
from stac_api.models.item import Item
from stac_api.utils import CommandHandler

logger = logging.getLogger(__name__)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from rest_framework.test import APIRequestFactory

from stac_api.models import Item
from stac_api.models.item import Item
from stac_api.utils import CommandHandler

logger = logging.getLogger(__name__)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from rest_framework.test import APIRequestFactory

from stac_api.models import Item
from stac_api.models.item import Item
from stac_api.utils import CommandHandler

logger = logging.getLogger(__name__)
Expand Down
6 changes: 3 additions & 3 deletions app/stac_api/management/commands/remove_expired_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from django.core.management.base import CommandParser
from django.utils import timezone

from stac_api.models import AssetUpload
from stac_api.models import BaseAssetUpload
from stac_api.models import Item
from stac_api.models.general import BaseAssetUpload
from stac_api.models.item import AssetUpload
from stac_api.models.item import Item
from stac_api.utils import CommandHandler
from stac_api.utils import CustomBaseCommand

Expand Down
4 changes: 2 additions & 2 deletions app/stac_api/management/commands/update_asset_file_size.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from django.core.management.base import BaseCommand
from django.core.management.base import CommandParser

from stac_api.models import Asset
from stac_api.models import CollectionAsset
from stac_api.models.collection import CollectionAsset
from stac_api.models.item import Asset
from stac_api.utils import CommandHandler
from stac_api.utils import get_s3_client
from stac_api.utils import select_s3_bucket
Expand Down
100 changes: 100 additions & 0 deletions app/stac_api/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,43 @@ def _filter_by_datetime_range(self, start_datetime, end_datetime):
)
)

def filter_by_forecast_reference_datetime(self, date_time):
'''Filter a queryset by forecast reference datetime

Args:
date_time:
A string containing datetime like "2020-10-28T13:05:10Z"

Returns:
The queryset filtered by date_time
'''
start, end = self._parse_datetime_query(date_time)
if end is not None:
return self._filter_by_forecast_reference_datetime_range(start, end)
return self.filter(forecast_reference_datetime=start)

def _filter_by_forecast_reference_datetime_range(self, start_datetime, end_datetime):
'''Filter a queryset by forecast reference datetime range

Helper function of filter_by_forecast_reference_datetime

Args:
start_datetime:
A string with the start datetime or ".." to denote open start
end_datetime:
A string with the end datetime or ".." to denote open end
Returns:
The queryset filtered by datetime range
'''
if start_datetime == '..':
# open start range
return self.filter(forecast_reference_datetime__lte=end_datetime)
if end_datetime == '..':
# open end range
return self.filter(forecast_reference_datetime__gte=start_datetime)
# else fixed range
return self.filter(forecast_reference_datetime__range=(start_datetime, end_datetime))

def _parse_datetime_query(self, date_time):
'''Parse the datetime query as specified in the api-spec.md.

Expand Down Expand Up @@ -189,6 +226,54 @@ def filter_by_intersects(self, intersects):
the_geom = GEOSGeometry(intersects)
return self.filter(geometry__intersects=the_geom)

def filter_by_forecast_horizon(self, duration):
'''Filter by forecast horizon

Args:
duration: string
ISO 8601 duration string

Returns:
queryset filtered by forecast horizon
'''
return self.filter(forecast_horizon=duration)

def filter_by_forecast_duration(self, duration):
'''Filter by forecast duration

Args:
duration: string
ISO 8601 duration string

Returns:
queryset filtered by forecast duration
'''
return self.filter(forecast_duration=duration)

def filter_by_forecast_variable(self, val):
'''Filter by forecast variable

Args:
val: string
foracast variable value

Returns:
queryset filtered by forecast variable
'''
return self.filter(forecast_variable=val)

def filter_by_forecast_perturbed(self, pert):
'''Filter by forecast perturbed

Args:
pert: boolean
foracast perturbed

Returns:
queryset filtered by forecast perturbed
'''
return self.filter(forecast_perturbed=pert)

def filter_by_query(self, query):
'''Filter by the query parameter

Expand Down Expand Up @@ -236,6 +321,21 @@ def filter_by_item_name(self, item_name_array):
def filter_by_query(self, query):
return self.get_queryset().filter_by_query(query)

def filter_by_forecast_reference_datetime(self, date_time):
return self.get_queryset().filter_by_forecast_reference_datetime(date_time)

def filter_by_forecast_horizon(self, duration):
return self.get_queryset().filter_by_forecast_horizon(duration)

def filter_by_forecast_duration(self, duration):
return self.get_queryset().filter_by_forecast_duration(duration)

def filter_by_forecast_variable(self, val):
return self.get_queryset().filter_by_forecast_variable(val)

def filter_by_forecast_perturbed(self, pert):
return self.get_queryset().filter_by_forecast_perturbed(pert)

Comment on lines +327 to +338
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't all these be replaced by something less redundant?

For example

def filter_by_field(self, field_name, value):
    return self.get_queryset().filter(**{field_name: value})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a deeper look at this, but am not sure it makes sense to just change this. I prefer to keep it consistent with the existing field filters. Maybe in the future we could refactor all of the search filters.


class AssetUploadQuerySet(models.QuerySet):

Expand Down
8 changes: 4 additions & 4 deletions app/stac_api/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.db import migrations
from django.db import models

import stac_api.models
import stac_api.models.general
import stac_api.validators


Expand Down Expand Up @@ -74,7 +74,7 @@ class Migration(migrations.Migration):
(
'summaries',
models.JSONField(
default=stac_api.models.get_default_summaries_value,
default=stac_api.models.general.get_default_summaries_value,
editable=False,
encoder=django.core.serializers.json.DjangoJSONEncoder
)
Expand Down Expand Up @@ -102,7 +102,7 @@ class Migration(migrations.Migration):
'conformsTo',
django.contrib.postgres.fields.ArrayField(
base_field=models.URLField(),
default=stac_api.models.get_conformance_default_links,
default=stac_api.models.general.get_conformance_default_links,
help_text='Comma-separated list of URLs for the value conformsTo',
size=None
)
Expand Down Expand Up @@ -335,7 +335,7 @@ class Migration(migrations.Migration):
(
'file',
models.FileField(
max_length=255, upload_to=stac_api.models.upload_asset_to_path_hook
max_length=255, upload_to=stac_api.models.general.upload_asset_to_path_hook
)
),
(
Expand Down
7 changes: 5 additions & 2 deletions app/stac_api/migrations/0005_auto_20210408_0821.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.db import migrations
from django.db import models

import stac_api.models
import stac_api.models.general


class Migration(migrations.Migration):
Expand Down Expand Up @@ -50,7 +50,10 @@ class Migration(migrations.Migration):
('created', models.DateTimeField(auto_now_add=True)),
('ended', models.DateTimeField(blank=True, default=None, null=True)),
('checksum_multihash', models.CharField(max_length=255)),
('etag', models.CharField(default=stac_api.models.compute_etag, max_length=56)),
(
'etag',
models.CharField(default=stac_api.models.general.compute_etag, max_length=56)
),
(
'asset',
models.ForeignKey(
Expand Down
8 changes: 4 additions & 4 deletions app/stac_api/migrations/0014_auto_20210715_1358.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.db import migrations
from django.db import models

import stac_api.models
import stac_api.models.general


class Migration(migrations.Migration):
Expand Down Expand Up @@ -49,7 +49,7 @@ class Migration(migrations.Migration):
model_name='asset',
name='etag',
field=models.CharField(
default=stac_api.models.compute_etag, editable=False, max_length=56
default=stac_api.models.general.compute_etag, editable=False, max_length=56
),
),
migrations.AlterField(
Expand All @@ -61,7 +61,7 @@ class Migration(migrations.Migration):
model_name='collection',
name='etag',
field=models.CharField(
default=stac_api.models.compute_etag, editable=False, max_length=56
default=stac_api.models.general.compute_etag, editable=False, max_length=56
),
),
migrations.AlterField(
Expand Down Expand Up @@ -89,7 +89,7 @@ class Migration(migrations.Migration):
model_name='item',
name='etag',
field=models.CharField(
default=stac_api.models.compute_etag, editable=False, max_length=56
default=stac_api.models.general.compute_etag, editable=False, max_length=56
),
),
migrations.AlterField(
Expand Down
6 changes: 3 additions & 3 deletions app/stac_api/migrations/0032_alter_asset_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.db import migrations

import stac_api.models
import stac_api.models.general


class Migration(migrations.Migration):
Expand All @@ -15,8 +15,8 @@ class Migration(migrations.Migration):
migrations.AlterField(
model_name='asset',
name='file',
field=stac_api.models.DynamicStorageFileField(
max_length=255, upload_to=stac_api.models.upload_asset_to_path_hook
field=stac_api.models.general.DynamicStorageFileField(
max_length=255, upload_to=stac_api.models.general.upload_asset_to_path_hook
),
),
]
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.db import migrations
from django.db import models

import stac_api.models
import stac_api.models.general
import stac_api.validators


Expand All @@ -21,7 +21,7 @@ class Migration(migrations.Migration):
name='conformsTo',
field=django.contrib.postgres.fields.ArrayField(
base_field=models.URLField(),
default=stac_api.models.get_conformance_default_links,
default=stac_api.models.general.get_conformance_default_links,
help_text='Comma-separated list of URLs for the value conformsTo',
size=None
),
Expand Down
Loading
Loading