def get(self, app=None, group=None, oid=None, name=None): """Get configuration properties. :param app: property app [optional] :param group: property group [optional] :param oid: property id [optional] :param name: property name [optional] :return: list of :class:`ConfigProp` :raises: :class:`gibbonutil.db.QueryError` """ session = self.get_session() if oid is not None: prop = session.query(ConfigProp).filter_by(id=oid).all() elif name is not None: prop = session.query(ConfigProp).filter_by(name=name).all() elif app is not None or group is not None: query = session.query(ConfigProp) if app is not None: query = query.filter_by(app=app) if group is not None: query = query.filter_by(group=group) prop = query.all() else: prop = session.query(ConfigProp).all() if len(prop) == 0: self.logger.warn( 'No properties (app=%s, group=%s oid=%s, name=%s) found' % (app, group, oid, name)) raise ModelError( 'No properties (app=%s, group=%s, oid=%s, name=%s) found' % (app, group, oid, name)) self.logger.debug('Get properties: %s' % prop) return prop
def run(self, tags, *args, **kvargs): """Make query :param list tags: list of permission tags """ if tags is None or len(tags) == 0: tags = [u''] # count all records stmp = self.base_stmp(count=True) total = self.session.query(u'count').\ from_statement(stmp).\ params(tags=tags, **kvargs).first()[0] # make query stmp = self.base_stmp() query = self.session.query(self.entity).\ from_statement(stmp).\ params(tags=tags, **kvargs) #self.logger.warn(u'stmp: %s' % query.statement.compile(dialect=mysql.dialect())) query = query.all() self.logger.debug(u'Get %ss (total:%s): %s' % (self.entity, total, truncate(query))) return query, total
def get_trace(self, task_id): """Get task trace :param task_id: task id :return: SchedulerTrace instance list """ session = self.get_session() query = session.query(SchedulerTrace).filter_by(task_id=task_id).order_by(asc(SchedulerTrace.date)) steps = query.all() self.logger.debug('Get task %s trace: %s' % (task_id, truncate(steps))) return steps
def get_steps(self, task_id): """Get task steps :param task_id: task id :return: SchedulerStep instance list """ session = self.get_session() query = session.query(SchedulerStep).filter_by(task_id=task_id).order_by(asc(SchedulerStep.start_time)) steps = query.all() self.logger.debug('Get task %s steps: %s' % (task_id, truncate(steps))) return steps
def run2(self, tags, *args, **kvargs): """Make query. Use base_smtp2 :param list tags: list of permission tags :param args: custom args :param kvargs: custom kvargs """ start = time() if self.with_perm_tag is True: self.logger.debug2('Authorization with permission tags ENABLED') else: self.logger.debug2('Authorization with permission tags DISABLED') if tags is None or len(tags) == 0: tags = [''] # make query if self.size > 0: # count all records stmp = self.base_stmp2(count=True) total = self.session.query('count').from_statement(stmp).params( tags=tags, **kvargs).first()[0] stmp = self.base_stmp2() # set query entities entities = [self.entity] entities.extend(self.other_entities) query = self.session.query(*entities).from_statement(stmp).params( tags=tags, **kvargs) self.logger.debug2('stmp: %s' % query.statement.compile(dialect=mysql.dialect())) self.logger.debug2('kvargs: %s' % truncate(kvargs)) self.logger.debug2('tags: %s' % truncate(tags)) res = query.all() if self.size == 0 or self.size == -1: total = len(res) elapsed = round(time() - start, 3) self.logger.debug2( 'Get %ss (total:%s): %s [%s]' % (self.entity.__tablename__, total, truncate(res), elapsed)) return res, total
def get_entities(self, entityclass, filters, *args, **kvargs): """Get model entities :param entityclass: entity model class :param filters: entity model filters function. Return qury with additional filter :param int oid: entity id. [optional] :param str objid: entity authorization id. [optional] :param str uuid: entity uuid. [optional] :param str name: entity name. [optional] :param args: custom params :param kvargs: custom params :return: list of entityclass :raises QueryError: raise :class:`QueryError` """ session = self.get_session() query = self.query_entities(entityclass, session, *args, **kvargs) query = filters(query, *args, **kvargs) # make query res = query.all() self.logger.debug(u'Get %s: %s' % (entityclass.__name__, truncate(res))) return res
def gets(self, oid=None, etype=None, data=None, source=None, dest=None, datefrom=None, dateto=None, page=0, size=10, objid=None, objdef=None, objtype=None): """Get events. :param oid str: event oid [optional] :param etype str: list of event type [optional] :param data str: event data [optional] :param source str: event source [optional] :param dest str: event destinatiaion [optional] :param datefrom: event data from. Ex. '2015-3-9-15-23-56' [optional] :param dateto: event data to. Ex. '2015-3-9-15-23-56' [optional] :param page: event list page to show [default=0] :param size: number of event to show in list per page [default=0] :param objid str: entity id [optional] :param objtype str: entity type [optional] :param objdef str: entity definition [optional] :raise QueryError: if query return error """ session = self.get_session() if oid is not None: query = session.query(DbEvent).filter_by(event_id=oid) count = query.count() res = query.all() else: query = session.query(DbEvent) if etype is not None: query = query.filter(DbEvent.type.in_(etype)) if objid is not None: query = query.filter(DbEvent.objid.like(objid)) if objtype is not None: query = query.filter(DbEvent.objtype.like(objtype)) if objdef is not None: query = query.filter(DbEvent.objdef.like(objdef)) if data is not None: query = query.filter(DbEvent.data.like('%' + data + '%')) if source is not None: query = query.filter(DbEvent.source.like('%' + source + '%')) if dest is not None: query = query.filter(DbEvent.dest.like('%' + dest + '%')) if datefrom is not None: query = query.filter(DbEvent.creation >= datefrom) if dateto is not None: query = query.filter(DbEvent.creation <= dateto) count = query.count() start = size * page end = size * (page + 1) res = query.order_by(DbEvent.creation.desc())[start:end] self.logger.debug('Get events count: %s' % count) if count == 0: self.logger.error("No events found") raise SQLAlchemyError("No events found") self.logger.debug('Get events: %s' % truncate(res)) return count, res