package com.dianping.cat.report.page.sql;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import com.dianping.cat.job.sql.dal.SqlReportRecord;

public class SqlReport {
	private Date m_startTime;

	private Date m_endTime;

	private List<String> m_domains;

	private String m_domain;

	private String m_sortBy;

	private List<SqlReportModel> m_reportRecords;

	public String getSortBy() {
		return m_sortBy;
	}

	public void setSortBy(String sortBy) {
		m_sortBy = sortBy;
	}

	public Date getStartTime() {
		return m_startTime;
	}

	public SqlReport setStartTime(Date startTime) {
		m_startTime = startTime;
		return this;
	}

	public Date getEndTime() {
		return m_endTime;
	}

	public SqlReport setEndTime(Date endTime) {
		m_endTime = endTime;
		return this;
	}

	public List<String> getDomains() {
		return m_domains;
	}

	public SqlReport setDomains(List<String> domains) {
		m_domains = domains;
		return this;
	}

	public String getDomain() {
		return m_domain;
	}

	public SqlReport setDomain(String domain) {
		m_domain = domain;
		return this;
	}

	public List<SqlReportModel> getReportRecords() {
		if (m_sortBy == null) {
			m_sortBy = "avg";
		}
		Collections.sort(m_reportRecords, new SqlReportModelCompartor(m_sortBy));
		return m_reportRecords;
	}

	public void setReportRecords(List<SqlReportModel> reportRecords) {
		m_reportRecords = reportRecords;
	}

	public static class SqlReportModelCompartor implements Comparator<SqlReportModel> {
		private String m_sorted;

		public SqlReportModelCompartor(String sortBy) {
			m_sorted = sortBy;
		}

		@Override
		public int compare(SqlReportModel m1, SqlReportModel m2) {
			SqlReportRecord record1 = m1.getRecord();
			SqlReportRecord record2 = m2.getRecord();
			
			if (m_sorted.equals("name")) {
				return record1.getName().compareTo(record2.getName());
			}
			if (m_sorted.equals("total")) {
				return (int) (record2.getTotalCount() - record1.getTotalCount());
			}
			if (m_sorted.equals("failure")) {
				return (int) (record2.getFailureCount() - record1.getFailureCount());
			}
			if (m_sorted.equals("failurePercent")) {
				return (int) (m2.getFailurePercent() * 100 - m1.getFailurePercent() * 100);
			}
			if (m_sorted.equals("avg")) {
				return (int) (m2.getAvg() * 100 - m1.getAvg() * 100);
			}
			if (m_sorted.equals("95Line")) {
				return (int) (record2.getAvg2Value() * 100 - record1.getAvg2Value() * 100);
			}
			if (m_sorted.equals("longsql")) {
				return (int) (record2.getLongSqls() - record1.getLongSqls());
			}
			if (m_sorted.equals("longsqlPercent")) {
				return (int) (m2.getLongPercent() * 100 - m1.getLongPercent() * 100);
			}
			return 0;
		}
	}

}

@@ -1289,23 +1289,16 @@
 By));%0A%09%09
-return 
 m_report
@@ -1296,32 +1296,1022 @@
 %09m_reportRecords
+.add(1, addTotal());%0A%09%09return m_reportRecords;%0A%09%7D%0A%0A%09private SqlReportModel addTotal() %7B%0A%09%09SqlReportRecord result = new SqlReportRecord();%0A%09%09int total = 0;%0A%09%09int failure = 0;%0A%09%09double min = 0;%0A%09%09double max = 0;%0A%09%09double sum2 = 0;%0A%09%09double sum = 0;%0A%09%09int longSql = 0;%0A%09%09for (SqlReportModel model : m_reportRecords) %7B%0A%09%09%09SqlReportRecord temp = model.getRecord();%0A%09%09%09total = total + temp.getTotalCount();%0A%09%09%09failure = failure + temp.getFailureCount();%0A%09%09%09min = min %3C temp.getMinValue() ? min : temp.getMinValue();%0A%09%09%09max = max %3E temp.getMaxValue() ? max : temp.getMaxValue();%0A%09%09%09sum2 = sum2 + temp.getSum2Value();%0A%09%09%09sum = sum + temp.getSumValue();%0A%09%09%09longSql = longSql + temp.getLongSqls();%0A%09%09%7D%0A%09%09result.setAvg2Value(0);%0A%09%09result.setStatement(%22All%22);%0A%09%09result.setTotalCount(total);%0A%09%09result.setFailureCount(failure);%0A%09%09result.setMaxValue(max);%0A%09%09result.setMinValue(min);%0A%09%09result.setSumValue(sum);%0A%09%09result.setSum2Value(sum2);%0A%09%09result.setLongSqls(longSql);%0A%09%09return new SqlReportModel(result)
 ;%0A%09%7D%0A%0A%09public vo
@@ -2760,19 +2760,16 @@
 cord();%0A
-%09%09%09
 %0A%09%09%09if (

modify the sql report