Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export de données : Ajouter filtres td afin d'être ISO avec données rapport #4641

Merged
merged 14 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,18 @@
"justMyCode": false,
"django": true
},
{
"name": "Export Dataset",
Copy link
Member

@raphodn raphodn Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

à voir pour le mettre dans le .gitignore ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je poserai la question à l'équipe entière. Personnellement je préférerai mettre ce fichier dans .gitignore

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

j'ai ouvert une PR à ce sujet car j'avais un hotfile qui se baladait/modifiait aussi
#4668

"type": "debugpy",
"request": "launch",
"stopOnEntry": false,
"python": "${workspaceFolder}/venv/bin/python",
"program": "${workspaceFolder}/manage.py",
"args": [
"export_dataset",
],
"justMyCode": false,
"django": true
},
]
}
21 changes: 18 additions & 3 deletions macantine/etl/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ def load_dataset(self):
"""
Load in database
"""
logger.info(f"Loading {len(self.df)} objects in db")
self.warehouse.insert_dataframe(self.df, self.extracted_table_name)

def _clean_dataset(self):
Expand All @@ -212,13 +213,24 @@ def __init__(self):
self.extracted_table_name = "teledeclarations"
self.warehouse = DataWareHouse()
self.schema = json.load(open("data/schemas/schema_analysis.json"))
self.columns = [field["name"] for field in self.schema["fields"]]

def extract_dataset(self):
# Load teledeclarations from prod database into the Data Warehouse
self.df = utils.fetch_teledeclarations(self.years)
self.df.index = self.df.id

if self.df.empty:
logger.warning("Dataset is empty. Creating an empty dataframe with columns from the schema")
self.df = pd.DataFrame(columns=self.columns)

def transform_dataset(self):
if self.df.empty:
logger.warning("Dataset is empty. Skipping transformation")
return

# Use id as index
self.df.index = self.df.id

# Flatten json 'declared_data' column
df_json = pd.json_normalize(self.df["declared_data"])
del df_json["year"]
Expand All @@ -230,6 +242,9 @@ def transform_dataset(self):
# Aggregate columns for complete TD - Must occur before other transformations
self.df = aggregate(self.df)

# Add additionnal filters (that couldn't be processed at queryset)
self.df = utils.filter_teledeclarations(self.df)

self.compute_miscellaneous_columns()

# Convert types
Expand All @@ -243,14 +258,14 @@ def transform_dataset(self):
self.fill_geo_names(prefix="canteen.")

# Fill campaign participation
logger.info("Canteens : Fill campaign participations...")
logger.info("TD : Fill campaign participations...")
for year in utils.CAMPAIGN_DATES.keys():
campaign_participation = utils.map_canteens_td(year)
col_name_campaign = f"declaration_{year}"
self.df[col_name_campaign] = self.df["id"].apply(lambda x: x in campaign_participation)

# Extract the sector names and categories
logger.info("Canteens : Extract sectors...")
logger.info("TD : Extract sectors...")
self.df[["secteur", "catégorie"]] = self.df.apply(
lambda x: utils.format_td_sector_column(x, "canteen.sectors"), axis=1, result_type="expand"
)
Expand Down
40 changes: 39 additions & 1 deletion macantine/etl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,33 @@ def map_sectors():
return sectors_mapper


def filter_empty_values(df: pd.DataFrame, col_name) -> pd.DataFrame:
"""
Filtering out the teledeclarations for wich a certain field is empty
"""
return df.dropna(subset=col_name)


def filter_aberrant_td(df: pd.DataFrame) -> pd.DataFrame:
"""
Filtering out the teledeclarations that :
* products > 1 million €
AND
* an avg meal cost > 20 €
"""
mask = (df["teledeclaration.value_total_ht"] > 1000000) & (
df["teledeclaration.value_total_ht"] / df["canteen.yearly_meal_count"] > 20
)
return df[~mask]


def filter_teledeclarations(df: pd.DataFrame):
df = filter_empty_values(df, col_name="teledeclaration.value_total_ht")
df = filter_empty_values(df, col_name="teledeclaration.value_bio_ht")
df = filter_aberrant_td(df)
return df


def fetch_teledeclarations(years: list) -> pd.DataFrame:
df = pd.DataFrame()
for year in years:
Expand All @@ -238,7 +265,18 @@ def fetch_teledeclarations(years: list) -> pd.DataFrame:
),
status=Teledeclaration.TeledeclarationStatus.SUBMITTED,
canteen_id__isnull=False,
).values()
canteen_siret__isnull=False,
diagnostic__value_total_ht__isnull=False,
diagnostic__value_bio_ht__isnull=False,
)
.exclude(
canteen__deletion_date__range=(
CAMPAIGN_DATES[year]["start_date"],
CAMPAIGN_DATES[year]["end_date"],
)
)
.exclude(canteen_siret="")
.values()
)
df = pd.concat([df, df_year])
else:
Expand Down
69 changes: 48 additions & 21 deletions macantine/tests/test_etl_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,57 @@ def test_extraction_teledeclaration(self):
"""
Only teledeclarations that occurred during teledeclaration campaigns should be extracted
"""
canteen = CanteenFactory.create()
canteen = CanteenFactory.create(siret="98648424243607")
canteen_no_siret = CanteenFactory.create()
applicant = UserFactory.create()
with freeze_time("1991-01-14"): # Faking time to mock creation_date
diagnostic_1990 = DiagnosticFactory.create(canteen=canteen, year=1990, diagnostic_type=None)
_ = Teledeclaration.create_from_diagnostic(diagnostic_1990, applicant)

