def create_report_schedules(self): with self.create_app().app_context(): report_schedules = [] admin_user = self.get_user("admin") alpha_user = self.get_user("alpha") chart = db.session.query(Slice).first() example_db = get_example_database() for cx in range(REPORTS_COUNT): recipients = [] logs = [] for cy in range(cx): config_json = {"target": f"target{cy}@email.com"} recipients.append( ReportRecipients( type=ReportRecipientType.EMAIL, recipient_config_json=json.dumps(config_json), ) ) logs.append( ReportExecutionLog( scheduled_dttm=datetime(2020, 1, 1), state=ReportState.ERROR, error_message=f"Error {cy}", ) ) report_schedules.append( insert_report_schedule( type=ReportScheduleType.ALERT, name=f"name{cx}", crontab=f"*/{cx} * * * *", sql=f"SELECT value from table{cx}", description=f"Some description {cx}", chart=chart, database=example_db, owners=[admin_user, alpha_user], recipients=recipients, logs=logs, ) ) yield report_schedules report_schedules = db.session.query(ReportSchedule).all() # rollback changes (assuming cascade delete) for report_schedule in report_schedules: db.session.delete(report_schedule) db.session.commit()
def create_log(self, error_message: Optional[str] = None) -> None: """ Creates a Report execution log, uses the current computed last_value for Alerts """ log = ReportExecutionLog( scheduled_dttm=self._scheduled_dttm, start_dttm=self._start_dttm, end_dttm=datetime.utcnow(), value=self._report_schedule.last_value, value_row_json=self._report_schedule.last_value_row_json, state=self._report_schedule.last_state, error_message=error_message, report_schedule=self._report_schedule, uuid=self._execution_id, ) self._session.add(log) self._session.commit()
def create_log( # pylint: disable=too-many-arguments self, state: ReportState, error_message: Optional[str] = None, ) -> None: """ Creates a Report execution log, uses the current computed last_value for Alerts """ log = ReportExecutionLog( scheduled_dttm=self._scheduled_dttm, start_dttm=self._start_dttm, end_dttm=datetime.utcnow(), value=self._report_schedule.last_value, value_row_json=self._report_schedule.last_value_row_json, state=state, error_message=error_message, report_schedule=self._report_schedule, ) self._session.add(log) self._session.commit()
def create_report_slack_chart_working(): with app.app_context(): chart = db.session.query(Slice).first() report_schedule = create_report_notification( slack_channel="slack_channel", chart=chart) report_schedule.last_state = ReportState.WORKING report_schedule.last_eval_dttm = datetime(2020, 1, 1, 0, 0) db.session.commit() log = ReportExecutionLog( scheduled_dttm=report_schedule.last_eval_dttm, start_dttm=report_schedule.last_eval_dttm, end_dttm=report_schedule.last_eval_dttm, state=ReportState.WORKING, report_schedule=report_schedule, uuid=uuid4(), ) db.session.add(log) db.session.commit() yield report_schedule cleanup_report_schedule(report_schedule)
def create_alert_slack_chart_grace(): with app.app_context(): chart = db.session.query(Slice).first() report_schedule = create_report_notification( slack_channel="slack_channel", chart=chart, report_type=ReportScheduleType.ALERT, ) report_schedule.last_state = ReportState.GRACE report_schedule.last_eval_dttm = datetime(2020, 1, 1, 0, 0) log = ReportExecutionLog( report_schedule=report_schedule, state=ReportState.SUCCESS, start_dttm=report_schedule.last_eval_dttm, end_dttm=report_schedule.last_eval_dttm, scheduled_dttm=report_schedule.last_eval_dttm, ) db.session.add(log) db.session.commit() yield report_schedule cleanup_report_schedule(report_schedule)
def create_log( # pylint: disable=too-many-arguments self, session: Session, start_dttm: datetime, end_dttm: datetime, state: ReportLogState, error_message: Optional[str] = None, ) -> None: """ Creates a Report execution log, uses the current computed last_value for Alerts """ if self._model: log = ReportExecutionLog( scheduled_dttm=self._scheduled_dttm, start_dttm=start_dttm, end_dttm=end_dttm, value=self._model.last_value, value_row_json=self._model.last_value_row_json, state=state, error_message=error_message, report_schedule=self._model, ) session.add(log)