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_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 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 test_sscs_creation(self): tile = self.test_insert(tile_options_ext={ 'sscs': dataseries.SeriesSpec(2, 0, dict(op='eq', args=['monique'])) }) place_tile(tile) self.assertTrue(self._select_sscs_rows(tile))
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_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_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_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 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_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_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_tpcreator_creation(self): tile = self.test_insert( tile_options_ext={ 'tpcreator_uispec': tpcreator.suggested_tpcreator_uispec(['ip:192.168.1.1']) }) place_tile(tile) self.assertTrue(self._select_tpcreator_rows(tile)) return tile
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_different_tag(self): rd, tile = self.test_sscs() tile_config = { 'tags': [], '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['tile_options']['sscs'] = tile_config['series_spec_list'][ 1] tile2 = Tile.insert(rd.owner_id, rd.report.report_id, rd.dashboard_id, tile_config) layouts.place_tile(tile2) d = [ OrderedDict([('user_name', 'robert'), ('is_active', True), ('points', 128)]), OrderedDict([('user_name', 'monique2'), ('is_active', True), ('points', 210)]), OrderedDict([('user_name', 'monique3'), ('is_active', True), ('points', 210)]), OrderedDict([('user_name', 'monique4'), ('is_active', True), ('points', 210)]), OrderedDict([('user_name', 'robert2'), ('is_active', True), ('points', 210)]), ] res = rd.report.process_input(json.dumps(d)) tile2 = rd.tile_from_layout(1, 2) self.assertEqual(7, len(tile2.series_specs())) self.assertEqual(SeriesSpec(2, 0, dict(op='eq', args=['robert2'])), tile2.series_specs()[-1]) d = [ OrderedDict([('user_name', 'monique4'), ('is_active', True), ('points', 128)]) ] res = rd.report.process_input(json.dumps(d), tags=['ip:192.168.1.1']) tile2 = rd.tile_from_layout(0, 2) self.assertEqual(7, len(tile2.series_specs())) tile = rd.tile_from_layout(1, 2) self.assertEqual(6, len(tile.series_specs())) self.assertEqual(SeriesSpec(2, 0, dict(op='eq', args=['monique4'])), tile.series_specs()[-1])
def test_layout_sorting(self): rd = new_report_data('points', ['p1:10']) 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)]) ] nums = [30, 20, 12, 11, 40, 98] for num in nums: rd.report.process_input(json.dumps(d), tags=['p1:%s' % num]) ld = rd.layout() self.assertEqual(len(nums) + 1, len(ld.layout_dict.keys())) self.assertEqual( len(nums) + 1, len(ld.layout_props['by_tile_id'].keys())) layout_items = sorted(ld.layout_dict.items(), key=lambda (tile_id, vo): (vo['y'], vo['x'])) layout_tags = [ ld.layout_props['by_tile_id'][tile_id]['tags'][0] for (tile_id, vo) in layout_items ] expected_tags = ['p1:10'] + sorted(['p1:%s' % num for num in nums]) self.assertEqual(set(expected_tags), set(layout_tags)) self.assertEqual(expected_tags, layout_tags)
def test_deleting_layout_by_report_row(self): owner_id = uuid.uuid1() dashboard_id_1 = uuid.uuid1() r = reports.Report.insert(owner_id, 'r') tile_config = { 'tw_type': 'Single', 'tags': ['p1:10'], 'series_spec_list': [ dataseries.SeriesSpec(0, -1, dict(op='eq', args=['0'])), ], 'tile_options': {} } tile_config['tile_options'][ 'tpcreator_uispec'] = tpcreator.suggested_tpcreator_uispec( ['p1:10']) master_tile_1 = Tile.insert(owner_id, r.report_id, dashboard_id_1, tile_config) layouts.place_tile(master_tile_1) r.process_input('1', tags=['p1:11']) self.assertEqual( 2, len( Layout.select(master_tile_1.owner_id, master_tile_1.dashboard_id).layout_dict)) rows = c.dao.LayoutDAO.select_layout_by_report_multi( master_tile_1.owner_id, master_tile_1.report_id, [], 'tpcreator', 100) self.assertTrue(rows) layouts.detach_tile(master_tile_1) r.process_input('1', tags=['p1:12']) self.assertEqual( 1, len( Layout.select(master_tile_1.owner_id, master_tile_1.dashboard_id).layout_dict)) rows = c.dao.LayoutDAO.select_layout_by_report_multi( master_tile_1.owner_id, master_tile_1.report_id, [], 'tpcreator', 100) self.assertFalse(rows)
def move_tile(): dashboard_id = request.get_json()['dashboard_id'] tile_id = request.get_json()['tile_id'] target_dashboard_id = request.get_json()['target_dashboard_id'] for_layout_id = request.get_json()['for_layout_id'] check_access(lambda: auth.access_dashboard(dashboard_id)) check_access(lambda: auth.access_dashboard(target_dashboard_id)) old_tile = tiles.Tile.select(dashboard_id, tile_id) if not old_tile: return error('Invalid source tile in move_tile %s %s' % (dashboard_id, tile_id)) new_tile = old_tile.copy(target_dashboard_id) if not layouts.place_tile(new_tile): return error('Cannot copy the source file') mres = layouts.detach_tile(old_tile, for_layout_id=for_layout_id) if not mres: return error(message='Deletion of the source tile unsuccessful due to old page data') log.info('Moved tile tile_id=%s dashboard_id=%s to tile tile_id=%s dashboard_id=%s', tile_id, dashboard_id, new_tile.tile_id, target_dashboard_id) return success(result=dict(new_layout_id=mres.new_layout.layout_id))
def create_tile(): dashboard_id = request.get_json()['dashboard_id'] report_id = request.get_json().get('report_id') report_instance_id = request.get_json().get('report_instance_id') tile_config = request.get_json()['tile_config'] moveresize = request.get_json()['moveresize'] for_layout_id = request.get_json()['for_layout_id'] check_access(lambda: auth.access_dashboard(dashboard_id)) check_access(lambda: auth.access_report_instances(report_id)) if tile_config['tags']: tile_config['tile_options']['tpcreator_uispec'] = tpcreator.suggested_tpcreator_uispec( tile_config['tags']) tile = tiles.Tile.insert(auth.logged_owner_id(), report_id, dashboard_id, tile_config) mres = layouts.place_tile(tile, for_layout_id=for_layout_id) if not mres: return error(message='Could not place the new tile on the dashboard, a page refresh is needed') dataseries.update_default_options(tile) log.info('Created new tile report_id=%s dashboard_id=%s tile_id=%s', report_id, dashboard_id, tile.tile_id) tile_html = get_template_attribute('m.html', 'dashboard_tile')\ (tile.tile_id, mres.new_tiles[tile], moveresize) return success(result=dict( tile_html=tile_html, new_layout_id=mres.new_layout.layout_id, ))
def place(self, dashboard_id, **kwargs): from mqe.tests import tiles_test tile = call(tiles_test.TileTest.test_insert, dashboard_id=dashboard_id) res = place_tile(tile, **kwargs) self.assertIsNotNone(res) return tile
def test_layout_sorting_complex_tag_names(self): owner_id = uuid.uuid4() dashboard_id = uuid.uuid4() rep = reports.Report.insert(owner_id, 'r') tile_config = { 'tags': ['server0:0', 'service1:search.1'], 'series_spec_list': [dataseries.SeriesSpec(0, -1, { 'op': 'eq', 'args': '0' })], 'tile_options': { 'tpcreator_uispec': [{ 'tag': 'server0:0', 'prefix': 'server0:0' }, { 'tag': 'service1:search.1', 'prefix': 'service1:' }], } } tile_config['tile_options'][ 'tpcreator_uispec'] = tpcreator.suggested_tpcreator_uispec( tile_config['tags']) master_tile = Tile.insert(owner_id, rep.report_id, dashboard_id, tile_config) layouts.place_tile(master_tile) for i in range(8, 13): rep.process_input(str(i), tags=['server0:0', 'service1:search.%d' % i]) 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([['server0:0', 'service1:search.1'], ['server0:0', 'service1:search.8'], ['server0:0', 'service1:search.9'], ['server0:0', 'service1:search.10'], ['server0:0', 'service1:search.11'], ['server0:0', 'service1:search.12']], tags_lists)
def test_place_wrong_id(self): dashboard_id = uuid.uuid1() tile = self.place(dashboard_id) layout = Layout.select(tile.owner_id, tile.dashboard_id) tile = self.place(dashboard_id) tile = call(tiles_test.TileTest.test_insert, dashboard_id=dashboard_id) res = place_tile(tile, for_layout_id=layout.layout_id) self.assertIsNone(res)
def test_place(self): tile = call(tiles_test.TileTest.test_insert, dashboard_id=uuid.uuid1()) res = place_tile(tile) self.assertIsNotNone(res) vo = {'width': 4, 'height': 4, 'x': 0, 'y': 0} self.assertEqual(vo, res.new_tiles[tile]) layout = Layout.select(tile.owner_id, tile.dashboard_id) self.assertEqual(res.new_layout.layout_id, layout.layout_id) self.assertEqual({tile.tile_id: vo}, layout.layout_dict)
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_tpcreator_as_mod_performance(self): owner_id = uuid.uuid4() dashboard_id = uuid.uuid4() tile_config = { 'tags': ['str:sample_string'], 'series_spec_list': [ dataseries.SeriesSpec(0, -1, dict(op='eq', args=['0'])), ], 'tile_options': { 'tpcreator_uispec': [{ 'tag': 'str:sample_string', 'prefix': 'str:' }], } } r = reports.Report.insert(owner_id, 'r') master_tile = Tile.insert(owner_id, r.report_id, dashboard_id, tile_config) layouts.place_tile(master_tile) strs = [random_string() for _ in xrange(199)] mods = [] layout_rows = c.dao.LayoutDAO.select_layout_by_report_multi( owner_id, r.report_id, [], 'tpcreator', 100) start = time() for str in strs: res = r.process_input('1', tags=['str:%s' % str], handle_tpcreator=False) mods.append( tpcreator.tpcreator_mod(res.report_instance, layout_rows[0], 200)) print 'Creating report instances took %.1f' % ((time() - start) * 1000) start = time() layouts.apply_mods(mods, owner_id, dashboard_id, None) print 'Applying tpcreator_mods took %.1f' % ((time() - start) * 1000) layout = Layout.select(owner_id, dashboard_id) self.assertEqual(200, len(layout.layout_dict)) tags_set = {tile.tags[0].split(':')[1] for tile in layout.tile_dict} self.assertEqual(200, len(tags_set)) self.assertEqual(tags_set, set(strs + ['sample_string']))
def test_no_repack(self): tile_config = { 'tags': ['p1:10'], 'series_spec_list': [ dataseries.SeriesSpec(0, -1, dict(op='eq', args=['0'])), ], 'tile_options': { 'tpcreator_uispec': [{ 'tag': 'p1:10', 'prefix': 'p1:' }], } } rd = ReportData('r') master_tile = Tile.insert(rd.owner_id, rd.report_id, rd.dashboard_id, tile_config) layouts.place_tile(master_tile) ri1 = rd.report.process_input('0', tags=['p1:8'], handle_tpcreator=False).report_instance ri2 = rd.report.process_input('0', tags=['p1:12'], handle_tpcreator=False).report_instance ri3 = rd.report.process_input('0', tags=['p1:6'], 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]), ] layouts.apply_mods(mods, rd.owner_id, rd.dashboard_id, None) self.assertEqual([['p1:10'], ['p1:8'], ['p1:12'], ['p1:6']], [tile.tags for tile in rd.tiles_sorted_by_vo()]) return rd
def test_tpcreator_as_mod(self): owner_id = uuid.uuid4() dashboard_id = uuid.uuid4() tile_config = { 'tags': ['p1:10'], 'series_spec_list': [ dataseries.SeriesSpec(0, -1, dict(op='eq', args=['0'])), ], 'tile_options': { 'tpcreator_uispec': [{ 'tag': 'p1:10', 'prefix': 'p1:' }], } } r = reports.Report.insert(owner_id, 'r') master_tile = Tile.insert(owner_id, r.report_id, dashboard_id, tile_config) layouts.place_tile(master_tile) ri1 = r.process_input('0', tags=['p1:11'], handle_tpcreator=False).report_instance ri2 = r.process_input('0', tags=['p1:12'], handle_tpcreator=False).report_instance ri3 = r.process_input('0', tags=['p1:12'], handle_tpcreator=False).report_instance layout_rows = c.dao.LayoutDAO.select_layout_by_report_multi( owner_id, r.report_id, [], 'tpcreator', 100) mods = [ tpcreator.tpcreator_mod(ri1, layout_rows[0]), tpcreator.tpcreator_mod(ri2, layout_rows[0]), tpcreator.tpcreator_mod(ri3, layout_rows[0]) ] layouts.apply_mods(mods, owner_id, dashboard_id, None) layout = Layout.select(owner_id, dashboard_id) self.assertEqual(3, len(layout.layout_dict)) self.assertEqual([['p1:10'], ['p1:11'], ['p1:12']], sorted(tile.tags for tile in layout.tile_dict))
def test_expire_tiles_without_data_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) #rd.report.process_input('1', tags=['p1:10']) rd.report.process_input('1\n2\n', tags=['p1:11']) rd.report.process_input('1\n2\n3\n', tags=['p1:12']) self.assertEqual(3, len(rd.layout().layout_dict)) self.assertEqual(3, len(rd.get_tile_by_tags(['p1:12']).series_specs())) tile1_created_ago = datetime.datetime.utcnow( ) - util.datetime_from_uuid1(tile1.tile_id) tiles.expire_tiles_without_data( rd.layout().tile_dict.keys(), tile1_created_ago.total_seconds() - 0.00001, rd.layout().layout_id) self.assertEqual(2, len(rd.layout().layout_dict)) self.assertTrue(rd.layout_has_tags([['p1:11'], ['p1:12']])) master_tile = rd.get_tile_by_tags(['p1:11']) 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()))
def test_make_first_master_multiple_masters(self): rd = self.test_make_first_master() 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 = reports.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 tpcreator.handle_tpcreator(rd.owner_id, r2.report_id, ri1, True) self.assertEqual( ['p1:4', 'p1:8', 'p1:9', 'p1:10', 'p1:11', 'q1:8', 'q1:10'], [tile.tags[0] for tile in rd.tiles_sorted_by_vo()]) master_tile = rd.get_tile_by_tags(['p1:4']) self.assertTrue(master_tile.is_master_tile()) for tile in rd.tiles_sorted_by_vo()[1:]: if tile.tags[0].startswith('p1'): self.assertEqual(master_tile.tile_id, tile.get_master_tile_id()) master_tile2 = rd.get_tile_by_tags(['q1:8']) self.assertTrue(master_tile2.is_master_tile()) tile2 = rd.get_tile_by_tags(['q1:10']) self.assertEqual(tile2.get_master_tile_id(), master_tile2.tile_id)