def select_multi(self, report_id, tags, min_report_instance_id, max_report_instance_id, columns, order, limit): tags_repr = tags_repr_from_tags(tags) what = what_from_columns(columns, 'report_instance') q_tpl = """SELECT day FROM mqe.report_instance_day WHERE report_id=? AND tags_repr=? {filter_min} {filter_max}""" params = [report_id, tags_repr] if min_report_instance_id is not None: filter_min = 'AND day >= ?' params.append(day_text(min_report_instance_id)) else: filter_min = '' if max_report_instance_id is not None: filter_max = 'AND day <= ?' params.append(day_text(max_report_instance_id)) else: filter_max = '' day_rows = c.cass.execute( q_tpl.format(filter_min=filter_min, filter_max=filter_max), params) days = [row['day'] for row in day_rows] if order == 'desc': days = list(reversed(days)) res = [] for day in days: day_instances = list(c.cass.execute( """SELECT {what} FROM mqe.report_instance WHERE report_id=? AND day=? AND tags_repr=? AND report_instance_id > ? AND report_instance_id < ? ORDER BY report_instance_id {order} LIMIT ?""". \ format(what=what, order=order), [report_id, day, tags_repr, min_report_instance_id, max_report_instance_id, limit] )) res.extend(day_instances) if len(res) >= limit: break res = res[:limit] for row in res: postprocess_tags(row) postprocess_col_renames(COLUMN_RENAMES['report_instance'], row) return res
def select_extra_ri_data(self, report_id, report_instance_id): return c.cass.execute_fst( """SELECT extra_ri_data FROM mqe.report_instance_metadata WHERE report_id=? AND day=? AND report_instance_id=?""", [report_id, day_text(report_instance_id), report_instance_id ])['extra_ri_data']
def select(self, report_id, report_instance_id, tags): rows = c.cass.execute( """SELECT * FROM mqe.report_instance WHERE report_id=? AND day=? AND tags_repr=? AND report_instance_id=?""", [ report_id, day_text(report_instance_id), tags_repr_from_tags(tags), report_instance_id ]) if not rows: return None postprocess_tags(rows[0]) postprocess_col_renames(COLUMN_RENAMES['report_instance'], rows[0]) return rows[0]
def insert(self, owner_id, report_id, report_instance_id, tags, ri_data, input_string, extra_ri_data, custom_created): created = util.datetime_from_uuid1(report_instance_id) day = day_text(created) all_tags_repr = tags_repr_from_tags(tags) qs = [] metadata_row = dict( report_id=report_id, day=day, report_instance_id=report_instance_id, all_tags_repr=all_tags_repr, inserted=datetime.datetime.utcnow(), ) if extra_ri_data: metadata_row['extra_ri_data'] = extra_ri_data qs.append(insert('mqe.report_instance_metadata', metadata_row)) first_row = None tags_powerset = util.powerset(tags[:mqeconfig.MAX_TAGS]) for tags_subset in tags_powerset: tags_repr = tags_repr_from_tags(tags_subset) row = dict(report_id=report_id, day=day, tags_repr=tags_repr, report_instance_id=report_instance_id, ri_data=ri_data, input_string=input_string, all_tags_repr=all_tags_repr) if first_row is None: first_row = row qs.append( insert('mqe.report_instance', row, COLUMN_RENAMES['report_instance'])) if not c.cass.execute( """SELECT day FROM mqe.report_instance_day WHERE report_id=? AND tags_repr=? AND day=?""", [report_id, tags_repr, day]): qs.append( insert( 'mqe.report_instance_day', dict(report_id=report_id, tags_repr=tags_repr, day=day))) qs.append( bind( """UPDATE mqe.report_instance_count SET count=count+1 WHERE report_id=? AND tags_repr=?""", [report_id, tags_repr])) diskspace = self._compute_ri_diskspace(row) qs.append( bind( """UPDATE mqe.report_instance_diskspace SET bytes=bytes+? WHERE report_id=? AND tags_repr=?""", [diskspace, report_id, tags_repr])) ### queries for all tags qs.append( bind( """UPDATE mqe.report_instance_count_for_owner SET count=count+1 WHERE owner_id=?""", [owner_id])) if first_row: diskspace = self._compute_ri_diskspace(first_row) qs.append( bind( """UPDATE mqe.report_instance_diskspace_for_owner SET bytes=bytes+? WHERE owner_id=?""", [diskspace, owner_id])) # avoid reinserting the same tag multiple times tag_rows = c.cass.execute( """SELECT tag FROM mqe.report_tag WHERE report_id=? AND tag_prefix='' AND tag IN ?""", [report_id, tags]) tags_from_rows = {row['tag'] for row in tag_rows} for tag in tags: if tag in tags_from_rows: continue for p in util.iter_prefixes(tag, include_empty=True): qs.append( insert('mqe.report_tag', dict(report_id=report_id, tag_prefix=p, tag=tag))) c.cass.execute_parallel(qs) return postprocess_tags(first_row)