def send_failure_report(user_id): user = models.User.get_by_id(user_id) errors = [ json_loads(e) for e in redis_connection.lrange(key(user_id), 0, -1) ] if errors: errors.reverse() occurrences = Counter((e.get('id'), e.get('message')) for e in errors) unique_errors = {(e.get('id'), e.get('message')): e for e in errors} context = { 'failures': [{ 'id': v.get('id'), 'name': v.get('name'), 'failed_at': v.get('failed_at'), 'failure_reason': v.get('message'), 'failure_count': occurrences[k], 'comment': comment_for(v) } for k, v in unique_errors.items()], 'base_url': base_url(user.org) } subject = "Redash failed to execute {} of your scheduled queries".format( len(unique_errors.keys())) html, text = [ render_template('emails/failures.{}'.format(f), context) for f in ['html', 'txt'] ] send_mail.delay([user.email], subject, html, text) redis_connection.delete(key(user_id))
def send_failure_report(user_id): user = models.User.get_by_id(user_id) errors = [ json_loads(e) for e in redis_connection.lrange(key(user_id), 0, -1) ] if errors: errors.reverse() occurrences = Counter((e.get("id"), e.get("message")) for e in errors) unique_errors = {(e.get("id"), e.get("message")): e for e in errors} context = { "failures": [{ "id": v.get("id"), "name": v.get("name"), "failed_at": v.get("failed_at"), "failure_reason": v.get("message"), "failure_count": occurrences[k], "comment": comment_for(v), } for k, v in unique_errors.items()], "base_url": base_url(user.org), } subject = "Redash failed to execute {} of your scheduled queries".format( len(unique_errors.keys())) html, text = [ render_template("emails/failures.{}".format(f), context) for f in ["html", "txt"] ] send_mail.delay([user.email], subject, html, text) redis_connection.delete(key(user_id))
def test_render(self): app = create_app() with app.app_context(): d = { "failures": [{ "id": 1, "name": "Failure Unit Test", "failed_at": "May 04, 2021 02:07PM UTC", "failure_reason": "", "failure_count": 1, "comment": None }] } html, text = [ render_template("emails/failures.{}".format(f), d) for f in ["html", "txt"] ] self.assertIn('Failure Unit Test', html) self.assertIn('Failure Unit Test', text)