use of datawave.microservice.querymetric.BaseQueryMetric.PageMetric in project datawave-query-metric-service by NationalSecurityAgency.
the class ShardTableQueryMetricHandler method toMetric.
public T toMetric(EventBase event) {
SimpleDateFormat sdf_date_time1 = new SimpleDateFormat("yyyyMMdd HHmmss");
SimpleDateFormat sdf_date_time2 = new SimpleDateFormat("yyyyMMdd HHmmss");
SimpleDateFormat sdf_date_time3 = new SimpleDateFormat("yyyyMMdd");
List<String> excludedFields = Arrays.asList("ELAPSED_TIME", "RECORD_ID", "NUM_PAGES", "NUM_RESULTS");
try {
T m = (T) metricFactory.createMetric();
List<FieldBase> field = event.getFields();
m.setMarkings(event.getMarkings());
TreeMap<Long, PageMetric> pageMetrics = Maps.newTreeMap();
boolean createDateSet = false;
for (FieldBase f : field) {
String fieldName = f.getName();
String fieldValue = f.getValueString();
if (!excludedFields.contains(fieldName)) {
if (fieldName.equals("AUTHORIZATIONS")) {
m.setQueryAuthorizations(fieldValue);
} else if (fieldName.equals("BEGIN_DATE")) {
try {
Date d = sdf_date_time1.parse(fieldValue);
m.setBeginDate(d);
} catch (Exception e) {
log.error(e.getMessage());
}
} else if (fieldName.equals("CREATE_CALL_TIME")) {
m.setCreateCallTime(Long.parseLong(fieldValue));
} else if (fieldName.equals("CREATE_DATE")) {
try {
Date d = sdf_date_time2.parse(fieldValue);
m.setCreateDate(d);
createDateSet = true;
} catch (Exception e) {
log.error(e.getMessage());
}
} else if (fieldName.equals("DOC_RANGES")) {
m.setDocRanges(Long.parseLong(fieldValue));
} else if (fieldName.equals("END_DATE")) {
try {
Date d = sdf_date_time1.parse(fieldValue);
m.setEndDate(d);
} catch (Exception e) {
log.error(e.getMessage());
}
} else if (fieldName.equals("ERROR_CODE")) {
m.setErrorCode(fieldValue);
} else if (fieldName.equals("ERROR_MESSAGE")) {
m.setErrorMessage(fieldValue);
} else if (fieldName.equals("FI_RANGES")) {
m.setFiRanges(Long.parseLong(fieldValue));
} else if (fieldName.equals("HOST")) {
m.setHost(fieldValue);
} else if (fieldName.equals("LAST_UPDATED")) {
try {
Date d = sdf_date_time2.parse(fieldValue);
m.setLastUpdated(d);
} catch (Exception e) {
log.error(e.getMessage());
}
} else if (fieldName.equals("LIFECYCLE")) {
m.setLifecycle(Lifecycle.valueOf(fieldValue));
} else if (fieldName.equals("LOGIN_TIME")) {
m.setLoginTime(Long.parseLong(fieldValue));
} else if (fieldName.equals("NEGATIVE_SELECTORS")) {
List<String> negativeSelectors = m.getNegativeSelectors();
if (negativeSelectors == null) {
negativeSelectors = new ArrayList<>();
}
negativeSelectors.add(fieldValue);
m.setNegativeSelectors(negativeSelectors);
} else if (fieldName.equals("NEXT_COUNT")) {
m.setNextCount(Long.parseLong(fieldValue));
} else if (fieldName.equals("NUM_UPDATES")) {
try {
long numUpdates = Long.parseLong(fieldValue);
m.setNumUpdates(numUpdates);
} catch (Exception e) {
log.error(e.getMessage());
}
} else if (fieldName.startsWith("PAGE_METRICS")) {
int index = fieldName.indexOf(".");
if (-1 == index) {
log.error("Could not parse field name to extract repetition count: " + fieldName);
} else {
Long pageNum = Long.parseLong(fieldName.substring(index + 1));
PageMetric pageMetric = PageMetric.parse(fieldValue);
if (pageMetric != null) {
pageMetric.setPageNumber(pageNum);
pageMetrics.put(pageNum, pageMetric);
}
}
} else if (fieldName.equals("PARAMETERS")) {
if (fieldValue != null) {
try {
m.setParameters(QueryUtil.parseParameters(fieldValue));
} catch (Exception e) {
log.error(e.getMessage());
}
}
} else if (fieldName.equals("PLAN")) {
m.setPlan(fieldValue);
} else if (fieldName.equals("POSITIVE_SELECTORS")) {
List<String> positiveSelectors = m.getPositiveSelectors();
if (positiveSelectors == null) {
positiveSelectors = new ArrayList<>();
}
positiveSelectors.add(fieldValue);
m.setPositiveSelectors(positiveSelectors);
} else if (fieldName.equals("PREDICTION")) {
if (fieldValue != null) {
try {
int x = fieldValue.indexOf(":");
if (x > -1) {
String predictionName = fieldValue.substring(0, x);
Double predictionValue = Double.parseDouble(fieldValue.substring(x + 1));
m.addPrediction(new Prediction(predictionName, predictionValue));
}
} catch (Exception e) {
log.error(e.getMessage());
}
}
} else if (fieldName.equals("PROXY_SERVERS")) {
m.setProxyServers(Arrays.asList(StringUtils.split(fieldValue, ",")));
} else if (fieldName.equals("QUERY")) {
m.setQuery(fieldValue);
} else if (fieldName.equals("QUERY_ID")) {
m.setQueryId(fieldValue);
} else if (fieldName.equals("QUERY_LOGIC")) {
m.setQueryLogic(fieldValue);
} else if (fieldName.equals("QUERY_NAME")) {
m.setQueryName(fieldValue);
} else if (fieldName.equals("QUERY_TYPE")) {
m.setQueryType(fieldValue);
} else if (fieldName.equals("SEEK_COUNT")) {
m.setSeekCount(Long.parseLong(fieldValue));
} else if (fieldName.equals("SETUP_TIME")) {
m.setSetupTime(Long.parseLong(fieldValue));
} else if (fieldName.equals("SOURCE_COUNT")) {
m.setSourceCount(Long.parseLong(fieldValue));
} else if (fieldName.equals("USER")) {
m.setUser(fieldValue);
} else if (fieldName.equals("USER_DN")) {
m.setUserDN(fieldValue);
} else if (fieldName.equals("VERSION")) {
m.setVersion(fieldValue);
} else if (fieldName.equals("YIELD_COUNT")) {
m.setYieldCount(Long.parseLong(fieldValue));
} else {
log.debug("encountered unanticipated field name: " + fieldName);
}
}
}
// if createDate has not been set, try to parse it from the event row
if (!createDateSet) {
try {
String dateStr = event.getMetadata().getRow().substring(0, 8);
m.setCreateDate(sdf_date_time3.parse(dateStr));
} catch (ParseException e) {
}
}
m.setPageTimes(new ArrayList<>(pageMetrics.values()));
return m;
} catch (RuntimeException e) {
return null;
}
}
use of datawave.microservice.querymetric.BaseQueryMetric.PageMetric in project datawave-query-metric-service by NationalSecurityAgency.
the class BaseQueryMetricListResponse method getMainContent.
@Override
public String getMainContent() {
StringBuilder builder = new StringBuilder();
builder.append("<table>\n");
builder.append("<tr>\n");
builder.append("<th>Visibility</th><th>Query Date</th><th>User</th><th>UserDN</th><th>Proxy Server(s)</th><th>Query ID</th><th>Query Type</th>");
builder.append("<th>Query Logic</th><th id=\"query-header\">Query</th><th>Begin Date</th><th>End Date</th><th>Query Auths</th><th>Server</th>");
builder.append("<th>Query Setup Time (ms)</th><th>Query Setup Call Time (ms)</th><th>Number Pages</th><th>Number Results</th>");
builder.append("<th>Total Page Time (ms)</th><th>Total Page Call Time (ms)</th><th>Total Page Serialization Time (ms)</th>");
builder.append("<th>Total Page Bytes Sent (uncompressed)</th><th>Lifecycle</th><th>Elapsed Time</th><th>Error Code</th><th>Error Message</th>");
builder.append("\n</tr>\n");
TreeMap<Date, T> metricMap = new TreeMap<Date, T>(Collections.reverseOrder());
for (T metric : this.getResult()) {
metricMap.put(metric.getCreateDate(), metric);
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HHmmss");
int x = 0;
for (T metric : metricMap.values()) {
// highlight alternating rows
if (x % 2 == 0) {
builder.append("<tr class=\"highlight\" style=\"vertical-align:top;\">\n");
} else {
builder.append("<tr style=\"vertical-align:top;\">\n");
}
x++;
builder.append("<td>").append(metric.getColumnVisibility()).append("</td>");
builder.append("<td style=\"min-width:125px !important;\">").append(sdf.format(metric.getCreateDate())).append("</td>");
builder.append("<td>").append(metric.getUser()).append("</td>");
String userDN = metric.getUserDN();
builder.append("<td style=\"min-width:500px !important;\">").append(userDN == null ? "" : userDN).append("</td>");
String proxyServers = metric.getProxyServers() == null ? "" : StringUtils.join(metric.getProxyServers(), "<BR/>");
builder.append("<td>").append(proxyServers).append("</td>");
if (this.isAdministratorMode()) {
builder.append("<td><a href=\"/DataWave/Query/Metrics/user/").append(metric.getUser()).append("/").append(metric.getQueryId()).append("/").append("\">").append(metric.getQueryId()).append("</a></td>");
} else {
builder.append("<td><a href=\"/DataWave/Query/Metrics/id/").append(metric.getQueryId()).append("/").append("\">").append(metric.getQueryId()).append("</a></td>");
}
builder.append("<td>").append(metric.getQueryType()).append("</td>");
builder.append("<td>").append(metric.getQueryLogic()).append("</td>");
builder.append("<td style=\"word-wrap: break-word;\">").append(StringEscapeUtils.escapeHtml4(metric.getQuery())).append("</td>");
String beginDate = metric.getBeginDate() == null ? "" : sdf.format(metric.getBeginDate());
builder.append("<td style=\"min-width:125px !important;\">").append(beginDate).append("</td>");
String endDate = metric.getEndDate() == null ? "" : sdf.format(metric.getEndDate());
builder.append("<td style=\"min-width:125px !important;\">").append(endDate).append("</td>");
String queryAuths = metric.getQueryAuthorizations() == null ? "" : metric.getQueryAuthorizations().replaceAll(",", " ");
builder.append("<td style=\"word-wrap: break-word; min-width:300px !important;\">").append(queryAuths).append("</td>");
builder.append("<td>").append(metric.getHost()).append("</td>");
builder.append("<td>").append(metric.getSetupTime()).append("</td>");
builder.append("<td>").append(numToString(metric.getCreateCallTime())).append("</td>\n");
builder.append("<td>").append(metric.getNumPages()).append("</td>");
builder.append("<td>").append(metric.getNumResults()).append("</td>");
long count = 0l;
long callTime = 0l;
long serializationTime = 0l;
long bytesSent = 0l;
for (PageMetric p : metric.getPageTimes()) {
count += p.getReturnTime();
callTime += (p.getCallTime()) == -1 ? 0 : p.getCallTime();
serializationTime += (p.getSerializationTime()) == -1 ? 0 : p.getSerializationTime();
bytesSent += (p.getBytesWritten()) == -1 ? 0 : p.getBytesWritten();
}
builder.append("<td>").append(count).append("</td>\n");
builder.append("<td>").append(numToString(callTime)).append("</td>\n");
builder.append("<td>").append(numToString(serializationTime)).append("</td>\n");
builder.append("<td>").append(numToString(bytesSent)).append("</td>\n");
builder.append("<td>").append(metric.getLifecycle()).append("</td>");
builder.append("<td>").append(metric.getElapsedTime()).append("</td>");
String errorCode = metric.getErrorCode();
builder.append("<td style=\"word-wrap: break-word;\">").append((errorCode == null) ? "" : StringEscapeUtils.escapeHtml4(errorCode)).append("</td>");
String errorMessage = metric.getErrorMessage();
builder.append("<td style=\"word-wrap: break-word;\">").append((errorMessage == null) ? "" : StringEscapeUtils.escapeHtml4(errorMessage)).append("</td>");
builder.append("\n</tr>\n");
}
builder.append("</table>\n");
return builder.toString();
}
Aggregations