def insert_dashboard(self, dashboard_name, dashboard_options={}): """Insert a dashboard having the passed ``dashboard_name`` and ``dashboard_options`` (a JSON-serializable value). """ from mqe import layouts row = c.dao.DashboardDAO.insert(self.owner_id, dashboard_name, serialize.mjson(dashboard_options)) if not row: return None dashboard = Dashboard(row) change_dashboards_ordering( self.owner_id, self.dashboard_id_ordering + [dashboard.dashboard_id]) empty_layout = layouts.Layout() empty_layout.set(self.owner_id, row['dashboard_id'], None) log.info('Inserted new dashboard dashboard_id=%s name=%r', dashboard.dashboard_id, dashboard.dashboard_name) self.reload() fire_signal(new_dashboard, dashboard=dashboard) return Dashboard(row)
def handle_tpcreator(owner_id, report_id, report_instance): """The method calls the TPCreator (see :ref:`guide_tpcreator`) for the given report instance, possibly creating new tiles from a master tile and altering dashboards' layouts. The signal :attr:`~mqe.signals.layout_modified` is issued for each modification.""" layout_rows = c.dao.LayoutDAO.select_layout_by_report_multi( owner_id, report_id, [], 'tpcreator', mqeconfig.MAX_TPCREATORS_PER_REPORT) if not layout_rows: log.debug('No layout_by_report tpcreator rows') return log.info( 'tpcreator is processing %s rows for owner_id=%s report_id=%s report_instance_id=%s', len(layout_rows), owner_id, report_id, report_instance.report_instance_id) for row in layout_rows: mods = [ tpcreator_mod(report_instance, row), layouts.if_mod(lambda layout_mod: layout_mod.new_tiles, layouts.repack_mod()) ] lmr = layouts.apply_mods(mods, owner_id, row['dashboard_id'], for_layout_id=None, max_tries=MAX_TPCREATE_TRIES) if lmr and lmr.new_layout.layout_id != lmr.old_layout.layout_id: fire_signal(layout_modified, reason='tpcreator', layout_modification_result=lmr)
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 select_or_insert(owner_id, report_name): """Returns a :class:`Report` having the passed name - either an existing row or a newly created""" inserted, row = c.dao.ReportDAO.select_or_insert(owner_id, report_name) if not row: return report = Report(row) if inserted: fire_signal(new_report, report=report) return report
def init_dao_modules(database_type): for module_path in DAO_MODULE_PATHS_BY_DATABASE_TYPE[mqeconfig.DATABASE_TYPE]: module = importlib.import_module(module_path) fire_signal(dao_module_loaded, database_type=database_type, module_path=module_path, module=module) if hasattr(module, 'initialize'): module.initialize() for v in vars(module).values(): if inspect.isclass(v) and issubclass(v, BaseDAO): register_dao_class(database_type, v) DAO_MODULE_PATHS_BY_DATABASE_TYPE[mqeconfig.DATABASE_TYPE] = []
def insert(owner_id, report_name): """Insert and return a new :class:`Report` having the passed name. Returns ``None`` if it already exists.""" row = c.dao.ReportDAO.insert(owner_id, report_name) if not row: return None report = Report(row) fire_signal(new_report, report=report) return report
def handle_sscreator(owner_id, report_id, report_instance): """The method calls the SSCS (see :ref:`guide_sscreator`) for the given report instance, possibly creating new series definitions for tiles and altering dashboards' layouts. The signal :attr:`~mqe.signals.layout_modified` is issued for each modification.""" layout_rows = c.dao.LayoutDAO.select_layout_by_report_multi(owner_id, report_id, [], 'sscs', mqeconfig.MAX_DASHBOARDS_WITH_SSCS_PER_REPORT) if not layout_rows: log.debug('No layout_by_report sscs rows') return log.info('sscreator is processing %s rows for owner_id=%s report_id=%s report_instance_id=%s', len(layout_rows), owner_id, report_id, report_instance.report_instance_id) for row in layout_rows: mods = [sscreator_mod(report_instance, row)] lmr = layouts.apply_mods(mods, owner_id, row['dashboard_id'], for_layout_id=None, max_tries=MAX_SSCS_TRIES) if lmr and lmr.new_layout.layout_id != lmr.old_layout.layout_id: fire_signal(layout_modified, reason='sscreator', layout_modification_result=lmr)
def handle_tpcreator(owner_id, report_id, report_instance, make_first_master=False): """The method calls the TPCreator (see :ref:`guide_tpcreator`) for the given report instance, possibly creating new tiles from a master tile and altering dashboards' layouts. The signal :attr:`~mqe.signals.layout_modified` is issued for each modification. :param bool make_first_master: whether to promote the first tile wrt. ordering of tag values to a master tile. The default is ``False``, which means that a new master tile will not be promoted. """ layout_rows = c.dao.LayoutDAO.select_layout_by_report_multi(owner_id, report_id, [], 'tpcreator', mqeconfig.MAX_TPCREATORS_PER_REPORT) if not layout_rows: log.debug('No layout_by_report tpcreator rows') return log.info('tpcreator is processing %s rows for owner_id=%s report_id=%s report_instance_id=%s', len(layout_rows), owner_id, report_id, report_instance.report_instance_id) for row in layout_rows: mods = [tpcreator_mod(report_instance, row), # run the repacking only if tpcreator created a new tile layouts.if_mod(lambda layout_mod: layout_mod.new_tiles, layouts.repack_mod(put_master_first=(not make_first_master)))] if make_first_master: mods.extend([ # run the promote_first... mod only if tpcreator created a new tile layouts.if_mod(lambda layout_mod: layout_mod.new_tiles, layouts.promote_first_as_master_mod()), # another repacking is needed if the promote_first... mod made replacements, # because the mod doesn't preserve ordering layouts.if_mod(lambda layout_mod: layout_mod.tile_replacement, layouts.repack_mod()), ]) lmr = layouts.apply_mods(mods, owner_id, row['dashboard_id'], for_layout_id=None, max_tries=MAX_TPCREATE_TRIES) if lmr and lmr.new_layout.layout_id != lmr.old_layout.layout_id: fire_signal(layout_modified, reason='tpcreator', layout_modification_result=lmr)