Code source de geonature.core.gn_meta.utils
import datetime as dt
from geonature.utils.env import db
from geonature.utils.module import is_module_installed
from sqlalchemy.sql import select
from sqlalchemy.sql.functions import func
from geonature.core.gn_synthese.models import Synthese
from geonature.core.gn_meta.models import TDatasets
from geonature.utils.config import config
import geonature.utils.filemanager as fm
if "OCCHAB" in config:
from gn_module_occhab.models import OccurenceHabitat, Station
[docs]
def get_acquisition_framework_stats(id_acquisition_framework):
dataset_ids = db.session.scalars(
select(TDatasets.id_dataset).where(
TDatasets.id_acquisition_framework == id_acquisition_framework
)
).all()
nb_datasets = len(dataset_ids)
nb_taxons = db.session.execute(
select(func.count(func.distinct(Synthese.cd_nom))).where(
Synthese.id_dataset.in_(dataset_ids)
)
).scalar_one()
nb_observations = db.session.execute(
select(func.count("*"))
.select_from(Synthese)
.where(Synthese.dataset.has(TDatasets.id_acquisition_framework == id_acquisition_framework))
).scalar_one()
nb_habitats = 0
if (
is_module_installed("gn_module_occhab", check_if_all_revisions_have_been_applied=False)
and nb_datasets > 0
):
nb_habitats = db.session.execute(
select(func.count("*"))
.select_from(OccurenceHabitat)
.join(Station)
.where(Station.id_dataset.in_(dataset_ids))
).scalar_one()
return {
"nb_dataset": nb_datasets,
"nb_taxons": nb_taxons,
"nb_observations": nb_observations,
"nb_habitats": nb_habitats,
}