def test_owner_has_reports(self): owner_id = uuid.uuid1() self.assertFalse(reports.owner_has_reports(owner_id)) Report.insert(owner_id, 'a') self.assertTrue(reports.owner_has_reports(owner_id))
def test_fetch_reports_by_name(self): owner_id = uuid.uuid1() all_rs = [ Report.insert(owner_id, 'rep%02d' % i) for i in range(10, 20) ] all_rs[:0] = [ Report.insert(owner_id, 'rep%02d' % i) for i in range(10) ] rs = reports.fetch_reports_by_name(owner_id) self.assertEqual(all_rs, rs) rs = reports.fetch_reports_by_name(owner_id, limit=5) self.assertEqual(all_rs[:5], rs) rs = reports.fetch_reports_by_name(owner_id, name_prefix='rep1') self.assertEqual(all_rs[10:], rs) rs = reports.fetch_reports_by_name(owner_id, name_prefix='rep1', after_name='rep15') self.assertEqual(all_rs[16:], rs) rs = reports.fetch_reports_by_name(owner_id, after_name='rep15') self.assertEqual(all_rs[16:], rs) rs = reports.fetch_reports_by_name(owner_id, after_name='x') self.assertFalse(rs) rs = reports.fetch_reports_by_name(uuid.uuid1()) self.assertFalse(rs) rs = reports.fetch_reports_by_name(owner_id, after_name='a', limit=1) self.assertEqual(all_rs[:1], rs)
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_counts(self): owner_id = uuid.uuid1() r = Report.insert(owner_id, 'a') r.process_input('1') r.process_input('2') r2 = Report.insert(owner_id, 'b') r.process_input('1') r.process_input('2') self.assertEqual(4, reports.report_instance_count_for_owner(owner_id)) self.assertEqual(4, reports.report_instance_diskspace_for_owner(owner_id))
def test_delete_simple(self): owner_id = uuid.uuid1() r1 = Report.insert(owner_id, 'r1') r1.process_input('1') r2 = Report.insert(owner_id, 'r2') r2.process_input('2') ids = reports.fetch_reports_by_name(owner_id, 'r') self.assertEqual(2, len(ids)) r2.delete() reps = reports.fetch_reports_by_name(owner_id, 'r') self.assertEqual([r1], reps) self.assertIsNone(Report.select(r2.report_id))
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_custom_get_parsing_result_desc(self): old = mqeconfig.get_parsing_result_desc try: def get_parsing_result_desc(parsing_result, table): res = {'num_rows': table.num_rows} if parsing_result.input_type == 'single': res['input_is_json'] = True return res mqeconfig.get_parsing_result_desc = get_parsing_result_desc owner_id = uuid.uuid1() r = Report.insert(owner_id, 'r') r.process_input('1\n2\n3\n') ri = r.fetch_instances(order='desc', limit=1)[0] self.assertEqual(3, ri.parsing_result_desc['num_rows']) self.assertEqual(None, ri.parsing_result_desc.get('input_is_json')) r.process_input('1\n2\n', input_type='single') ri = r.fetch_instances(order='desc', limit=1)[0] self.assertEqual(1, ri.parsing_result_desc['num_rows']) self.assertEqual(True, ri.parsing_result_desc.get('input_is_json')) finally: mqeconfig.get_parsing_result_desc = old
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_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_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_get_dashboards_displaying_report(self): od = self.test_inserting() res = od.get_dashboards_displaying_report(uuid.uuid4()) self.assertEqual([], res) r = Report.insert(od.owner_id, 'r') tile_config = { 'series_spec_list': [ dataseries.SeriesSpec(0, -1, dict(op='eq', args=['0'])), ], } res = od.get_dashboards_displaying_report(r.report_id) self.assertEqual([], res) t1 = Tile.insert(od.owner_id, r.report_id, od.dashboards[3].dashboard_id, tile_config) place_tile(t1) res = od.get_dashboards_displaying_report(r.report_id) self.assertEqual(1, len(res)) self.assertEqual('Dash 4', res[0].dashboard_name) t2 = Tile.insert(od.owner_id, r.report_id, od.dashboards[1].dashboard_id, tile_config) place_tile(t2) res = od.get_dashboards_displaying_report(r.report_id) self.assertEqual(2, len(res)) self.assertEqual('Dash 2', res[0].dashboard_name) self.assertEqual('Dash 4', res[1].dashboard_name) t3 = Tile.insert(od.owner_id, r.report_id, od.dashboards[1].dashboard_id, tile_config) place_tile(t3) res = od.get_dashboards_displaying_report(r.report_id) self.assertEqual(2, len(res)) self.assertEqual('Dash 2', res[0].dashboard_name) self.assertEqual('Dash 4', res[1].dashboard_name) detach_tile(t3) res = od.get_dashboards_displaying_report(r.report_id) self.assertEqual(2, len(res)) self.assertEqual('Dash 2', res[0].dashboard_name) self.assertEqual('Dash 4', res[1].dashboard_name) detach_tile(t2) res = od.get_dashboards_displaying_report(r.report_id) self.assertEqual(1, len(res)) self.assertEqual('Dash 4', res[0].dashboard_name) od.dashboards[3].delete() res = od.get_dashboards_displaying_report(r.report_id) self.assertEqual(0, len(res))
def test_tags(self): r, all_ris = self.create_multi_day_report() self.assertTrue(r.has_tags()) self.assertEqual(['t1', 't2'], r.fetch_tags_sample()) self.assertEqual(['t1'], r.fetch_tags_sample('t1')) r2 = Report.insert(uuid.uuid1(), 'r2') self.assertFalse(r2.has_tags()) self.assertFalse(r2.fetch_tags_sample('t'))
def test_fetch_days(self): r, all_ris = self.create_multi_day_report() dts = r.fetch_days() for dt in dts: self.assertIsInstance(dt, datetime.datetime) r2 = Report.insert(uuid.uuid1(), 'r2') self.assertFalse(r2.fetch_days()) ri = r2.process_input('2').report_instance self.assertEqual(datetime.datetime.utcnow().date(), ri.created.date())
def test_insert_and_select(self): owner_id = uuid.uuid4() r1 = Report.insert(owner_id, 'rep') self.assertIsNotNone(r1) r2 = Report.insert(owner_id, 'rep') self.assertIsNone(r2) rs = Report.select_by_name(owner_id, 'rep') self.assertEqual(r1, rs) rs2 = Report.select(rs.report_id) self.assertEqual(rs2, rs) r2 = Report.insert(owner_id, 'rep_2') self.assertIsNotNone(r2) self.assertNotEqual(r2, rs) r3 = Report.select_or_insert(owner_id, 'rep_3') r3_2 = Report.select(r3.report_id) r3_3 = Report.select_or_insert(owner_id, 'rep_3') self.assertEqual(r3, r3_2) self.assertEqual(r3, r3_3)
def test_delete_single_instance_same_created_dt(self): owner_id = uuid.uuid1() r = Report.insert(owner_id, 'r') created_ris = [] for i in xrange(20): res = r.process_input(str(i), created=datetime.datetime(2017, 1, 1)) created_ris.append(res.report_instance) r.delete_single_instance(created_ris[10].report_instance_id) ris = r.fetch_instances() self.assertEqual({str(i) for i in xrange(20) if i != 10}, {ri['input_string'] for ri in ris})
def test_find_report_instance_by_dt(self): r, all_ris = self.create_multi_day_report() ri = r.find_report_instance_by_dt(all_ris[3].created + datetime.timedelta(hours=1)) self.assertEqual(all_ris[3], ri) ri = r.find_report_instance_by_dt(all_ris[3].created - datetime.timedelta(hours=1)) self.assertEqual(all_ris[3], ri) ri = r.find_report_instance_by_dt(util.MIN_DATETIME) self.assertEqual(all_ris[0], ri) ri = r.find_report_instance_by_dt(util.MAX_DATETIME) self.assertEqual(all_ris[-1], ri) r2 = Report.insert(uuid.uuid1(), 'r2') ri = r2.find_report_instance_by_dt(util.MIN_DATETIME) self.assertIsNone(ri)
def test_delete_single_instance_multiple_days(self): r, all_ris = self.create_multi_day_report() r.delete_single_instance(all_ris[-1].report_instance_id) self.assertEqual(['t1', 't2'], r.fetch_tags_sample()) latest_instance_id = r.fetch_latest_instance_id() self.assertTrue(latest_instance_id) ri = r.fetch_single_instance(latest_instance_id) self.assertEqual('6', ri.input_string) latest_instance_id = r.fetch_latest_instance_id(['t1']) self.assertTrue(latest_instance_id) ri = r.fetch_single_instance(latest_instance_id) self.assertEqual('5', ri.input_string) latest_instance_id = r.fetch_latest_instance_id(['t2']) self.assertTrue(latest_instance_id) ri = r.fetch_single_instance(latest_instance_id) self.assertEqual('6', ri.input_string) r2 = Report.insert(r.owner_id, 'r2') r2.process_input('1', created=utcnow() - datetime.timedelta(days=5, seconds=2)) r2.process_input('2', created=utcnow() - datetime.timedelta(days=5, seconds=1), tags=['x']) latest_instance_id = r2.fetch_latest_instance_id() r2.delete_single_instance(latest_instance_id) self.assertEqual([], r2.fetch_tags_sample()) latest_instance_id = r2.fetch_latest_instance_id() ri = r2.fetch_single_instance(latest_instance_id) self.assertEqual('1', ri.input_string)
def main(): SECTION('Creating a report and report instances') import uuid from mqe.reports import Report owner_id = uuid.uuid4() simple_report = Report.insert(owner_id, 'simple') res = simple_report.process_input('10 20') print res.report_instance.table points_report = Report.insert(owner_id, 'points') input = """\ user_name is_active points john true 128 monique true 210 """ res = points_report.process_input(input) print res.report_instance.table SECTION('Creating a dashboard and a tile') from mqe.dashboards import OwnerDashboards owner_dashboards = OwnerDashboards(owner_id) dashboard = owner_dashboards.insert_dashboard('My Dashboard') from mqe.dataseries import SeriesSpec 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', } } from mqe.tiles import Tile tile = Tile.insert(owner_id, points_report.report_id, dashboard.dashboard_id, tile_config) pprint(tile.get_tile_data()) SECTION('Placing a tile in a dashboard layout') from mqe.layouts import place_tile res = place_tile(tile) if not res: raise ValueError('Placing the tile unsuccessful') from mqe.layouts import Layout layout = Layout.select(owner_id, dashboard.dashboard_id) pprint(layout.layout_dict) def render_dashboard(owner_id, dashboard): print 'Rendering dashboard %r' % dashboard.dashboard_name layout = Layout.select(owner_id, dashboard.dashboard_id) for tile, visual_options in layout.tile_dict.items(): tile_data = tile.get_tile_data() print 'Rendering tile %r at position %s/%s' % ( tile_data['generated_tile_title'], visual_options['x'], visual_options['y']) # render tile_data['series_data'] render_dashboard(owner_id, dashboard) return locals()
def test_delete_with_tiles(self): owner_id = uuid.uuid1() r1 = Report.insert(owner_id, 'r1') r1.process_input('1') r2 = Report.insert(owner_id, 'r2') r2.process_input('2') r3 = Report.insert(owner_id, 'r3') r3.process_input('3') tile_config = { 'series_spec_list': [SeriesSpec(0, -1, { 'op': 'eq', 'args': '0' })], } od = OwnerDashboards(owner_id) od.insert_dashboard('Second') od.insert_dashboard('Third') tile1_1 = Tile.insert(owner_id, r1.report_id, od.dashboards[0].dashboard_id, tile_config) assert tile1_1.get_tile_data()['series_data'] tile2_1 = Tile.insert(owner_id, r2.report_id, od.dashboards[1].dashboard_id, tile_config) assert tile2_1.get_tile_data()['series_data'] tile3_1 = Tile.insert(owner_id, r3.report_id, od.dashboards[2].dashboard_id, tile_config) assert tile3_1.get_tile_data()['series_data'] layouts.place_tile(tile1_1) layouts.place_tile(tile2_1) layouts.place_tile(tile3_1) r2.delete() reps = reports.fetch_reports_by_name(owner_id, '') self.assertEqual([r1, r3], reps) self.assertEqual( 1, len( layouts.Layout.select( owner_id, od.dashboards[0].dashboard_id).layout_dict)) self.assertEqual( 0, len( layouts.Layout.select( owner_id, od.dashboards[1].dashboard_id).layout_dict)) self.assertEqual( 1, len( layouts.Layout.select( owner_id, od.dashboards[2].dashboard_id).layout_dict)) tile1_2 = Tile.insert(owner_id, r1.report_id, od.dashboards[0].dashboard_id, tile_config) tile1_3 = Tile.insert(owner_id, r1.report_id, od.dashboards[1].dashboard_id, tile_config) tile1_4 = Tile.insert(owner_id, r1.report_id, od.dashboards[1].dashboard_id, tile_config) tile1_5 = Tile.insert(owner_id, r1.report_id, od.dashboards[2].dashboard_id, tile_config) layouts.place_tile(tile1_2) layouts.place_tile(tile1_3) layouts.place_tile(tile1_4) layouts.place_tile(tile1_5) r1.delete() self.assertEqual( 0, len( layouts.Layout.select( owner_id, od.dashboards[0].dashboard_id).layout_dict)) self.assertEqual( 0, len( layouts.Layout.select( owner_id, od.dashboards[1].dashboard_id).layout_dict)) self.assertEqual( 1, len( layouts.Layout.select( owner_id, od.dashboards[2].dashboard_id).layout_dict)) reps = reports.fetch_reports_by_name(owner_id, '') self.assertEqual([r3], reps)