<commit_before>/*
 * Copyright 2012 LinkedIn Corp.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package azkaban.viewer.reportal;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

import org.apache.commons.io.IOUtils;

import azkaban.executor.ExecutableFlow;
import azkaban.executor.ExecutionOptions;
import azkaban.executor.mail.DefaultMailCreator;
import azkaban.executor.mail.MailCreator;
import azkaban.project.Project;
import azkaban.reportal.util.IStreamProvider;
import azkaban.reportal.util.ReportalHelper;
import azkaban.reportal.util.ReportalUtil;
import azkaban.reportal.util.StreamProviderHDFS;
import azkaban.security.commons.HadoopSecurityManager;
import azkaban.utils.EmailMessage;
import azkaban.webapp.AzkabanWebServer;

public class ReportalMailCreator implements MailCreator {
	public static AzkabanWebServer azkaban = null;
	public static HadoopSecurityManager hadoopSecurityManager = null;
	public static String outputLocation = "";
	public static String outputFileSystem = "";
	public static String reportalStorageUser = "";
	public static File reportalMailDirectory;
	public static final String REPORTAL_MAIL_CREATOR = "ReportalMailCreator";
	public static final int NUM_PREVIEW_ROWS = 50;

	static {
		DefaultMailCreator.registerCreator(REPORTAL_MAIL_CREATOR, new ReportalMailCreator());
	}

	@Override
	public boolean createFirstErrorMessage(ExecutableFlow flow, EmailMessage message, String azkabanName, String clientHostname, String clientPortNumber, String... vars) {

		ExecutionOptions option = flow.getExecutionOptions();
		List<String> emailList = option.getFailureEmails();

		return createEmail(flow, emailList, message, "failed", azkabanName, clientHostname, clientPortNumber, false);
	}

	@Override
	public boolean createErrorEmail(ExecutableFlow flow, EmailMessage message, String azkabanName, String clientHostname, String clientPortNumber, String... vars) {

		ExecutionOptions option = flow.getExecutionOptions();
		List<String> emailList = option.getFailureEmails();

		return createEmail(flow, emailList, message, "failed", azkabanName, clientHostname, clientPortNumber, false);
	}

	@Override
	public boolean createSuccessEmail(ExecutableFlow flow, EmailMessage message, String azkabanName, String clientHostname, String clientPortNumber, String... vars) {

		ExecutionOptions option = flow.getExecutionOptions();
		List<String> emailList = option.getSuccessEmails();
		
		return createEmail(flow, emailList, message, "succeeded", azkabanName, clientHostname, clientPortNumber, true);
	}
	
	private boolean createEmail(ExecutableFlow flow, List<String> emailList, EmailMessage message, String status, String azkabanName, String clientHostname, String clientPortNumber, boolean printData) {
		
		Project project = azkaban.getProjectManager().getProject(flow.getProjectId());

		if (emailList != null && !emailList.isEmpty()) {
			message.addAllToAddress(emailList);
			message.setMimeType("text/html");
			message.setSubject("Report '" + project.getMetadata().get("title") + "' has " + status + " on " + azkabanName);
			String urlPrefix = "https://" + clientHostname + ":" + clientPortNumber + "/reportal";
			try {
				createMessage(project, flow, message, urlPrefix, printData);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return true;
		}

		return false;
	}

	private void createMessage(Project project, ExecutableFlow flow, EmailMessage message, String urlPrefix, boolean printData) throws Exception {
		message.println("<html>");
		message.println("<head></head>");
		message.println("<body style='font-family: verdana; color: #000000; background-color: #cccccc; padding: 20px;'>");
		message.println("<div style='background-color: #ffffff; border: 1px solid #aaaaaa; padding: 20px;-webkit-border-radius: 15px; -moz-border-radius: 15px; border-radius: 15px;'>");
		// Title
		message.println("<b>" + project.getMetadata().get("title") + "</b>");
		message.println("<div style='font-size: .8em; margin-top: .5em; margin-bottom: .5em;'>");
		// Status<commit_msg>Remove duplicates from email list<commit_after>/*
 * Copyright 2012 LinkedIn Corp.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package azkaban.viewer.reportal;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

import org.apache.commons.io.IOUtils;

import azkaban.executor.ExecutableFlow;
import azkaban.executor.ExecutionOptions;
import azkaban.executor.mail.DefaultMailCreator;
import azkaban.executor.mail.MailCreator;
import azkaban.project.Project;
import azkaban.reportal.util.IStreamProvider;
import azkaban.reportal.util.ReportalHelper;
import azkaban.reportal.util.ReportalUtil;
import azkaban.reportal.util.StreamProviderHDFS;
import azkaban.security.commons.HadoopSecurityManager;
import azkaban.utils.EmailMessage;
import azkaban.webapp.AzkabanWebServer;

public class ReportalMailCreator implements MailCreator {
	public static AzkabanWebServer azkaban = null;
	public static HadoopSecurityManager hadoopSecurityManager = null;
	public static String outputLocation = "";
	public static String outputFileSystem = "";
	public static String reportalStorageUser = "";
	public static File reportalMailDirectory;
	public static final String REPORTAL_MAIL_CREATOR = "ReportalMailCreator";
	public static final int NUM_PREVIEW_ROWS = 50;

	static {
		DefaultMailCreator.registerCreator(REPORTAL_MAIL_CREATOR, new ReportalMailCreator());
	}

	@Override
	public boolean createFirstErrorMessage(ExecutableFlow flow, EmailMessage message, String azkabanName, String clientHostname, String clientPortNumber, String... vars) {

		ExecutionOptions option = flow.getExecutionOptions();
		Set<String> emailList = new HashSet<String>(option.getFailureEmails());

		return createEmail(flow, emailList, message, "failed", azkabanName, clientHostname, clientPortNumber, false);
	}

	@Override
	public boolean createErrorEmail(ExecutableFlow flow, EmailMessage message, String azkabanName, String clientHostname, String clientPortNumber, String... vars) {

		ExecutionOptions option = flow.getExecutionOptions();
		Set<String> emailList = new HashSet<String>(option.getFailureEmails());

		return createEmail(flow, emailList, message, "failed", azkabanName, clientHostname, clientPortNumber, false);
	}

	@Override
	public boolean createSuccessEmail(ExecutableFlow flow, EmailMessage message, String azkabanName, String clientHostname, String clientPortNumber, String... vars) {

		ExecutionOptions option = flow.getExecutionOptions();
		Set<String> emailList = new HashSet<String>(option.getSuccessEmails());
		
		return createEmail(flow, emailList, message, "succeeded", azkabanName, clientHostname, clientPortNumber, true);
	}
	
	private boolean createEmail(ExecutableFlow flow, Set<String> emailList, EmailMessage message, String status, String azkabanName, String clientHostname, String clientPortNumber, boolean printData) {
		
		Project project = azkaban.getProjectManager().getProject(flow.getProjectId());

		if (emailList != null && !emailList.isEmpty()) {
			message.addAllToAddress(emailList);
			message.setMimeType("text/html");
			message.setSubject("Report '" + project.getMetadata().get("title") + "' has " + status + " on " + azkabanName);
			String urlPrefix = "https://" + clientHostname + ":" + clientPortNumber + "/reportal";
			try {
				createMessage(project, flow, message, urlPrefix, printData);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return true;
		}

		return false;
	}

	private void createMessage(Project project, ExecutableFlow flow, EmailMessage message, String urlPrefix, boolean printData) throws Exception {
		message.println("<html>");
		message.println("<head></head>");
		message.println("<body style='font-family: verdana; color: #000000; background-color: #cccccc; padding: 20px;'>");
		message.println("<div style='background-color: #ffffff; border: 1px solid #aaaaaa; padding: 20px;-webkit-border-radius: 15px; -moz-border-radius: 15px; border-radius: 15px;'>");
		// Title
		message.println("<b>" + project.getMetadata().get("title") + "</b>");
		message.println("<div style='font-size: .8em; margin-top: .5em; margin-bottom: .5em;'>");
		// Status
{'fraction_non_alphanumeric': 0.10701069078947369, 'fraction_numerical': 0.006167763157894737, 'mean_word_length': 5.718922651933702, 'pattern_counts': {'":': 2, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6441001', 'n_tokens_mistral': 2928, 'n_tokens_neox': 2735, 'n_words': 756}