Skip to content
Snippets Groups Projects
Commit fdf662e9 authored by Matteo Westerwinter's avatar Matteo Westerwinter
Browse files

cleaned up querry

parent 9794e810
Branches
No related tags found
2 merge requests!227merge dev into testing,!226enable aggregated filtering
Pipeline #251204 failed
......@@ -231,12 +231,9 @@ class TimeseriesQuery:
# If only certain fields are selected the return type is not a orm object anymore but a dict
# sort input fields to be sure to replace station_changelog before changelog
fields = sorted(fields.split(","), reverse=True)
lconstr_roles = any(field in roles_params for field in fields)
if "role" in fields:
fields.remove("role")
fields += roles_params
# lconstr_glob = True #used to be set here but is unused
field_map = {
"id": models.Timeseries.id,
......@@ -249,50 +246,41 @@ class TimeseriesQuery:
"station_country": StationmetaCore.country,
}
query = db.query(*[field_map.get(field, text(field)) for field in fields])
query_select = [field_map.get(field, text(field)) for field in fields]
# ordering is needed (because of limit/offset-option)
# --> Timeseries.id is added to columns while doing distinct! (see: https://docs.sqlalchemy.org/en/14/changelog/migration_20.html#migration-20-query-distinct)
# (duplicates are being created, which means that limit/offset in the query itself is useless!)
# hot fix to speed up the search
else:
lconstr_roles = False
query = db.query(models.Timeseries)
query_select = [models.Timeseries]
query = (
query.select_from(models.Timeseries)
db.query(*query_select)
.select_from(models.Timeseries)
.distinct()
.filter(text(t_filter), text(s_c_filter), text(s_g_filter), text(t_r_filter))
.join(StationmetaCore, models.Timeseries.station_id == StationmetaCore.id)
.join(StationmetaGlobal, StationmetaCore.id == StationmetaGlobal.station_id)
.join(
timeseries_timeseries_roles_table,
models.Timeseries.id == timeseries_timeseries_roles_table.c.timeseries_id,
)
.join(models.TimeseriesRole, timeseries_timeseries_roles_table.c.role_id == models.TimeseriesRole.id)
.join(Contact, models.TimeseriesRole.contact_id == Contact.id)
.join(Organisation, Contact.organisation_id == Organisation.id)
.join(Person, Contact.person_id == Person.id)
.join(Variable, Variable.id == models.Timeseries.variable_id)
.join(StationmetaCore)
.join(StationmetaGlobal)
.join(timeseries_timeseries_roles_table)
.join(models.TimeseriesRole)
.join(Contact)
.join(Organisation)
.join(Person)
.join(Variable)
.execution_options(stream_results=True)
.order_by(models.Timeseries.id)
)
# Apply NOT filter with role logic
if "NOT" in t_r_filter:
role_string = query_params.get("has_role")[1:]
role_ids = get_role_id_from_string(db, role_string)
query = query.filter(
~models.Timeseries.id.in_(
select(timeseries_timeseries_roles_table.c.timeseries_id).where(
timeseries_timeseries_roles_table.c.role_id.in_(role_ids)
)
select(timeseries_timeseries_roles_table.c.timeseries_id)
.where(timeseries_timeseries_roles_table.c.role_id.in_(get_role_id_from_string(db, query_params.get("has_role")[1:])))
)
)
if limit:
query = query.limit(limit).offset(offset)
return TimeseriesQuery(sign, query, fields, lconstr_roles)
return TimeseriesQuery(sign, query, fields, lconstr_roles=any(field in roles_params for field in fields))
def adapt_objects(self, db):
return [adapt_db_object(db_object, db, self.fields, self.lconstr_roles) for db_object in self.query]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment