Skip to content

Commit

Permalink
feat(sudocuh): add competence urba for commune
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisig committed Dec 17, 2024
1 parent 1699e1d commit 6d13b1b
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 1 deletion.
55 changes: 55 additions & 0 deletions airflow/dags/ingest_plan_communal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from airflow.decorators import dag, task
from include.domain.container import Container
from include.pools import DBT_POOL
from include.utils import get_dbt_command_from_directory
from pendulum import datetime

URL = "https://api-aln.datahub.din.developpement-durable.gouv.fr/sudocuh/enquetes/ref/plan/communal/CSV?annee_cog=2024"


@dag(
start_date=datetime(2024, 1, 1),
schedule="@once",
catchup=False,
doc_md=__doc__,
max_active_runs=1,
default_args={"owner": "Alexis Athlani", "retries": 3},
tags=["SUDOCUH"],
)
def ingest_plan_communal():
bucket_name = "airflow-staging"
plan_communal_filename = "plan_communal.csv"
table_name = "sudocuh_plan_communal"

@task.python
def download_plan_communal() -> str:
return (
Container()
.remote_to_s3_file_handler()
.download_http_file_and_upload_to_s3(
url=URL,
s3_key=plan_communal_filename,
s3_bucket=bucket_name,
)
)

@task.python
def ingest_plan_communal() -> int | None:
return (
Container()
.s3_csv_file_to_db_table_handler()
.ingest_s3_csv_file_to_db_table(
s3_bucket=bucket_name,
s3_key=plan_communal_filename,
table_name=table_name,
)
)

@task.bash(retries=0, trigger_rule="all_success", pool=DBT_POOL)
def dbt_build():
return get_dbt_command_from_directory(cmd="dbt build -s sudocuh+")

(download_plan_communal() >> ingest_plan_communal() >> dbt_build())


ingest_plan_communal()
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ select
end as surface_artif,
commune.surface / 10000 as area,
ST_Transform(commune.geom, 4326) as mpoly,
consommation.correction_status as consommation_correction_status
consommation.correction_status as consommation_correction_status,
competence.competence_planification
from
{{ ref('commune') }} as commune
left join
Expand All @@ -72,3 +73,7 @@ left join
{{ ref("consommation_cog_2024") }} as consommation
on
commune.code = consommation.commune_code
left join
{{ ref('competence_plan_commune')}} as competence
on
commune.code = competence.commune_code
3 changes: 3 additions & 0 deletions airflow/include/sql/sparte/models/for_app/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ models:
- not_null
- name: for_app_commune
columns:
- name: competence_planification
data_tests:
- not_null
- name: insee
data_tests:
- not_null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{{ config(materialized='table') }}

SELECT
commune.code as commune_code,
CASE
WHEN code_etat_libelle_bcsi LIKE '%Compétence commune' THEN TRUE
ELSE FALSE
END AS competence_planification
FROM
{{ ref('commune') }} as commune
LEFT JOIN
{{ source('public', 'sudocuh_plan_communal') }} as plan_communal
ON
commune.code = plan_communal.code_insee
1 change: 1 addition & 0 deletions airflow/include/sql/sparte/models/sudocuh/models.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ sources:
tables:
- name: sudocuh_scot
- name: sudocuh_scot_communes
- name: sudocuh_plan_communal
4 changes: 4 additions & 0 deletions public_data/models/administration/Commune.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ class Meta:
choices=ConsommationCorrectionStatus.choices,
)

competence_planification = models.BooleanField(
"Compétence planification",
)

# DataColorationMixin properties that need to be set when heritating
default_property = "insee" # need to be set correctly to work
default_color = "Yellow"
Expand Down

0 comments on commit 6d13b1b

Please sign in to comment.