use of datawave.webservice.query.result.event.FieldBase in project datawave-query-metric-service by NationalSecurityAgency.
the class QueryMetricConsistencyTest method ToMetricTest.
@Test
public void ToMetricTest() {
ContentQueryMetricsIngestHelper.HelperDelegate<QueryMetric> helper = new ContentQueryMetricsIngestHelper.HelperDelegate<>();
QueryMetric queryMetric = (QueryMetric) createMetric();
Multimap<String, String> fieldsToWrite = helper.getEventFieldsToWrite(queryMetric);
EventBase event = new DefaultEvent();
long now = System.currentTimeMillis();
List<FieldBase> fields = new ArrayList<>();
fieldsToWrite.asMap().forEach((k, set) -> {
set.forEach(v -> {
fields.add(new DefaultField(k, "", now, v));
});
});
event.setFields(fields);
event.setMarkings(queryMetric.getMarkings());
BaseQueryMetric newMetric = shardTableQueryMetricHandler.toMetric(event);
QueryMetricTestBase.assertEquals("metrics are not equal", queryMetric, newMetric);
}
use of datawave.webservice.query.result.event.FieldBase 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;
}
}
Aggregations