Skip to content

Commit

Permalink
Fix istsos (#127)
Browse files Browse the repository at this point in the history
* Fix empty geom for not having latitiude

* Update views
  • Loading branch information
meomancer authored Apr 17, 2024
1 parent e2e7dab commit 2fe28ae
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 13 deletions.
6 changes: 6 additions & 0 deletions harvesters/harvester/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.contrib.auth import get_user_model
from django.contrib.gis.geos import Point
from django.contrib.gis.measure import D
from django.core.management import call_command
from django.db.models.signals import post_save
from django.utils import timezone

Expand Down Expand Up @@ -92,6 +93,11 @@ def __init__(self, harvester: Harvester, replace: bool = False,
except Exception:
self._error(traceback.format_exc())

# RUN MATERIALIZED VIEW
running = Harvester.objects.filter(is_run=True).first()
if not running:
call_command('refresh_materialized_views')

@staticmethod
def additional_attributes() -> dict:
"""
Expand Down
7 changes: 5 additions & 2 deletions management/commands/refresh_materialized_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@

class Command(BaseCommand):
help = 'Refresh materialized views'
views = ['mv_well_ggmn', 'mv_well', 'mv_well_measurement']

def handle(self, *args, **options):
with connections[settings.GWML2_DATABASE_CONFIG].cursor() as cursor:
cursor.execute('REFRESH MATERIALIZED VIEW mv_well_ggmn')
cursor.execute('REFRESH MATERIALIZED VIEW mv_well')
for view in self.views:
sql = f'REFRESH MATERIALIZED VIEW {view}'
print(sql)
cursor.execute(sql)
26 changes: 26 additions & 0 deletions migrations/0082_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 2.2.12 on 2020-07-03 09:03

from django.db import migrations

from gwml2.migrations.sql.utils import load_sql


class Migration(migrations.Migration):
dependencies = [
('gwml2', '0081_auto_20240322_1435'),
]

gwml_default = load_sql('gwml', 'default.sql')
gwml_views = load_sql('gwml', 'views.sql')
gwml_functions = load_sql('gwml', 'functions.sql')

istsos_default = load_sql('istsos', 'default.sql')
istsos_views = load_sql('istsos', 'views.sql')

operations = [
migrations.RunSQL(istsos_default, istsos_default),
migrations.RunSQL(gwml_default, gwml_default),
migrations.RunSQL(gwml_views, gwml_default),
migrations.RunSQL(gwml_functions, gwml_default),
migrations.RunSQL(istsos_views, istsos_default),
]
1 change: 1 addition & 0 deletions migrations/sql/gwml/default.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
DROP MATERIALIZED VIEW IF EXISTS mv_well_ggmn;
DROP MATERIALIZED VIEW IF EXISTS mv_well;
DROP MATERIALIZED VIEW IF EXISTS mv_well_measurement;
DROP VIEW IF EXISTS vw_well_measurement;
DROP VIEW IF EXISTS vw_well;

Expand Down
7 changes: 6 additions & 1 deletion migrations/sql/gwml/views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,9 @@ select id,
UNION
SELECT id, time, well_id, default_unit_id, default_value, parameter_id
from well_yield_measurement where default_value IS NOT NULL
) as measurement;
) as measurement;

-- MATERIALIZED VIEW FOR ISTSOS MEASUREMENT --
-- SCHEDULED REFRESH --
CREATE MATERIALIZED VIEW mv_well_measurement AS
select * FROM vw_well_measurement;
17 changes: 7 additions & 10 deletions migrations/sql/istsos/views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,19 @@ SELECT w.description as desc_foi,
w.feature_type_id as id_fty_fk,
w.id as id_foi,
w.name as name_foi,
ST_SetSRID(
ST_MakePoint(
ST_X(w.location),
ST_Y(w.location),
w.altitiude
),
4326)
as geom_foi
CASE
WHEN w.altitiude is not null THEN ST_SetSRID(ST_MakePoint(ST_X(w.location),ST_Y(w.location),w.altitiude),4326)
ELSE ST_SetSRID(ST_MakePoint(ST_X(w.location),ST_Y(w.location)),4326)
END
as geom_foi
from vw_well as w;

-- EVENT_TIME --
CREATE VIEW istsos.event_time AS
SELECT id as id_eti,
well_id as id_prc_fk,
time as time_eti
from vw_well_measurement;
from mv_well_measurement;

-- MEASURES --
CREATE VIEW istsos.measures AS
Expand All @@ -119,7 +116,7 @@ SELECT id as id_msr,
null as id_qi_fk,
concat(parameter_id, '-', default_unit_id, '-', well_id) as id_pro_fk,
default_value as val_msr
from vw_well_measurement;
from mv_well_measurement;

-- POSITIONS --
CREATE VIEW istsos.positions AS
Expand Down

0 comments on commit 2fe28ae

Please sign in to comment.