Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Donald Oakes committed Jun 8, 2019
1 parent 49693dd commit da13805
Show file tree
Hide file tree
Showing 21 changed files with 187 additions and 159 deletions.
4 changes: 4 additions & 0 deletions mdw-common/src/com/centurylink/mdw/common/service/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ public static String getString(Date date) {
}
}

public static String getString(Instant instant) {
return instant == null ? null : instant.toString();
}

/**
* Builds a path/query string for an endpoint.
* Don't change this without extensive regression testing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.*;

public class CommonDataAccess {
Expand Down Expand Up @@ -1003,6 +1004,10 @@ protected static String getOracleDt(Date date) {
return new SimpleDateFormat(ORACLE_DT_FORMAT).format(date);
}

protected String getDbDt(Instant instant) {
return getDt(Date.from(instant.plusMillis(DatabaseAccess.getDbTimeDiff())));
}

protected String getDt(Date date) {
return db.isMySQL() ? getMySqlDt(date) : getOracleDt(date);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
import com.centurylink.mdw.model.workflow.WorkStatuses;

import java.sql.SQLException;
import java.text.ParseException;
import java.util.*;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TreeMap;

public class ActivityAggregation extends AggregateDataAccess<ActivityAggregate> {

Expand All @@ -28,7 +31,7 @@ else if (by.equals("status"))
else
throw new ServiceException(ServiceException.BAD_REQUEST, "Unsupported filter: by=" + by);
}
catch (SQLException | ParseException ex) {
catch (SQLException ex) {
throw new DataAccessException(ex.getMessage(), ex);
}
finally {
Expand All @@ -37,7 +40,7 @@ else if (by.equals("status"))
}

private List<ActivityAggregate> getTopsByStuck(Query query)
throws ParseException, DataAccessException, SQLException, ServiceException {
throws DataAccessException, SQLException, ServiceException {
PreparedWhere preparedWhere = getActivityWhere(query);
String sql = "select count(act_unique_id) as ct, act_unique_id\n" +
getUniqueIdFrom() +
Expand Down Expand Up @@ -66,7 +69,7 @@ private String getUniqueIdFrom() {
}

private List<ActivityAggregate> getTopsByStatus(Query query)
throws ParseException, DataAccessException, SQLException, ServiceException {
throws DataAccessException, SQLException, ServiceException {
PreparedWhere preparedWhere = getActivityWhere(query);
String sql = "select status_cd, count(status_cd) as ct " +
"from ACTIVITY_INSTANCE ai\n" +
Expand All @@ -83,7 +86,7 @@ private List<ActivityAggregate> getTopsByStatus(Query query)
});
}

public TreeMap<Date,List<ActivityAggregate>> getBreakdown(Query query) throws DataAccessException, ServiceException {
public TreeMap<Instant,List<ActivityAggregate>> getBreakdown(Query query) throws DataAccessException, ServiceException {
String by = query.getFilter("by");
if (by == null)
throw new ServiceException(ServiceException.BAD_REQUEST, "Missing required filter: 'by'");
Expand Down Expand Up @@ -143,10 +146,8 @@ else if (by.equals("total")) {
sql.append(", status_cd");
else if (by.equals("throughput"))
sql.append(", act_unique_id");
if (db.isMySQL())
sql.append("\norder by st");
else
sql.append("\norder by to_date(st, 'DD-Mon-yyyy')");

sql.append("\norder by st\n");

PreparedSelect select = new PreparedSelect(sql.toString(), params.toArray(), "Breakdown by " + by);

Expand Down Expand Up @@ -177,21 +178,21 @@ else if (!by.equals("total")) {
}
}

protected PreparedWhere getActivityWhere(Query query) throws ParseException, DataAccessException {
protected PreparedWhere getActivityWhere(Query query) throws DataAccessException {
String by = query.getFilter("by");
Date start = getStartDate(query);
Instant start = getStart(query);

StringBuilder where = new StringBuilder();
List<Object> params = new ArrayList<>();
if (by.equals("throughput"))
where.append(" where ai.process_instance_id = pi.process_instance_id ");
where.append(where.length() > 0 ? " and " : "where ");
where.append("ai.start_dt >= ? ");
params.add(getDt(start));
Date end = getEndDate(query);
params.add(getDbDt(start));
Instant end = getEnd(query);
if (end != null) {
where.append(" and ai.start_dt <= ?");
params.add(getDt(end));
params.add(getDbDt(end));
}

String status = query.getFilter("Status");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.temporal.TemporalField;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TreeMap;

Expand All @@ -46,27 +46,20 @@ public abstract class AggregateDataAccess<T extends Aggregate> extends CommonDat
* Return the data according to the user-selected values from getTops()
* @return TreeMap where the Date keys are sorted according to natural ordering.
*/
abstract TreeMap<Date,List<T>> getBreakdown(Query query) throws DataAccessException, ServiceException;
abstract TreeMap<Instant,List<T>> getBreakdown(Query query) throws DataAccessException, ServiceException;

