def replace_tiles(old_to_new_tile_dict, for_layout_id, sync_tpcreated=True): """Replace tiles in the :attr:`~Layout.layout_dict`. The ``old_to_new_tile_dict`` maps a :class:`~mqe.tiles.Tile` object already present in the layout to either a new :class:`~mqe.tiles.Tile` object acting as the replacement, or ``None`` which means that the old tile should be detached. If the operations is successful, the tiles present in the ``old_to_new_tile_dict`` as keys are deleted. Otherwise, the values of the dictionary are deleted. :param old_to_new_tile_dict: a dictionary defining the replacement :param ~uuid.UUID for_layout_id: if ``None``, do the replacement in a layout with any :attr:`~mqe.layouts.Layout.layout_id`, possibly making multiple tries. Otherwise, perform the operation only if the current :attr:`~mqe.layouts.Layout.layout_id` matches the parameter :param sync_tpcreated: relevant if the ``old_to_new_tile_dict`` contains a master tile - tells whether the :data:`tile_options` of tpcreated tiles should be synchronized (by replacing the tpcreated tiles) :return: a :class:`LayoutModificationResult` if the operation was successful, ``None`` otherwise. """ sample_tile = util.first(old_to_new_tile_dict.iterkeys()) dashboard_id = sample_tile.dashboard_id return apply_mods([replace_tiles_mod(old_to_new_tile_dict, sync_tpcreated=sync_tpcreated)], sample_tile.owner_id, sample_tile.dashboard_id, for_layout_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 test_promote_other_master_tile(self): rd, master_tile = self.test_handle_tpcreator() tpcreated_ids = tpcreator.select_tpcreated_tile_ids(master_tile) tpcreated_tiles = Tile.select_multi(rd.dashboard_id, tpcreated_ids).values() #print 'tpcreated', [t.tags for t in tpcreated_tiles] new_master_repl = util.first(t for t in tpcreated_tiles if t.tile_options['tile_title'] == 'tpc0') new_master_tile = tpcreator.make_master_from_tpcreated( master_tile, new_master_repl) self.assertEqual('tpc0', new_master_tile.tile_options['tile_title']) self.assertTrue( layouts.replace_tiles({master_tile: new_master_tile}, None)) self.assertFalse(tpcreator.select_tpcreated_tile_ids(master_tile)) new_tpcreated_ids = tpcreator.select_tpcreated_tile_ids( new_master_tile) new_tpcreated_tiles = Tile.select_multi(rd.dashboard_id, new_tpcreated_ids).values() self.assertTrue(new_tpcreated_tiles) self.assertEqual([['p1:20'], ['p1:30']], sorted([t.tags for t in new_tpcreated_tiles])) d = [ OrderedDict([('user_name', 'robert10'), ('is_active', True), ('points', 128)]) ] rd.report.process_input(json.dumps(d), tags=['p1:15', 'p2:34']) self.assertEqual( len(new_tpcreated_ids) + 1, len(tpcreator.select_tpcreated_tile_ids(new_master_tile))) latest_tile = Tile.select(rd.dashboard_id, _select_tile_ids(rd.dashboard_id)[-1]) self.assertEqual(['p1:15'], latest_tile.tags) self.assertEqual(['monique', 'robert3', 'robert10'], [ ss.params['filtering_expr']['args'][0] for ss in latest_tile.series_specs() ])
def test_handle_tpcreator(self): rd = new_report_data('points') tile_config = { 'tw_type': 'Range', 'tags': ['p1:10'], 'series_spec_list': [ dataseries.SeriesSpec(2, 0, dict(op='eq', args=['monique'])), ], 'tile_options': { 'seconds_back': 600, 'tile_title': 'm0', 'sscs': dataseries.SeriesSpec(2, 0, dict(op='eq', args=['monique'])), } } tile_config['tile_options'][ 'tpcreator_uispec'] = tpcreator.suggested_tpcreator_uispec( tile_config['tags']) master_tile = Tile.insert(rd.owner_id, rd.report.report_id, rd.dashboard_id, tile_config) layouts.place_tile(master_tile) d = [ OrderedDict([('user_name', 'robert3'), ('is_active', True), ('points', 128)]) ] rd.report.process_input(json.dumps(d), tags=['p1:10']) master_tile = rd.only_tile_from_layout() self.assertEqual([], tpcreator.select_tpcreated_tile_ids(master_tile)) self.assertEqual([master_tile.tile_id], _select_tile_ids(rd.dashboard_id)) d = [ OrderedDict([('user_name', 'robert3'), ('is_active', True), ('points', 128)]) ] rd.report.process_input(json.dumps(d), tags=['p1:20']) self.assertEqual(2, len(_select_tile_ids(rd.dashboard_id))) tiles = Tile.select_multi(rd.dashboard_id, _select_tile_ids(rd.dashboard_id)).values() created_tile = util.first(tiles, key=lambda t: not t.is_master_tile()) self.assertEqual(['p1:20'], created_tile.tile_options['tags']) self.assertEqual(600, created_tile.tile_options['seconds_back']) self.assertEqual(tile_config['tile_options']['sscs'], created_tile.tile_options['sscs']) td = created_tile.get_tile_data() self.assertEqual('points (monique, robert3)', td['generated_tile_title']) self.assertEqual('[p1:20]', td['generated_tile_title_postfix']) d = [ OrderedDict([('user_name', 'robert3'), ('is_active', True), ('points', 128)]) ] rd.report.process_input(json.dumps(d), tags=['p1:30', 'p2:30']) self.assertEqual(3, len(_select_tile_ids(rd.dashboard_id))) del tile_config['tile_options']['tpcreator_uispec'] tile_config['tile_options']['tile_title'] = 'ot0' other_tile = Tile.insert(rd.owner_id, rd.report.report_id, rd.dashboard_id, tile_config) layouts.place_tile(other_tile) self.assertEqual(4, len(_select_tile_ids(rd.dashboard_id))) self.assertEqual(2, len(tpcreator.select_tpcreated_tile_ids(master_tile))) for i, tile_id in enumerate( tpcreator.select_tpcreated_tile_ids(master_tile)): tile = Tile.select(rd.dashboard_id, tile_id) tile_config = tile.get_tile_config() tile_config['tile_options']['tile_title'] = 'tpc%d' % i new_tile = tile.insert_similar(tile_config) layouts.replace_tiles({tile: new_tile}, None) return rd, Tile.select(master_tile.dashboard_id, master_tile.tile_id)
def test_expire_tiles_without_data_two_masters(self): rd = new_report_data('points', ['p3:30']) tile_config1 = { 'series_spec_list': [ dataseries.SeriesSpec(2, 0, dict(op='eq', args=['monique'])), ], 'tile_options': { 'seconds_back': 1000, 'tile_title': 'tile1' } } tile1 = Tile.insert(rd.owner_id, rd.report.report_id, rd.dashboard_id, tile_config1) place_tile(tile1) tile_config2 = { 'tags': ['p1:10'], 'series_spec_list': [ dataseries.SeriesSpec(2, 0, dict(op='eq', args=['monique'])), ], 'tile_options': { 'seconds_back': 1000, 'tile_title': 'tile2 [p1:10]', 'tpcreator_uispec': tpcreator.suggested_tpcreator_uispec(['p1:10']), } } tile2 = Tile.insert(rd.owner_id, rd.report.report_id, rd.dashboard_id, tile_config2) place_tile(tile2) tile_config3 = { 'tags': ['p2:20'], 'series_spec_list': [ dataseries.SeriesSpec(2, 0, dict(op='eq', args=['monique'])), ], 'tile_options': { 'seconds_back': 1000, 'tile_title': 'tile3', 'tpcreator_uispec': tpcreator.suggested_tpcreator_uispec(['p2:20']), } } tile3 = Tile.insert(rd.owner_id, rd.report.report_id, rd.dashboard_id, tile_config3) place_tile(tile3) #rd.report.process_input(json.dumps([dict(user_name='monique', is_active=True, points=128)]), tags=['p1:10']) #rd.report.process_input(json.dumps([dict(user_name='monique', is_active=True, points=128)]), tags=['p2:20']) time.sleep(1) rd.report.process_input(json.dumps([ OrderedDict([('user_name', 'monique'), ('is_active', True), ('points', 128)]) ]), tags=['p1:11']) rd.report.process_input(json.dumps([ OrderedDict([('user_name', 'monique'), ('is_active', True), ('points', 128)]) ]), tags=['p1:12']) rd.report.process_input(json.dumps([ OrderedDict([('user_name', 'monique'), ('is_active', True), ('points', 128)]) ]), tags=['p1:13']) rd.report.process_input(json.dumps([ OrderedDict([('user_name', 'monique'), ('is_active', True), ('points', 128)]) ]), tags=['p2:21']) rd.report.process_input(json.dumps([ OrderedDict([('user_name', 'monique'), ('is_active', True), ('points', 128)]) ]), tags=['p2:22']) lid = tiles.expire_tiles_without_data(rd.layout().tile_dict.keys(), 2000, rd.layout().layout_id) self.assertFalse(lid) lid = tiles.expire_tiles_without_data(rd.layout().tile_dict.keys(), 1, rd.layout().layout_id) self.assertTrue(lid) self.assertEqual(6, len(rd.layout().layout_dict)) self.assertEqual(6, len(_select_tile_ids(rd.dashboard_id))) tile_list = rd.layout().tile_dict.keys() self.assertEqual( sorted([[], ['p1:11'], ['p1:12'], ['p1:13'], ['p2:21'], ['p2:22']]), sorted([t.tags for t in tile_list])) master1 = first( t for t in tile_list if t.tags and t.tags[0].startswith('p1') and t.is_master_tile()) master2 = first( t for t in tile_list if t.tags and t.tags[0].startswith('p2') and t.is_master_tile()) self.assertEqual( 2, len(rd.layout().get_tpcreated_tile_ids(master1.tile_id))) self.assertEqual( 1, len(rd.layout().get_tpcreated_tile_ids(master2.tile_id))) self.assertEqual(['p1:11'], master1.tags) self.assertEqual(['p2:21'], master2.tags) self.assertEqual('tile2 [p1:11]', master1.tile_options['tile_title']) self.assertEqual('tile3', master2.tile_options['tile_title'])
def test_layout_modification_by_tpcreator(self): data = {} @signals.layout_modified.connect def on_tiles_replaced(c, **kwargs): if kwargs['reason'] != 'tpcreator': return data.clear() data.update(kwargs) rd = new_report_data('points') tile_config = { 'tw_type': 'Range', 'tags': ['p1:10'], 'series_spec_list': [ dataseries.SeriesSpec(2, 0, dict(op='eq', args=['monique'])), ], 'tile_options': { 'seconds_back': 600, 'tile_title': 'm0', 'sscs': dataseries.SeriesSpec(2, 0, dict(op='eq', args=['monique'])), } } tile_config['tile_options'][ 'tpcreator_uispec'] = tpcreator.suggested_tpcreator_uispec( tile_config['tags']) master_tile = Tile.insert(rd.owner_id, rd.report.report_id, rd.dashboard_id, tile_config) layouts.place_tile(master_tile) d = [ OrderedDict([('user_name', 'robert3'), ('is_active', True), ('points', 128)]) ] rd.report.process_input(json.dumps(d), tags=['p1:10']) d = [ OrderedDict([('user_name', 'robert3'), ('is_active', True), ('points', 128)]) ] rd.report.process_input(json.dumps(d), tags=['p1:20']) d = [ OrderedDict([('user_name', 'robert3'), ('is_active', True), ('points', 128)]) ] rd.report.process_input(json.dumps(d), tags=['p1:30', 'p2:30']) tile = util.first(tile for tile in rd.layout().tile_dict if tile.tags == \ ['p1:30']) self.assertIsNotNone(tile) self.assertEqual( tile, data['layout_modification_result'].new_tiles.keys()[0]) self.assertEqual(rd.layout().layout_id, data['layout_modification_result']. \ new_layout.layout_id)