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 handle_tpcreator(owner_id, report_id, report_instance): """The method calls the TPCreator (see :ref:`guide_tpcreator`) for the given report instance, possibly creating new tiles from a master tile and altering dashboards' layouts. The signal :attr:`~mqe.signals.layout_modified` is issued for each modification.""" layout_rows = c.dao.LayoutDAO.select_layout_by_report_multi( owner_id, report_id, [], 'tpcreator', mqeconfig.MAX_TPCREATORS_PER_REPORT) if not layout_rows: log.debug('No layout_by_report tpcreator rows') return log.info( 'tpcreator is processing %s rows for owner_id=%s report_id=%s report_instance_id=%s', len(layout_rows), owner_id, report_id, report_instance.report_instance_id) for row in layout_rows: mods = [ tpcreator_mod(report_instance, row), layouts.if_mod(lambda layout_mod: layout_mod.new_tiles, layouts.repack_mod()) ] lmr = layouts.apply_mods(mods, owner_id, row['dashboard_id'], for_layout_id=None, max_tries=MAX_TPCREATE_TRIES) if lmr and lmr.new_layout.layout_id != lmr.old_layout.layout_id: fire_signal(layout_modified, reason='tpcreator', layout_modification_result=lmr)
def test_repack(self): rd = self.test_no_repack() layouts.apply_mods([layouts.repack_mod()], rd.owner_id, rd.dashboard_id, None) self.assertEqual([['p1:10'], ['p1:6'], ['p1:8'], ['p1:12']], [tile.tags for tile in rd.tiles_sorted_by_vo()])
def do_synchronize(layout_mod): master_vo = layout_mod.layout.layout_dict.get(master_tile.tile_id) if not master_vo: log.warn('No master_tile_id in layout_data.layout_dict for %s', master_tile) raise layouts.LayoutModificationImpossible() changed = False for tile_id, vo in layout_mod.layout.layout_dict.iteritems(): props = layout_mod.layout.get_tile_props(tile_id) if tile_id != master_tile.tile_id and \ props.get('master_id') == master_tile.tile_id: if vo['width'] != master_vo['width']: vo['width'] = master_vo['width'] changed = True if vo['height'] != master_vo['height']: vo['height'] = master_vo['height'] changed = True if not changed: raise layouts.LayoutModificationImpossible() layouts.repack_mod()(layout_mod) layouts.pack_upwards_mod()(layout_mod)
def test_repack_dont_put_master_first(self): rd = self.test_no_repack() layouts.apply_mods([layouts.repack_mod(put_master_first=False)], rd.owner_id, rd.dashboard_id, None) self.assertEqual([['p1:6'], ['p1:8'], ['p1:10'], ['p1:12']], [tile.tags for tile in rd.tiles_sorted_by_vo()]) self.assertFalse(rd.tiles_sorted_by_vo()[0].is_master_tile()) self.assertTrue(rd.tiles_sorted_by_vo()[2].is_master_tile()) return rd
def handle_tpcreator(owner_id, report_id, report_instance, make_first_master=False): """The method calls the TPCreator (see :ref:`guide_tpcreator`) for the given report instance, possibly creating new tiles from a master tile and altering dashboards' layouts. The signal :attr:`~mqe.signals.layout_modified` is issued for each modification. :param bool make_first_master: whether to promote the first tile wrt. ordering of tag values to a master tile. The default is ``False``, which means that a new master tile will not be promoted. """ layout_rows = c.dao.LayoutDAO.select_layout_by_report_multi(owner_id, report_id, [], 'tpcreator', mqeconfig.MAX_TPCREATORS_PER_REPORT) if not layout_rows: log.debug('No layout_by_report tpcreator rows') return log.info('tpcreator is processing %s rows for owner_id=%s report_id=%s report_instance_id=%s', len(layout_rows), owner_id, report_id, report_instance.report_instance_id) for row in layout_rows: mods = [tpcreator_mod(report_instance, row), # run the repacking only if tpcreator created a new tile layouts.if_mod(lambda layout_mod: layout_mod.new_tiles, layouts.repack_mod(put_master_first=(not make_first_master)))] if make_first_master: mods.extend([ # run the promote_first... mod only if tpcreator created a new tile layouts.if_mod(lambda layout_mod: layout_mod.new_tiles, layouts.promote_first_as_master_mod()), # another repacking is needed if the promote_first... mod made replacements, # because the mod doesn't preserve ordering layouts.if_mod(lambda layout_mod: layout_mod.tile_replacement, layouts.repack_mod()), ]) lmr = layouts.apply_mods(mods, owner_id, row['dashboard_id'], for_layout_id=None, max_tries=MAX_TPCREATE_TRIES) if lmr and lmr.new_layout.layout_id != lmr.old_layout.layout_id: fire_signal(layout_modified, reason='tpcreator', layout_modification_result=lmr)
def test_synchronize_sizes_of_tpcreated(self): rd, master_tile = self.test_handle_tpcreator() layout = layouts.Layout.select(rd.owner_id, rd.dashboard_id) ld = layout.layout_dict ld[master_tile.tile_id]['width'] = 8 ld[master_tile.tile_id]['height'] = 3 ld = layouts.apply_mods_for_noninserted_layout( [layouts.repack_mod()], Layout(ld)).new_layout.layout_dict layout_id = layout.set(rd.owner_id, rd.dashboard_id) self.assertTrue(layout_id) self.assertTrue( tpcreator.synchronize_sizes_of_tpcreated(master_tile, layout_id)) layout = Layout.select(rd.owner_id, rd.dashboard_id) ld = layout.layout_dict for tile_id in tpcreator.select_tpcreated_tile_ids(master_tile): self.assertEqual(8, ld[tile_id]['width']) self.assertEqual(3, ld[tile_id]['height'])
def test_promote_first_as_master(self): rd = self.test_repack_dont_put_master_first() layouts.apply_mods( [layouts.promote_first_as_master_mod(), layouts.repack_mod()], rd.owner_id, rd.dashboard_id, None) #for tile in rd.tiles_sorted_by_vo(): # print tile.tile_id, tile.tags, tile.get_master_tile_id() self.assertEqual([['p1:6'], ['p1:8'], ['p1:10'], ['p1:12']], [tile.tags for tile in rd.tiles_sorted_by_vo()]) first_tile = rd.tiles_sorted_by_vo()[0] self.assertTrue(first_tile.is_master_tile()) for tile in rd.tiles_sorted_by_vo()[1:]: self.assertFalse(tile.is_master_tile()) self.assertEqual(first_tile.tile_id, tile.get_master_tile_id()) return rd