def insert(self, owner_id, report_name): log.debug('Trying to insert new report %r', report_name) report_id = gen_timeuuid() def insert_report_name(): return c.cass.execute( """INSERT INTO mqe.report_by_owner_id_report_name (owner_id, report_name, report_id) VALUES (?, ?, ?) IF NOT EXISTS""", [owner_id, report_name, report_id]) lwt_res = execute_lwt(insert_report_name) if lwt_res == False: log.info('Race condition in creating a new report %r', report_name) return None elif lwt_res == None: rows = c.cass.execute( """SELECT report_id FROM mqe.report_by_owner_id_report_name WHERE owner_id=? AND report_name=? /* SERIAL */""", [owner_id, report_name], ConsistencyLevel.SERIAL) if not rows or rows[0]['report_id'] != report_id: log.info( 'Race condition in creating a new report when lwt_res==None: %r', report_name) return None row = { 'report_id': report_id, 'report_name': report_name, 'owner_id': owner_id } c.cass.execute(insert('mqe.report', row)) log.info('Inserted new report report_id=%s report_name=%r', report_id, report_name) qs = [] for prefix in util.iter_prefixes(report_name, include_empty=True): qs.append( insert( 'mqe.report_name_by_owner_id_report_name_prefix', dict(owner_id=owner_id, report_name_prefix=prefix, report_name=report_name))) c.cass.execute_parallel(qs) return row
def insert(self, owner_id, dashboard_name, dashboard_options): dashboard_id = gen_uuid() row = dict(owner_id=owner_id, dashboard_id=dashboard_id, dashboard_name=dashboard_name, dashboard_options=dashboard_options) c.cass.execute(insert('mqe.dashboard', row)) return row
def insert(self, email, password): if self.select_by_email(email) is not None: raise ValueError('User exists: %r' % email) user_id = gen_uuid() pw_hash = werkzeug.security.generate_password_hash(password) c.cass.execute( batch( insert( 'mqe.user', { 'user_id': user_id, 'email': email, 'password': pw_hash, 'created': datetime.datetime.utcnow(), 'user_data': serialize.mjson({}), }), insert('mqe.user_by_email', { 'email': email, 'user_id': user_id }), )) return self.select(user_id)
def set_multi(self, report_id, kind, key_value_list): assert kind == 'SeriesSpec' qs = [] for (key, value) in key_value_list: qs.append( insert( 'mqe.series_spec_default_options', dict( report_id=report_id, series_spec=key, default_options=value, ))) c.cass.execute_parallel(qs)
def insert_multi(self, report_id, tags_series_spec_list): if not tags_series_spec_list: return [] qs = [] res = [] for tags, series_spec in tags_series_spec_list: series_id = gen_timeuuid() qs.append( insert( 'mqe.series_def', dict(report_id=report_id, tags_repr=tags_repr_from_tags(tags), series_id=series_id, series_spec=serialize.mjson(series_spec)))) qs.append( insert( 'mqe.series_def_by_series_spec', dict(report_id=report_id, tags_repr=tags_repr_from_tags(tags), series_spec=serialize.mjson(series_spec), series_id=series_id))) res.append(series_id) c.cass.execute_parallel(qs) return res
def insert_multi(self, owner_id, dashboard_id, tile_options_list): qs = [] res = [] for tile_options in tile_options_list: row = dict(dashboard_id=dashboard_id, tile_id=gen_timeuuid(), tile_options=tile_options) qs.append(insert('mqe.tile', row)) qs.append( bind( """UPDATE mqe.tile_count SET count=count+1 WHERE owner_id=?""", [owner_id])) res.append(row) c.cass.execute_parallel(qs) return res
def insert_layout_by_report_multi(self, owner_id, report_id_list, tags, label, dashboard_id, layout_id): qs = [] for report_id in report_id_list: qs.append( insert( 'mqe.layout_by_report', dict( owner_id=owner_id, report_id=report_id, tags_repr=tags_repr_from_tags(tags), label=label, dashboard_id=dashboard_id, layout_id=layout_id, ), COLUMN_RENAMES['layout_by_report'])) c.cass.execute_parallel(qs)
def do_set_layout(): if not old_layout_id: update_rows = c.cass.execute( insert('mqe.dashboard_layout_def', dict(owner_id=owner_id, dashboard_id=dashboard_id, layout_id=new_layout_id, layout_def=new_layout_def, layout_props=new_layout_props), COLUMN_RENAMES['dashboard_layout_def'], if_not_exists=True)) else: update_rows = c.cass.execute( """UPDATE mqe.dashboard_layout_def SET {layout_id_colname}=?, layout_def=?, layout_props=? WHERE owner_id=? AND dashboard_id=? IF {layout_id_colname}=?""".format( layout_id_colname=layout_id_colname), [ new_layout_id, new_layout_def, new_layout_props, owner_id, dashboard_id, old_layout_id ]) return update_rows
def set(self, user_id, api_key): c.cass.execute( batch( insert('mqe.api_key', dict(api_key=api_key, user_id=user_id)), insert('mqe.api_key_by_user', dict(user_id=user_id, api_key=api_key))))
def qs_it(): for row in data: row['series_id'] = series_id yield insert('mqe.series_value', row)
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)
def insert_multi(self, series_id, data): qs = [] for row in data: row['series_id'] = series_id qs.append(insert('mqe.series_value', row)) c.cass.execute_parallel(qs)