Skip to content

Commit

Permalink
Fix observation api response time (#261)
Browse files Browse the repository at this point in the history
* add index to measurement table

* fix query measurements to_csv

* refactor to_netcdf_stream

* fix to json for observation data

* update uwsgi harakiri value

* fix tests

* remove debug

* update version
  • Loading branch information
danangmassandy authored Nov 15, 2024
1 parent 297b2d4 commit efa3ab4
Show file tree
Hide file tree
Showing 15 changed files with 400 additions and 236 deletions.
2 changes: 1 addition & 1 deletion deployment/docker/uwsgi.conf
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ env = DJANGO_SETTINGS_MODULE=core.settings.prod
#uid = 1000
#gid = 1000
memory-report = true
harakiri = 120
harakiri = 600

2 changes: 1 addition & 1 deletion django_project/_version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.10
0.0.11
3 changes: 2 additions & 1 deletion django_project/gap/admin/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ class PreferencesAdmin(admin.ModelAdmin):
}
),
(
'Logging', {
'API Config', {
'fields': (
'dask_threads_num_api',
'api_log_batch_size',
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 4.2.7 on 2024-11-14 19:48

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('gap', '0037_alter_collectorsession_ingestor_type_and_more'),
]

operations = [
migrations.AddIndex(
model_name='measurement',
index=models.Index(fields=['dataset_attribute', 'date_time'], name='gap_measure_dataset_c4f74a_idx'),
),
migrations.AddIndex(
model_name='measurement',
index=models.Index(fields=['station_history', 'dataset_attribute', 'date_time'], name='gap_measure_station_694206_idx'),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.7 on 2024-11-15 06:32

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('gap', '0038_measurement_gap_measure_dataset_c4f74a_idx_and_more'),
]

operations = [
migrations.AddField(
model_name='preferences',
name='dask_threads_num_api',
field=models.IntegerField(default=2, help_text='Number of threads for dask parallel computation in API, higher number will use more memory.'),
),
]
6 changes: 6 additions & 0 deletions django_project/gap/models/measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,9 @@ def __str__(self):

class Meta: # noqa
unique_together = ('station', 'dataset_attribute', 'date_time')
indexes = [
models.Index(fields=['dataset_attribute', 'date_time']),
models.Index(
fields=['station_history', 'dataset_attribute', 'date_time']
),
]
8 changes: 8 additions & 0 deletions django_project/gap/models/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ class Preferences(SingletonModel):
)
)

dask_threads_num_api = models.IntegerField(
default=2,
help_text=(
'Number of threads for dask parallel computation in API, '
'higher number will use more memory.'
)
)

# ingestor config
ingestor_config = models.JSONField(
default=dict,
Expand Down
19 changes: 14 additions & 5 deletions django_project/gap/providers/airborne_observation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from datetime import datetime

from django.db.models import F, QuerySet
from django.contrib.gis.db.models.functions import Distance
from django.contrib.gis.geos import Polygon, Point

Expand Down Expand Up @@ -79,15 +80,23 @@ def _find_nearest_station_by_points(self):
def get_measurements(self, start_date: datetime, end_date: datetime):
"""Return measurements."""
nearest_histories = self.get_nearest_stations()
if nearest_histories is None or len(nearest_histories) == 0:
if isinstance(nearest_histories, QuerySet):
nearest_histories = nearest_histories.filter(
date_time__gte=start_date,
date_time__lte=end_date
)
if (
nearest_histories is None or
self._get_count(nearest_histories) == 0
):
return None

return Measurement.objects.select_related(
'dataset_attribute', 'dataset_attribute__attribute',
'station', 'station_history'
return Measurement.objects.annotate(
geom=F('station_history__geometry'),
alt=F('station_history__altitude')
).filter(
date_time__gte=start_date,
date_time__lte=end_date,
dataset_attribute__in=self.attributes,
station_history__in=nearest_histories
).order_by('date_time', 'station', 'dataset_attribute')
).order_by('date_time')
Loading

0 comments on commit efa3ab4

Please sign in to comment.