Skip to content

Commit

Permalink
fix attributes from tomorrow.io API (#149)
Browse files Browse the repository at this point in the history
* fix attributes from tomorrow.io API

* rename typo on function
  • Loading branch information
danangmassandy committed Sep 10, 2024
1 parent bf513d9 commit a197aaf
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 52 deletions.
171 changes: 157 additions & 14 deletions django_project/gap/fixtures/8.dataset_attribute.json
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,152 @@
"ensembles": true
}
},
{
{
"model": "gap.datasetattribute",
"pk": 39,
"fields": {
"dataset": 5,
"attribute": 15,
"source": "rainAccumulationSum",
"source_unit": 1,
"ensembles": false
}
},
{
"model": "gap.datasetattribute",
"pk": 40,
"fields": {
"dataset": 6,
"attribute": 15,
"source": "rainAccumulationSum",
"source_unit": 1,
"ensembles": false
}
},
{
"model": "gap.datasetattribute",
"pk": 41,
"fields": {
"dataset": 7,
"attribute": 15,
"source": "rainAccumulationSum",
"source_unit": 1,
"ensembles": false
}
},
{
"model": "gap.datasetattribute",
"pk": 42,
"fields": {
"dataset": 5,
"attribute": 8,
"source": "evapotranspirationSum",
"source_unit": 1,
"ensembles": false
}
},
{
"model": "gap.datasetattribute",
"pk": 43,
"fields": {
"dataset": 6,
"attribute": 8,
"source": "evapotranspirationSum",
"source_unit": 1,
"ensembles": false
}
},
{
"model": "gap.datasetattribute",
"pk": 44,
"fields": {
"dataset": 7,
"attribute": 8,
"source": "evapotranspirationSum",
"source_unit": 1,
"ensembles": false
}
},
{
"model": "gap.datasetattribute",
"pk": 45,
"fields": {
"dataset": 5,
"attribute": 9,
"source": "temperatureMax",
"source_unit": 5,
"ensembles": false
}
},
{
"model": "gap.datasetattribute",
"pk": 46,
"fields": {
"dataset": 6,
"attribute": 9,
"source": "temperatureMax",
"source_unit": 5,
"ensembles": false
}
},
{
"model": "gap.datasetattribute",
"pk": 47,
"fields": {
"dataset": 7,
"attribute": 9,
"source": "temperatureMax",
"source_unit": 5,
"ensembles": false
}
},
{
"model": "gap.datasetattribute",
"pk": 48,
"fields": {
"dataset": 5,
"attribute": 17,
"source": "temperatureMin",
"source_unit": 5,
"ensembles": false
}
},
{
"model": "gap.datasetattribute",
"pk": 49,
"fields": {
"dataset": 6,
"attribute": 17,
"source": "temperatureMin",
"source_unit": 5,
"ensembles": false
}
},
{
"model": "gap.datasetattribute",
"pk": 50,
"fields": {
"dataset": 7,
"attribute": 17,
"source": "temperatureMin",
"source_unit": 5,
"ensembles": false
}
},
{
"model": "gap.datasetattribute",
"pk": 51,
"fields": {
"dataset": 6,
"attribute": 36,
"source": "precipitationProbability",
"source_unit": 6,
"ensembles": false
}
},
{
"model": "gap.datasetattribute",
"pk": 52,
"fields": {
"dataset": 8,
"attribute": 8,
Expand All @@ -430,7 +573,7 @@
},
{
"model": "gap.datasetattribute",
"pk": 40,
"pk": 53,
"fields": {
"dataset": 8,
"attribute": 5,
Expand All @@ -441,7 +584,7 @@
},
{
"model": "gap.datasetattribute",
"pk": 41,
"pk": 54,
"fields": {
"dataset": 8,
"attribute": 14,
Expand All @@ -452,7 +595,7 @@
},
{
"model": "gap.datasetattribute",
"pk": 42,
"pk": 55,
"fields": {
"dataset": 8,
"attribute": 37,
Expand All @@ -463,7 +606,7 @@
},
{
"model": "gap.datasetattribute",
"pk": 43,
"pk": 56,
"fields": {
"dataset": 8,
"attribute": 38,
Expand All @@ -474,7 +617,7 @@
},
{
"model": "gap.datasetattribute",
"pk": 44,
"pk": 57,
"fields": {
"dataset": 8,
"attribute": 4,
Expand All @@ -485,7 +628,7 @@
},
{
"model": "gap.datasetattribute",
"pk": 45,
"pk": 58,
"fields": {
"dataset": 8,
"attribute": 16,
Expand All @@ -496,7 +639,7 @@
},
{
"model": "gap.datasetattribute",
"pk": 46,
"pk": 59,
"fields": {
"dataset": 8,
"attribute": 39,
Expand All @@ -507,7 +650,7 @@
},
{
"model": "gap.datasetattribute",
"pk": 47,
"pk": 60,
"fields": {
"dataset": 8,
"attribute": 7,
Expand All @@ -518,7 +661,7 @@
},
{
"model": "gap.datasetattribute",
"pk": 48,
"pk": 61,
"fields": {
"dataset": 8,
"attribute": 40,
Expand All @@ -529,7 +672,7 @@
},
{
"model": "gap.datasetattribute",
"pk": 49,
"pk": 62,
"fields": {
"dataset": 8,
"attribute": 43,
Expand All @@ -540,7 +683,7 @@
},
{
"model": "gap.datasetattribute",
"pk": 50,
"pk": 63,
"fields": {
"dataset": 8,
"attribute": 33,
Expand All @@ -551,7 +694,7 @@
},
{
"model": "gap.datasetattribute",
"pk": 51,
"pk": 64,
"fields": {
"dataset": 8,
"attribute": 42,
Expand All @@ -562,7 +705,7 @@
},
{
"model": "gap.datasetattribute",
"pk": 52,
"pk": 65,
"fields": {
"dataset": 8,
"attribute": 41,
Expand Down
4 changes: 2 additions & 2 deletions django_project/gap/models/crop_insight.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,9 @@ class CropPlanData:
@staticmethod
def default_fields():
"""Return shortterm default fields."""
from gap.providers.tio import tomorrowio_shortterm_forcast_dataset
from gap.providers.tio import tomorrowio_shortterm_forecast_dataset

dataset = tomorrowio_shortterm_forcast_dataset()
dataset = tomorrowio_shortterm_forecast_dataset()
forecast_fields = list(
DatasetAttribute.objects.filter(
dataset=dataset
Expand Down
36 changes: 2 additions & 34 deletions django_project/gap/providers/tio.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
}


def tomorrowio_shortterm_forcast_dataset() -> Dataset:
def tomorrowio_shortterm_forecast_dataset() -> Dataset:
"""Return dataset object for tomorrow.io Dataset for sort term forecast."""
provider, _ = Provider.objects.get_or_create(name='Tomorrow.io')
dt_shorttermforecast, _ = DatasetType.objects.get_or_create(
Expand Down Expand Up @@ -125,7 +125,7 @@ def init_provider(cls):
'is_internal_use': True
}
)
ds_forecast = tomorrowio_shortterm_forcast_dataset()
tomorrowio_shortterm_forecast_dataset()
dt_ltn, _ = DatasetType.objects.get_or_create(
name=cls.LONG_TERM_NORMALS_TYPE,
defaults={
Expand All @@ -143,38 +143,6 @@ def init_provider(cls):
}
)

for key, val in TIO_VARIABLES.items():
attr = val.get_gap_attribute()
# add to dataset attribute
DatasetAttribute.objects.get_or_create(
dataset=ds_historical,
attribute=attr,
source=key,
source_unit=attr.unit
)
DatasetAttribute.objects.get_or_create(
dataset=ds_forecast,
attribute=attr,
source=key,
source_unit=attr.unit
)
DatasetAttribute.objects.get_or_create(
dataset=ds_ltn,
attribute=attr,
source=key,
source_unit=attr.unit
)

# For shorterm forecast
for key, val in TIO_SHORT_TERM_FORCAST_VARIABLES.items():
attr = val.get_gap_attribute()
DatasetAttribute.objects.get_or_create(
dataset=ds_forecast,
attribute=attr,
source=key,
source_unit=attr.unit
)

def _is_ltn_request(self):
"""Check if the request is for Long Term Normal (LTN) request."""
return (
Expand Down
10 changes: 10 additions & 0 deletions django_project/gap/tests/providers/test_tio.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
class TestTomorrowIODatasetReader(TestCase):
"""Test class for Tomorrow io dataset reader."""

fixtures = [
'2.provider.json',
'3.observation_type.json',
'4.dataset_type.json',
'5.dataset.json',
'6.unit.json',
'7.attribute.json',
'8.dataset_attribute.json'
]

def setUp(self):
"""Set test class."""
TomorrowIODatasetReader.init_provider()
Expand Down
4 changes: 2 additions & 2 deletions django_project/gap_api/tests/test_crop_plan_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from gap.models import (
DatasetAttribute, FarmShortTermForecast, FarmShortTermForecastData
)
from gap.providers.tio import tomorrowio_shortterm_forcast_dataset
from gap.providers.tio import tomorrowio_shortterm_forecast_dataset
from gap_api.api_views.crop_insight import CropPlanAPI


Expand All @@ -34,7 +34,7 @@ def setUp(self):
"""Init test class."""
super().setUp()

ds_forecast = tomorrowio_shortterm_forcast_dataset()
ds_forecast = tomorrowio_shortterm_forecast_dataset()
attr = AttributeFactory()
self.farm = FarmFactory.create(
unique_id='farm-1',
Expand Down

0 comments on commit a197aaf

Please sign in to comment.