我们从Python开源项目中,提取了以下17个代码示例,用于说明如何使用sqlalchemy.sql.expression.false()。
def set_rse_usage(rse, source, used, free, session=None): """ Set RSE usage information. :param rse: the location name. :param source: The information source, e.g. srm. :param used: the used space in bytes. :param free: the free in bytes. :param session: The database session in use. :returns: True if successful, otherwise false. """ rse_id = get_rse_id(rse, session=session) rse_usage = models.RSEUsage(rse_id=rse_id, source=source, used=used, free=free) # versioned_session(session) rse_usage = session.merge(rse_usage) rse_usage.save(session=session) # rse_usage_history = models.RSEUsage.__history_mapper__.class_(rse_id=rse.id, source=source, used=used, free=free) # rse_usage_history.save(session=session) return True
def get_rse_usage(rse, source=None, rse_id=None, session=None): """ get rse usage information. :param rse: The rse name. :param source: The information source, e.g. srm. :param rse_id: The RSE id. :param session: The database session in use. :returns: True if successful, otherwise false. """ if not rse_id: rse_id = get_rse_id(rse, session=session) query = session.query(models.RSEUsage).filter_by(rse_id=rse_id) if source: query = query.filter_by(source=source) usage = list() for row in query: usage.append({'rse': rse, 'source': row.source, 'used': row.used, 'free': row.free, 'total': (row.free or 0) + (row.used or 0), 'updated_at': row.updated_at}) return usage
def set_rse_limits(rse, name, value, session=None): """ Set RSE limits. :param rse: The RSE name. :param name: The name of the limit. :param value: The feature value. Set to -1 to remove the limit. :param session: The database session in use. :returns: True if successful, otherwise false. """ rse_id = get_rse_id(rse, session=session) rse_limit = models.RSELimit(rse_id=rse_id, name=name, value=value) rse_limit = session.merge(rse_limit) rse_limit.save(session=session) return True
def set_rse_transfer_limits(rse, activity, rse_id=None, rse_expression=None, max_transfers=0, transfers=0, waitings=0, session=None): """ Set RSE transfer limits. :param rse: The RSE name. :param activity: The activity. :param rse_expression: RSE expression string. :param max_transfers: Maximum transfers. :param transfers: Current number of tranfers. :param waitings: Current number of waitings. :param session: The database session in use. :returns: True if successful, otherwise false. """ try: if not rse_id: rse_id = get_rse_id(rse=rse, session=session) rse_tr_limit = models.RSETransferLimit(rse_id=rse_id, activity=activity, rse_expression=rse_expression, max_transfers=max_transfers, transfers=transfers, waitings=waitings) rse_tr_limit = session.merge(rse_tr_limit) rowcount = rse_tr_limit.save(session=session) return rowcount except IntegrityError, e: raise exception.RucioException(e.args)
def has_auth_webhooks(user_id): """??????????Webhooks""" # create webhooks created_webhooks = WebHook.query.filter_by( user_id=user_id, deleted=False).all() # collaborator webhooks collaborated_webhooks = \ WebHook.query.join(Collaborator, Collaborator.webhook_id == WebHook.id) \ .filter(Collaborator.user_id == user_id) \ .filter(WebHook.deleted == false()).all() webhooks = created_webhooks + collaborated_webhooks # ????? webhooks = list(sorted(set(webhooks), key=lambda x: x.id, reverse=True)) return webhooks
def transformations(self, relationship="all"): """Get all the transformations of this info. Return a list of transformations involving this info. ``relationship`` can be "parent" (in which case only transformations where the info is the ``info_in`` are returned), "child" (in which case only transformations where the info is the ``info_out`` are returned) or ``all`` (in which case any transformations where the info is the ``info_out`` or the ``info_in`` are returned). The default is ``all`` """ if relationship not in ["all", "parent", "child"]: raise(ValueError( "You cannot get transformations of relationship {}" .format(relationship) + "Relationship can only be parent, child or all.")) if relationship == "all": return Transformation\ .query\ .filter(and_(Transformation.failed == false(), or_(Transformation.info_in == self, Transformation.info_out == self)))\ .all() if relationship == "parent": return Transformation\ .query\ .filter_by(info_in_id=self.id, failed=False)\ .all() if relationship == "child": return Transformation\ .query\ .filter_by(info_out_id=self.id, failed=False)\ .all()
def __query__(self): name = create_search_name(camel_to_underscore(self.cls.__name__)) type_ = self.request_args.get(name['type']) word = self.request_args.get(name['word']) q = None if type_ == self.attribute_name: try: q = self.attribute == self.type_(word) except ValueError: return false() return q
def __query__(self): name = create_search_name(self.identifier) word = self.request_args.get(name['word']) type = self.request_args.get(name['type']) q = None if word and type == self.alias_attr.name: try: q = self.alias_attr == self.type_(word) except ValueError: q = false() return q
def _list_replicas_for_datasets(dataset_clause, state_clause, rse_clause, session): """ List file replicas for a list of datasets. :param session: The database session in use. """ replica_query = session.query(models.DataIdentifierAssociation.child_scope, models.DataIdentifierAssociation.child_name, models.DataIdentifierAssociation.bytes, models.DataIdentifierAssociation.md5, models.DataIdentifierAssociation.adler32, models.RSEFileAssociation.path, models.RSEFileAssociation.state, models.RSE.rse, models.RSE.rse_type, models.RSE.volatile).\ with_hint(models.RSEFileAssociation, text="INDEX_RS_ASC(CONTENTS CONTENTS_PK) INDEX_RS_ASC(REPLICAS REPLICAS_PK) NO_INDEX_FFS(CONTENTS CONTENTS_PK)", dialect_name='oracle').\ outerjoin(models.RSEFileAssociation, and_(models.DataIdentifierAssociation.child_scope == models.RSEFileAssociation.scope, models.DataIdentifierAssociation.child_name == models.RSEFileAssociation.name)).\ join(models.RSE, models.RSE.id == models.RSEFileAssociation.rse_id).\ filter(models.RSE.deleted == false()).\ filter(models.RSE.staging_area == false()).\ filter(or_(*dataset_clause)).\ order_by(models.DataIdentifierAssociation.child_scope, models.DataIdentifierAssociation.child_name) if state_clause is not None: replica_query = replica_query.filter(and_(state_clause)) if rse_clause is not None: replica_query = replica_query.filter(or_(*rse_clause)) for replica in replica_query.yield_per(500): yield replica
def list_rses(session=None): """ List RSEs in the Quarantined Queues. :param session: The database session in use. :returns: a list of RSEs. """ query = session.query(models.RSE.rse).distinct(models.RSE.rse).\ filter(models.QuarantinedReplica.rse_id == models.RSE.id).\ filter(models.RSE.deleted == false()) return [rse for (rse,) in query]
def rse_exists(rse, session=None): """ Checks to see if RSE exists. This procedure does not check its status. :param rse: Name of the rse. :param session: The database session in use. :returns: True if found, otherwise false. """ return True if session.query(models.RSE).filter_by(rse=rse).first() else False
def patients_by_recruitment_group_date(group, interval='month'): """ Number of patients recruited by each group over time. """ group_id_c = func.first_value(GroupPatient.created_group_id)\ .over(partition_by=GroupPatient.patient_id, order_by=GroupPatient.from_date)\ .label('group_id') date_c = func.min(GroupPatient.from_date)\ .over(partition_by=GroupPatient.patient_id)\ .label('date') query = db.session.query(GroupPatient.patient_id, group_id_c, date_c) query = query.distinct() query = query.join(GroupPatient.patient) query = query.filter(GroupPatient.group_id == group.id) query = query.filter(Patient.test == false()) if group.type == GROUP_TYPE.SYSTEM: query = query.filter(Patient.current(group) == true()) else: query = query.filter(Patient.current() == true()) query = query.cte() results = _get_results(query, interval) return results
def get_pfn_to_rse(pfns, session=None): """ Get the RSE associated to a list of PFNs. :param pfns: The list of pfn. :param session: The database session in use. """ unknown_replicas = {} storage_elements = [] se_condition = [] dict_rse = {} surls = clean_surls(pfns) scheme = surls[0].split(':')[0] for surl in surls: if surl.split(':')[0] != scheme: raise exception.InvalidType('The PFNs specified must have the same protocol') split_se = surl.split('/')[2].split(':') storage_element = split_se[0] if storage_element not in storage_elements: storage_elements.append(storage_element) se_condition.append(models.RSEProtocols.hostname == storage_element) query = session.query(models.RSE.rse, models.RSEProtocols.scheme, models.RSEProtocols.hostname, models.RSEProtocols.port, models.RSEProtocols.prefix).\ filter(models.RSEProtocols.rse_id == models.RSE.id).filter(and_(or_(*se_condition), models.RSEProtocols.scheme == scheme)).filter(models.RSE.staging_area == false()) protocols = {} for rse, protocol, hostname, port, prefix in query.yield_per(10000): protocols[rse] = ('%s://%s%s' % (protocol, hostname, prefix), '%s://%s:%s%s' % (protocol, hostname, port, prefix)) hint = None for surl in surls: if hint and (surl.find(protocols[hint][0]) > -1 or surl.find(protocols[hint][1]) > -1): dict_rse[hint].append(surl) else: mult_rse_match = 0 for rse in protocols: if surl.find(protocols[rse][0]) > -1 or surl.find(protocols[rse][1]) > -1: mult_rse_match += 1 if mult_rse_match > 1: print 'ERROR, multiple matches : %s at %s' % (surl, rse) raise exception.RucioException('ERROR, multiple matches : %s at %s' % (surl, rse)) hint = rse if hint not in dict_rse: dict_rse[hint] = [] dict_rse[hint].append(surl) if mult_rse_match == 0: if 'unknown' not in unknown_replicas: unknown_replicas['unknown'] = [] unknown_replicas['unknown'].append(surl) return scheme, dict_rse, unknown_replicas
def list_datasets_per_rse(rse, filters=None, limit=None, session=None): """ List datasets at a RSE. :param rse: the rse name. :param filters: dictionary of attributes by which the results should be filtered. :param limit: limit number. :param session: Database session to use. :returns: A list of dict dataset replicas """ query = session.query(models.CollectionReplica.scope, models.CollectionReplica.name, models.RSE.rse, models.CollectionReplica.bytes, models.CollectionReplica.length, models.CollectionReplica.available_bytes, models.CollectionReplica.available_replicas_cnt.label("available_length"), models.CollectionReplica.state, models.CollectionReplica.created_at, models.CollectionReplica.updated_at, models.CollectionReplica.accessed_at)\ .filter_by(did_type=DIDType.DATASET)\ .filter(models.CollectionReplica.rse_id == models.RSE.id)\ .filter(models.RSE.rse == rse)\ .filter(models.RSE.deleted == false()) for (k, v) in filters and filters.items() or []: if k == 'name' or k == 'scope': if '*' in v or '%' in v: if session.bind.dialect.name == 'postgresql': # PostgreSQL escapes automatically query = query.filter(getattr(models.CollectionReplica, k).like(v.replace('*', '%'))) else: query = query.filter(getattr(models.CollectionReplica, k).like(v.replace('*', '%'), escape='\\')) else: query = query.filter(getattr(models.CollectionReplica, k) == v) # hints ? elif k == 'created_before': created_before = str_to_date(v) query = query.filter(models.CollectionReplica.created_at <= created_before) elif k == 'created_after': created_after = str_to_date(v) query = query.filter(models.CollectionReplica.created_at >= created_after) else: query = query.filter(getattr(models.CollectionReplica, k) == v) if limit: query = query.limit(limit) for row in query: yield row._asdict()
def patients_by_group(group=None, group_type=None): """ Calculate the number of patients in each group. Optionally specify a group to only include patients that are a member of that group. You can also specify a group type so only groups of that type are included in the results. """ # Count the number of distinct patients in each group. Patients can have # multiple memberships for each group but should only be counted once. count_query = db.session.query( GroupPatient.group_id.label('group_id'), func.count(distinct(Patient.id)).label('patient_count') ) count_query = count_query.select_from(Patient) count_query = count_query.join(Patient.group_patients) count_query = count_query.filter(Patient.test == false()) if group is not None and group.type == GROUP_TYPE.SYSTEM: count_query = count_query.filter(Patient.current(group) == true()) else: count_query = count_query.filter(Patient.current() == true()) count_query = count_query.group_by(GroupPatient.group_id) # Filter the results to only include patients belonging to the # specified group. if group is not None: patient_alias = aliased(Patient) group_subquery = db.session.query(patient_alias) group_subquery = group_subquery.join(patient_alias.group_patients) group_subquery = group_subquery.filter( patient_alias.id == Patient.id, GroupPatient.group == group, ) group_subquery = group_subquery.exists() count_query = count_query.filter(group_subquery) # We are only interested in groups other than the specified # group so exclude it from the results. count_query = count_query.filter(GroupPatient.group != group) count_subquery = count_query.subquery() # Join the results with the groups table. query = db.session\ .query(Group, count_subquery.c.patient_count)\ .join(count_subquery, Group.id == count_subquery.c.group_id)\ .order_by(Group.id) # Filter the results to only include groups of the specified # type (e.g. COHORT). By default all group types are included. if group_type is not None: query = query.filter(Group.type == group_type) return query.all()
def patients_by_recruitment_group(group): """ Calculate the number of patients each group has recruited to the specified group. The recruiting group is the first group to add the patient to the group we're querying. In practice this is the membership record with the earliest from date. """ # Use a window function to find the group that recruited each # patient. The recruiting group is the group that created the # earliest membership record (determined by from date). The query # is filtered by the specified group and the distinct clause ensures # we only get one result per patient. first_created_group_id_column = func.first_value(GroupPatient.created_group_id)\ .over(partition_by=GroupPatient.patient_id, order_by=GroupPatient.from_date)\ .label('created_group_id') q1 = db.session.query(first_created_group_id_column) q1 = q1.distinct(GroupPatient.patient_id) q1 = q1.join(GroupPatient.patient) q1 = q1.filter(GroupPatient.group_id == group.id) q1 = q1.filter(Patient.test == false()) if group.type == GROUP_TYPE.SYSTEM: q1 = q1.filter(Patient.current(group) == true()) else: q1 = q1.filter(Patient.current() == true()) q1 = q1.subquery() # Aggregate the results by recruiting group to get the number of # patients recruited by each group. created_group_id_column = q1.c.created_group_id.label('created_group_id') patient_count_column = func.count().label('patient_count') q2 = db.session.query(created_group_id_column, patient_count_column)\ .group_by(created_group_id_column)\ .subquery() # Join the results with the groups table. q3 = db.session.query(Group, q2.c.patient_count)\ .join(q2, Group.id == q2.c.created_group_id)\ .order_by(Group.id) return q3.all()
def patients_by_group_date(group=None, group_type=None, interval='month'): """ Number of patients in each group over time. """ query = db.session.query( GroupPatient.group_id, GroupPatient.patient_id, func.min(GroupPatient.from_date).label('date') ) query = query.join(GroupPatient.patient) query = query.filter(Patient.test == false()) if group is not None and group.type == 'SYSTEM': query = query.filter(Patient.current(group) == true()) else: query = query.filter(Patient.current() == true()) # Filter by patients beloning to the specified group if group is not None: patient_alias = aliased(Patient) group_subquery = db.session.query(patient_alias) group_subquery = group_subquery.join(patient_alias.group_patients) group_subquery = group_subquery.filter( patient_alias.id == Patient.id, GroupPatient.group == group, ) group_subquery = group_subquery.exists() query = query.filter(group_subquery) # Filter by group type if group_type is not None: query = query.join(GroupPatient.group) query = query.filter(Group.type == group_type) query = query.group_by(GroupPatient.group_id, GroupPatient.patient_id) query = query.cte() results = _get_results(query, interval) return results