Skip to content

Commit 894221b

Browse files
committed
ajout de models opendata
1 parent ba1817a commit 894221b

15 files changed

+685
-9
lines changed

dags/.env.template

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ AIRFLOW__SCHEDULER__ENABLE_HEALTH_CHECK='true'
2828
# WARNING=Use _PIP_ADDITIONAL_REQUIREMENTS option ONLY for a quick checks
2929
# for other purpose (development, test and especially production usage) build/extend Airflow image.
3030
_PIP_ADDITIONAL_REQUIREMENTS=${_PIP_ADDITIONAL_REQUIREMENTS:-}
31-
AIRFLOW_CONN_QFDMO-DJANGO-DB='postgres://qfdmo:qfdmo@lvao-db:5432/qfdmo' # pragma: allowlist secret
31+
AIRFLOW_CONN_QFDMO_DJANGO_DB='postgres://qfdmo:qfdmo@lvao-db:5432/qfdmo' # pragma: allowlist secret
3232
DATABASE_URL=postgis://qfdmo:qfdmo@lvao-db:5432/qfdmo # pragma: allowlist secret
3333

3434
# DBT env vars
@@ -37,4 +37,4 @@ POSTGRES_PORT=5432
3737
POSTGRES_USER=qfdmo
3838
POSTGRES_PASSWORD=qfdmo
3939
POSTGRES_DB=qfdmo
40-
POSTGRES_SCHEMA=public
40+
POSTGRES_SCHEMA=public

dags/acteur_views/dags/build_vue.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,49 @@
2727
) as dag:
2828