protected Date getStartDate(Query query) throws ParseException, DataAccessException {
Instant instant = query.getInstantFilter("Starting");
Date start = instant == null ? query.getDateFilter("startDate") : Date.from(instant);
protected Instant getStart(Query query) throws DataAccessException {
Instant start = query.getInstantFilter("Starting");
if (start == null)
throw new DataAccessException("Parameter Starting is required");
// adjust to db time
return new Date(start.getTime() + DatabaseAccess.getDbTimeDiff());
return start;
}

/**
* This is not completion date. It's ending start date.
*/
protected Date getEndDate(Query query) {
Instant instant = query.getInstantFilter("Ending");
if (instant == null)
return null;
else {
return new Date(Date.from(instant).getTime() + DatabaseAccess.getDbTimeDiff());
}
protected Instant getEnd(Query query) {
return query.getInstantFilter("Ending");
}

protected TimeIncrement getIncrement(Query query) {
Expand Down Expand Up @@ -114,25 +107,25 @@ protected List<T> getTopAggregates(PreparedSelect select, Query query, Aggregate
* Relies on convention that resultSet start date column name is "st", and value
* column is identified as "val".
*/
protected TreeMap<Date,List<T>> handleBreakdownResult(PreparedSelect select, Query query, AggregateSupplier supplier)
protected TreeMap<Instant,List<T>> handleBreakdownResult(PreparedSelect select, Query query, AggregateSupplier supplier)
throws DataAccessException, SQLException, ParseException {
try {
db.openConnection();
ResultSet resultSet = db.runSelect(select);

TreeMap<Date,List<T>> map = new TreeMap<>();
Date start = getStartDate(query);
TreeMap<Instant,List<T>> map = new TreeMap<>();
Instant start = getStart(query);
TimeIncrement increment = getIncrement(query);

Date prevStartDate = start;
Instant prevStartDate = start;
while (resultSet.next()) {
String startDateStr = resultSet.getString("st");
Date startDate = parseSt(startDateStr, increment);
Instant startDate = parseDbSt(startDateStr, increment);

// fill in gaps
while (startDate.getTime() - prevStartDate.getTime() > increment.ms) {
prevStartDate = new Date(prevStartDate.getTime() + increment.ms);
map.put(getRoundDate(prevStartDate, increment), new ArrayList<>());
while (startDate.toEpochMilli() - prevStartDate.toEpochMilli() > increment.ms) {
prevStartDate = Instant.ofEpochMilli(prevStartDate.toEpochMilli() + increment.ms);
map.put(prevStartDate, new ArrayList<>());
}
List<T> aggregates = map.get(startDate);
if (aggregates == null) {
Expand All @@ -145,14 +138,13 @@ protected TreeMap<Date,List<T>> handleBreakdownResult(PreparedSelect select, Que
prevStartDate = startDate;
}
// missing start date
Date roundStartDate = getRoundDate(start, increment);
if (map.get(roundStartDate) == null)
map.put(roundStartDate, new ArrayList<>());
if (map.get(start) == null)
map.put(start, new ArrayList<>());
// gaps at end
Date end = getEndDate(query);
while ((end != null) && ((end.getTime() - prevStartDate.getTime()) > increment.ms)) {
prevStartDate = new Date(prevStartDate.getTime() + increment.ms);
map.put(getRoundDate(prevStartDate, increment), new ArrayList<>());
Instant end = getEnd(query);
while ((end != null) && ((end.toEpochMilli() - prevStartDate.toEpochMilli()) > increment.ms)) {
prevStartDate = Instant.ofEpochMilli(prevStartDate.toEpochMilli() + increment.ms);
map.put(prevStartDate, new ArrayList<>());
}

return map;
Expand All @@ -162,27 +154,35 @@ protected TreeMap<Date,List<T>> handleBreakdownResult(PreparedSelect select, Que
}
}

protected Date parseSt(String st, TimeIncrement increment) throws ParseException {
/**
* Converts back to server time from db time.
*/
protected Instant parseDbSt(String st, TimeIncrement increment) throws ParseException {
if (increment == TimeIncrement.minute || increment == TimeIncrement.hour) {
return new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(st);
return new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(st).toInstant();
}
else {
return new SimpleDateFormat("yyyy-MM-dd").parse(st);
return new SimpleDateFormat("yyyy-MM-dd").parse(st).toInstant();
}
}

/**
* Start datetime to select, adjusted for discrepancy between db time and server time.
*/
protected String getSt(String col, Query query) {
TimeIncrement increment = getIncrement(query);
long hoursDiff = DatabaseAccess.getDbTimeDiff() / 3600000;
if (db.isOracle()) {
if (increment == TimeIncrement.minute) {
return "to_char(" + col + ",'YYYY-MM-DD HH24:MI') as st";
return "to_char(" + col + " - interval '" + hoursDiff + "' hour,'YYYY-MM-DD HH24:MI') as st";
}
else if (increment == TimeIncrement.hour) {
return "to_char(" + col + ",'YYYY-MM-DD HH24:00') as st";
return "to_char(" + col + " - interval '" + hoursDiff + "' hour,'YYYY-MM-DD HH24:\"00\"') as st";

}
else {
return "to_char(" + col + ",'YYYY-MM-DD') as st";

return "to_char(" + col + " - interval '" + hoursDiff + "' hour,'YYYY-MM-DD') as st";
}
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
import com.centurylink.mdw.model.workflow.WorkStatuses;

import java.sql.SQLException;
import java.text.ParseException;
import java.util.*;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TreeMap;

public class ProcessAggregation extends AggregateDataAccess<ProcessAggregate> {

Expand All @@ -29,7 +32,7 @@ else if (by.equals("completionTime"))
else
throw new ServiceException(ServiceException.BAD_REQUEST, "Unsupported filter: by=" + by);
}
catch (SQLException | ParseException ex) {
catch (SQLException ex) {
throw new DataAccessException(ex.getMessage(), ex);
}
finally {
Expand All @@ -38,7 +41,7 @@ else if (by.equals("completionTime"))
}

private List<ProcessAggregate> getTopsByThroughput(Query query)
throws ParseException, DataAccessException, SQLException, ServiceException {
throws DataAccessException, SQLException, ServiceException {
PreparedWhere preparedWhere = getProcessWhere(query);
String sql = db.pagingQueryPrefix() +
"select process_id, count(process_id) as ct\n" +
Expand All @@ -59,7 +62,7 @@ private List<ProcessAggregate> getTopsByThroughput(Query query)
}

private List<ProcessAggregate> getTopsByStatus(Query query)
throws ParseException, DataAccessException, SQLException, ServiceException {
throws DataAccessException, SQLException, ServiceException {
PreparedWhere preparedWhere = getProcessWhere(query);
String sql = db.pagingQueryPrefix() +
"select status_cd, count(status_cd) as ct from PROCESS_INSTANCE\n" +
Expand All @@ -79,7 +82,7 @@ private List<ProcessAggregate> getTopsByStatus(Query query)
}

private List<ProcessAggregate> getTopsByCompletionTime(Query query)
throws ParseException, DataAccessException, SQLException, ServiceException {
throws DataAccessException, SQLException, ServiceException {
PreparedWhere preparedWhere = getProcessWhere(query);
String sql = db.pagingQueryPrefix() +
"select process_id, avg(elapsed_ms) as elapsed, count(process_id) as ct\n" +
Expand All @@ -100,7 +103,7 @@ private List<ProcessAggregate> getTopsByCompletionTime(Query query)
});
}

public TreeMap<Date,List<ProcessAggregate>> getBreakdown(Query query) throws DataAccessException, ServiceException {
public TreeMap<Instant,List<ProcessAggregate>> getBreakdown(Query query) throws DataAccessException, ServiceException {
String by = query.getFilter("by");
if (by == null)
throw new ServiceException(ServiceException.BAD_REQUEST, "Missing required filter: 'by'");
Expand Down Expand Up @@ -156,10 +159,8 @@ else if (!by.equals("total")) {
sql.append(", status_cd");
else if (!by.equals("total"))
sql.append(", process_id");
if (db.isMySQL())
sql.append("\norder by st\n");
else
sql.append("\norder by to_date(st, 'DD-Mon-yyyy')\n");

sql.append("\norder by st\n");

PreparedSelect select = new PreparedSelect(sql.toString(), params.toArray(), "Breakdown by " + by);
return handleBreakdownResult(select, query, rs -> {
Expand All @@ -183,9 +184,9 @@ else if (!by.equals("total")) {
}
}

protected PreparedWhere getProcessWhere(Query query) throws ParseException, DataAccessException {
protected PreparedWhere getProcessWhere(Query query) throws DataAccessException {
String by = query.getFilter("by");
Date start = getStartDate(query);
Instant start = getStart(query);

StringBuilder where = new StringBuilder();
List<Object> params = new ArrayList<>();
Expand All @@ -197,12 +198,12 @@ protected PreparedWhere getProcessWhere(Query query) throws ParseException, Data
where.append(where.length() > 0 ? " and " : "where ");

where.append("start_dt >= ?\n");
params.add(getDt(start));
params.add(getDbDt(start));

Date end = getEndDate(query);
Instant end = getEnd(query);
if (end != null) {
where.append(" and start_dt <= ?\n");
params.add(getDt(end));
params.add(getDbDt(end));
}

where.append(" and owner not in (?");
Expand Down
Loading

0 comments on commit da13805

Please sign in to comment.