def get_all(cls, handler=None, limit=None, offset=None, project_id=None, name=None, build_url=None, region=None, environment=None, status=None, **tag_filters): handler = handler or get_handler() and_clause = cls._and_clause( project_id=project_id, name=name, build_url=build_url, region=region, environment=environment, ) # match the build if its tags are a superset of the filters if tag_filters: and_clause &= cls.TABLE.c.tags.contains(tag_filters) query = cls._get_all_query( and_clause=and_clause, limit=limit, offset=offset, ) builds = handler.get_all(resource_class=cls, query=query) return builds
def get_last_count_by_test_name(cls, handler=None, limit=None, offset=None, **kwargs): handler = handler or get_handler() if (kwargs and 'project_id' in kwargs and 'test_name' in kwargs and 'count' in kwargs): # select * from results # where build_id in ( # select build_id from build # where test_name = kwargs['test_name'] # and project_id = kwargs['project_id'] # order by build_id desc # limit kwargs['count'] # ) query_by_build_name = select([Build.TABLE.c.id]).select_from( Build.TABLE).where( Build.TABLE.c.project_id == kwargs['project_id'], ).order_by( desc(Build.TABLE.c.id) ).limit( kwargs['count'] ) last_count_by_status_query = cls.TABLE.select().where( and_( cls.TABLE.c.test_name == kwargs['test_name'], cls.TABLE.c.build_id.in_(query_by_build_name))) return handler.get_all( resource_class=cls, query=last_count_by_status_query, limit=limit, offset=offset) else: return []
def get_all(cls, handler=None, limit=None, offset=None, project_id=None, test_name=None, build_id=None, timestamp=None, result=None, result_message=None, **tag_filters): handler = handler or get_handler() and_clause = cls._and_clause( project_id=project_id, test_name=test_name, build_id=build_id, timestamp=timestamp, result=result, result_message=result_message, ) if tag_filters: and_clause &= cls.TABLE.c.tags.contains(tag_filters) query = cls._get_all_query(and_clause, limit=limit, offset=offset) results = handler.get_all(resource_class=cls, query=query) # This `and_clause` should not have the limit/offset included. We want # to run stats on the whole result, not just the first page. metadata = cls._get_results_metadata(and_clause, handler=handler) results_dict = { "results": results, "metadata": metadata.to_dict(), } return results_dict
def _get_results_metadata(cls, handler=None, **kwargs): handler = handler or get_handler() query = select( [cls.TABLE.c.result, func.count(cls.TABLE.c.result).label("count")] ).where(cls._and_clause(**kwargs)).group_by(cls.TABLE.c.result) count_results = handler.get_all(resource_class=Result, query=query) return ResultMetadata.from_database_counts(count_results)
def _get_results_metadata(cls, and_clause, handler=None): handler = handler or get_handler() query = select( [cls.TABLE.c.result, func.count(cls.TABLE.c.result).label("count")] ).where(and_clause).group_by(cls.TABLE.c.result) count_results = handler.get_all(resource_class=Result, query=query) return ResultMetadata.from_database_counts(count_results)
def get_all(cls, handler=None, limit=None, offset=None, **kwargs): handler = handler or get_handler() and_clause = cls._and_clause(**kwargs) query = cls._get_all_query( and_clause=and_clause, limit=limit, offset=offset, ) return handler.get_all(resource_class=cls, query=query, limit=limit, offset=offset)
def create_many(cls, resources, handler=None, **kwargs): handler = handler or get_handler() super(cls, Result).create_many(resources, handler=handler) metadata = ResultMetadata.from_results_list(resources) results_dict = { "metadata": metadata.to_dict(), } return results_dict
def get_all(cls, handler=None, limit=None, offset=None, **kwargs): handler = handler or get_handler() results = super(cls, Result).get_all(handler=handler, limit=limit, offset=offset, **kwargs) metadata = cls._get_results_metadata(handler, **kwargs) results_dict = { "results": results, "metadata": metadata.to_dict(), } return results_dict
def get_all(cls, handler=None, limit=None, offset=None, **kwargs): handler = handler or get_handler() limit = limit or conf.api.default_limit query = cls.TABLE.select() and_clause = cls._and_clause(**kwargs) if and_clause is not None: query = query.where(and_clause) query = query.order_by(cls.TABLE.c.id) return handler.get_all(resource_class=cls, query=query, limit=limit, offset=offset)
def get_last_count_by_status(cls, handler=None, limit=None, offset=None, **kwargs): handler = handler or get_handler() if (kwargs and 'project_id' in kwargs and 'build_name' in kwargs and 'status' in kwargs and 'count' in kwargs): # select * from results # where status = kwargs['status'] # and build_id in ( # select build_id from build # where build_name = kwargs['build_name'] # and project_id = kwargs['project_id'] # order by build_id desc # limit kwargs['count'] # ) query_by_build_name = select([Build.TABLE.c.id]).select_from( Build.TABLE).where( and_( Build.TABLE.c.project_id == kwargs['project_id'], Build.TABLE.c.name == kwargs['build_name'] )).order_by( desc(Build.TABLE.c.id) ).limit( kwargs['count'] ) last_count_by_status_query = cls.TABLE.select().where( and_( cls.TABLE.c.result == kwargs['status'], cls.TABLE.c.build_id.in_(query_by_build_name))) return handler.get_all( resource_class=cls, query=last_count_by_status_query, limit=limit, offset=offset) else: return []
def delete(cls, resource_id, handler=None): handler = handler or get_handler() return handler.delete(resource_id=resource_id, resource_class=cls)
def get_all(cls, handler=None, limit=None, offset=None, project_id=None, name=None, build_url=None, region=None, environment=None, **kwargs): handler = handler or get_handler() if kwargs: # Create a table that joins the builds, build tags, and tags joined_table = cls.TABLE.outerjoin( cls.RESOURCE_TAGS_TABLE, and_(cls.TABLE.c.id == cls.RESOURCE_TAGS_TABLE.c.build_id) ).outerjoin( cls.TAGS_TABLE, and_(cls.RESOURCE_TAGS_TABLE.c.tag_id == cls.TAGS_TABLE.c.id)) # Select only the build info and the key/value tag pairs joined_table_select = select([ cls.TABLE, cls.TAGS_TABLE.c.key, cls.TAGS_TABLE.c.value, ]).select_from(joined_table) # Query for the non-tag values as well builds_and_clause = cls._and_clause( project_id=project_id, name=name, build_url=build_url, region=region, environment=environment) # Create a table for each tag we're looking for tables = [] for i, (key, value) in enumerate(kwargs.iteritems(), 1): alias = "t%s" % i tag_and_clause = Tag._and_clause(key=key, value=value) full_and_clause = and_(tag_and_clause, builds_and_clause) table = joined_table_select.where(full_and_clause).alias(alias) tables.append(table) # Join all the tag tables together to determine which builds have # all the desired tags desired_builds_table = tables[0] for i, table in enumerate(tables[1:], 2): desired_builds_table = desired_builds_table.join( table, text("t1.id = t%s.id" % i)) # Get all tags for each build that has the desired tags # 1.) Join table with desired builds with builds tags table # to determine what build tags we care about # 2.) Join that table with the tags table to get the key/values joined_table_w_tags = desired_builds_table.join( cls.RESOURCE_TAGS_TABLE, and_( text("t1.id") == cls.RESOURCE_TAGS_TABLE.c.build_id) ).join( cls.TAGS_TABLE, and_(cls.RESOURCE_TAGS_TABLE.c.tag_id == cls.TAGS_TABLE.c.id) ).alias("joined_table_w_tags") tags_query = joined_table_w_tags.select() build_tag_combinations = handler.get_all( resource_class=cls, query=tags_query, limit=limit, offset=offset) # Map tag key/value pairs to build ids build_tags = {} for build in build_tag_combinations: if build_tags.get(build.get("build_tags_build_id")): tags = build_tags[build.get("build_tags_build_id")] tags[build.get("tags_key")] = build.get("tags_value") build_tags[build.get("build_tags_build_id")] = tags else: build_tags[build.get("build_tags_build_id")] = { build.get("tags_key"): build.get("tags_value")} # Get desired builds and add their tags before returning query = desired_builds_table.select() query = query.order_by(text("t1.id")) builds = handler.get_all(resource_class=cls, query=query, limit=limit, offset=offset) for build in builds: build["tags"] = build_tags.get(build.get("id")) del build["key"] del build["value"] return builds else: return super(Build, cls).get_all( handler=handler, limit=limit, offset=offset, project_id=project_id, name=name, build_url=build_url, region=region, environment=environment)
def create(cls, resource, handler=None): handler = handler or get_handler() return handler.create(resource)
def create_many(cls, resources, handler=None): handler = handler or get_handler() return handler.create_many(resources)
def get(cls, resource_id, handler=None): handler = handler or get_handler() return handler.get(resource_id=resource_id, resource_class=cls)