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_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 main(): vars = tutorial.main() owner_id = vars['owner_id'] owner_dashboards = vars['owner_dashboards'] dashboard = vars['dashboard'] SECTION('Using tags for identifying entities') from mqe.reports import Report cpu_report = Report.select_or_insert(owner_id, 'cpu_usage') metrics = [ ('user', 42.3), ('system', 13.4), ('io', 8.4), ] cpu_report.process_input(json.dumps(metrics), tags=['ip:192.168.1.18']) SECTION('Creating a master tile') from mqe.dataseries import SeriesSpec from mqe.tiles import Tile from mqe.layouts import place_tile, Layout, replace_tiles dashboard = owner_dashboards.insert_dashboard('CPU') master_tile_config = { 'tw_type': 'Range', 'tags': ['ip:192.168.1.18'], 'series_spec_list': [ SeriesSpec(1, 0, { 'op': 'eq', 'args': ['user'] }), SeriesSpec(1, 0, { 'op': 'eq', 'args': ['system'] }), ], 'tile_options': { 'tile_title': 'CPU usage', 'tpcreator_uispec': [{ 'tag': 'ip:192.168.1.18', 'prefix': 'ip:' }] } } master_tile = Tile.insert(owner_id, cpu_report.report_id, dashboard.dashboard_id, master_tile_config) print place_tile(master_tile) SECTION('Creating tiles from a master tile') metrics = json.dumps(metrics) cpu_report.process_input(metrics, tags=['ip:192.168.1.30']) cpu_report.process_input(metrics, tags=['ip:192.168.2.51']) cpu_report.process_input(metrics, tags=['ip:192.168.2.51']) layout = Layout.select(owner_id, dashboard.dashboard_id) for tile in layout.tile_dict: print tile.tags SECTION('Synchronizing options of tpcreated tiles') new_master_tile_config = { 'tw_type': 'Range', 'tags': ['ip:192.168.1.18'], 'series_spec_list': [ SeriesSpec(1, 0, { 'op': 'eq', 'args': ['user'] }), SeriesSpec(1, 0, { 'op': 'eq', 'args': ['system'] }), SeriesSpec(1, 0, { 'op': 'eq', 'args': ['io'] }), ], 'tile_options': { 'tile_title': 'CPU usage', 'tpcreator_uispec': [{ 'tag': 'ip:192.168.1.18', 'prefix': 'ip:' }] } } new_master_tile = Tile.insert(owner_id, cpu_report.report_id, dashboard.dashboard_id, new_master_tile_config) assert replace_tiles({master_tile: new_master_tile}, for_layout_id=None) layout = Layout.select(owner_id, dashboard.dashboard_id) for tile in layout.tile_dict: print len(tile.get_tile_data()['series_data']) SECTION('Expiring tiles and promoting new masters') from mqe.tpcreator import make_master_from_tpcreated old_master = [tile for tile in layout.tile_dict if tile.is_master_tile()][0] new_chosen_master = [ tile for tile in layout.tile_dict if tile.tags == ['ip:192.168.2.51'] ][0] assert not new_chosen_master.is_master_tile() new_master = make_master_from_tpcreated(old_master, new_chosen_master) res = replace_tiles({ old_master: new_master, new_chosen_master: None }, for_layout_id=None) print 'replaced %d tiles' % len(res.tile_replacement) layout = Layout.select(owner_id, dashboard.dashboard_id) tile = [ tile for tile in layout.tile_dict if tile.tags == ['ip:192.168.2.51'] ][0] print tile.is_master_tile()
def expire_tiles_without_data(tile_list, max_seconds_without_data, for_layout_id): """Delete and detach tiles from a dashboard which don't have data for at least the specified time period. :param tile_list: a list of :class:`Tile` objects to expire, belonging to the same dashboard :param int max_seconds_without_data: the maximal age (specified in seconds) of the tile's data to avoid the expiration :param ~uuid.UUID for_layout_id: the version of the layout to perform the expiration :return: ``layout_id`` of the new layout if the operation was successful, ``None`` otherwise """ from mqe import layouts from mqe import tpcreator regular_tiles = [t for t in tile_list if not t.is_master_tile() \ and _should_expire_tile(t, max_seconds_without_data)] master_tiles = [t for t in tile_list if t.is_master_tile() \ and _should_expire_tile(t, max_seconds_without_data)] log.info( 'Will try to expire %s regular and %s master tiles out of %s passed', len(regular_tiles), len(master_tiles), len(tile_list)) layout_id = for_layout_id if regular_tiles: repl_res = layouts.replace_tiles( {tile: None for tile in regular_tiles}, layout_id) if repl_res: log.info('Successfully expired regular tiles') layout_id = repl_res.new_layout.layout_id else: log.warn('Failed to expire regular tiles') master_repl = {} for master_tile in master_tiles: tpcreated_tile_ids = tpcreator.select_tpcreated_tile_ids(master_tile, layout_id, sort=True) if not tpcreated_tile_ids: continue new_master_base = Tile.select(master_tile.dashboard_id, tpcreated_tile_ids[0]) if not new_master_base: log.warn('Could not select master tile replacement') continue new_master = tpcreator.make_master_from_tpcreated( master_tile, new_master_base) master_repl[master_tile] = new_master master_repl[new_master_base] = None if master_repl: repl_res = layouts.replace_tiles(master_repl, layout_id) if repl_res: log.info('Successfully expired master tiles') layout_id = repl_res.new_layout.layout_id else: log.warn('Failed to expire master tiles') if layout_id == for_layout_id: return None return layout_id