com.centurylink.mdw.dataaccess.reports.RequestInsights Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mdw-common Show documentation
Show all versions of mdw-common Show documentation
MDW is a workflow framework specializing in microservice orchestration
package com.centurylink.mdw.dataaccess.reports;
import com.centurylink.mdw.common.service.Query;
import com.centurylink.mdw.common.service.ServiceException;
import com.centurylink.mdw.constant.OwnerType;
import com.centurylink.mdw.dataaccess.DatabaseAccess;
import com.centurylink.mdw.dataaccess.PreparedWhere;
import com.centurylink.mdw.dataaccess.db.CommonDataAccess;
import com.centurylink.mdw.model.report.Insight;
import com.centurylink.mdw.model.report.Timepoint;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
public class RequestInsights extends CommonDataAccess {
public List getInsights(Query query) throws SQLException, ParseException, ServiceException {
PreparedWhere where = getWhere(query, false);
String sql = "select count(req.status_code) as ct, req.st, req.status_code\n" +
"from (" + getSelectDate("create_dt") + " as st, status_code \n" +
" from DOCUMENT\n" + where.getWhere() + ") req\n" +
"group by st, status_code\n" +
"order by st";
try {
db.openConnection();
List insights = new ArrayList<>();
Date prevStart = getStartDate(query);
ResultSet rs = db.runSelect("Request insights ", sql, where.getParams());
while (rs.next()) {
String startStr = rs.getString("st");
Date start = getDateFormat().parse(startStr);
// fill in gaps
while (start.getTime() - prevStart.getTime() > Query.Timespan.Day.millis()) {
prevStart = new Date(prevStart.getTime() + Query.Timespan.Day.millis());
insights.add(new Insight(getRoundDate(prevStart).toInstant(), new LinkedHashMap<>()));
}
Insight insight = insights.stream().filter(in -> in.getTime().equals(start.toInstant())).findAny().orElse(null);
if (insight == null) {
LinkedHashMap map = new LinkedHashMap<>();
insight = new Insight(getRoundDate(start).toInstant(), map);
insights.add(insight);
}
String status = String.valueOf(rs.getInt("status_code"));
insight.getElements().put(status, rs.getInt("ct"));
prevStart = start;
}
// gaps at end
Date endDate = new Date(System.currentTimeMillis() + DatabaseAccess.getDbTimeDiff());
while ((endDate.getTime() - prevStart.getTime()) > Query.Timespan.Day.millis()) {
prevStart = new Date(prevStart.getTime() + Query.Timespan.Day.millis());
insights.add(new Insight(getRoundDate(prevStart).toInstant(), new LinkedHashMap<>()));
}
return insights;
}
finally {
db.closeConnection();
}
}
public List getTrend(Query query) throws SQLException, ParseException, ServiceException {
PreparedWhere where = getWhere(query, true);
String sql = "select avg(req.elapsed_ms) as ms, req.st\n" +
"from (" + getSelectDate("create_dt") + " as st, elapsed_ms\n" +
" from DOCUMENT, INSTANCE_TIMING\n" + where.getWhere() + ") req\n" +
"group by st\n" +
"order by st";
try {
db.openConnection();
List timepoints = new ArrayList<>();
ResultSet rs = db.runSelect("Request trend ", sql, where.getParams());
while (rs.next()) {
String startStr = rs.getString("st");
Date start = getDateFormat().parse(startStr);
timepoints.add(new Timepoint(getRoundDate(start).toInstant(), Math.round(rs.getDouble("ms"))));
}
return timepoints;
}
finally {
db.closeConnection();
}
}
private PreparedWhere getWhere(Query query, boolean isTrend) throws ServiceException {
StringBuilder where = new StringBuilder();
List