[docs]defget_taxa_count():""" Get taxa count in synthese filtering with generic parameters .. :quickref: Synthese; Parameters ---------- id_dataset: `int` (query parameter) Returns ------- count: `int`: the number of taxon """params=request.argsreturndb.session.scalar(select(func.count(distinct(Synthese.cd_nom))).where(Synthese.id_dataset==params["id_dataset"]if"id_dataset"inparamselseTrue))
[docs]defget_observation_count():""" Get observations found in a given dataset .. :quickref: Synthese; Parameters ---------- id_dataset: `int` (query parameter) Returns ------- count: `int`: the number of observation """params=request.argsquery=select(func.count(Synthese.id_synthese)).select_from(Synthese)if"id_dataset"inparams:query=query.where(Synthese.id_dataset==params["id_dataset"])returnDB.session.execute(query).scalar_one()
[docs]defobservation_count_per_column(column):""" Get observations count group by a given column This function was used to count observations per dataset, but this usage have been replaced by TDatasets.synthese_records_count. Remove this function as it is very inefficient? """ifcolumnnotininspect(Synthese).column_attrs:raiseBadRequest(f"No column name {column} in Synthese")synthese_column=getattr(Synthese,column)stmt=(select(func.count(Synthese.id_synthese).label("count"),synthese_column.label(column),).select_from(Synthese).group_by(synthese_column))returnjsonify(DB.session.execute(stmt).fetchall())
[docs]defgeneral_stats(permissions):"""Return stats about synthese. .. :quickref: Synthese; - nb of observations - nb of distinct species - nb of distinct observer - nb of datasets """nb_allowed_datasets=db.session.scalar(select(func.count("*")).select_from(TDatasets).where(TDatasets.filter_by_readable().whereclause))results={"nb_allowed_datasets":nb_allowed_datasets}queries={"nb_obs":select(Synthese.id_synthese),"nb_distinct_species":select(func.distinct(Synthese.cd_nom),),"nb_distinct_observer":select(func.distinct(Synthese.observers)),}forkey,queryinqueries.items():synthese_query=SyntheseQuery(Synthese,query,{})synthese_query.filter_query_with_permissions(g.current_user,permissions)results[key]=db.session.scalar(select(func.count("*")).select_from(synthese_query.query))data={"nb_data":results["nb_obs"],"nb_species":results["nb_distinct_species"],"nb_observers":results["nb_distinct_observer"],"nb_dataset":results["nb_allowed_datasets"],}returndata