提交 ded7f773 编写于 作者: Y You Yong

modify the cat sql job

上级 a3e90ea6
......@@ -9,25 +9,15 @@ public enum ReportPage implements Page {
PROBLEM("problem", "p", "Problem", "Problem discovered", true),
<<<<<<< HEAD
LOGVIEW("logview", "m", "Logview", "CAT log details view for a given message.", false),
=======
TRANSACTION("transaction", "t", "Transaction", "Transaction summary report", true),
LOGVIEW("logview", "m", "Logview", "Log view details", false),
>>>>>>> 1ce8aecf691c885c4773c6c52388dad926ad9557
IP("ip", "ip", "Top IP List", "Top visited IP addresses", true),
<<<<<<< HEAD
MODEL("model", "model", "Model", "Model Service", false),
PROBLEM("problem", "p", "Problem", "Problem", true);
=======
MODEL("model", "model", "Model", "Service model", false),
SQL("sql", "sql", "Sql", "Sql", true);
>>>>>>> 1ce8aecf691c885c4773c6c52388dad926ad9557
private String m_name;
......
package com.dianping.cat.report.page.sql;
public enum Action implements com.site.web.mvc.Action {
VIEW("view");
VIEW("view"),
GRAPHS("graphs");
private String m_name;
......
......@@ -11,6 +11,7 @@ import com.dianping.cat.job.sql.dal.SqlReportRecord;
import com.dianping.cat.job.sql.dal.SqlReportRecordDao;
import com.dianping.cat.job.sql.dal.SqlReportRecordEntity;
import com.dianping.cat.report.ReportPage;
import com.dianping.cat.report.graph.GraphBuilder;
import com.site.dal.jdbc.DalException;
import com.site.dal.jdbc.Readset;
import com.site.lookup.annotation.Inject;
......@@ -25,6 +26,9 @@ public class Handler implements PageHandler<Context> {
@Inject
private SqlReportRecordDao m_dao;
@Inject
private GraphBuilder m_builder;
@Override
@PayloadMeta(Payload.class)
......@@ -37,11 +41,49 @@ public class Handler implements PageHandler<Context> {
@OutboundActionMeta(name = "sql")
public void handleOutbound(Context ctx) throws ServletException, IOException {
Model model = new Model(ctx);
model.setAction(Action.VIEW);
model.setPage(ReportPage.SQL);
Payload payload = ctx.getPayload();
SqlReport report = new SqlReport();
Action action = payload.getAction();
if(action==null||action==Action.VIEW){
model.setAction(Action.VIEW);
showReport(model, payload);
}else{
model.setAction(Action.GRAPHS);
showGraphs(model, payload);
}
m_jspViewer.view(ctx, model);
}
public void showGraphs(Model model,Payload payload){
int id = payload.getId();
try {
SqlReportRecord record= m_dao.findByPK(id, SqlReportRecordEntity.READSET_FULL);
String statement = record.getStatement();
String durationDistribution =record.getDurationdistribution();
String durationOvertime =record.getDurationovertime();
String hitsovOvrtime =record.getHitsovertime();
String failureOvertime =record.getFailureovertime();
String graph1 = m_builder.build(new SqlGraphPayload(0,"SQL Exeture Time Distribution", "Duration (ms)", "Count", durationDistribution));
String graph2 = m_builder.build(new SqlGraphPayload(1,"SQL Hits Over One Hour", "Time (min)", "Count", hitsovOvrtime));
String graph3 = m_builder.build(new SqlGraphPayload(2,"SQL Exeture Average Time Over One Hour", "Time (min)",
"Average Duration (ms)", durationOvertime));
String graph4 = m_builder.build(new SqlGraphPayload(3,"SQL Failures Over One Hour", "Time (min)", "Count", failureOvertime));
model.setGraph1(graph1);
model.setGraph2(graph2);
model.setGraph3(graph3);
model.setGraph4(graph4);
model.setStatement(statement);
} catch (DalException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void showReport(Model model, Payload payload) {
SqlReport report = new SqlReport();
String domain = payload.getDomain();
long startDate = payload.getDate();
model.setDate(startDate);
......@@ -82,7 +124,5 @@ public class Handler implements PageHandler<Context> {
} catch (DalException e) {
e.printStackTrace();
}
m_jspViewer.view(ctx, model);
}
}
}
......@@ -3,6 +3,7 @@ package com.dianping.cat.report.page.sql;
public enum JspFile {
VIEW("/jsp/report/sql.jsp"),
GRAPHS("/jsp/report/sql_graphs.jsp"),
;
private String m_path;
......
......@@ -11,6 +11,8 @@ public class JspViewer extends BaseJspViewer<ReportPage, Action, Context, Model>
switch (action) {
case VIEW:
return JspFile.VIEW.getPath();
case GRAPHS:
return JspFile.GRAPHS.getPath();
}
throw new RuntimeException("Unknown action: " + action);
......
......@@ -7,6 +7,16 @@ import com.dianping.cat.report.page.AbstractReportModel;
public class Model extends AbstractReportModel<Action, Context> {
private SqlReport m_report;
private String m_graph1;
private String m_graph2;
private String m_graph3;
private String m_graph4;
private String m_statement;
public Model(Context ctx) {
super(ctx);
......@@ -41,5 +51,45 @@ public class Model extends AbstractReportModel<Action, Context> {
} else {
return m_report.getDomains();
}
}
public String getGraph1() {
return m_graph1;
}
public void setGraph1(String graph1) {
m_graph1 = graph1;
}
public String getGraph2() {
return m_graph2;
}
public void setGraph2(String graph2) {
m_graph2 = graph2;
}
public String getGraph3() {
return m_graph3;
}
public void setGraph3(String graph3) {
m_graph3 = graph3;
}
public String getGraph4() {
return m_graph4;
}
public void setGraph4(String graph4) {
m_graph4 = graph4;
}
public String getStatement() {
return m_statement;
}
public void setStatement(String statement) {
m_statement = statement;
}
}
......@@ -2,38 +2,36 @@ package com.dianping.cat.report.page.sql;
import com.dianping.cat.report.ReportPage;
import com.dianping.cat.report.page.AbstractReportPayload;
import com.dianping.cat.report.page.transaction.Action;
import com.site.web.mvc.ActionContext;
import com.site.web.mvc.payload.annotation.FieldMeta;
public class Payload extends AbstractReportPayload<Action> {
private ReportPage m_page;
@FieldMeta("op")
private Action m_action;
@FieldMeta("id")
private int id;
public Payload() {
super(ReportPage.SQL);
}
public void setAction(Action action) {
m_action = action;
public int getId() {
return id;
}
@Override
public Action getAction() {
return m_action;
public void setId(int id) {
this.id = id;
}
@Override
public ReportPage getPage() {
return m_page;
public void setAction(String action) {
m_action = Action.getByName(action, Action.VIEW);
}
@Override
public void setPage(String page) {
m_page = ReportPage.getByName(page, ReportPage.SQL);
public Action getAction() {
return m_action;
}
@Override
......
package com.dianping.cat.report.page.sql;
import java.util.List;
import org.apache.commons.lang.math.NumberUtils;
import com.dianping.cat.report.graph.AbstractGraphPayload;
import com.site.helper.Splitters;
public class SqlGraphPayload extends AbstractGraphPayload {
private String[] m_labels;
private double[] m_values;
private int m_size;
private int m_index;
public SqlGraphPayload(int index, String title, String axisXLabel, String axisYLabel, String metaData) {
super(title, axisXLabel, axisYLabel);
m_index = index;
List<String> data = Splitters.by(",").noEmptyItem().split(metaData);
if (data != null) {
m_size = data.size();
m_labels = new String[m_size];
m_values = new double[m_size - 1];
for (int i = 0; i < m_size; i++) {
String temp = data.get(i);
String[] s = temp.split(":");
if (i == m_size - 1) {
m_labels[i] = s[0];
} else {
m_labels[i] = s[0];
m_values[i] = NumberUtils.toDouble(s[1], 0d);
}
}
} else {
m_labels = new String[1];
m_values = new double[1];
}
}
@Override
public String getAxisXLabel(int index) {
return m_labels[index];
}
@Override
public int getOffsetX() {
if (m_index % 2 == 1) {
return getDisplayWidth();
} else {
return 0;
}
}
@Override
public int getOffsetY() {
if (m_index / 2 == 1) {
return getDisplayHeight() + 20;
} else {
return 0;
}
}
@Override
protected double[] loadValues() {
return m_values;
}
@Override
public int getDisplayHeight() {
return (int) (super.getDisplayHeight() * 0.7);
}
@Override
public int getDisplayWidth() {
return (int) (super.getDisplayWidth() * 0.7);
}
@Override
public int getWidth() {
return super.getWidth() + 120;
}
@Override
public boolean isStandalone() {
return false;
}
}
package com.dianping.cat.report.page.sql;
import java.text.DecimalFormat;
import com.dianping.cat.job.sql.dal.SqlReportRecord;
public class SqlReportModel {
private SqlReportRecord m_record;
private DecimalFormat percent = new DecimalFormat("#.##%");
private DecimalFormat number = new DecimalFormat("#.##");
public SqlReportModel(SqlReportRecord record) {
m_record = record;
}
public String getFailurePercent() {
double value = m_record.getFailures() / m_record.getTotalcount();
return percent.format(value);
public double getFailurePercent() {
return (double)m_record.getFailures() / (double)m_record.getTotalcount();
}
public String getLongPercent() {
double value = m_record.getLongsqls() / m_record.getTotalcount();
return percent.format(value);
public double getLongPercent() {
return (double)m_record.getLongsqls() / (double)m_record.getTotalcount();
}
public String getAvg() {
double value = m_record.getSumvalue() / m_record.getTotalcount();
return number.format(value);
public double getAvg() {
return (double)m_record.getSumvalue() / (double)m_record.getTotalcount();
}
public String getStd() {
public double getStd() {
double sum2 = m_record.getSum2value();
int count = m_record.getTotalcount();
double avg = m_record.getSumvalue() / m_record.getTotalcount();
double std = Math.sqrt(sum2 / count - avg * avg);
return number.format(std);
return Math.sqrt(sum2 / count - avg * avg);
}
public SqlReportRecord getRecord() {
......
......@@ -9,22 +9,14 @@ public class NavigationBar {
ReportPage.HOME,
<<<<<<< HEAD
=======
//ReportPage.FAILURE,
>>>>>>> 1ce8aecf691c885c4773c6c52388dad926ad9557
ReportPage.TRANSACTION,
ReportPage.PROBLEM,
ReportPage.IP,
<<<<<<< HEAD
=======
ReportPage.SQL,
//ReportPage.SERVICE,
>>>>>>> 1ce8aecf691c885c4773c6c52388dad926ad9557
ReportPage.LOGVIEW
......
......@@ -214,8 +214,6 @@
<implementation>com.dianping.cat.report.page.problem.JspViewer</implementation>
</component>
<component>
<<<<<<< HEAD
=======
<role>com.dianping.cat.report.page.transaction.Handler</role>
<implementation>com.dianping.cat.report.page.transaction.Handler</implementation>
<requirements>
......@@ -237,7 +235,6 @@
<implementation>com.dianping.cat.report.page.transaction.JspViewer</implementation>
</component>
<component>
>>>>>>> 1ce8aecf691c885c4773c6c52388dad926ad9557
<role>com.dianping.cat.report.page.logview.Handler</role>
<implementation>com.dianping.cat.report.page.logview.Handler</implementation>
<requirements>
......@@ -352,6 +349,7 @@
</requirement>
<requirement>
<role>com.dianping.cat.message.spi.MessageCodec</role>
<role-hint>html</role-hint>
</requirement>
</requirements>
</component>
......@@ -365,6 +363,9 @@
<requirement>
<role>com.dianping.cat.job.sql.dal.SqlReportRecordDao</role>
</requirement>
<requirement>
<role>com.dianping.cat.report.graph.GraphBuilder</role>
</requirement>
</requirements>
</component>
<component>
......
......@@ -26,10 +26,10 @@
<c:forEach var="domain" items="${model.domains}">
&nbsp;<c:choose>
<c:when test="${model.domain eq domain}">
<a href="${model.baseUri}?domain=${domain}" class="current">[&nbsp;${domain}&nbsp;]</a>
<a href="${model.baseUri}?domain=${domain}&date=${model.date}" class="current">[&nbsp;${domain}&nbsp;]</a>
</c:when>
<c:otherwise>
<a href="${model.baseUri}?domain=${domain}">[&nbsp;${domain}&nbsp;]</a>
<a href="${model.baseUri}?domain=${domain}&date=${model.date}">[&nbsp;${domain}&nbsp;]</a>
</c:otherwise>
</c:choose>&nbsp;
</c:forEach>
......
var isCtrl = false;
function showGraphs(anchor,target, id) {
if (isCtrl) return true;
var cell = document.getElementById(target);
var text = anchor.innerHTML;
if (text == '[:: show ::]') {
anchor.innerHTML = '[:: hide ::]';
if (cell.nodeName == 'IMG') { // <img src='...'/>
cell.src = "?op=graphs&id="+id;
} else { // <div>...</div>
$.ajax({
type: "get",
url: "?op=graphs&id="+id,
success : function(data, textStatus) {
cell.innerHTML = data;
}
});
}
cell.style.display = 'block';
cell.parentNode.style.display = 'block';
} else {
anchor.innerHTML = '[:: show ::]';
cell.style.display = 'none';
cell.parentNode.style.display = 'none';
}
return false;
}
$(document).keydown(function(e) {
if(e.ctrlKey || e.metaKey) isCtrl = true;
}).keyup(function(e) {
isCtrl = false;
});
\ No newline at end of file
......@@ -9,16 +9,15 @@
<c:set var="report" value="${model.report}"/>
<c:set var="threads" value="${report.machines[model.ipAddress].threads}"/>
<res:useCss value="${res.css.local.problem_css}" target="head-css"/>
<res:useJs value="${res.js.local['jquery-1.7.1.js']}" target="head-js"/>
<res:useJs value="${res.js.local.problem_js}" target="head-js"/>
<a:report title="Problem Report" navUrlPrefix="domain=${model.domain}&ip=${model.ipAddress}" timestamp="${w:format(model.currentTime,'yyyy-MM-dd HH:mm:ss')}">
<jsp:attribute name="subtitle">From ${w:format(report.startTime,'yyyy-MM-dd HH:mm:ss')} to ${w:format(report.endTime,'yyyy-MM-dd HH:mm:ss')}</jsp:attribute>
<jsp:body>
<res:useCss value="${res.css.local.problem_css}" target="head-css"/>
<res:useJs value="${res.js.local['jquery-1.7.1.js']}" target="head-js"/>
<res:useJs value="${res.js.local.problem_js}" target="head-js"/>
<table class="machines">
<tr>
......
......@@ -24,29 +24,34 @@
<table class="sql">
<tr>
<th>SQL</th>
<th>Total</th>
<th>Exec Counts</th>
<th>Failure</th>
<th>Failure%</th>
<th>Min / Max / Avg / Std(ms)</th>
<th>95% Line</th>
<th>DB Time</th>
<th>LongSQL</th>
<th>Long%</th>
<th>Min/Max/Avg/Std(ms)</th>
<th>95% Avg</th>
<th>Sample Link</th>
<th>Sample</th>
</tr>
<c:forEach var="reportRecord" items="${model.report.reportRecords}" varStatus="status">
<tr class="${status.index mod 2 != 0 ? 'odd' : 'even'}">
<td>${reportRecord.record.name}</td>
<td>${reportRecord.record.totalcount}</td>
<td>${reportRecord.record.failures}</td>
<td>${reportRecord.failurePercent}</td>
<td>${reportRecord.record.longsqls}</td>
<td>${reportRecord.longPercent}</td>
<td>${reportRecord.record.minvalue}/${reportRecord.record.maxvalue}/${reportRecord.avg}/${reportRecord.std}</td>
<td>${reportRecord.avg}</td>
<td><a href="?op=graphs&id=${reportRecord.record.id}" onclick="return showGraphs(this,${status.index},'${reportRecord.record.id}');">[:: show ::]</a> ${reportRecord.record.name}</td>
<td>${w:format(reportRecord.record.totalcount,'0.00')}</td>
<td>${w:format(reportRecord.record.failures,'0.00')}</td>
<td>${w:format(reportRecord.failurePercent,'0.00%')}</td>
<td>${w:format(reportRecord.record.minvalue,'0.00')} / ${w:format(reportRecord.record.maxvalue,'0.00')} / ${w:format(reportRecord.avg,'0.00')} / ${w:format(reportRecord.std,'0.00')}</td>
<td>${w:format(reportRecord.record.avg2value,'0.00')}</td>
<td>${w:format(reportRecord.record.sumvalue,'0.00')}</td>
<td>${w:format(reportRecord.record.longsqls,'0.00')}</td>
<td>${w:format(reportRecord.longPercent,'0.00%')}</td>
<td><a href='cat/r/m/${reportRecord.record.samplelink}/logview.html'>Link</a></td>
</tr>
<tr class="graphs"><td colspan="10" align="center"><div id="${status.index}" style="display:none"></div></td></tr>
</c:forEach>
</table>
<br>
</jsp:body>
</a:report>
<res:useJs value="${res.js.local.sql_js}" target="bottom-js" />
<%@ page contentType="image/svg+xml; charset=utf-8"%>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" width="600" height="300" xmlns="http://www.w3.org/2000/svg">
<title>Duration</title>
<g id="coordinate" stroke="#003f7f" fill="white">
<path id="xy" d="M90,200 h480 m-480,0 v-150"/>
<path id="xy-2" d="M90,50 m480,0 v150" stroke-dasharray="1,5"/>
<path id="lines" d="M90,50 h480 m-480,30 h480 m-480,30 h480 m-480,30 h480 m-480,30 h480" stroke-dasharray="1,5"/>
<path id="ys" d="M90,50 h-6 m6,30 h-6 m6,30 h-6 m6,30 h-6 m6,30 h-6 m6,30 h-6"/>
<path id="xs" d="M90,200 v10 m20,-10 v6 m20,-6 v10 m20,-10 v6 m20,-6 v10 m20,-10 v6 m20,-6 v10 m20,-10 v6 m20,-6 v10 m20,-10 v6 m20,-6 v10 m20,-10 v6 m20,-6 v10 m20,-10 v6 m20,-6 v10 m20,-10 v6 m20,-6 v10 m20,-10 v6 m20,-6 v10 m20,-10 v6 m20,-6 v10 m20,-10 v6 m20,-6 v10 m20,-10 v6 m20,-6 v10"/>
</g>
<g id="yt" direction="rtl">
<text x="80" y="54">200</text>
<text x="80" y="84">160</text>
<text x="80" y="114">120</text>
<text x="80" y="144">80</text>
<text x="80" y="174">40</text>
<text x="80" y="204">0</text>
</g>
<g id="xt">
<text x="86" y="225">0</text>
<text x="126" y="225">2</text>
<text x="166" y="225">4</text>
<text x="206" y="225">6</text>
<text x="246" y="225">8</text>
<text x="282" y="225">10</text>
<text x="322" y="225">12</text>
<text x="362" y="225">14</text>
<text x="402" y="225">16</text>
<text x="442" y="225">18</text>
<text x="482" y="225">20</text>
<text x="522" y="225">22</text>
</g>
<g>
<text x="255" y="258" font-size="20" alignment-baseline="central">Bottom</text>
<text x="276" y="30" font-size="28">Title</text>
</g>
<g fill="#ff0000">
<rect x="90" y="140" width="19" height="60"/>
<text transform="rotate(-90,110,140) translate(4,-4)" x="110" y="140">60</text>
<rect x="110" y="170" width="19" height="30"/>
<text transform="rotate(-90,130,170) translate(4,-4)" x="130" y="170">30</text>
<rect x="130" y="150" width="19" height="50"/>
<text transform="rotate(-90,150,150) translate(4,-4)" x="150" y="150">50</text>
</g>
</svg>
\ No newline at end of file
<%@ page contentType="text/html; charset=utf-8"%>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<jsp:useBean id="model" type="com.dianping.cat.report.page.sql.Model" scope="request" />
SQL Statement: ${model.statement}
<svg version="1.1" width="980" height="380" xmlns="http://www.w3.org/2000/svg">
${model.graph1}
${model.graph2}
${model.graph3}
${model.graph4}
</svg>
......@@ -40,7 +40,7 @@ public class BrowserAnalyzer extends Configured implements Tool {
job.setInputFormatClass(MessageTreeInputFormat.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path("target/hdfs/20120215/17/null"));
FileInputFormat.addInputPath(job, new Path("target/hdfs/20120312/22/null/"));
FileOutputFormat.setOutputPath(job, new Path("target/browser"));
Files.forDir().delete(new File("target/browser"), true);
......
......@@ -52,17 +52,18 @@ public class SqlJobMain extends Configured implements Tool {
@Override
public int run(String[] args) throws Exception {
Configuration conf = getConf();
Job job = new Job(conf, "Sql Analyzer");
job.setJarByClass(SqlJobMain.class);
job.setMapperClass(SqlJobMapper.class);
job.setReducerClass(SqlJobReducer.class);
job.setInputFormatClass(MessageTreeInputFormat.class);
job.setOutputKeyClass(SqlStatementKey.class);
job.setOutputValueClass(SqlJobResult.class);
job.setPartitionerClass(SqlJobPatitioner.class);
job.setMapOutputKeyClass(SqlStatementKey.class);
job.setMapOutputValueClass(SqlStatementValue.class);
job.setPartitionerClass(SqlJobPatitioner.class);
job.setNumReduceTasks(DEFAULT_REDUCE_NUMBER);
if (args.length > 0) {
......@@ -103,6 +104,7 @@ public class SqlJobMain extends Configured implements Tool {
* insert the result to mysql
*/
private int runSqlRecordJob(String currentHour) throws Exception {
System.out.println("Insert database job start!");
Configuration conf = getConf();
conf.set("JobHour", currentHour);
Job job = new Job(conf, "Sql Record");
......
......@@ -49,7 +49,10 @@ public class SqlJobMapper extends Mapper<Object, MessageTreeWritable, SqlStateme
if (!transaction.getStatus().equals(Transaction.SUCCESS)) {
flag = 1;
}
SqlStatementValue result = new SqlStatementValue(flag, duration ,tree.getMessageId());
long transactionTime = transaction.getTimestamp();
long hour = transactionTime - transactionTime % (60 * 60 * 1000);
int minute = (int) Math.floor((double) (transactionTime - hour) /(60* 1000.0));
SqlStatementValue result = new SqlStatementValue(flag, duration, tree.getMessageId(), minute);
context.write(statementKey, result);
}
......
......@@ -8,9 +8,8 @@ public class SqlJobReducer extends Reducer<SqlStatementKey, SqlStatementValue, S
public void reduce(SqlStatementKey key, Iterable<SqlStatementValue> values, Context context) throws IOException,
InterruptedException {
SqlJobResult result = new SqlJobResult();
for (SqlStatementValue val : values) {
result.add(val.getValue(), val.getFlag(), val.getSampleUrl());
result.add(val.getValue(), val.getFlag(), val.getSampleUrl().toString() ,val.getMinute());
}
context.write(key, result);
}
......
......@@ -6,7 +6,10 @@ import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.hadoop.io.Writable;
......@@ -32,24 +35,47 @@ public class SqlJobResult implements Writable {
private List<String> m_urls = new ArrayList<String>();
private Map<Integer, Integer> m_durationDistribution = new LinkedHashMap<Integer, Integer>();
private Map<Integer, Integer> m_hitsOverTime = new LinkedHashMap<Integer, Integer>();
private Map<Integer, Double> m_durationOverTime = new LinkedHashMap<Integer, Double>();
private Map<Integer, Double> m_durationOverTimeSum = new LinkedHashMap<Integer, Double>();
private Map<Integer, Integer> m_failureOverTime = new LinkedHashMap<Integer, Integer>();
private DecimalFormat df = new DecimalFormat("#.##");
public void add(double value, int flag, String url ) {
m_sum += value;
m_sum2 += value * value;
public SqlJobResult() {
for (int i = 0; i <= 60; i = i + 5) {
m_hitsOverTime.put(i, 0);
m_durationOverTime.put(i, 0.0);
m_failureOverTime.put(i, 0);
}
m_durationDistribution.put(0, 0);
for (int i = 1; i <= 65536; i = i * 2) {
m_durationDistribution.put(i, 0);
}
}
public void add(double duration, int flag, String url, int minute) {
m_sum += duration;
m_sum2 += duration * duration;
if (flag == 1) {
m_failureCount++;
}
if (value > LONG_TIME) {
if (duration > LONG_TIME) {
m_longTimeCount++;
}
if (value < m_min) {
m_min = value;
if (duration < m_min) {
m_min = duration;
}
if (value > m_max) {
m_max = value;
if (duration > m_max) {
m_max = duration;
}
m_durations.add(value);
m_durations.add(duration);
int size = m_urls.size();
if (size == 0) {
......@@ -57,6 +83,24 @@ public class SqlJobResult implements Writable {
} else if (size == 1 && flag == 1) {
m_urls.add(url);
}
int minuteKey = minute - minute % 5;
m_hitsOverTime.put(minuteKey, m_hitsOverTime.get(minuteKey) + 1);
m_durationOverTime.put(minuteKey, m_durationOverTime.get(minuteKey) + 1);
if (flag == 1) {
m_failureOverTime.put(minuteKey, m_failureOverTime.get(minuteKey) + 1);
}
int durationKey = getDuration(duration);
m_durationDistribution.put(durationKey, m_durationDistribution.get(durationKey) + 1);
}
public int getDuration(double duration) {
int min = 0;
while (duration > Math.pow(2, min + 1)) {
min++;
}
return (int) Math.pow(2, min);
}
public double getAvg() {
......@@ -84,10 +128,46 @@ public class SqlJobResult implements Writable {
.append(SPIT);
sb.append(df.format(m_min)).append(SPIT).append(df.format(m_max)).append(SPIT).append(df.format(m_sum))
.append(SPIT).append(df.format(m_sum2)).append(SPIT).append(df.format(getAvg())).append(SPIT);
int size = m_urls.size();
sb.append(m_urls.get(size-1));
sb.append(m_urls.get(size - 1)).append(SPIT);
sb.append(mapToString(m_durationDistribution)).append(SPIT);
sb.append(mapToString(m_hitsOverTime)).append(SPIT);
for (Entry<Integer, Double> entry : m_durationOverTimeSum.entrySet()) {
Integer key = entry.getKey();
double value = 0;
int count = m_hitsOverTime.get(key);
if (count > 0) {
value = m_durationOverTimeSum.get(key) / count;
}
m_durationOverTime.put(key, value);
}
sb.append(map2String(m_durationOverTime)).append(SPIT);
sb.append(mapToString(m_failureOverTime)).append(SPIT);
return sb.toString();
}
private String map2String(Map<Integer, Double> map) {
StringBuilder sb = new StringBuilder();
if (map != null) {
for (Entry<Integer, Double> entry : map.entrySet()) {
sb.append(entry.getKey().toString()).append(":").append(entry.getValue().toString()).append(",");
}
sb.deleteCharAt(sb.length() - 1);
}
return sb.toString();
}
private String mapToString(Map<Integer, Integer> map) {
StringBuilder sb = new StringBuilder();
if (map != null) {
for (Entry<Integer, Integer> entry : map.entrySet()) {
sb.append(entry.getKey().toString()).append(":").append(entry.getValue().toString()).append(",");
}
sb.deleteCharAt(sb.length() - 1);
}
return sb.toString();
}
......
......@@ -13,6 +13,8 @@ public class SqlStatementValue implements Writable {
public double m_value;
public int m_minute;
public Text m_sampleUrl;
public SqlStatementValue(){
......@@ -26,14 +28,19 @@ public class SqlStatementValue implements Writable {
public double getValue() {
return m_value;
}
public String getSampleUrl(){
return m_sampleUrl.toString();
}
public SqlStatementValue(int flag, double value ,String url) {
public int getMinute() {
return m_minute;
}
public Text getSampleUrl() {
return m_sampleUrl;
}
public SqlStatementValue(int flag, double value ,String url ,int minute) {
m_flag = flag;
m_value = value;
m_minute = minute;
m_sampleUrl=new Text(url);
}
......@@ -42,6 +49,7 @@ public class SqlStatementValue implements Writable {
m_flag = input.readInt();
m_value = input.readDouble();
m_sampleUrl.readFields(input);
m_minute = input.readInt();
}
@Override
......@@ -49,5 +57,6 @@ public class SqlStatementValue implements Writable {
output.writeInt(m_flag);
output.writeDouble(m_value);
m_sampleUrl.write(output);
output.writeInt(m_minute);
}
}
......@@ -11,34 +11,37 @@ import com.dianping.cat.job.sql.dal.SqlReportRecord;
import com.dianping.cat.job.sql.dal.SqlReportRecordDao;
import com.site.dal.jdbc.DalException;
public class SqlRecordJobReducer extends Reducer<Text, Text, Text, Text>{
public void reduce(Text key, Iterable<Text> values, Context context) throws IOException,
InterruptedException {
Text currentHour = values.iterator().next();
SqlReportJobRecord sql = new SqlReportJobRecord(currentHour.toString(),key.toString());
public class SqlRecordJobReducer extends Reducer<Text, Text, Text, Text> {
public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
Text currentHour = values.iterator().next();
SqlReportJobRecord sql = new SqlReportJobRecord(currentHour.toString(), key.toString());
try {
SqlReportRecordDao dao = ContainerBootstrap.INSTANCE.lookup(SqlReportRecordDao.class);
SqlReportRecord row = dao.createLocal();
row.setDomain(sql.getDomain());
row.setTotalcount(sql.getTotalCount());
row.setFailures(sql.getFailureCount());
row.setLongsqls(sql.getLongCount());
row.setAvg2value(sql.getAvg2());
row.setSumvalue(sql.getSum());
row.setSum2value(sql.getSum2());
row.setMaxvalue(sql.getMax());
row.setMinvalue(sql.getMin());
row.setStatement(sql.getStatement());
row.setName(sql.getName());
row.setSamplelink(sql.getSampleLink());
row.setTransactiondate(sql.getDate());
row.setCreationdate(new Date());
dao.insert(row);
} catch (ComponentLookupException e) {
e.printStackTrace();
} catch (DalException e) {
e.printStackTrace();
}
SqlReportRecordDao dao = ContainerBootstrap.INSTANCE.lookup(SqlReportRecordDao.class);
SqlReportRecord row = dao.createLocal();
row.setDomain(sql.getDomain());
row.setTotalcount(sql.getTotalCount());
row.setFailures(sql.getFailureCount());
row.setLongsqls(sql.getLongCount());
row.setAvg2value(sql.getAvg2());
row.setSumvalue(sql.getSum());
row.setSum2value(sql.getSum2());
row.setMaxvalue(sql.getMax());
row.setMinvalue(sql.getMin());
row.setStatement(sql.getStatement());
row.setName(sql.getName());
row.setSamplelink(sql.getSampleLink());
row.setTransactiondate(sql.getDate());
row.setCreationdate(new Date());
row.setDurationdistribution(sql.getDurationDistribution());
row.setHitsovertime(sql.getHitsOverTime());
row.setDurationovertime(sql.getDurationOverTime());
row.setFailureovertime(sql.getFailureOverTime());
dao.insert(row);
} catch (ComponentLookupException e) {
e.printStackTrace();
} catch (DalException e) {
e.printStackTrace();
}
}
}
}
......@@ -7,7 +7,7 @@ import java.util.Date;
public class SqlReportJobRecord {
private String m_domain;
private String m_name;
private String m_statement;
......@@ -37,16 +37,24 @@ public class SqlReportJobRecord {
private Date m_creationDate;
private String m_durationDistribution;
private String m_hitsOverTime;
private String m_durationOverTime;
private String m_failureOverTime;
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private SimpleDateFormat hourFormat = new SimpleDateFormat("yyyyMMdd/HH");
private static final String SPIT = "\t";
public SqlReportJobRecord(){
public SqlReportJobRecord() {
}
public String toString() {
StringBuilder sb = new StringBuilder();
......@@ -68,15 +76,15 @@ public class SqlReportJobRecord {
}
// domain1 SQLStatement Internal9 500 500 500 100 199 74750 11591750 147
public SqlReportJobRecord(String currentHour,String text) {
public SqlReportJobRecord(String currentHour, String text) {
try {
m_transactionDate = hourFormat.parse(currentHour);
} catch (ParseException e) {
Date error = new Date();
error.setTime(0);
m_transactionDate =error ;
}
m_transactionDate = hourFormat.parse(currentHour);
} catch (ParseException e) {
Date error = new Date();
error.setTime(0);
m_transactionDate = error;
}
m_creationDate = new Date();
String[] params = text.split("\t");
......@@ -92,16 +100,19 @@ public class SqlReportJobRecord {
m_sum2 = Double.parseDouble(params[9]);
m_avg2 = Double.parseDouble(params[10]);
m_sampleLink = params[11];
m_durationDistribution = params[12];
m_hitsOverTime = params[13];
m_durationOverTime = params[14];
m_failureOverTime = params[15];
}
public String getName() {
return m_name;
}
return m_name;
}
public void setName(String name) {
m_name = name;
}
m_name = name;
}
public String getDomain() {
return m_domain;
......@@ -207,4 +218,36 @@ public class SqlReportJobRecord {
m_creationDate = creatTime;
}
public Date getTransactionDate() {
return m_transactionDate;
}
public Date getCreationDate() {
return m_creationDate;
}
public String getDurationDistribution() {
return m_durationDistribution;
}
public String getHitsOverTime() {
return m_hitsOverTime;
}
public String getDurationOverTime() {
return m_durationOverTime;
}
public String getFailureOverTime() {
return m_failureOverTime;
}
public SimpleDateFormat getSdf() {
return sdf;
}
public SimpleDateFormat getHourFormat() {
return hourFormat;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<entities>
<entity name="sqlreport" table="SqlReport" alias="s">
<entity name="sqlreport" table="sqlreport" alias="s">
<member name="id" field="Id" value-type="int" length="10" nullable="false" key="true" auto-increment="true" />
<member name="domain" field="Domain" value-type="String" length="50" nullable="false" />
<member name="statement" field="Statement" value-type="String" length="500" nullable="false" />
......@@ -16,6 +16,10 @@
<member name="transactiondate" field="TransactionDate" value-type="Date" nullable="false" />
<member name="creationdate" field="CreationDate" value-type="Date" nullable="false" />
<member name="name" field="Name" value-type="String" length="100" nullable="false" />
<member name="durationdistribution" field="durationDistribution" value-type="String" length="500" nullable="false" />
<member name="hitsovertime" field="hitsOverTime" value-type="String" length="500" nullable="false" />
<member name="durationovertime" field="durationOverTime" value-type="String" length="500" nullable="false" />
<member name="failureovertime" field="failureOverTime" value-type="String" length="500" nullable="false" />
<var name="key-id" value-type="int" key-member="id" />
<primary-key name="PRIMARY" members="Id" />
<readsets>
......
......@@ -2,22 +2,18 @@
<entities do-package="com.dianping.cat.job.sql.dal" gen="true">
<entity name="sqlreport" class-name="SqlReportRecord" table="SqlReport" alias="s">
<member name="minvalue" field="MinValue" value-type="double"
length="22" nullable="false" />
<member name="maxvalue" field="MaxValue" value-type="double"
length="22" nullable="false" />
<member name="avg2value" field="Avg2Value" value-type="double"
length="22" nullable="false" />
<member name="sumvalue" field="SumValue" value-type="double"
length="22" nullable="false" />
<member name="sum2value" field="Sum2Value" value-type="double"
length="22" nullable="false" />
<member name="minvalue" field="MinValue" value-type="double" length="22" nullable="false" />
<member name="maxvalue" field="MaxValue" value-type="double" length="22" nullable="false" />
<member name="avg2value" field="Avg2Value" value-type="double" length="22" nullable="false" />
<member name="sumvalue" field="SumValue" value-type="double" length="22" nullable="false" />
<member name="sum2value" field="Sum2Value" value-type="double" length="22" nullable="false" />
<readsets>
<readset name="DOMAIN">
<member name="domain"/>
</readset>
</readsets>
<query-defs>
<query name="find-all-by-domain-and-date" type="SELECT" multiple="true">
<param name="domain" />
......
......@@ -22,8 +22,10 @@ public class SqlJobDataProduceTest extends CatTestCase {
MessageStorage storage = lookup(MessageStorage.class, "hdfs");
MessageProducer producer = lookup(MessageProducer.class);
InMemoryQueue queue = lookup(InMemoryQueue.class);
long currentHour = System.currentTimeMillis() - System.currentTimeMillis() / (60 * 60 * 1000);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 10000; j++) {
for (int j = 0; j < 1200; j++) {
Transaction t = producer.newTransaction("URL", "MyPage" + (int) (j / 500));
try {
......@@ -39,7 +41,7 @@ public class SqlJobDataProduceTest extends CatTestCase {
producer.logEvent("URL", "Payload", Message.SUCCESS, "host=my-host&ip=127.0.0.1&agent=...");
producer.logEvent("URL", "Payload", Message.SUCCESS, "host=my-host&ip=127.0.0.1&agent=...");
String sqlName = "SQLStatement" + j / 500;
String sqlName = "Project.insert" + j / 500;
String sqlParaMeter = "SQLParaMeter" + j / 500;
String sqlStatement = "select * from table where id=\"1\"\n order by id desc";
Transaction sqlTran = producer.newTransaction("SQL", sqlName);
......@@ -48,34 +50,20 @@ public class SqlJobDataProduceTest extends CatTestCase {
Stringizers.forJson().compact().from(sqlParaMeter));
sqlTran.addData(sqlStatement);
String sqlInternalName = "SQLStatement Internal" + j / 500;
String sqlParaInternal = "SQLParaMeter Internal" + j / 500;
String sqlInternal = "select * from intenal table where id=\"1\"\n order by id desc";
Transaction internal = producer.newTransaction("SQL", sqlInternalName);
producer.logEvent("SQL.PARAM", sqlParaInternal, Transaction.SUCCESS, Stringizers.forJson().compact()
.from(sqlParaInternal));
internal.addData(sqlInternal);
internal.complete();
if (j % 2 == 1) {
internal.setStatus(Message.SUCCESS);
} else {
internal.setStatus("Error");
}
sqlTran.complete();
DefaultTransaction sqlInternalTran = (DefaultTransaction) internal;
sqlInternalTran.setDuration(j % 100 + 100);
DefaultTransaction sqlInternalTran = (DefaultTransaction) sqlTran;
sqlInternalTran.setDuration((long)Math.pow(2, j % 12));
if (j % 2 == 1) {
sqlTran.setStatus(Message.SUCCESS);
} else {
sqlTran.setStatus("Error");
}
sqlInternalTran.setTimestamp(currentHour + (j % 60) * 1000 * 60);
DefaultTransaction def = (DefaultTransaction) sqlTran;
def.setDuration(j % 100 + 50);
def.setTimestamp(currentHour + (j % 60) * 1000 * 60);
t.setStatus(Message.SUCCESS);
} catch (Exception e) {
t.setStatus(e);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册