Search in sources :

Example 31 with FieldBase

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);
}
Also used : EventBase(datawave.webservice.query.result.event.EventBase) DefaultEvent(datawave.webservice.query.result.event.DefaultEvent) ArrayList(java.util.ArrayList) ContentQueryMetricsIngestHelper(datawave.microservice.querymetric.handler.ContentQueryMetricsIngestHelper) FieldBase(datawave.webservice.query.result.event.FieldBase) DefaultField(datawave.webservice.query.result.event.DefaultField) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 32 with FieldBase

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;
    }
}
Also used : PageMetric(datawave.microservice.querymetric.BaseQueryMetric.PageMetric) Prediction(datawave.microservice.querymetric.BaseQueryMetric.Prediction) ArrayList(java.util.ArrayList) Date(java.util.Date) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) ParseException(java.text.ParseException) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException) TableExistsException(org.apache.accumulo.core.client.TableExistsException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) AccumuloException(org.apache.accumulo.core.client.AccumuloException) FieldBase(datawave.webservice.query.result.event.FieldBase) List(java.util.List) ArrayList(java.util.ArrayList) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

FieldBase (datawave.webservice.query.result.event.FieldBase)32 EventBase (datawave.webservice.query.result.event.EventBase)24 ArrayList (java.util.ArrayList)20 Metadata (datawave.webservice.query.result.event.Metadata)15 CacheableQueryRow (datawave.webservice.query.cachedresults.CacheableQueryRow)9 Map (java.util.Map)6 Exception (datawave.marking.MarkingFunctions.Exception)4 QueryException (datawave.webservice.query.exception.QueryException)4 ColumnVisibility (org.apache.accumulo.core.security.ColumnVisibility)4 QueryImpl (datawave.webservice.query.QueryImpl)3 CacheableQueryRowImpl (datawave.webservice.query.cachedresults.CacheableQueryRowImpl)3 EventQueryResponseBase (datawave.webservice.result.EventQueryResponseBase)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 TransformIterator (org.apache.commons.collections4.iterators.TransformIterator)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 GenericQueryConfiguration (datawave.webservice.query.configuration.GenericQueryConfiguration)2 DatawaveTransformIterator (datawave.webservice.query.iterator.DatawaveTransformIterator)2 BaseQueryResponse (datawave.webservice.result.BaseQueryResponse)2 DefaultEventQueryResponse (datawave.webservice.result.DefaultEventQueryResponse)2