def get_itemversions_for_request(self, request): from synoptic.query import parse_query parsed_query = parse_query(request.GET.get("query", "")) if "max_timestamp" in request.GET: max_timestamp = float(request.GET["max_timestamp"]) else: max_timestamp = None session = request.dbsession model = request.datamodel # grab the ORMed instances return (session .query(ItemVersion) .from_statement(query_itemversions( session, model, parsed_query, max_timestamp)))
def get_tags_with_usecounts(self, session, model, parsed_query=None, max_timestamp=None, startswith=None, limit=None): from sqlalchemy.sql import select, func if parsed_query is not None: itemversions_q = query_itemversions(session, model, parsed_query, max_timestamp) else: itemversions_q = select([model.itemversions], from_obj=[get_current_itemversions_join( model, max_timestamp) ]) itemversions_q = itemversions_q.alias("currentversions") # twuc_q stands for tags_with_usecount_query twuc_q = ( select([ model.tags.c.name, func.count(itemversions_q.c.id).label("use_count"), ], from_obj=[ model.itemversions_tags .join(itemversions_q, itemversions_q.c.id == model.itemversions_tags.c.itemversion_id) .join(model.tags) ]) ) if startswith: twuc_q = twuc_q.where(model.tags.c.name.startswith(startswith)) twuc_q = ( twuc_q .group_by(model.tags.c.id) .having(func.count(itemversions_q.c.id) > 0) .order_by(model.tags.c.name) ) if limit is not None: twuc_q = twuc_q.limit(limit) return session.execute(twuc_q)
def get_json_items(self, session, model, parsed_query, max_timestamp): itemversions_query = query_itemversions( session, model, parsed_query, max_timestamp)\ .alias("currentitemversions") # prepare eager loading of tags from sqlalchemy.sql import select iv_and_t_query = select([itemversions_query, model.tags], from_obj=[ itemversions_query .outerjoin(model.itemversions_tags, itemversions_query.c.id == model.itemversions_tags.c.itemversion_id) .outerjoin(model.tags, model.itemversions_tags.c.tag_id == model.tags.c.id) ], use_labels=True) last_id = None result = [] for row in session.execute(iv_and_t_query): if last_id != row[itemversions_query.c.id]: last_id = row[itemversions_query.c.id] result.append( { "id": row[itemversions_query.c.item_id], "version_id": row[itemversions_query.c.id], "contents": row[itemversions_query.c.contents], "contents_html": ItemVersion.htmlize( row[itemversions_query.c.contents]), "tags": [], "all_day": row[itemversions_query.c.all_day], "start_date": row[itemversions_query.c.start_date], "end_date": row[itemversions_query.c.end_date], "bump_interval": row[itemversions_query.c.bump_interval], "hide_until": row[itemversions_query.c.hide_until], "highlight_at": row[itemversions_query.c.highlight_at], }) if row[model.tags.c.name] is not None: result[-1]["tags"].append(row[model.tags.c.name]) return result