with freeze_time("2023-05-14"): # Faking time to mock creation_date
diagnostic_2022 = DiagnosticFactory.create(canteen=canteen, year=2022, diagnostic_type=None)
td_2022 = Teledeclaration.create_from_diagnostic(diagnostic_2022, applicant)

with freeze_time("2024-02-14"): # Faking time to mock creation_date
diagnostic_2023 = DiagnosticFactory.create(canteen=canteen, year=2023, diagnostic_type=None)
td_2023 = Teledeclaration.create_from_diagnostic(diagnostic_2023, applicant)

etl_stats = ETL_ANALYSIS_TD()

etl_stats.extract_dataset()
self.assertEqual(
len(etl_stats.df),
2,
"There should be two teledeclaration. None for 1990 (no campaign). One for 2022 and one for 2023",
)
self.assertEqual(etl_stats.df[etl_stats.df.id == td_2022.id].year.iloc[0], 2022)
self.assertEqual(etl_stats.df[etl_stats.df.id == td_2023.id].year.iloc[0], 2023)
test_cases = [
{
"date_mocked": "1991-01-14",
"year": 1990,
"canteen": canteen,
"delete_canteen": False,
"expected_outcome": "no_extraction",
"msg": "Outside any campaign date",
},
{
"date_mocked": "2023-05-14",
"year": 2022,
"canteen": canteen,
"delete_canteen": False,
"expected_outcome": "extraction",
"msg": "Valid",
},
{
"date_mocked": "2024-02-14",
"year": 2023,
"canteen": canteen,
"delete_canteen": True,
"expected_outcome": "no_extraction",
"msg": "Canteen deleted during campaign",
},
{
"date_mocked": "2024-02-14",
"year": 2023,
"canteen": canteen_no_siret,
"delete_canteen": False,
"expected_outcome": "no_extraction",
"msg": "Canteen without a siret",
},
]
for tc in test_cases:
with freeze_time(tc["date_mocked"]): # Faking time to mock creation_date
diag = DiagnosticFactory.create(canteen=tc["canteen"], year=tc["year"], diagnostic_type=None)
td = Teledeclaration.create_from_diagnostic(diag, applicant)
if tc["delete_canteen"]:
tc["canteen"].delete()

etl_stats.extract_dataset()
if tc["expected_outcome"] == "extraction":
self.assertEqual(len(etl_stats.df[etl_stats.df.id == td.id]), 1)
else:
self.assertEqual(len(etl_stats.df[etl_stats.df.id == td.id]), 0)

def test_get_egalim_hors_bio(self):
data = {
Expand Down
8 changes: 4 additions & 4 deletions macantine/tests/test_etl_open_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_td_range_years(self, mock):
"""
Only teledeclarations that occurred during one specific teledeclaration campaign should be extracted
"""
canteen = CanteenFactory.create()
canteen = CanteenFactory.create(siret="98648424243607")
applicant = UserFactory.create()
test_cases = [
{"name": "Ignore years out of range", "year": 1990, "expected_outcome": 0},
Expand All @@ -37,7 +37,7 @@ def test_td_range_years(self, mock):

@freeze_time("2023-05-14") # Faking time to mock creation_date
def test_ignore_cancelled_tds(self, mock):
canteen = CanteenFactory.create()
canteen = CanteenFactory.create(siret="98648424243607")
applicant = UserFactory.create()
diagnostic = DiagnosticFactory.create(canteen=canteen, year=2022, diagnostic_type=None)
teledeclaration = Teledeclaration.create_from_diagnostic(diagnostic, applicant)
Expand Down Expand Up @@ -119,10 +119,10 @@ def test_extraction_canteen(self, mock):
self.assertEqual(etl_canteen.len_dataset(), 0, "There shoud be an empty dataframe")

# Adding data in the db
canteen_1 = CanteenFactory.create()
canteen_1 = CanteenFactory.create(siret="98648424243607")
canteen_1.managers.add(UserFactory.create())

canteen_2 = CanteenFactory.create() # Another canteen, but without a manager
canteen_2 = CanteenFactory.create(siret="98648424243607") # Another canteen, but without a manager
canteen_2.managers.clear()

etl_canteen.extract_dataset()
Expand Down
Loading