74 lines
3.2 KiB
Python
74 lines
3.2 KiB
Python
|
import json
|
||
|
import pendulum
|
||
|
import requests
|
||
|
from logger import Logger
|
||
|
from settings import Settings
|
||
|
from sql import crud, models
|
||
|
|
||
|
import pprint
|
||
|
|
||
|
settings = Settings()
|
||
|
logging = Logger(__name__)
|
||
|
log = logging.log
|
||
|
|
||
|
|
||
|
def parseBoucles(boucles):
|
||
|
for boucle in boucles:
|
||
|
log.info(f'Get {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)
|
||
|
|
||
|
def parseRecord(record, force = False):
|
||
|
if not crud.is_boucle_id_exist(record["fields"]["boucle_num"]):
|
||
|
log.warning(f'Boucle {record["fields"]["boucle_num"]} not exist')
|
||
|
log.warning(f'Add this boucle in temp table')
|
||
|
unaccounted_table = True
|
||
|
else:
|
||
|
unaccounted_table = False
|
||
|
|
||
|
for hour in ["00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"]:
|
||
|
if hour in record["fields"]:
|
||
|
if record["fields"]["vacances_zone_b"] == "Hors Vacances":
|
||
|
holiday = False
|
||
|
else:
|
||
|
holiday = True
|
||
|
date = pendulum.parse(record["fields"]["dateformat"]).at(int(hour), 0, 0)
|
||
|
if not crud.is_comptage_exist(date, record["fields"]["boucle_num"], unaccounted_table):
|
||
|
comptage = models.Comptage(
|
||
|
id_boucle=record["fields"]["boucle_num"],
|
||
|
datetime=date,
|
||
|
count=record["fields"][hour],
|
||
|
holiday = holiday,
|
||
|
week_day = record["fields"]["jour_de_la_semaine"]
|
||
|
)
|
||
|
crud.create_comptage(comptage, unaccounted_table)
|
||
|
else:
|
||
|
db_comptage = crud.get_comptage_by_date_and_boucle(date, record["fields"]["boucle_num"], unaccounted_table)
|
||
|
log.error(f"Entry already exist ({record['fields']['boucle_num']}, {date})")
|
||
|
log.debug(f'Check if {db_comptage.count} != {record["fields"][hour]}')
|
||
|
if db_comptage.count != record["fields"][hour] 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["field"][hour]}')
|
||
|
comptage = models.Comptage(
|
||
|
id=db_comptage.id,
|
||
|
id_boucle=db_comptage.id_boucle,
|
||
|
datetime=db_comptage.datetime,
|
||
|
count=record["fields"][hour],
|
||
|
holiday = holiday,
|
||
|
week_day = record["fields"]["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')
|
||
|
else:
|
||
|
log.error(f'{hour} is not present for {record["fields"]["boucle_num"]}')
|
||
|
|