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_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_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_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 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 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_insert_similar_inserting(self): old_tile = self.test_insert_similar() tile = Tile.select(old_tile.dashboard_id, old_tile.tile_id) self.assertEqual([], tile.tile_options['tags']) self.assertEqual(tile.tile_options['seconds_back'], 600) self.assertEqual( [dataseries.SeriesSpec(2, 0, dict(op='eq', args=['monique']))], tile.series_specs())
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 do(layout_mod): tile_ids = [ tile_id for tile_id, visual_options in layout_mod.layout.layout_dict.items() if visual_options['y'] == 0 ] if not tile_ids: raise LayoutModificationImpossible() for tile_id in tile_ids: del layout_mod.layout.layout_dict[tile_id] layout_mod.detached_tiles.append( Tile.select(layout_mod.layout.dashboard_id, tile_id))
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 test_deleting_layout_by_report_row(self): tile, report = self.test_sscs_virtual_column() report.process_input('3\n4\n5\n6') tile = Tile.select(tile.dashboard_id, Layout.select(tile.owner_id, tile.dashboard_id).layout_dict.keys()[0]) self.assertEqual(4, len(tile.series_specs())) layouts.detach_tile(tile) rows = c.dao.LayoutDAO.select_layout_by_report_multi(tile.owner_id, tile.report_id, [], 'sscs', 100) self.assertTrue(rows) report.process_input('3\n4\n5\n6') rows = c.dao.LayoutDAO.select_layout_by_report_multi(tile.owner_id, tile.report_id, [], 'sscs', 100) self.assertFalse(rows)
def test_insert_different_tw_type(self): tile = self.test_insert() self.assertIsInstance(tile.tilewidget, tilewidgets.TilewidgetForRange) tile_config = { 'tw_type': 'Single', 'series_spec_list': [ dataseries.SeriesSpec(2, 0, dict(op='eq', args=['monique'])), ], 'tile_options': { 'tile_title': 'Points by user' } } tile = tile.insert_similar(tile_config) self.assertIsInstance(tile.tilewidget, tilewidgets.TilewidgetForSingle) tile = Tile.select(tile.dashboard_id, tile.tile_id) self.assertIsInstance(tile.tilewidget, tilewidgets.TilewidgetForSingle)
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_insert_similar(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_similar(tile_config) self.assertEqual([], tile.tile_options['tags']) self.assertEqual(tile.tile_options['seconds_back'], 600) self.assertEqual(tile_config['series_spec_list'], tile.series_specs()) return tile
def test_deleting_layout_by_report_row_after_deleting_dashboard(self): tile, report = self.test_sscs_virtual_column() report.process_input('3\n4\n5\n6') tile = Tile.select( tile.dashboard_id, Layout.select(tile.owner_id, tile.dashboard_id).layout_dict.keys()[0]) self.assertEqual(4, len(tile.series_specs())) od = dashboards.OwnerDashboards(tile.owner_id) for db in od.dashboards: db.delete() rows = c.dao.LayoutDAO.select_layout_by_report_multi( tile.owner_id, tile.report_id, [], 'sscs', 100) self.assertTrue(rows) report.process_input('3\n4\n5\n6\n7\n8') rows = c.dao.LayoutDAO.select_layout_by_report_multi( tile.owner_id, tile.report_id, [], 'sscs', 100) self.assertFalse(rows)
def test_copy(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) self.assertTrue(place_tile(tile)) self.assertTrue(self._select_tpcreator_rows(tile)) self.assertTrue(self._select_sscs_rows(tile)) new_dashboard_id = uuid.uuid1() tile2 = tile.copy(new_dashboard_id) tile2 = Tile.select(tile2.dashboard_id, tile2.tile_id) place_tile(tile2) self.assertNotEqual(tile, tile2) self.assertEqual(new_dashboard_id, tile2.dashboard_id) self.assertTrue(self._select_tpcreator_rows(tile2)) self.assertTrue(self._select_sscs_rows(tile2))
def tiles_sorted_by_vo(self): layout = self.layout() items = layouts._sort_layout_items(layout.layout_dict, 'y') return [Tile.select(self.dashboard_id, tile_id) for tile_id, vo in items]
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 main(): vars = tutorial.main() points_report = vars['points_report'] tile = vars['tile'] owner_id = vars['owner_id'] owner_dashboards = vars['owner_dashboards'] dashboard = vars['dashboard'] SECTION('Placing, detaching, replacing tiles') from mqe.layouts import Layout, place_tile layout = Layout.select(owner_id, dashboard.dashboard_id) new_tile = tile.copy(dashboard.dashboard_id) # we decided that new_tile should be put in the current layout res = place_tile(new_tile, for_layout_id=layout.layout_id) if not res: raise ValueError('Placing the tile unsuccessful') else: print 'New tile placed with visual_options', res.new_tiles[new_tile] from mqe.layouts import replace_tiles from mqe.tiles import Tile layout = Layout.select(owner_id, dashboard.dashboard_id) tile = Tile.select(dashboard.dashboard_id, layout.layout_dict.keys()[0]) tile_config = tile.get_tile_config() tile_config['tile_options']['tile_title'] = 'New Title' repl_tile = tile.insert_similar(tile_config) res = replace_tiles({tile: repl_tile}, for_layout_id=layout.layout_id) if not res: raise ValueError('Replacement of tiles unsuccessful') else: print 'Tiles replaced:', res.tile_replacement SECTION('Setting a custom layout') layout = Layout.select(owner_id, dashboard.dashboard_id) for visual_options in layout.layout_dict.values(): visual_options['height'] += 1 new_layout_id = layout.set() if not new_layout_id: raise ValueError('Updating the layout failed') SECTION('Layout mods') from mqe.layouts import replace_tiles_mod, place_tile_mod, apply_mods tile = repl_tile tile1 = tile.copy(dashboard.dashboard_id) tile2 = tile.copy(dashboard.dashboard_id) tile3 = tile.copy(dashboard.dashboard_id) layout = Layout.select(owner_id, dashboard.dashboard_id) mods = [ replace_tiles_mod({tile: tile1}), place_tile_mod(tile2), place_tile_mod(tile3), ] res = apply_mods(mods, owner_id, dashboard.dashboard_id, for_layout_id=layout.layout_id) if not res: raise ValueError('Operation failed') else: print res from mqe.layouts import LayoutModificationImpossible def detach_top_tiles_mod(): def do(layout_mod): tile_ids = [ tile_id for tile_id, visual_options in layout_mod.layout.layout_dict.items() if visual_options['y'] == 0 ] if not tile_ids: raise LayoutModificationImpossible() for tile_id in tile_ids: del layout_mod.layout.layout_dict[tile_id] layout_mod.detached_tiles.append( Tile.select(layout_mod.layout.dashboard_id, tile_id)) return do res = apply_mods([detach_top_tiles_mod()], owner_id, dashboard.dashboard_id, None) if not res: raise ValueError('Operation failed') else: print res def detach_top_tiles_using_replacement_mod(): def do(layout_mod): tiles = [ tile for tile, visual_options in layout_mod.layout.tile_dict.items() if visual_options['y'] == 0 ] if not tiles: raise LayoutModificationImpossible() replace_tiles_mod({tile: None for tile in tiles})(layout_mod) return do
def main(): vars = tutorial.main() points_report = vars['points_report'] tile = vars['tile'] owner_id = vars['owner_id'] owner_dashboards = vars['owner_dashboards'] SECTION('Auto-creating new data series') input = """\ user_name is_active points john true 128 monique true 210 alex true 12 """ points_report.process_input(input) series_names = [ series_data['name'] for series_data in tile.get_tile_data()['series_data'] ] print series_names new_dashboard = owner_dashboards.insert_dashboard('Points') tile_config = { 'tw_type': 'Range', 'series_spec_list': [ SeriesSpec(2, 0, { 'op': 'eq', 'args': ['john'] }), SeriesSpec(2, 0, { 'op': 'eq', 'args': ['monique'] }), ], 'tile_options': { 'seconds_back': 86400, 'tile_title': 'Points by user', 'sscs': SeriesSpec(2, 0, { 'op': 'eq', 'args': ['john'] }) } } new_tile = Tile.insert(owner_id, points_report.report_id, new_dashboard.dashboard_id, tile_config) place_tile(new_tile) input = """\ user_name is_active points john true 133 monique true 220 alex true 18 andrew true 6 """ points_report.process_input(input) layout = Layout.select(owner_id, new_dashboard.dashboard_id) tile_id = layout.layout_dict.keys()[0] tile = Tile.select(new_dashboard.dashboard_id, tile_id) series_names = [ series_data['name'] for series_data in tile.get_tile_data()['series_data'] ] print series_names
def test_select(self): inserted_tile = self.test_insert() tile = Tile.select(inserted_tile.dashboard_id, inserted_tile.tile_id) self.assertIsNotNone(tile) self.assertEqual(inserted_tile.tile_options, tile.tile_options)
def only_tile_from_layout(self): layout = layouts.Layout.select(self.owner_id, self.dashboard_id) assert len(layout.layout_dict) == 1 tile = Tile.select(self.dashboard_id, layout.layout_dict.keys()[0]) assert tile return tile
def test_is_master_tile(self): tile = self.test_tpcreator_creation() self.assertTrue(tile.is_master_tile()) tile = Tile.select(tile.dashboard_id, tile.tile_id) self.assertTrue(tile.is_master_tile())