From 8833c1e5c28b0c7932a1435debf641392e3219fa Mon Sep 17 00:00:00 2001 From: laurinehu Date: Tue, 17 Dec 2024 19:19:11 +0100 Subject: [PATCH] solve histo of file active --- .../legacy/daily/candidats_sans_solution.sql | 101 ------------------ .../daily/candidats_recherche_active.sql | 8 +- ...andidatures_candidats_recherche_active.sql | 5 +- dbt/models/marts/daily/properties.yml | 3 + .../candidats_recherche_active_histo.sql | 44 ++++++++ dbt/tests/test_candidats_file_active.sql | 4 + 6 files changed, 60 insertions(+), 105 deletions(-) delete mode 100644 dbt/models/legacy/daily/candidats_sans_solution.sql create mode 100644 dbt/models/marts/weekly/candidats_recherche_active_histo.sql create mode 100644 dbt/tests/test_candidats_file_active.sql diff --git a/dbt/models/legacy/daily/candidats_sans_solution.sql b/dbt/models/legacy/daily/candidats_sans_solution.sql deleted file mode 100644 index 5c784117..00000000 --- a/dbt/models/legacy/daily/candidats_sans_solution.sql +++ /dev/null @@ -1,101 +0,0 @@ -/* - -L'objectif est de suivre le nombre de candidats sans solution à 30 jours et pourcentage de ces candidats dans la totalité des candidats: - - nb candidats qui 30 jours après leur inscription restent sans candidatures -ou - - avec candidatures dont l’état est différent de “acceptée” - -*/ -with candidature as ( - select distinct - date_candidature, - "état", - date_inscription, - (id_candidat) as identifiant_candidat, - count(distinct candidatures.id) as nombre_candidature - from - {{ source('emplois', 'candidatures') }} - left join - candidats on id_candidat = public.candidats.id - where - date_candidature <= date_trunc('month', date_inscription) + interval '1 month' - and date_candidature >= date_inscription - and candidatures.injection_ai = 0 - and candidats.injection_ai = 0 - group by - identifiant_candidat, - date_candidature, - date_inscription, - "état" - order by - identifiant_candidat -), - -candidats_sans_candidatures as ( - select distinct - identifiant_candidat, - date_inscription - from candidature - where - nombre_candidature = 0 -), - -candidats_avc_candidature_acceptee as ( - select - identifiant_candidat, - date_inscription, - sum( - case - when "état" = 'Candidature acceptée' then 1 - else 0 - end - ) as nb_candidature_acceptee - from candidature - group by - identifiant_candidat, - date_inscription - order by - identifiant_candidat -), - -union_table as ( - select * - from - candidats_sans_candidatures - union - ( - select - identifiant_candidat, - date_inscription - from - candidats_avc_candidature_acceptee - where - nb_candidature_acceptee = 0 - ) -), - -b as ( - select - date_inscription, - count(distinct id) as nombre_candidats - from - {{ ref('candidats') }} - where candidats.injection_ai = 0 - group by date_inscription -) - -select - a.nombre_candidats_ss_solution, - a.date_inscription, - b.nombre_candidats -from ( - select - date_inscription, - count(distinct identifiant_candidat) as nombre_candidats_ss_solution - from - union_table - group by - date_inscription -) as a -left join b - on a.date_inscription = b.date_inscription diff --git a/dbt/models/marts/daily/candidats_recherche_active.sql b/dbt/models/marts/daily/candidats_recherche_active.sql index 16570d7f..dfd0360c 100644 --- a/dbt/models/marts/daily/candidats_recherche_active.sql +++ b/dbt/models/marts/daily/candidats_recherche_active.sql @@ -27,7 +27,13 @@ select sum(case when cc."état" = 'Candidature acceptée' then 1 else 0 end) as nb_candidatures_acceptees, sum(case when cc."état" != 'Candidature acceptée' then 1 else 0 end) as nb_candidatures_sans_accept, coalesce(sum(case when cc."état" = 'Candidature acceptée' then 1 else 0 end) > 0) as a_eu_acceptation, - coalesce(max(cc.date_embauche) >= current_date - interval '6 months', max(cc.date_embauche) is not null) as a_eu_embauche + coalesce(max(cc.date_embauche) >= current_date - interval '6 months', max(cc.date_embauche) is not null) as a_eu_embauche, + case + when min(cc.date_premiere_candidature) <= current_date - interval '30 days' + and coalesce(sum(case when cc."état" = 'Candidature acceptée' then 1 else 0 end), 0) = 0 + then true + else false + end as file_active_30_jours from {{ ref('stg_candidats_candidatures') }} as cc where cc.date_candidature >= current_date - interval '6 months' group by diff --git a/dbt/models/marts/daily/candidatures_candidats_recherche_active.sql b/dbt/models/marts/daily/candidatures_candidats_recherche_active.sql index faf1e023..5d0a8f66 100644 --- a/dbt/models/marts/daily/candidatures_candidats_recherche_active.sql +++ b/dbt/models/marts/daily/candidatures_candidats_recherche_active.sql @@ -1,7 +1,6 @@ select - {{ pilo_star(ref('stg_candidats_candidatures'), relation_alias="cdd") }}, - cra.nb_candidatures_acceptees, - cra.delai_derniere_candidature + cdd.id as id_candidature, + cra.id as id_candidat from {{ ref('stg_candidats_candidatures') }} as cdd right join {{ ref('candidats_recherche_active') }} as cra on cra.id = cdd.id diff --git a/dbt/models/marts/daily/properties.yml b/dbt/models/marts/daily/properties.yml index 6ce630b9..022ee89a 100644 --- a/dbt/models/marts/daily/properties.yml +++ b/dbt/models/marts/daily/properties.yml @@ -172,6 +172,9 @@ models: description: > Table contenant les candidats actifs les 6 derniers mois et permettant de savoir s'ils ont eu des candidatures acceptées ainsi que le délai depuis la dernière candidature émise. Utile pour le calcul des candidats sans solution selon la définition simplifiée 'candidat actif les 6 derniers mois n'ayant eu aucune candidature acceptée'. + - name: candidatures_candidats_recherche_active + description: > + Contient les candidatures des 6 derniers mois pour les candidats en recherche active. - name: visites_ft description: > Table permettant le suivi des visites et visiteurs uniques sur le TB 406 - Requetage FT. diff --git a/dbt/models/marts/weekly/candidats_recherche_active_histo.sql b/dbt/models/marts/weekly/candidats_recherche_active_histo.sql new file mode 100644 index 00000000..b81fdb11 --- /dev/null +++ b/dbt/models/marts/weekly/candidats_recherche_active_histo.sql @@ -0,0 +1,44 @@ +{{ + config( + materialized='incremental', + unique_key=['id', 'semaine_valide'], + post_hook=""" + DELETE FROM {{ this }} + WHERE date_derniere_candidature_acceptee is not null and delai_premiere_candidature > 30 + """ + ) +}} + +-- recover last known date in the table to increment for new period +with last_known_date as ( + select + max(date_trunc('week', dbt_valid_to)) as lkd + from {{ ref('candidats_recherche_active_snapshot') }} +) + +select + {{ pilo_star(ref('candidats_recherche_active_snapshot'), except=["dbt_valid_from", "dbt_valid_to"]) }}, + date_trunc('week', week_series) as semaine_valide +from + {{ ref('candidats_recherche_active_snapshot') }} +cross join + last_known_date +cross join + generate_series( + date_trunc('week', dbt_valid_from), + -- when dbt_valid_to is empty, state is still the same today + -- so we compute row for each week from valid_to to today + coalesce(date_trunc('week', dbt_valid_to), last_known_date.lkd), + interval '1 week' + ) as week_series + +{% if is_incremental() %} + +-- we only update rows that have changed +-- the previous state is no longer current if : +-- 1: dbt_valid_from is bigger than last week date (this line is the new state) // dbt_valid_from is bigger than the last known date +-- 2: dbt_valid_to is bigger than last week date (this line is the last state that is now finished) // dbt_valid +where dbt_valid_from >= last_known_date.lkd +or dbt_valid_to >= last_known_date.lkd + +{% endif %} diff --git a/dbt/tests/test_candidats_file_active.sql b/dbt/tests/test_candidats_file_active.sql new file mode 100644 index 00000000..03f065cb --- /dev/null +++ b/dbt/tests/test_candidats_file_active.sql @@ -0,0 +1,4 @@ +-- if there is a date_derniere_candidature_acceptee, the candidate is not in the file active +select * +from candidats_recherche_active +where file_active_30_jours = true and date_derniere_candidature_acceptee is not null