87 lines
3.1 KiB
Python
87 lines
3.1 KiB
Python
import pendulum
|
|
from logger import Logger
|
|
from settings import Settings
|
|
from sql import crud, models
|
|
|
|
|
|
settings = Settings()
|
|
logging = Logger(__name__)
|
|
log = logging.log
|
|
|
|
|
|
def parseBoucles(boucles):
|
|
for boucle in boucles:
|
|
log.info(f'Get {boucle["fields"]["libelle"]}')
|
|
if not crud.is_boucle_id_exist(boucle["fields"]["boucle_num"]):
|
|
log.info(f'Insert {boucle["fields"]["libelle"]}')
|
|
boucleModel = models.Boucle(
|
|
id=boucle["fields"]["boucle_num"],
|
|
name=boucle["fields"]["libelle"],
|
|
geolocalisation=f'POINT({boucle["fields"]["geolocalisation"][0]} {boucle["fields"]["geolocalisation"][1]})',
|
|
)
|
|
crud.create_boucle(boucleModel)
|
|
else:
|
|
log.info(f'Update {boucle["fields"]["libelle"]}')
|
|
boucleModel = models.Boucle(
|
|
id=boucle["fields"]["boucle_num"],
|
|
name=boucle["fields"]["libelle"],
|
|
geolocalisation=f'POINT({boucle["fields"]["geolocalisation"][0]} {boucle["fields"]["geolocalisation"][1]})',
|
|
)
|
|
crud.update_boucle(boucleModel)
|
|
|
|
|
|
def parseRecord(record, force=False):
|
|
if not crud.is_boucle_id_exist(record["boucle_num"]):
|
|
log.warning(f'Boucle {record["boucle_num"]} not exist')
|
|
log.warning("Add this boucle in temp table")
|
|
unaccounted_table = True
|
|
else:
|
|
unaccounted_table = False
|
|
|
|
if record["vacances_zone_b"] == "Hors Vacances":
|
|
holiday = False
|
|
else:
|
|
holiday = True
|
|
date = pendulum.parse(record["dateformat"])
|
|
if not crud.is_comptage_exist(
|
|
date, record["boucle_num"], unaccounted_table
|
|
):
|
|
comptage = models.Comptage(
|
|
id_boucle=record["boucle_num"],
|
|
datetime=date,
|
|
count=record["total"],
|
|
holiday=holiday,
|
|
week_day=record["jour_de_la_semaine"],
|
|
)
|
|
crud.create_comptage(comptage, unaccounted_table)
|
|
else:
|
|
db_comptage = crud.get_comptage_by_date_and_boucle(
|
|
date, record["boucle_num"], unaccounted_table
|
|
)
|
|
log.error(
|
|
f"Entry already exist ({record['results']['boucle_num']}, {date})"
|
|
)
|
|
log.debug(
|
|
f'Check if {db_comptage.count} != {record["total"]}'
|
|
)
|
|
if db_comptage.count != record["total"] or force:
|
|
if force:
|
|
log.info(f"Update with force {db_comptage.id}")
|
|
else:
|
|
log.warning(
|
|
f'Entry {db_comptage.id} have a different count (DB : {db_comptage.count}, data {record["total"]}'
|
|
)
|
|
comptage = models.Comptage(
|
|
id=db_comptage.id,
|
|
id_boucle=db_comptage.id_boucle,
|
|
datetime=db_comptage.datetime,
|
|
count=record["total"],
|
|
holiday=holiday,
|
|
week_day=record["jour_de_la_semaine"],
|
|
)
|
|
crud.update_comptage(comptage, unaccounted_table)
|
|
else:
|
|
log.info(f"Skip {db_comptage.id}, already exist")
|
|
# else:
|
|
# log.error(f'Boucle {record["fields"]["boucle_num"]} not exist')
|