Skip to content

Commit

Permalink
Cohabitation df1-df3 + fix df3_chantier (#671)
Browse files Browse the repository at this point in the history
* dbt.model.new: df1_indic et df1_ch

Ajout de ces models pour changer facilement les tables d'exposition (indicateur et chantier)

* dbt.model.new: df1_indic et df1_ch

Ajout de ces models pour changer facilement les tables d'exposition (indicateur et chantier)

* fix empty array

* fix: directions_administration_centrale

on récupère correctement les directions_administration_centrale à l'aide de la table metadata_porteur et non plus dans la colonne porteur_shorts_DAC de metadata_chantier qui était présente pour information donc sur laquelle il ne faut pas se baser.

* replace dy dbt ref

* dbt.model.new: df1_indic et df1_ch

Ajout de ces models pour changer facilement les tables d'exposition (indicateur et chantier)

* fix empty array

* fix: directions_administration_centrale

on récupère correctement les directions_administration_centrale à l'aide de la table metadata_porteur et non plus dans la colonne porteur_shorts_DAC de metadata_chantier qui était présente pour information donc sur laquelle il ne faut pas se baser.

* replace dy dbt ref

* update dbt tests

* fix tests

issue: dbt-labs/dbt-core#3229
solution: given by dbt cli when error
  • Loading branch information
cdugeai committed Dec 4, 2023
1 parent 6b58d95 commit bb945ed
Show file tree
Hide file tree
Showing 56 changed files with 279 additions and 221 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ with jjoin as (select
else false
end as ignore_row
from {{ ref('df3_indicateur') }} a
full join {{ ref('indicateur') }} b
full join {{ ref('df1_indicateur') }} b
on a.territoire_code=b.territoire_code and a.id=b.id
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ with jjoin as (select
else false
end as ignore_row
from {{ ref('df3_indicateur') }} a
full join {{ ref('indicateur') }} b
full join {{ ref('df1_indicateur') }} b
on a.territoire_code=b.territoire_code and a.id=b.id
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ with jjoin as (select
end as ratio

from {{ ref('df3_indicateur') }} a
full join {{ ref('indicateur') }} b
full join {{ ref('df1_indicateur') }} b
on a.territoire_code=b.territoire_code and a.id=b.id
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ with jjoin as (select
null::numeric as ratio

from {{ ref('df3_indicateur') }} a
full join {{ ref('indicateur') }} b
full join {{ ref('df1_indicateur') }} b
on a.territoire_code=b.territoire_code and a.id=b.id
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ with jjoin as (select
a.{{colname}} as computed_value,
b.{{colname}} as target_value
from {{ ref('df3_indicateur') }} a
full join {{ ref('indicateur') }} b
full join {{ ref('df1_indicateur') }} b
on a.territoire_code=b.territoire_code and a.id=b.id
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ with jjoin as (select
a.{{colname}} as computed_value,
b.{{colname}} as target_value
from {{ ref('df3_indicateur') }} a
full join {{ ref('indicateur') }} b
full join {{ ref('df1_indicateur') }} b
on a.territoire_code=b.territoire_code and a.id=b.id
)

Expand Down
24 changes: 12 additions & 12 deletions data_management/data_factory/macros/df3_validation/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,64 @@ version: 2
macros:
- name: df3_date_early
description: |
Macro de test de la `df2.df3_indicateur` par rapport à `public.indicateur`.
Macro de test de la `df2.df3_indicateur` par rapport à `public.df1_indicateur`.
Compare si une date est **antérieure** à ce qui est attendu.
arguments:
- name: colname_date
type: string
description: Nom de la colonne de `df2.df3_indicateur` et `public.indicateur` contenant une date à vérifier.
description: Nom de la colonne de `df2.df3_indicateur` et `public.df1_indicateur` contenant une date à vérifier.
- name: ignore_day
type: boolean
description: Indique si on doit ignorer le jour du mois. Ie, si le mois est identique, on considère les dates identiques.
- name: df3_date_late
description: |
Macro de test de la `df2.df3_indicateur` par rapport à `public.indicateur`.
Macro de test de la `df2.df3_indicateur` par rapport à `public.df1_indicateur`.
Compare si une date est **postérieure** à ce qui est attendu.
arguments:
- name: colname_date
type: string
description: Nom de la colonne de `df2.df3_indicateur` et `public.indicateur` contenant une date à vérifier.
description: Nom de la colonne de `df2.df3_indicateur` et `public.df1_indicateur` contenant une date à vérifier.
- name: ignore_day
type: boolean
description: Indique si on doit ignorer le jour du mois. Ie, si le mois est identique, on considère les dates identiques.
- name: df3_not_equal_bool
description: |
Macro de test de la `df2.df3_indicateur` par rapport à `public.indicateur`.
Macro de test de la `df2.df3_indicateur` par rapport à `public.df1_indicateur`.
Compare si deux booléens sont **non null et identiques**.
arguments:
- name: colname
type: string
description: Nom de la colonne de `df2.df3_indicateur` et `public.indicateur` contenant un booléen à comparer.
description: Nom de la colonne de `df2.df3_indicateur` et `public.df1_indicateur` contenant un booléen à comparer.
- name: df3_not_equal
description: |
Macro de test de la `df2.df3_indicateur` par rapport à `public.indicateur`.
Macro de test de la `df2.df3_indicateur` par rapport à `public.df1_indicateur`.
Compare si deux valeurs numériques sont égales. Le paramètre `tolerance` permet d'indiquer une fenêtre de précision.
arguments:
- name: colname
type: string
description: Nom de la colonne de `df2.df3_indicateur` et `public.indicateur` contenant un nombre à comparer.
description: Nom de la colonne de `df2.df3_indicateur` et `public.df1_indicateur` contenant un nombre à comparer.
- name: tolerance
type: float
description: Nombre dans `[0,1]` qui indique le seuil de pourcentage de différence des deux nombres au dessus duquel on considère que les deux nombres sont **différents**. Par exemple, `0.01` signifie que deux nombres seront considérés non égaux s'ils varient de `1%` ou plus.
- name: df3_should_notnull
description: |
Macro de test de la `df2.df3_indicateur` par rapport à `public.indicateur`.
Macro de test de la `df2.df3_indicateur` par rapport à `public.df1_indicateur`.
Indique lorsqu'une valeur vaut `NULL`, mais ne **devrait pas**.
arguments:
- name: colname
type: string
description: Nom de la colonne de `df2.df3_indicateur` et `public.indicateur` contenant une valeur à vérifier.
description: Nom de la colonne de `df2.df3_indicateur` et `public.df1_indicateur` contenant une valeur à vérifier.
- name: df3_should_null
description: |
Macro de test de la `df2.df3_indicateur` par rapport à `public.indicateur`.
Macro de test de la `df2.df3_indicateur` par rapport à `public.df1_indicateur`.
Indique lorsqu'une valeur **n'est pas** `NULL`, mais **devrait l'être**.
arguments:
- name: colname
type: string
description: Nom de la colonne de `df2.df3_indicateur` et `public.indicateur` contenant une valeur à vérifier.
description: Nom de la colonne de `df2.df3_indicateur` et `public.df1_indicateur` contenant une valeur à vérifier.
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,17 @@ group by chantier_id

-- On récupère les directeurs des directions porteuses de chaque chantier
ch_unnest_porteurs_dac as (
select chantier_id, unnest(string_to_array("porteur_ids_DAC", ' | ')) as pi from raw_data.metadata_chantiers mc
select chantier_id, unnest(string_to_array("porteur_ids_DAC", ' | ')) as pi from {{ ref('metadata_chantiers') }} mc
), ch_unnest_porteurs_dac_pnames as (
select a.*, mp.porteur_directeur from ch_unnest_porteurs_dac a
left join raw_data.metadata_porteurs mp on a.pi=mp.porteur_id
select a.*, mp.porteur_directeur, mp.porteur_short from ch_unnest_porteurs_dac a
left join {{ ref('metadata_porteurs') }} mp on a.pi=mp.porteur_id
),
ch_unnest_porteurs_dac_pnames_agg as (
select
chantier_id,
array_agg(pi) p_id,
array_agg(porteur_directeur) p_directeurs
array_agg(porteur_directeur) p_directeurs,
array_agg(porteur_short) p_shorts
from ch_unnest_porteurs_dac_pnames
group by chantier_id
)
Expand All @@ -94,9 +95,11 @@ select
t.nom as territoire_nom,
string_to_array(ch_per, ' | ') as perimetre_ids,
z.zone_type as "maille",
p_names.p_directeurs as directeurs_administration_centrale,
-- coalesce with empty array
coalesce(p_names.p_directeurs, string_to_array('','')) as directeurs_administration_centrale,
string_to_array("porteur_ids_noDAC" , ' | ') as ministeres,
string_to_array("porteur_shorts_DAC" , ' | ') as directions_administration_centrale,
-- coalesce with empty array
coalesce(p_names.p_shorts, string_to_array('','')) as directions_administration_centrale,
string_to_array("ch_dp" , ' | ') as directeurs_projet,
sr.meteo as meteo,
ax.axe_name as axe,
Expand Down
148 changes: 2 additions & 146 deletions data_management/data_factory/models/exposition/chantier.sql
Original file line number Diff line number Diff line change
@@ -1,147 +1,3 @@
with
{{config(materialized = 'view')}}

dfakto_chantier as (

SELECT fact_progress_chantier.tree_node_id,
fact_progress_chantier.avancement_borne,
fact_progress_chantier.avancement_precedent,
dim_tree_nodes.code_chantier,
dim_tree_nodes.zone_code as zone_id,
dim_structures.nom as structure_nom
FROM {{ ref('stg_dfakto__fact_progress_chantiers') }} fact_progress_chantier
JOIN {{ ref('stg_dfakto__dim_tree_nodes') }} dim_tree_nodes ON fact_progress_chantier.tree_node_id = dim_tree_nodes.id
JOIN {{ ref('stg_dfakto__dim_structures') }} dim_structures ON dim_tree_nodes.structure_id = dim_structures.id

),

chantier_est_barometre as (
SELECT m_indicateurs.chantier_id,
bool_or(m_indicateurs.est_barometre) as est_barometre
FROM {{ ref('stg_ppg_metadata__indicateurs') }} m_indicateurs
GROUP BY m_indicateurs.chantier_id
),

synthese_triee_par_date as (
SELECT ROW_NUMBER() OVER (PARTITION BY chantier_id, code_insee, maille ORDER BY date_meteo DESC) AS row_id_by_date_meteo_desc,
chantier_id,
code_insee,
maille,
meteo,
date_meteo
FROM {{ ref('synthese_des_resultats') }}
),

chantier_taux_avancement_departemental as (
SELECT d_chantier.code_chantier,
count(*) > 0 as a_taux_avancement_departemental
FROM dfakto_chantier d_chantier
LEFT JOIN {{ ref('stg_ppg_metadata__zones') }} ppg_zones ON d_chantier.zone_id = ppg_zones.id
WHERE avancement_borne is not NULL
AND ppg_zones.maille = 'DEPT'
GROUP BY d_chantier.code_chantier
),

chantier_taux_avancement_regional as (
SELECT d_chantier.code_chantier,
count(*) > 0 as a_taux_avancement_regional
FROM dfakto_chantier d_chantier
LEFT JOIN {{ ref('stg_ppg_metadata__zones') }} ppg_zones ON d_chantier.zone_id = ppg_zones.id
WHERE avancement_borne is not NULL
AND ppg_zones.maille = 'REG'
GROUP BY d_chantier.code_chantier
),

chantier_meteo_departemental as (
SELECT chantier_id,
count(*) > 0 as a_meteo_departemental
FROM synthese_triee_par_date
WHERE meteo is not NULL
AND maille = 'DEPT'
GROUP BY chantier_id
),

chantier_meteo_regional as (
SELECT chantier_id,
count(*) > 0 as a_meteo_regional
FROM synthese_triee_par_date
WHERE meteo is not NULL
AND maille = 'REG'
GROUP BY chantier_id
),

indicateurs_zones AS (
SELECT DISTINCT
spmi.id as indic_id,
chantier_id,
zones.id as zone_id
FROM {{ ref('stg_ppg_metadata__indicateurs') }} spmi
CROSS JOIN (
SELECT DISTINCT
id
FROM {{ ref('stg_ppg_metadata__zones') }}
) as zones
),
indicateurs_zones_applicables AS (
SELECT DISTINCT
iz.indic_id,
iz.chantier_id,
iz.zone_id,
COALESCE(iiza.est_applicable, TRUE) AS est_applicable
FROM indicateurs_zones iz
LEFT JOIN {{ ref('int_indicateurs_zones_applicables') }} iiza
ON iz.indic_id = iiza.indic_id AND iz.zone_id = iiza.zone_id
),
chantiers_zones_applicables AS (
SELECT
chantier_id,
zone_id,
bool_or(est_applicable) as est_applicable
FROM indicateurs_zones_applicables
GROUP BY chantier_id, zone_id
)

SELECT m_chantiers.id,
m_chantiers.nom,
m_chantiers.code_insee,
d_chantiers.avancement_borne AS taux_avancement,
d_chantiers.avancement_precedent AS taux_avancement_precedent,
m_chantiers.nom AS territoire_nom,
m_chantiers.perimetre_ids,
m_chantiers.maille,
m_chantiers.directeurs_administration_centrale,
m_chantiers.ministeres,
m_chantiers.directions_administration_centrale,
m_chantiers.directeurs_projet,
synthese_triee_par_date.meteo AS meteo,
m_axes.nom AS axe,
m_ppgs.nom AS ppg,
m_chantiers.directeurs_projet_mails,
chantier_est_barometre.est_barometre,
m_chantiers.est_territorialise,
CONCAT(m_chantiers.maille, '-', m_chantiers.code_insee) as territoire_code,
LOWER(m_chantiers.ate)::type_ate as ate,
chantier_ta_dep.a_taux_avancement_departemental,
chantier_ta_reg.a_taux_avancement_regional,
chantier_meteo_dep.a_meteo_departemental,
chantier_meteo_reg.a_meteo_regional,
chantier_za.est_applicable,
false AS a_supprimer
FROM {{ ref('int_chantiers_with_mailles_and_territoires') }} m_chantiers
LEFT JOIN dfakto_chantier d_chantiers
ON m_chantiers.id = d_chantiers.code_chantier
AND m_chantiers.zone_id = d_chantiers.zone_id
AND d_chantiers.structure_nom IN ('Région', 'Département', 'Chantier')
LEFT JOIN {{ ref('stg_ppg_metadata__ppgs') }} m_ppgs ON m_ppgs.id = m_chantiers.ppg_id
LEFT JOIN {{ ref('stg_ppg_metadata__axes') }} m_axes ON m_axes.id = m_ppgs.axe_id
LEFT JOIN chantier_est_barometre on m_chantiers.id = chantier_est_barometre.chantier_id
LEFT JOIN synthese_triee_par_date
ON synthese_triee_par_date.chantier_id = m_chantiers.id
AND synthese_triee_par_date.maille = m_chantiers.maille
AND synthese_triee_par_date.code_insee = m_chantiers.code_insee
LEFT JOIN chantier_taux_avancement_departemental chantier_ta_dep ON m_chantiers.id = chantier_ta_dep.code_chantier
LEFT JOIN chantier_taux_avancement_regional chantier_ta_reg ON m_chantiers.id = chantier_ta_reg.code_chantier
LEFT JOIN chantier_meteo_departemental chantier_meteo_dep ON m_chantiers.id = chantier_meteo_dep.chantier_id
LEFT JOIN chantier_meteo_regional chantier_meteo_reg ON m_chantiers.id = chantier_meteo_reg.chantier_id
LEFT JOIN chantiers_zones_applicables chantier_za ON chantier_za.chantier_id = m_chantiers.id AND chantier_za.zone_id = m_chantiers.zone_id
WHERE synthese_triee_par_date.row_id_by_date_meteo_desc = 1
OR synthese_triee_par_date.row_id_by_date_meteo_desc IS NULL
SELECT * from {{ ref('df1_chantier') }}
Loading

0 comments on commit bb945ed

Please sign in to comment.