2929
run_dbt_exhaustive_acteurs_model = BashOperator(
30-
task_id="build_exhaustive_acteur",
30+
task_id="build_exhaustive_acteurs",
3131
bash_command=(
3232
"cd /opt/airflow/dbt/ && dbt run --select qfdmo.exhaustive_acteurs"
3333
),
3434
dag=dag,
3535
)
3636
test_dbt_exhaustive_acteurs_model = BashOperator(
37-
task_id="test_exhaustive_acteur",
37+
task_id="test_exhaustive_acteurs",
3838
bash_command=(
3939
"cd /opt/airflow/dbt/ && dbt test --select qfdmo.exhaustive_acteurs"
4040
),
4141
dag=dag,
4242
)
4343
run_dbt_carte_acteurs_model = BashOperator(
44-
task_id="build_exhaustive_acteur",
44+
task_id="build_carte_acteurs",
4545
bash_command=("cd /opt/airflow/dbt/ && dbt run --select qfdmo.carte_acteurs"),
4646
dag=dag,
4747
)
4848
test_dbt_carte_acteurs_model = BashOperator(
49-
task_id="test_exhaustive_acteur",
49+
task_id="test_carte_acteurs",
5050
bash_command=("cd /opt/airflow/dbt/ && dbt test --select qfdmo.carte_acteurs"),
5151
dag=dag,
5252
)
53+
run_dbt_opendata_acteurs_model = BashOperator(
54+
task_id="build_opendata_acteurs",
55+
bash_command=(
56+
"cd /opt/airflow/dbt/ && dbt run --select qfdmo.opendata_acteurs"
57+
),
58+
dag=dag,
59+
)
60+
test_dbt_opendata_acteurs_model = BashOperator(
61+
task_id="test_opendata_acteurs",
62+
bash_command=(
63+
"cd /opt/airflow/dbt/ && dbt test --select qfdmo.opendata_acteurs"
64+
),
65+
dag=dag,
66+
)
67+
5368
(
5469
run_dbt_exhaustive_acteurs_model
5570
>> test_dbt_exhaustive_acteurs_model
5671
>> run_dbt_carte_acteurs_model
5772
>> test_dbt_carte_acteurs_model
73+
>> run_dbt_opendata_acteurs_model
74+
>> test_dbt_opendata_acteurs_model
5875
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SELECT DISTINCT va.*
2+
FROM {{ ref('temp_opendata_filteredacteur') }} AS va
3+
INNER JOIN {{ ref('opendata_propositionservice') }} AS cps
4+
ON va.identifiant_unique = cps.acteur_id
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
WITH nochild_acteur_acteur_services AS (
2+
SELECT
3+
aas.vueacteur_id AS acteur_id,
4+
aas.acteurservice_id AS acteurservice_id
5+
FROM qfdmo_vueacteur_acteur_services aas
6+
INNER JOIN {{ ref('temp_opendata_filteredacteur') }} AS a ON aas.vueacteur_id = a.identifiant_unique AND a.parent_id is null
7+
GROUP BY aas.vueacteur_id, aas.acteurservice_id
8+
),
9+
parentacteur_acteur_services AS (
10+
SELECT
11+
a.parent_id AS acteur_id,
12+
aas.acteurservice_id AS acteurservice_id
13+
FROM qfdmo_vueacteur_acteur_services aas
14+
INNER JOIN {{ ref('temp_opendata_filteredacteur') }} AS a ON aas.vueacteur_id = a.identifiant_unique AND a.parent_id is not null
15+
GROUP BY a.parent_id, aas.acteurservice_id
16+
),
17+
acteur_acteur_services AS (
18+
SELECT * FROM nochild_acteur_acteur_services
19+
UNION ALL
20+
SELECT * FROM parentacteur_acteur_services
21+
)
22+
23+
SELECT ROW_NUMBER() OVER (ORDER BY acteur_id, aas.acteurservice_id) AS id, aas.*
24+
FROM acteur_acteur_services AS aas
25+
INNER JOIN {{ ref('opendata_acteur') }} AS a ON a.identifiant_unique = acteur_id
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
WITH deduplicated_opened_sources AS (
2+
SELECT
3+
da.uuid,
4+
string_agg(DISTINCT source.libelle, '|' ORDER BY source.libelle) as sources_list
5+
FROM {{ ref('opendata_acteur') }} AS da
6+
LEFT JOIN {{ ref('opendata_acteur_sources') }} AS das
7+
ON da.identifiant_unique = das.acteur_id
8+
LEFT JOIN qfdmo_source AS source
9+
ON das.source_id = source.id
10+
GROUP BY da.uuid
11+
),
12+
proposition_services AS (
13+
SELECT
14+
da.uuid,
15+
jsonb_agg(
16+
jsonb_build_object(
17+
'action', a.code,
18+
'sous_categories', (
19+
SELECT jsonb_agg(sco.code)
20+
FROM {{ ref('opendata_propositionservice_sous_categories') }} AS pssc
21+
JOIN qfdmo_souscategorieobjet AS sco ON pssc.souscategorieobjet_id = sco.id
22+
WHERE pssc.propositionservice_id = ps.id
23+
)
24+
)
25+
) as services
26+
FROM {{ ref('opendata_acteur') }} AS da
27+
JOIN {{ ref('opendata_propositionservice') }} AS ps ON ps.acteur_id = da.identifiant_unique
28+
JOIN qfdmo_action AS a ON ps.action_id = a.id
29+
GROUP BY da.uuid
30+
),
31+
acteur_labels AS (
32+
SELECT
33+
da.uuid,
34+
string_agg(DISTINCT lq.code, '|' ORDER BY lq.code) as labels
35+
FROM {{ ref('opendata_acteur') }} AS da
36+
LEFT JOIN {{ ref('opendata_acteur_labels') }} AS dal
37+
ON da.identifiant_unique = dal.acteur_id
38+
LEFT JOIN qfdmo_labelqualite AS lq ON dal.labelqualite_id = lq.id
39+
GROUP BY da.uuid
40+
),
41+
acteur_services AS (
42+
SELECT
43+
da.uuid,
44+
string_agg(DISTINCT as2.code, '|' ORDER BY as2.code) as services
45+
FROM {{ ref('opendata_acteur') }} AS da
46+
LEFT JOIN {{ ref('opendata_acteur_acteur_services') }} AS daas
47+
ON da.identifiant_unique = daas.acteur_id
48+
LEFT JOIN qfdmo_acteurservice AS as2 ON daas.acteurservice_id = as2.id
49+
GROUP BY da.uuid
50+
)
51+
SELECT
52+
da.uuid as "Identifiant",
53+
CASE
54+
WHEN ds.sources_list IS NOT NULL
55+
THEN 'Longue Vie Aux Objets|ADEME|' || ds.sources_list
56+
ELSE 'Longue Vie Aux Objets|ADEME'
57+
END as "Paternité",
58+
da.nom as "Nom",
59+
da.nom_commercial as "Nom commercial",
60+
da.siren as "SIREN",
61+
da.siret as "SIRET",
62+
da.description as "Description",
63+
at.code as "Type d'acteur",
64+
da.url as "Site web",
65+
CASE
66+
WHEN da.telephone ~ '^0[67]' THEN NULL
67+
WHEN EXISTS (
68+
SELECT 1
69+
FROM {{ ref('opendata_acteur_sources') }} das2
70+
JOIN qfdmo_source s ON das2.source_id = s.id
71+
WHERE das2.acteur_id = da.identifiant_unique
72+
AND s.code = 'carteco'
73+
) THEN NULL
74+
ELSE da.telephone
75+
END as "Téléphone",
76+
da.adresse as "Adresse",
77+
da.adresse_complement as "Complément d'adresse",
78+
da.code_postal as "Code postal",
79+
da.ville as "Ville",
80+
ST_Y(da.location::geometry) as "latitude",
81+
ST_X(da.location::geometry) as "longitude",
82+
al.labels as "Qualités et labels",
83+
da.public_accueilli as "Public accueilli",
84+
da.reprise as "Reprise",
85+
da.exclusivite_de_reprisereparation as "Exclusivité de reprise/réparation",
86+
da.uniquement_sur_rdv as "Uniquement sur RDV",
87+
acs.services as "Type de services",
88+
ps.services::text as "Propositions de services",
89+
to_char(da.modifie_le, 'YYYY-MM-DD') as "Date de dernière modification"
90+
FROM {{ ref('opendata_acteur') }} AS da
91+
LEFT JOIN qfdmo_acteurtype AS at ON da.acteur_type_id = at.id
92+
-- INNER JOIN : Only open lisense
93+
INNER JOIN deduplicated_opened_sources AS ds ON da.uuid = ds.uuid
94+
LEFT JOIN proposition_services AS ps ON da.uuid = ps.uuid
95+
LEFT JOIN acteur_labels AS al ON da.uuid = al.uuid
96+
LEFT JOIN acteur_services AS acs ON da.uuid = acs.uuid
97+
WHERE da.statut = 'ACTIF'
98+
AND da.public_accueilli NOT IN ('AUCUN', 'PROFESSIONNELS')
99+
AND da.identifiant_unique NOT LIKE '%_reparation_%'
100+
ORDER BY da.uuid
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
WITH nochild_acteur_labels AS (
2+
SELECT
3+
al.vueacteur_id AS acteur_id,
4+
al.labelqualite_id AS labelqualite_id
5+
FROM qfdmo_vueacteur_labels al
6+
INNER JOIN {{ ref('temp_opendata_filteredacteur') }} AS a ON al.vueacteur_id = a.identifiant_unique AND a.parent_id is null
7+
GROUP BY al.vueacteur_id, al.labelqualite_id
8+
),
9+
parentacteur_labels AS (
10+
SELECT
11+
a.parent_id AS acteur_id,
12+
al.labelqualite_id AS labelqualite_id
13+
FROM qfdmo_vueacteur_labels al
14+
INNER JOIN {{ ref('temp_opendata_filteredacteur') }} AS a ON al.vueacteur_id = a.identifiant_unique AND a.parent_id is not null
15+
GROUP BY a.parent_id, al.labelqualite_id
16+
),
17+
acteur_labels AS (
18+
SELECT * FROM nochild_acteur_labels
19+
UNION ALL
20+
SELECT * FROM parentacteur_labels
21+
)
22+
23+
SELECT ROW_NUMBER() OVER (ORDER BY acteur_id, al.labelqualite_id) AS id, al.*
24+
FROM acteur_labels AS al
25+
INNER JOIN {{ ref('opendata_acteur') }} AS a ON a.identifiant_unique = acteur_id
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
WITH nochild_acteur_labels AS (
2+
SELECT
3+
a.identifiant_unique AS acteur_id,
4+
a.source_id AS source_id
5+
FROM {{ ref('temp_opendata_filteredacteur') }} AS a
6+
WHERE a.parent_id is null AND a.source_id is not null
7+
GROUP BY a.identifiant_unique, a.source_id
8+
),
9+
parentacteur_labels AS (
10+
SELECT
11+
a.parent_id AS acteur_id,
12+
a.source_id AS source_id
13+
FROM {{ ref('temp_opendata_filteredacteur') }} AS a
14+
WHERE a.parent_id is not null
15+
GROUP BY a.parent_id, a.source_id
16+
),
17+
acteur_sources AS (
18+
SELECT * FROM nochild_acteur_labels
19+
UNION ALL
20+
SELECT * FROM parentacteur_labels
21+
)
22+
23+
SELECT ROW_NUMBER() OVER (ORDER BY acteur_id, s.source_id) AS id, s.*
24+
FROM acteur_sources AS s
25+
INNER JOIN {{ ref('opendata_acteur') }} AS a ON a.identifiant_unique = acteur_id
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
SELECT
2+
MIN(ps.id) AS id,
3+
ps.acteur_id,
4+
ps.action_id
5+
FROM {{ ref('temp_opendata_propositionservice') }} AS ps
6+
INNER JOIN {{ ref('opendata_propositionservice_sous_categories') }} AS pssscat
7+
ON ps.id = pssscat.propositionservice_id
8+
GROUP BY acteur_id, action_id
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
with
2+
parent_vuepropositionservice_sous_categories
3+
AS
4+
(
5+
SELECT
6+
MIN(qfdmo_vuepropositionservice_sous_categories.id) AS id,
7+
CONCAT(temp_opendata_parentpropositionservice.parent_id::text, '_', temp_opendata_parentpropositionservice.action_id::text) AS propositionservice_id,
8+
qfdmo_vuepropositionservice_sous_categories.souscategorieobjet_id AS souscategorieobjet_id
9+
FROM qfdmo_vuepropositionservice_sous_categories
10+
INNER JOIN {{ ref('temp_opendata_parentpropositionservice') }} AS temp_opendata_parentpropositionservice
11+
ON temp_opendata_parentpropositionservice.id = qfdmo_vuepropositionservice_sous_categories.vuepropositionservice_id
12+
GROUP BY
13+
propositionservice_id,
14+
souscategorieobjet_id
15+
),
16+
nochild_vuepropositionservice_sous_categories
17+
AS
18+
(
19+
SELECT
20+
qfdmo_vuepropositionservice_sous_categories.id AS id,
21+
qfdmo_vuepropositionservice_sous_categories.vuepropositionservice_id AS propositionservice_id,
22+
qfdmo_vuepropositionservice_sous_categories.souscategorieobjet_id AS souscategorieobjet_id
23+
FROM qfdmo_vuepropositionservice_sous_categories
24+
INNER JOIN {{ ref('temp_opendata_propositionservice') }} AS ps ON qfdmo_vuepropositionservice_sous_categories.vuepropositionservice_id = ps.id
25+
INNER JOIN {{ ref('temp_opendata_filteredacteur') }} AS cfa ON ps.acteur_id = cfa.identifiant_unique AND cfa.parent_id is null
26+
)
27+
28+
SELECT *
29+
FROM parent_vuepropositionservice_sous_categories
30+
UNION ALL
31+
SELECT *
32+
FROM nochild_vuepropositionservice_sous_categories

0 commit comments

Comments
 (0)