def do_promote_first_as_master_mod(layout_mod): from mqe import tpcreator layout_dict_items = _sort_layout_items(layout_mod.layout.layout_dict, 'y') by_master_id = defaultdict(list) for tile_id, vo in layout_dict_items: props = layout_mod.layout.get_tile_props(tile_id) if not props: continue master_id = props.get('master_id') if not master_id and props.get('is_master'): master_id = tile_id if not master_id: continue by_master_id[master_id].append(tile_id) tile_repl = {} for master_id, tile_id_list in by_master_id.items(): if tile_id_list[0] != master_id and master_id in layout_mod.layout.layout_dict: old_master = Tile.select(layout_mod.layout.dashboard_id, master_id) if not old_master: continue new_chosen_master = Tile.select(layout_mod.layout.dashboard_id, tile_id_list[0]) if not new_chosen_master: continue new_master = tpcreator.make_master_from_tpcreated(old_master, new_chosen_master) old_master_repl = tpcreator.make_tpcreated_from_master(old_master, new_master) tile_repl.update({old_master: new_master, new_chosen_master: old_master_repl}) if not tile_repl: return replace_tiles_mod(tile_repl, do_repacking=False)(layout_mod)
def test_get_dashboards_by_report_id(self): od = self.test_inserting() res = od.get_dashboards_by_report_id() self.assertEqual({}, res) r = Report.insert(od.owner_id, 'r') r2 = Report.insert(od.owner_id, 'r2') r3 = Report.insert(od.owner_id, 'r3') tile_config = { 'series_spec_list': [ dataseries.SeriesSpec(0, -1, dict(op='eq', args=['0'])), ], } place_tile(Tile.insert(od.owner_id, r.report_id, od.dashboards[3].dashboard_id, tile_config)) place_tile(Tile.insert(od.owner_id, r.report_id, od.dashboards[3].dashboard_id, tile_config)) place_tile(Tile.insert(od.owner_id, r.report_id, od.dashboards[0].dashboard_id, tile_config)) place_tile(Tile.insert(od.owner_id, r2.report_id, od.dashboards[0].dashboard_id, tile_config)) place_tile(Tile.insert(od.owner_id, r3.report_id, od.dashboards[1].dashboard_id, tile_config)) res = od.get_dashboards_by_report_id() self.assertEqual(3, len(res)) self.assertEqual([od.dashboards[0], od.dashboards[3]], res[r.report_id]) self.assertEqual([od.dashboards[0]], res[r2.report_id]) self.assertEqual([od.dashboards[1]], res[r3.report_id])
def _on_failure(self): Tile.delete_multi(self.new_tiles.keys()) Tile.delete_multi(self.tile_replacement.values()) self.tile_replacement.clear() self.new_tiles.clear() self.detached_tiles[:] = []
def test_select_multi(self): owner_id = uuid.uuid1() d_id1 = uuid.uuid1() d_id2 = uuid.uuid1() r = Report.insert(owner_id, 'r') res = Layout.select_multi(owner_id, [d_id1, d_id2]) self.assertEqual([], res) tile_config = { 'series_spec_list': [ dataseries.SeriesSpec(0, -1, dict(op='eq', args=['0'])), ], } t1 = Tile.insert(owner_id, r.report_id, d_id1, tile_config) place_tile(t1) res = Layout.select_multi(owner_id, [d_id1, d_id2]) self.assertEqual(1, len(res)) self.assertEqual( Layout.select(owner_id, d_id1).layout_id, res[0].layout_id) t2 = Tile.insert(owner_id, r.report_id, d_id2, tile_config) place_tile(t2) res = Layout.select_multi(owner_id, [d_id1, d_id2]) self.assertEqual(2, len(res)) self.assertEqual( Layout.select(owner_id, d_id1).layout_id, res[0].layout_id) self.assertEqual( Layout.select(owner_id, d_id2).layout_id, res[1].layout_id)
def test_insert_skip_db(self): inserted_tile = self.test_insert() tile = Tile.select(inserted_tile.dashboard_id, inserted_tile.tile_id) tile_config = { 'tw_type': 'Range', 'series_spec_list': [ dataseries.SeriesSpec(2, 0, dict(op='eq', args=['monique'])), ], 'tile_options': { 'seconds_back': 600, 'tile_title': 'Points by user' } } tile = tile.insert(tile.owner_id, tile.report_id, tile.dashboard_id, tile_config, skip_db=True) self.assertEqual([], tile.tile_options['tags']) self.assertEqual(tile.tile_options['seconds_back'], 600) self.assertEqual(tile_config['series_spec_list'], tile.series_specs()) db_tile = Tile.select(inserted_tile.dashboard_id, inserted_tile.tile_id) self.assertEqual(db_tile.tile_options['tags'], ['ip:192.168.1.1']) self.assertEqual(db_tile.tile_options['seconds_back'], 86400)
def test_sscs_virtual_column(self): owner_id = uuid.uuid1() od = dashboards.OwnerDashboards(owner_id) dashboard_id = od.dashboards[0].dashboard_id r = Report.insert(owner_id, 'r') tile_config = { 'tw_type': 'Single', 'series_spec_list': [ SeriesSpec(0, -1, dict(op='eq', args=['0'])), ], 'tile_options': {} } tile_config['tile_options']['sscs'] = tile_config['series_spec_list'][ 0] tile = Tile.insert(owner_id, r.report_id, dashboard_id, tile_config) layouts.place_tile(tile) for inp in ['0', '1', '2\n3', '3\n4\n5\n']: res = r.process_input(inp) tile = Tile.select( dashboard_id, Layout.select(owner_id, dashboard_id).layout_dict.keys()[0]) self.assertEqual(3, len(tile.series_specs())) return tile, r
def handle_tpcreator_row(row, report_instance): log.debug('Processing row %s', row) layout = layouts.Layout.select(row['owner_id'], row['dashboard_id']) if not layout: log.warn('No layout') return True layout_id = layout.layout_id tpcreator_spec_by_master_id, tpcreated_tags_by_master_id = _get_tpcreator_data(layout, row['report_id']) log.debug('tpcreator data: %s, %s', tpcreator_spec_by_master_id, tpcreated_tags_by_master_id) if not tpcreator_spec_by_master_id: log.info('Deleting obsoleted layout_by_report tpcreator row') c.dao.LayoutDAO.delete_layout_by_report(row['owner_id'], row['report_id'], row['tags'], row['label'], row['dashboard_id'], row['layout_id']) return True for master_id, tpcreator_spec in tpcreator_spec_by_master_id.items(): log.debug('Processing master_id=%s tpcreator_spec=%s', master_id, tpcreator_spec) tpcreated_tags = tpcreated_tags_by_master_id[master_id] if len(tpcreated_tags) >= mqeconfig.MAX_TPCREATED: log.warn('Too many tpcreated for master_id=%s', master_id) continue matching_tags = tags_matching_tpcreator_spec(tpcreator_spec, report_instance.all_tags) if not matching_tags: log.debug('No tags match the tpcreator_spec') continue if tuple(matching_tags) in tpcreated_tags: log.debug('A tpcreated tile already exists for the matched tags %s', matching_tags) continue master_tile = Tile.select(row['dashboard_id'], master_id) if not master_tile: log.warn('No master_tile') continue new_tile_options = _tile_options_of_tpcreated(master_tile, tpcreator_spec, matching_tags) new_tile = Tile.insert_with_tile_options(master_tile.dashboard_id, new_tile_options) log.info('tpcreator created new tile with tags %s for report_id=%s', matching_tags, row['report_id']) mres = layouts.place_tile(new_tile, size_of=master_tile.tile_id, for_layout_id=layout_id) if not mres: log.debug('Placing new tile failed') return False fire_signal(layout_modified, reason='tpcreator', layout_modification_result=mres) layout_id = mres.new_layout.layout_id return True
def test_get_dashboards_displaying_report(self): od = self.test_inserting() res = od.get_dashboards_displaying_report(uuid.uuid4()) self.assertEqual([], res) r = Report.insert(od.owner_id, 'r') tile_config = { 'series_spec_list': [ dataseries.SeriesSpec(0, -1, dict(op='eq', args=['0'])), ], } res = od.get_dashboards_displaying_report(r.report_id) self.assertEqual([], res) t1 = Tile.insert(od.owner_id, r.report_id, od.dashboards[3].dashboard_id, tile_config) place_tile(t1) res = od.get_dashboards_displaying_report(r.report_id) self.assertEqual(1, len(res)) self.assertEqual('Dash 4', res[0].dashboard_name) t2 = Tile.insert(od.owner_id, r.report_id, od.dashboards[1].dashboard_id, tile_config) place_tile(t2) res = od.get_dashboards_displaying_report(r.report_id) self.assertEqual(2, len(res)) self.assertEqual('Dash 2', res[0].dashboard_name) self.assertEqual('Dash 4', res[1].dashboard_name) t3 = Tile.insert(od.owner_id, r.report_id, od.dashboards[1].dashboard_id, tile_config) place_tile(t3) res = od.get_dashboards_displaying_report(r.report_id) self.assertEqual(2, len(res)) self.assertEqual('Dash 2', res[0].dashboard_name) self.assertEqual('Dash 4', res[1].dashboard_name) detach_tile(t3) res = od.get_dashboards_displaying_report(r.report_id) self.assertEqual(2, len(res)) self.assertEqual('Dash 2', res[0].dashboard_name) self.assertEqual('Dash 4', res[1].dashboard_name) detach_tile(t2) res = od.get_dashboards_displaying_report(r.report_id) self.assertEqual(1, len(res)) self.assertEqual('Dash 4', res[0].dashboard_name) od.dashboards[3].delete() res = od.get_dashboards_displaying_report(r.report_id) self.assertEqual(0, len(res))
def test_expire_tiles_without_data(self): rd1 = new_report_data('points') rd2 = new_report_data('points') tile_config_1 = { 'tw_type': 'Range', 'series_spec_list': [ dataseries.SeriesSpec(2, 0, dict(op='eq', args=['monique'])), ], 'tile_options': { 'seconds_back': 86400, } } tile1 = tiles.Tile.insert(rd1.owner_id, rd1.report.report_id, rd1.dashboard_id, tile_config_1) place_tile(tile1) self.assertTrue(tile1.get_tile_data()['series_data'][0]['data_points']) tile_config_2 = { 'tw_type': 'Range', 'series_spec_list': [ dataseries.SeriesSpec(2, 0, dict(op='eq', args=['john20'])), ], 'tile_options': { 'seconds_back': 86400, } } tile2 = tiles.Tile.insert(rd2.owner_id, rd2.report.report_id, rd2.dashboard_id, tile_config_2) place_tile(tile2) self.assertFalse( tile2.get_tile_data()['series_data'][0]['data_points']) res = tiles.expire_tiles_without_data([tile1, tile2], 3600, Layout.select( rd1.owner_id, rd1.dashboard_id).layout_id, optimize_check=True) self.assertIsNone(res) time.sleep(0.5) rd2.report.process_input('0') res = tiles.expire_tiles_without_data([tile1, tile2], 0.5, Layout.select( rd1.owner_id, rd1.dashboard_id).layout_id, optimize_check=True) self.assertTrue(res) self.assertFalse(Tile.select(rd1.dashboard_id, tile1.tile_id)) self.assertTrue(Tile.select(rd2.dashboard_id, tile2.tile_id))
def test_filling_default_options(self): rd = new_report_data('points', tags=['ip:192.168.1.1']) for tw_type in ('Range', 'Single'): ss1 = dataseries.SeriesSpec(2, 0, dict(op='eq', args=['mike'])) ss1.promote_colnos_to_headers(rd.instances[-1]) ss1.set_name('mikepoints') tile_config = { 'tw_type': tw_type, 'tags': ['ip:192.168.1.1'], 'series_spec_list': [ ss1, dataseries.SeriesSpec(2, 0, dict(op='eq', args=['monique'])), ], 'tile_options': { 'colors': ['red', 'blue', 'pink', 'cyan'], 'seconds_back': 600, 'tile_title': 'Points by user', 'sscs': dataseries.SeriesSpec(2, 0, dict(op='eq', args=['monique'])), } } tile = Tile.insert(rd.owner_id, rd.report.report_id, rd.dashboard_id, tile_config) dataseries.update_default_options(tile) data = tile.get_tile_data() self.assertEqual(['red', 'blue'], data['combined_colors']) tile_config2 = { 'tw_type': tw_type, 'tags': ['ip:192.168.1.1'], 'series_spec_list': [ dataseries.guess_series_spec(rd.report, rd.instances[-1], 1, 2), dataseries.SeriesSpec(2, 0, dict(op='eq', args=['monique'])), ], } self.assertEqual('mikepoints', tile_config2['series_spec_list'][0].name()) tile2 = Tile.insert(rd.owner_id, rd.report.report_id, rd.dashboard_id, tile_config) dataseries.update_default_options(tile2) data2 = tile2.get_tile_data() self.assertEqual('mikepoints', data2['series_data'][0]['name']) self.assertEqual(['red', 'blue'], data2['combined_colors'])
def do_tpcreator_mod(layout_mod): tpcreator_spec_by_master_id, tpcreated_tags_by_master_id = _get_tpcreator_data( layout_mod, report_instance.report_id) log.debug('tpcreator data: %s, %s', tpcreator_spec_by_master_id, tpcreated_tags_by_master_id) if not tpcreator_spec_by_master_id: log.info('Deleting obsoleted layout_by_report tpcreator row') c.dao.LayoutDAO.delete_layout_by_report(layout_row['owner_id'], layout_row['report_id'], layout_row['tags'], layout_row['label'], layout_row['dashboard_id'], layout_row['layout_id']) return for master_id, tpcreator_spec in tpcreator_spec_by_master_id.items(): log.debug('Processing master_id=%s tpcreator_spec=%s', master_id, tpcreator_spec) tpcreated_tags = tpcreated_tags_by_master_id[master_id] if len(tpcreated_tags) >= max_tpcreated: log.warn('Too many tpcreated for master_id=%s: %s', master_id, len(tpcreated_tags)) continue matching_tags = tags_matching_tpcreator_spec( tpcreator_spec, report_instance.all_tags) if not matching_tags: log.debug('No tags match the tpcreator_spec') continue if tuple(matching_tags) in tpcreated_tags: log.debug( 'A tpcreated tile already exists for the matched tags %s', matching_tags) continue master_tile = Tile.select(layout_row['dashboard_id'], master_id) if not master_tile: log.warn('No master_tile') continue new_tile_options = _tile_options_of_tpcreated( master_tile, tpcreator_spec, matching_tags) new_tile = Tile.insert_with_tile_options(master_tile.dashboard_id, new_tile_options) log.info( 'tpcreator created new tile with tags %s for report_id=%s', matching_tags, layout_row['report_id']) layouts.place_tile_mod(new_tile, size_of=master_tile.tile_id)(layout_mod)
def replace_tpcreated(layout, old_master, new_master, sync_tpcreated=True, skip_replacements=set()): tpcreator_spec = layout.layout_props['by_tile_id'][ old_master.tile_id]['tpcreator_spec'] tpcreated_tile_id_list = layout.get_tpcreated_tile_ids(old_master.tile_id) if skip_replacements: tpcreated_tile_id_list = [ tid for tid in tpcreated_tile_id_list if tid not in skip_replacements ] tpcreated_tiles = tiles.Tile.select_multi(old_master.dashboard_id, tpcreated_tile_id_list) tpcreated_tiles_new_tos = [] for tile in tpcreated_tiles.itervalues(): if sync_tpcreated: to = _tile_options_of_tpcreated(new_master, tpcreator_spec, tile.tags) else: to = _sync_tpcreator_data(new_master, tile, tpcreator_spec) tpcreated_tiles_new_tos.append(to) tile_replacement = {} tpcreated_tiles_repl = Tile.insert_with_tile_options_multi( old_master.dashboard_id, tpcreated_tiles_new_tos) for tt, ttr in zip(tpcreated_tiles.values(), tpcreated_tiles_repl): tile_replacement[tt] = ttr return tile_replacement
def _tile_options_of_tpcreated(master_tile, tpcreator_spec, tags): partial_new_tile = Tile.insert( master_tile.owner_id, master_tile.report_id, master_tile.dashboard_id, skip_db=True, tile_config={ 'tw_type': master_tile.tile_options['tw_type'], 'tags': tags, 'series_spec_list': master_tile.series_specs(), 'tile_options': { k: v for k, v in master_tile.tile_options.items() if k not in {'series_configs', 'tags', 'tile_title'} } }) new_tile_options = partial_new_tile.tile_options if 'tpcreator_data' not in new_tile_options: new_tile_options['tpcreator_data'] = {} tile_title = master_tile.tile_options.get('tile_title') if tile_title: postfix = master_tile.tilewidget.generate_tile_title_postfix() if postfix: tile_title = tile_title.replace(postfix, '').strip() new_tile_options['tpcreator_data']['tile_title_base'] = tile_title new_tile_options.pop('tpcreator_uispec', None) new_tile_options['tpcreator_data']['master_tile_id'] = master_tile.tile_id new_tile_options['tpcreator_data'][ 'master_tpcreator_uispec'] = copy.deepcopy( master_tile.tile_options['tpcreator_uispec']) new_tile_options['tpcreator_data'][ 'master_tpcreator_spec'] = tpcreator_spec return new_tile_options
def test_promote_first_as_master_losing_sscreated(self): rd = ReportData('r') ss = dataseries.SeriesSpec(0, -1, dict(op='eq', args=['0'])) tile_config1 = { 'series_spec_list': [ss], 'tags': ['p1:10'], 'tile_options': { 'tpcreator_uispec': tpcreator.suggested_tpcreator_uispec(['p1:10']), 'sscs': ss } } tile1 = Tile.insert(rd.owner_id, rd.report.report_id, rd.dashboard_id, tile_config1) place_tile(tile1) ri1 = rd.report.process_input('1\n2\n', tags=['p1:9'], handle_tpcreator=False, handle_sscreator=False).report_instance ri2 = rd.report.process_input('1\n2\n3\n', tags=['p1:12'], handle_tpcreator=False, handle_sscreator=False).report_instance tpcreator.handle_tpcreator(rd.owner_id, rd.report_id, ri1, make_first_master=False) sscreator.handle_sscreator(rd.owner_id, rd.report_id, ri1) tpcreator.handle_tpcreator(rd.owner_id, rd.report_id, ri2, make_first_master=False) sscreator.handle_sscreator(rd.owner_id, rd.report_id, ri2) self.assertEqual(3, len(rd.layout().layout_dict)) self.assertEqual(3, len(rd.get_tile_by_tags(['p1:12']).series_specs())) ri3 = rd.report.process_input('1\n2\n3\n', tags=['p1:13'], handle_tpcreator=False).report_instance tpcreator.handle_tpcreator(rd.owner_id, rd.report_id, ri3, make_first_master=True) self.assertEqual(4, len(rd.layout().layout_dict)) master_tile = rd.get_tile_by_tags(['p1:9']) self.assertTrue(master_tile.is_master_tile()) self.assertEqual(master_tile.tile_id, rd.get_tile_by_tags(['p1:12']).get_master_tile_id()) self.assertEqual(3, len(rd.get_tile_by_tags(['p1:12']).series_specs())) self.assertEqual(['0', '1', '2'], [ ss.params['filtering_expr']['args'][0] for ss in rd.get_tile_by_tags(['p1:12']).series_specs() ])
def main(): owner_id = uuid.uuid4() report = Report.select_or_insert(owner_id, 'simple_report') res = report.process_input('1 2 3') assert res.report_instance owner_dashboards = OwnerDashboards( owner_id, insert_if_no_dashboards='Default Dashboard') assert owner_dashboards.dashboards tile_config = { 'series_spec_list': [SeriesSpec(0, -1, { 'op': 'eq', 'args': ['0'] })], } tile = Tile.insert(owner_id, report.report_id, owner_dashboards.dashboards[0].dashboard_id, tile_config) assert tile assert tile.get_tile_data() print 'Successfully created a dashboard with a tile' owner_dashboards.dashboards[0].delete()
def test_sscs_override_static_name(self): tile_config = { 'tags': ['ip:192.168.1.1'], 'tw_type': 'Range', 'series_spec_list': [ SeriesSpec(2, 0, dict(op='eq', args=['monique'])), SeriesSpec(2, 0, dict(op='eq', args=['john'])), ], 'tile_options': { 'seconds_back': 86400, 'tile_title': 'Points by user' } } tile_config['series_spec_list'][0].params['static_name'] = 'monique_points' self.assertEqual('monique_points', tile_config['series_spec_list'][0].name()) tile_config['tile_options']['sscs'] = tile_config['series_spec_list'][0] rd = new_report_data('points') tile = Tile.insert(rd.owner_id, rd.report.report_id, rd.dashboard_id, tile_config) layouts.place_tile(tile) d = [OrderedDict([('user_name', 'robert'), ('is_active', True), ('points', 128)]), OrderedDict([('user_name', 'monique'), ('is_active', True), ('points', 210)])] res = rd.report.process_input(json.dumps(d), tags=tile_config['tags']) tile = rd.only_tile_from_layout() self.assertEqual(3, len(tile.series_specs())) expected_ss = SeriesSpec(2, 0, dict(op='eq', args=['robert'])) self.assertEqual('robert', expected_ss.name()) self.assertEqual('robert', tile.series_specs()[-1].name())
def test_sscreator_as_mod(self): owner_id = uuid.uuid4() dashboard_id = uuid.uuid4() tile_config = { 'series_spec_list': [ SeriesSpec(1, 0, dict(op='eq', args=['label1'])), ], 'tile_options': { 'sscs': SeriesSpec(1, 0, dict(op='eq', args=['label1'])) } } r = Report.insert(owner_id, 'r') tile = Tile.insert(owner_id, r.report_id, dashboard_id, tile_config) layouts.place_tile(tile) ri1 = r.process_input('label1 1', handle_sscreator=False).report_instance ri2 = r.process_input('label11 11\nlabel12 12', handle_sscreator=False).report_instance ri3 = r.process_input('label21 21\nlabel22 22', handle_sscreator=False).report_instance layout_rows = c.dao.LayoutDAO.select_layout_by_report_multi(owner_id, r.report_id, [], 'sscs', 100) mods = [sscreator.sscreator_mod(ri1, layout_rows[0]), sscreator.sscreator_mod(ri2, layout_rows[0]), sscreator.sscreator_mod(ri3, layout_rows[0])] layouts.apply_mods(mods, owner_id, dashboard_id, None) layout = Layout.select(owner_id, dashboard_id) tile = layout.tile_dict.keys()[0]
def make_master_from_tpcreated(old_master, tpcreated): """Based on an old master |Tile| ``old_master``, creates a new master |Tile| from ``tpcreated`` |Tile| which must be a |Tile| tpcreated from ``old_master``.""" assert old_master.is_master_tile() assert not tpcreated.is_master_tile() new_master_to = copy.deepcopy(tpcreated.tile_options) new_master_to['tpcreator_uispec'] = old_master.tile_options[ 'tpcreator_uispec'] del new_master_to['tpcreator_data'] if new_master_to.get('tile_title'): postfix = tpcreated.tilewidget.generate_tile_title_postfix() if postfix and postfix in new_master_to['tile_title']: new_master_to['tile_title'].replace(postfix, '').strip() old_master_title = old_master.tile_options.get('tile_title') if old_master_title and not new_master_to.get('tile_title'): # strip old postfix old_postfix = old_master.tilewidget.generate_tile_title_postfix() if old_postfix in old_master_title: new_postfix = tpcreated.tilewidget.generate_tile_title_postfix() old_master_title = old_master_title.replace( old_postfix, new_postfix) new_master_to['tile_title'] = old_master_title return Tile.insert_with_tile_options(old_master.dashboard_id, new_master_to)
def test_single__text_single_drawer(self): rd = new_report_data('points', tags=['ip:192.168.1.1']) tile_config = { 'tw_type': 'Single', 'tags': ['ip:192.168.1.1'], 'series_spec_list': [ dataseries.SeriesSpec(2, 0, dict(op='eq', args=['monique'])), ], 'tile_options': { 'seconds_back': 600, 'tile_title': 'Points by user', 'sscs': dataseries.SeriesSpec(2, 0, dict(op='eq', args=['monique'])), 'drawer_type': 'TextSingleDrawer' } } tile_config['tile_options'][ 'tpcreator_uispec'] = tpcreator.suggested_tpcreator_uispec( tile_config['tags']) tile = Tile.insert(rd.owner_id, rd.report.report_id, rd.dashboard_id, tile_config) layouts.place_tile(tile) self.assertEqual(tile.tile_options['drawer_type'], 'TextSingleDrawer') d = [ OrderedDict([('user_name', 'robert3'), ('is_active', True), ('points', 128)]) ] pr = rd.report.process_input(json.dumps(d), tags=['ip:192.168.1.1'], extra_ri_data={'ed': 88}) tile = rd.only_tile_from_layout() data = tile.get_new_tile_data(pr.report_instance.report_instance_id) self.assertEqual(2, len(data['series_data'])) self.assertEqual([], data['series_data'][0]['data_points']) self.assertEqual([], data['series_data'][1]['data_points']) data = tile.get_new_tile_data(rd.instances[-1].report_instance_id) self.assertEqual(2, len(data['series_data'])) self.assertEqual([], data['series_data'][0]['data_points']) self.assertEqual(128, data['series_data'][1]['data_points'][0].value) data = tile.get_new_tile_data(rd.instances[-3].report_instance_id) self.assertEqual(2, len(data['series_data'])) self.assertEqual([], data['series_data'][0]['data_points']) self.assertEqual(128, data['series_data'][1]['data_points'][0].value) data = tile.get_new_tile_data(None) self.assertEqual(2, len(data['series_data'])) self.assertEqual([], data['series_data'][0]['data_points']) self.assertEqual(128, data['series_data'][1]['data_points'][0].value) return tile
def test_layout_sorting_complex_tag_names_2(self): owner_id = uuid.uuid4() dashboard_id = uuid.uuid4() rep = reports.Report.insert(owner_id, 'r') tile_config = { 'tags': ['server:web.1'], 'series_spec_list': [dataseries.SeriesSpec(0, -1, { 'op': 'eq', 'args': '0' })], 'tile_options': { 'tpcreator_uispec': tpcreator.suggested_tpcreator_uispec(['server:web.1']) } } master_tile = Tile.insert(owner_id, rep.report_id, dashboard_id, tile_config) layouts.place_tile(master_tile) rep.process_input('1', tags=['server:worker.1']) rep.process_input('1', tags=['server:web.2']) layout = layouts.Layout.select(owner_id, dashboard_id) tiles = sorted( layout.tile_dict, key=lambda tile: (layout.tile_dict[tile]['y'], layout.tile_dict[tile]['x'])) tags_lists = [tile.tags for tile in tiles] self.assertEqual( [['server:web.1'], ['server:web.2'], ['server:worker.1']], tags_lists)
def test_repack_performance(self): rd = ReportData('r') tile_config = { 'tags': ['p1:1'], 'series_spec_list': [ dataseries.SeriesSpec(0, -1, dict(op='eq', args=['0'])), ], 'tile_options': { 'tpcreator_uispec': [{ 'tag': 'p1:1', 'prefix': 'p1:' }], } } tile = Tile.insert(rd.owner_id, rd.report_id, rd.dashboard_id, tile_config) place_tile(tile) start = time() for i in range(1, 201): rd.report.process_input('1', tags=['p1:%s' % i]) print 'Tiles created in %.1f' % ((time() - start) * 1000) start = time() layouts.repack(rd.owner_id, rd.dashboard_id) print 'Single repack in %.1f' % ((time() - start) * 1000)
def test_get_tile_config(self): tile_config = { 'tw_type': 'Range', 'tags': ['ip:192.168.1.1'], 'series_spec_list': [ dataseries.SeriesSpec(2, 0, dict(op='eq', args=['monique'])), ], 'tile_options': { 'seconds_back': 600, 'tile_title': 'Points by user', 'sscs': dataseries.SeriesSpec(2, 0, dict(op='eq', args=['monique'])), 'tpcreator_uispec': tpcreator.suggested_tpcreator_uispec(['ip:192.168.1.1']) } } owner_id = uuid.uuid1() report_id = report_data('points').report.report_id dashboard_id = report_data('points').dashboard_id tile = Tile.insert(owner_id, report_id, dashboard_id, tile_config) res = tile.get_tile_config() self.assertDictContainsSubset(dictwithout(tile_config, 'tile_options'), dictwithout(res, 'tile_options')) self.assertDictContainsSubset(tile_config['tile_options'], res['tile_options'])
def test_select_multi(self): tiles = [self.test_insert() for _ in range(3)] tile_ids = [t.tile_id for t in tiles] selected_tiles = Tile.select_multi( report_data('points').dashboard_id, tile_ids) self.assertItemsEqual(tile_ids, selected_tiles.keys()) self.assertItemsEqual(tiles, selected_tiles.values())
def test_delete(self): tile_config = { 'tw_type': 'Range', 'tags': ['ip:192.168.1.1'], 'series_spec_list': [ dataseries.SeriesSpec(2, 0, dict(op='eq', args=['monique'])), ], 'tile_options': { 'seconds_back': 600, 'tile_title': 'Points by user', 'sscs': dataseries.SeriesSpec(2, 0, dict(op='eq', args=['monique'])), 'tpcreator_uispec': tpcreator.suggested_tpcreator_uispec(['ip:192.168.1.1']) } } owner_id = uuid.uuid1() report_id = report_data('points').report.report_id dashboard_id = report_data('points').dashboard_id tile = Tile.insert(owner_id, report_id, dashboard_id, tile_config) place_tile(tile) self.assertTrue(self._select_tpcreator_rows(tile)) self.assertTrue(self._select_sscs_rows(tile)) tile = tile.select(tile.dashboard_id, tile.tile_id) detach_tile(tile) deleted_tile = tile.select(tile.dashboard_id, tile.tile_id) self.assertIsNone(deleted_tile)
def test_range__chart_range_drawer(self): rd = new_report_data('points', tags=['ip:192.168.1.1']) tile_config = { 'tw_type': 'Range', 'tags': ['ip:192.168.1.1'], 'series_spec_list': [ dataseries.SeriesSpec(2, 0, dict(op='eq', args=['monique'])), ], 'tile_options': { 'seconds_back': 600, 'tile_title': 'Points by user', 'sscs': dataseries.SeriesSpec(2, 0, dict(op='eq', args=['monique'])), } } tile_config['tile_options'][ 'tpcreator_uispec'] = tpcreator.suggested_tpcreator_uispec( tile_config['tags']) tile = Tile.insert(rd.owner_id, rd.report.report_id, rd.dashboard_id, tile_config) layouts.place_tile(tile) self.assertEqual(tile.tile_options['drawer_type'], 'ChartRangeDrawer') d = [ OrderedDict([('user_name', 'robert3'), ('is_active', True), ('points', 128)]) ] rd.report.process_input(json.dumps(d), tags=['ip:192.168.1.1'], extra_ri_data={'ed': 88}) tile = rd.only_tile_from_layout() data = tile.get_tile_data() self.assertEqual('points (monique, robert3)', data['generated_tile_title']) self.assertEqual('[ip:192.168.1.1]', data['generated_tile_title_postfix']) self.assertEqual(0, data['extra_options']['y_axis_min']) self.assertIsInstance(data['fetched_from_dt'], datetime.datetime) self.assertIsInstance(data['fetched_to_dt'], datetime.datetime) self.assertEqual({'ed': 88}, data['latest_extra_ri_data']) self.assertEqual('points', data['report_name']) self.assertEqual(2, len(data['series_data'])) for sd in data['series_data']: self.assertIsInstance(sd['series_id'], uuid.UUID) self.assertEqual('points', data['common_header']) self.assertTrue(sd['data_points']) for dp in sd['data_points']: self.assertIsInstance(dp, tilewidgets.DataPoint) self.assertEqual(mqeconfig.DEFAULT_COLORS[:2], data['combined_colors']) self.assertEqual('points', data['common_header']) return tile
def test_promote_first_as_master_multiple_masters_one_apply_mods_run(self): rd = self.test_repack_dont_put_master_first() tile_config = { 'tags': ['q1:10'], 'series_spec_list': [ dataseries.SeriesSpec(0, -1, dict(op='eq', args=['0'])), ], 'tile_options': { 'tpcreator_uispec': [{ 'tag': 'q1:10', 'prefix': 'q1:' }], } } r2 = Report.insert(rd.owner_id, 'r2') master_tile2 = Tile.insert(rd.owner_id, r2.report_id, rd.dashboard_id, tile_config) layouts.place_tile(master_tile2) ri1 = r2.process_input('0', tags=['q1:8'], handle_tpcreator=False).report_instance ri2 = r2.process_input('0', tags=['q1:12'], handle_tpcreator=False).report_instance ri3 = r2.process_input('0', tags=['q1:6'], handle_tpcreator=False).report_instance ri4 = rd.report.process_input('0', tags=['p1:2'], handle_tpcreator=False).report_instance layout_rows_tpcreator = c.dao.LayoutDAO.select_layout_by_report_multi( rd.owner_id, rd.report_id, [], 'tpcreator', 100) mods = [ tpcreator.tpcreator_mod(ri1, layout_rows_tpcreator[0]), tpcreator.tpcreator_mod(ri2, layout_rows_tpcreator[0]), tpcreator.tpcreator_mod(ri3, layout_rows_tpcreator[0]), tpcreator.tpcreator_mod(ri4, layout_rows_tpcreator[0]), layouts.repack_mod(put_master_first=False), layouts.promote_first_as_master_mod(), layouts.if_mod(lambda layout_mod: layout_mod.tile_replacement, layouts.repack_mod()) ] layouts.apply_mods(mods, rd.owner_id, rd.dashboard_id, None) self.assertEqual([['p1:2'], ['p1:6'], ['p1:8'], ['p1:10'], ['p1:12'], ['q1:6'], ['q1:8'], ['q1:10'], ['q1:12']], [tile.tags for tile in rd.tiles_sorted_by_vo()]) master1 = rd.get_tile_by_tags(['p1:2']) master2 = rd.get_tile_by_tags(['q1:6']) self.assertTrue(master1.is_master_tile()) self.assertTrue(master2.is_master_tile()) for tile in rd.tiles_sorted_by_vo(): if tile.tags[0].startswith( 'p') and tile.tile_id != master1.tile_id: self.assertEqual(master1.tile_id, tile.get_master_tile_id()) elif tile.tags[0].startswith( 'q') and tile.tile_id != master2.tile_id: self.assertEqual(master2.tile_id, tile.get_master_tile_id())
def test_removing_tiles(self): rd, master_tile = self.test_handle_tpcreator() tiles = Tile.select_multi(rd.dashboard_id, _select_tile_ids(rd.dashboard_id)).values() tpcreated_tile = util.first(t for t in tiles if t.get_master_tile_id()) layouts.detach_tile(tpcreated_tile) self.assertEqual(1, len(tpcreator.select_tpcreated_tile_ids(master_tile)))
def tile_from_layout(self, idx, expected_layout_size=None): layout = layouts.Layout.select(self.owner_id, self.dashboard_id) if expected_layout_size is not None: assert len(layout.layout_dict) == expected_layout_size ids = sorted(layout.layout_dict.keys(), key=lambda ud: ud.time) tile = Tile.select(self.dashboard_id, ids[idx]) assert tile return tile
def test_equality(self): tile1 = self.test_insert() tile2 = self.test_insert() self.assertNotEqual(tile1, tile2) tile3 = Tile.select(tile1.dashboard_id, tile1.tile_id) self.assertEqual(tile3, tile1) self.assertDictEqual(tile1.tile_options, tile3.tile_options)
def test_tile_options_validation(self): rd = report_data('points') tile_config = { 'tags': ['1', '2', '3', '4'], 'series_spec_list': [], } self.assertRaises( ValueError, lambda: Tile.insert(rd.owner_id, rd.report.report_id, rd.dashboard_id, tile_config))