def send_alert_mail(threshold_rate, name, use_rate, used_size, use_limit): """Send storage use rate alert mail.""" try: # mail title subject = '[{0}] '.format(current_app.config['THEME_SITENAME']) + \ _('Storage usage report') # recipient mail list users = [] users += get_user_info_by_role_name('Repository Administrator') mail_list = [] for user in users: mail_list.append(user.email) with current_app.test_request_context() as ctx: default_lang = AdminLangSettings.get_registered_language()[0] # setting locale setattr(ctx, 'babel_locale', default_lang['lang_code']) # send alert mail send_mail(subject, mail_list, html=render_template( 'admin/alert_mail.html', location_name=name, use_rate=use_rate, used_size=used_size, use_limit=use_limit, lang_code=default_lang['lang_code'])) except Exception as ex: current_app.logger.error(ex)
def send_all_reports(report_type=None, year=None, month=None): """Query elasticsearch for each type of stats report.""" # By default get the current month and year now = datetime.now() month = month or now.month year = year or now.year tsv_files = [] all_results = { 'file_download': QueryFileReportsHelper.get( year=year, month=month, event='file_download'), 'file_preview': QueryFileReportsHelper.get( year=year, month=month, event='file_preview'), 'index_access': QueryRecordViewPerIndexReportHelper.get( year=year, month=month), 'detail_view': QueryRecordViewReportHelper.get( year=year, month=month), 'file_using_per_user': QueryFileReportsHelper.get( year=year, month=month, event='file_using_per_user'), 'top_page_access': QueryCommonReportsHelper.get( year=year, month=month, event='top_page_access'), 'search_count': QuerySearchReportHelper.get( year=year, month=month), 'user_roles': get_user_report_data(), 'site_access': QueryCommonReportsHelper.get( year=year, month=month, event='site_access') } with current_app.app_context(): # Allow for this to be used to get specific emails as well reports = {} if report_type is not None and report_type in all_results: reports[report_type] = all_results[report_type] else: reports = all_results zip_date = str(year) + '-' + str(month).zfill(2) zip_name = 'logReport_' + zip_date + '.zip' zip_stream = package_reports(reports, year, month) recepients = StatisticsEmail.get_all_emails() attachments = [Attachment(zip_name, 'application/x-zip-compressed', zip_stream.getvalue())] html_body = render_template( current_app.config['WEKO_ADMIN_REPORT_EMAIL_TEMPLATE'], report_date=zip_date, attached_file=zip_name) subject = zip_date + _(' Log report.') try: send_mail(subject, recepients, html=html_body, attachments=attachments) current_app.logger.info('[{0}] [{1}] '.format(0, 'Sent email')) except Exception as e: current_app.logger.info('[{0}] [{1}] '.format(1, 'Could not send'))
def send_run_status_mail(harvesting, harvest_log): """Send harvest runnig status mail.""" try: result = _('Running') if harvest_log.status == 'Successful': result = _('Successful') elif harvest_log.status == 'Suspended': result = _('Suspended') elif harvest_log.status == 'Cancel': result = _('Cancel') elif harvest_log.status == 'Failed': result = _('Failed') # mail title subject = '[{0}] '.format(current_app.config['THEME_SITENAME']) + \ _('Hervesting Result') # recipient mail list users = [] users += get_user_info_by_role_name('Repository Administrator') users += get_user_info_by_role_name('Community Administrator') mail_list = [] for user in users: mail_list.append(user.email) update_style_name = \ HarvestSettings.UpdateStyle(int(harvesting.update_style)).name if update_style_name == 'Difference': update_style = _('Difference') else: update_style = _('Bulk') with current_app.test_request_context() as ctx: default_lang = AdminLangSettings.get_registered_language()[0] # setting locale setattr(ctx, 'babel_locale', default_lang['lang_code']) # send mail send_mail(subject, mail_list, html=render_template('invenio_oaiharvester/run_stat_mail.html', result_text=result, errmsg=harvest_log.errmsg, harvesting=harvesting, counter=harvest_log.counter, start_time=harvest_log.start_time, end_time=harvest_log.end_time, update_style=update_style, lang_code=default_lang['lang_code'])) except Exception as ex: current_app.logger.error(ex)
def get_file_stats_tsv(self): """Get file download/preview stats report.""" stats_json = json.loads(request.form.get('report')) file_type = request.form.get('type') year = request.form.get('year') month = request.form.get('month').zfill(2) # current_app.logger.info(request.form.to_dict()) # File Format: logReport__YYYY-MM.zip zip_date = str(year) + '-' + str(month).zfill(2) zip_name = 'logReport_' + zip_date + '.zip' try: # Dynamically create zip from StringIO data into BytesIO zip_stream = package_reports(stats_json, year, month) # Make the send email function a task so it if request.form.get('send_email') == 'True': recepients = StatisticsEmail.get_all_emails() html_body = render_template( current_app.config['WEKO_ADMIN_REPORT_EMAIL_TEMPLATE'], report_date=zip_date, attached_file=zip_name) subject = zip_date + _(' Log report.') attachments = [ Attachment(zip_name, 'application/x-zip-compressed', zip_stream.getvalue()) ] send_mail(subject, recepients, html=html_body, attachments=attachments) flash(_('Successfully sent the reports to the recepients.')) else: resp = make_response() resp.data = zip_stream.getvalue() resp.headers['Content-Type'] = 'application/x-zip-compressed' resp.headers['Content-Disposition'] = 'attachment; filename=' + \ zip_name return resp except Exception as e: current_app.logger.error('Unexpected error: ', e) flash(_('Unexpected error occurred.'), 'error') return redirect(url_for('report.index'))
def send_site_license_mail(organization_name, mail_list, agg_date, data): """Send site license statistics mail.""" try: # mail title subject = '[{0}] {1} '.format(organization_name, agg_date) + \ _('statistics report') with current_app.test_request_context() as ctx: default_lang = get_system_default_language() # setting locale setattr(ctx, 'babel_locale', default_lang) # send alert mail send_mail( subject, mail_list, body=str( render_template( 'weko_admin/email_templates/site_license_report.html', agg_date=agg_date, data=data, lang_code=default_lang))) except Exception as ex: current_app.logger.error(ex)