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_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)
def test_make_first_master(self): rd = ReportData('r') 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:' }], 'sscs': dataseries.SeriesSpec(0, -1, dict(op='eq', args=['0'])), } } master_tile = Tile.insert(rd.owner_id, rd.report_id, rd.dashboard_id, tile_config) layouts.place_tile(master_tile) rd.report.process_input('0', tags=['p1:10']) rd.report.process_input('0', tags=['p1:11']) ri1 = rd.report.process_input('0\n1\n2', tags=['p1:10'], handle_tpcreator=False).report_instance tpcreator.handle_tpcreator(rd.owner_id, rd.report_id, ri1, make_first_master=True) ri2 = rd.report.process_input('0\n1\n2', tags=['p1:8'], handle_tpcreator=False).report_instance tpcreator.handle_tpcreator(rd.owner_id, rd.report_id, ri2, make_first_master=True) self.assertEqual(['p1:8', 'p1:10', 'p1:11'], [tile.tags[0] for tile in rd.tiles_sorted_by_vo()]) master_tile = rd.get_tile_by_tags(['p1:8']) self.assertTrue(master_tile.is_master_tile()) for tile in rd.tiles_sorted_by_vo()[1:]: self.assertEqual(master_tile.tile_id, tile.get_master_tile_id()) ri3 = rd.report.process_input('0\n1\n2', tags=['p1:4'], handle_tpcreator=False).report_instance tpcreator.handle_tpcreator(rd.owner_id, rd.report_id, ri3, make_first_master=True) self.assertEqual(['p1:4', 'p1:8', 'p1:10', 'p1:11'], [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:]: self.assertEqual(master_tile.tile_id, tile.get_master_tile_id()) ri4 = rd.report.process_input('0\n1\n2', tags=['p1:9'], handle_tpcreator=False).report_instance tpcreator.handle_tpcreator(rd.owner_id, rd.report_id, ri4, make_first_master=True) self.assertEqual(['p1:4', 'p1:8', 'p1:9', 'p1:10', 'p1:11'], [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:]: self.assertEqual(master_tile.tile_id, tile.get_master_tile_id()) ri5 = rd.report.process_input('0\n1\n2', tags=['p1:9'], handle_tpcreator=False).report_instance tpcreator.handle_tpcreator(rd.owner_id, rd.report_id, ri5, make_first_master=True) self.assertEqual(['p1:4', 'p1:8', 'p1:9', 'p1:10', 'p1:11'], [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:]: self.assertEqual(master_tile.tile_id, tile.get_master_tile_id()) return rd
def process_input(self, input_string, tags=None, created=None, input_type='any', ip_options={}, force_header=None, extra_ri_data=None, handle_tpcreator=True, handle_sscreator=True): """Process an input string - parse it into a table and create a report instance belonging to the report. :param str|unicode input_string: the input string :param list tags: a list of string tags attached to the report instance :param ~datetime.datetime created: an explicit creation datetime of the report instance ( default: the current datetime) :param str input_type: input type (see :func:`mqetables.parseany.parse_input`) :param dict ip_options: extra parser options (see :func:`mqetables.parsing.InputParser`) :param force_header: a list of header rows indexes to set as a header (defaults to auto-detection) :param extra_ri_data: a custom JSON-serializable document attached to the report instance :param handle_tpcreator: whether to handle TPCreator for the created report instance by calling :func:`~mqe.tpcreator.handle_tpcreator` :param handle_sscreator: whether to handle SSCS by calling :func:`~mqe.sscreator.handle_sscreator` :return: an :class:`InputProcessingResult` """ assert isinstance(input_string, (str, unicode)) # disallow 'created' in the future now = datetime.datetime.utcnow() if created is not None and created.tzinfo: created = util.make_tz_naive(created) if created is not None and created.year < 2000: raise ValueError('created cannot be before the year 2000') if created is not None and created < now: report_instance_id = util.uuid_with_dt(created) custom_created = True else: custom_created = False report_instance_id = gen_timeuuid() created = util.datetime_from_uuid1(report_instance_id) if tags is None: tags = [] parsing_result = parseany.parse_input(input_string, input_type, ip_options) table = mqeconfig.get_table_from_parsing_result(parsing_result) if table is None: return InputProcessingResult(None, parsing_result) if force_header is not None: log.debug('Overwriting header detection due to force_header') table.header_idxs = [ i for i in force_header if util.valid_index(table.num_rows, i) ] table.header_idxs_source = parsing.HEADER_IDXS_SOURCE_USER ri_data_dict = { 'table': table, } result_desc = self._get_result_desc(parsing_result) if result_desc: ri_data_dict['result_desc'] = result_desc report_instance_row = c.dao.ReportInstanceDAO.insert( owner_id=self.owner_id, report_id=self.report_id, report_instance_id=report_instance_id, tags=tags, ri_data=serialize.mjson(ri_data_dict), input_string=parsing_result.input_string, extra_ri_data=serialize.mjson(extra_ri_data) if extra_ri_data else None, custom_created=custom_created) report_instance = ReportInstance(report_instance_row) log.info( 'Created new report instance report_id=%s report_name=%r tags=%s ' 'report_instance_id=%s created=%s', self.report_id, self.report_name, tags, report_instance_id, report_instance.created) if tags and handle_tpcreator: from mqe import tpcreator tpcreator.handle_tpcreator(self.owner_id, self.report_id, report_instance) if handle_sscreator: from mqe import sscreator sscreator.handle_sscreator(self.owner_id, self.report_id, report_instance) if custom_created: from mqe import dataseries dataseries.clear_series_defs(self.report_id, util.powerset(tags)) return InputProcessingResult(report_instance, parsing_result)