Skip to content

Commit

Permalink
[WIP] python model to calculate new and ancient visitors for each das…
Browse files Browse the repository at this point in the history
…hboard
  • Loading branch information
laurinehu authored and vperron committed Jun 23, 2023
1 parent c11a9f3 commit fe865b7
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
11 changes: 11 additions & 0 deletions dbt/models/marts/properties.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,14 @@ models:
description: >
Table créée pour récupérer la dernière embauche (si elle existe) d'un candidat.
Cette information est utilisée dans la table stats du C1 afin des filtrer les données des candidats par dernière date d'embauche.
- name: suivi_utilisateurs_tb_prive_semaine
description: >
Table créée pour permettre le suivi des visites des utilisateurs de nos tableaux de bord.
Permet de récupérer le nombre de visites distincte par tableau de bord et par semaine pour chaque utilisateur, et ajoute des informations sur les utilisateurs (email, type).
- name: suivi_visites_tb_prive_semaine
description: >
Table permettant de suivre pour chaque tb et chaque semaine la liste des utilisateurs qui se sont connecté.
- name: suivi_tb_prive_semaine
description: >
Ce modèle python permet de calculer les visiteurs anciens et nouveaux chaque semaine à partir de la table suivi_visites_tb_prive_semaine.
Il permet de calculer la rétention des utilisateurs sur les tableaux de bords privés.
68 changes: 68 additions & 0 deletions dbt/models/marts/suivi_tb_prive_semaine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import pandas as pd


def add_past_and_current_users(dtf):
"""
for one dataframe containing the list of weekly users for a dashboard,
calculates ancient and new users and add them as new column of the dataframe
"""
preceding = set()
all_col = []
nb_all = []
new_col = []
nb_new = []
already_visited_col = []
nb_ancient = []

# for all row in the dtf
# recover all preceding lines
# concatenate users list
for ix, row in dtf.iterrows():
# recover visitors of this current week
current = set(dtf.loc[ix]["liste_utilisateurs"])

# get list of users that are new this week
new = current.difference(preceding)
new_col.append(list(new))
nb_new.append(len(new))

# get list of users that already visited previous weeks
already_visited = preceding.intersection(current)
already_visited_col.append(list(already_visited))
nb_ancient.append(len(already_visited))

# add these users to list of users that already visited
preceding = preceding.union(current)
all_col.append(list(preceding))
nb_all.append(len(preceding))

dtf["nouveaux"] = new_col
dtf["nb_nouveaux"] = nb_new
dtf["anciens"] = already_visited_col
dtf["nb_anciens"] = nb_ancient
dtf["tous"] = all_col
dtf["nb_visiteurs_cumulé"] = nb_all
return dtf


def follow_visits(dtf):
"""
to be applied on the table suivi_tb_prive_semaine
adds new and previous users by week for each dashboard
"""
# recover dict of tbs follow up
df_dict = dict(tuple(dtf.groupby("nom_tb")))
# sort by week
df_dict = {k: v.sort_values(by="semaine") for k, v in df_dict.items()}
df_new_dict = {}
# get users follow up
for k, v in df_dict.items():
df_new_dict[k] = add_past_and_current_users(v)
return pd.concat(df_new_dict.values())


def model(dbt, session):
# dbt.config(materialized="table")
df = dbt.ref("suivi_visites_tb_prive_semaine")
df = follow_visits(df)
return df
File renamed without changes.

0 comments on commit fe865b7

Please sign in to comment.