def send_monthly_report(domain_name): logging.info("Starting send monthly report for domain %s" % domain_name) domain = models.Domain.get(domain_name, True) template = "monthly_report.html" subject = "Monthly Report" longest_outage, average_downtime = domain.longest_average_downtime_in_past_30_days num_of_outages = domain.num_outages_in_past_30_days average_number_of_outages = domain.num_outages_in_past_year / 12 diff = average_number_of_outages - num_of_outages if not diff: diff = "equal to " else: diff = abs(diff) + (" more than " if diff < 0 else " less than ") tpl_context = dict( domain_name = domain_name, month_name = datetime.now().strftime("%B %y"), num_of_outages = num_of_outages, longest_outage = longest_outage, average_downtime = "%0.1f" % average_downtime, diff = diff, from_address = config.notification_from_address, site_url = config.site_url ) images = [ ['snapshot', join(config.snapshot_shared_directory, '<path to report snapshot>')] ] users = models.User.objects(__raw__={"domain_data.domain_name" : domain_name, "domain_data.report_status" : "verified"}) for user in users: tpl_context['contact_name'] = 'Unknown' #FIXME User model has no field for name tpl_context['to_address'] = user.email_address tpl_context['email_domain_hash'] = utils.Cipher.encode(config.secret_key, "%s,,%s,,%s" % (config.mail_salt, user.email_address_hash, domain_name)) utils.send_notification_mail(user.email_address, subject, template, tpl_context, config.notification_from_address, images) logging.info("Completed send monthly report for domain %s" % domain_name)
def send_outage_clear_notification(domain_name, optime, status): domain = models.Domain.get(domain_name, True) # determine if we're a valid event new_status = status old_status = domain.status # update the status always domain.status = new_status domain.save() if new_status == old_status: logging.info("Event is a checkpoint, ignoring") return template = "clear" subject = "Clear" if not status: template = "outage" subject = "Outage" template += ".html" longest_outage, average_downtime = domain.longest_average_downtime_in_past_30_days tpl_context = dict( domain_name = domain_name, optime = datetime.fromtimestamp(optime).strftime('at %I:%M %p on %B %d, %Y'), num_of_outages = domain.num_outages_in_past_30_days, longest_outage = longest_outage, average_downtime = "%0.1f" % average_downtime, from_address = config.notification_from_address, site_url = config.site_url ) # outage if not status: outages = domain.outages_in_past_30_days[-5:] outages.reverse() tpl_context['outages'] = outages images = [ ['character', 'outage_character.png'], ['snapshot', domain.thumbnail_url] ] # clear else: outage = domain.get_outages_data_in_past_30_days(False) if outage: outage = outage[-1] tpl_context['outage_date'] = outage[0] tpl_context['clear_date'] = outage[1] tpl_context['outage_time'] = outage[2] tpl_context['clear_time'] = outage[3] tpl_context['duration'] = utils.convert_seconds_to_duration(timedelta(seconds=outage[4])) if outage[4] != -1 else "N/A" if average_downtime: average = average_downtime * 60 if average > outage[4]: tpl_context['diff'] = utils.convert_seconds_to_duration(timedelta(seconds=average - outage[4])) + " less " else: tpl_context['diff'] = utils.convert_seconds_to_duration(timedelta(seconds=outage[4] - average)) + " more " else: tpl_context['diff'] = None else: tpl_context['outage_date'] = "N/A" tpl_context['clear_date'] = "N/A" tpl_context['outage_time'] = "N/A" tpl_context['clear_time'] = "N/A" tpl_context['duration'] = "N/A" tpl_context['diff'] = None images = [ ['character', 'clear_character.png'], ['snapshot', domain.thumbnail_url] ] domain_search_key = "domain_data.%s.notify_status" % domain.hash users = models.User.objects(__raw__={domain_search_key : "verified"}) for user in users: logging.info("Sending email to user %s" % user.email_address) tpl_context['to_address'] = user.email_address tpl_context['email_domain_hash'] = utils.Cipher.encode(config.secret_key, "%s,,%s,,%s" % (config.mail_salt, user.email_address_hash, domain_name)) utils.send_notification_mail(user.email_address, subject, template, tpl_context, config.notification_from_address, images)