Search in sources :

Example 1 with AnalyticsException

use of io.gravitee.repository.analytics.AnalyticsException in project gravitee-management-rest-api by gravitee-io.

the class AnalyticsServiceImpl method execute.

@Override
public TopHitsAnalytics execute(GroupByQuery query) {
    try {
        GroupByQueryBuilder queryBuilder = QueryBuilders.groupBy().query(query.getQuery()).timeRange(DateRangeBuilder.between(query.getFrom(), query.getTo()), IntervalBuilder.interval(query.getInterval())).root(query.getRootField(), query.getRootIdentifier()).field(query.getField());
        if (query.getGroups() != null) {
            query.getGroups().forEach(queryBuilder::range);
        }
        if (query.getOrder() != null) {
            GroupByQuery.Order order = query.getOrder();
            queryBuilder.sort(SortBuilder.on(order.getField(), order.isOrder() ? Order.ASC : Order.DESC, (order.getType() == null) ? SortType.AVG : SortType.valueOf(order.getType().toUpperCase())));
        }
        GroupByResponse response = analyticsRepository.query(queryBuilder.build());
        return convert(response);
    } catch (AnalyticsException ae) {
        logger.error("Unable to calculate analytics: ", ae);
        throw new TechnicalManagementException("Unable to calculate analytics", ae);
    }
}
Also used : GroupByQuery(io.gravitee.management.model.analytics.query.GroupByQuery) GroupByResponse(io.gravitee.repository.analytics.query.groupby.GroupByResponse) GroupByQueryBuilder(io.gravitee.repository.analytics.query.groupby.GroupByQueryBuilder) AnalyticsException(io.gravitee.repository.analytics.AnalyticsException)

Example 2 with AnalyticsException

use of io.gravitee.repository.analytics.AnalyticsException in project gravitee-management-rest-api by gravitee-io.

the class AnalyticsServiceImpl method execute.

@Override
public HistogramAnalytics execute(DateHistogramQuery query) {
    try {
        DateHistogramQueryBuilder queryBuilder = QueryBuilders.dateHistogram().query(query.getQuery()).timeRange(DateRangeBuilder.between(query.getFrom(), query.getTo()), IntervalBuilder.interval(query.getInterval())).root(query.getRootField(), query.getRootIdentifier());
        if (query.getAggregations() != null) {
            query.getAggregations().stream().forEach(aggregation -> queryBuilder.aggregation(AggregationType.valueOf(aggregation.type().name()), aggregation.field()));
        }
        DateHistogramResponse response = analyticsRepository.query(queryBuilder.build());
        return convert(response);
    } catch (AnalyticsException ae) {
        logger.error("Unable to calculate analytics: ", ae);
        throw new TechnicalManagementException("Unable to calculate analytics", ae);
    }
}
Also used : AnalyticsException(io.gravitee.repository.analytics.AnalyticsException) DateHistogramResponse(io.gravitee.repository.analytics.query.response.histogram.DateHistogramResponse)

Example 3 with AnalyticsException

use of io.gravitee.repository.analytics.AnalyticsException in project gravitee-management-rest-api by gravitee-io.

the class LogsServiceImpl method findByApplication.

@Override
public SearchLogResponse findByApplication(String application, LogQuery query) {
    try {
        TabularResponse response = logRepository.query(QueryBuilders.tabular().page(query.getPage()).size(query.getSize()).query(query.getQuery()).timeRange(DateRangeBuilder.between(query.getFrom(), query.getTo()), IntervalBuilder.interval(query.getInterval())).root("application", application).build());
        SearchLogResponse<ApplicationRequestItem> logResponse = new SearchLogResponse<>(response.getSize());
        // Transform repository logs
        logResponse.setLogs(response.getLogs().stream().map(this::toApplicationRequestItem).collect(Collectors.toList()));
        // Add metadata (only if they are results)
        if (response.getSize() > 0) {
            Map<String, Map<String, String>> metadata = new HashMap<>();
            logResponse.getLogs().forEach(logItem -> {
                String api = logItem.getApi();
                String plan = logItem.getPlan();
                if (api != null) {
                    metadata.computeIfAbsent(api, getAPIMetadata(api));
                }
                if (plan != null) {
                    metadata.computeIfAbsent(plan, getPlanMetadata(plan));
                }
            });
            logResponse.setMetadata(metadata);
        }
        return logResponse;
    } catch (AnalyticsException ae) {
        logger.error("Unable to retrieve logs: ", ae);
        throw new TechnicalManagementException("Unable to retrieve logs", ae);
    }
}
Also used : TabularResponse(io.gravitee.repository.analytics.query.tabular.TabularResponse) HashMap(java.util.HashMap) AnalyticsException(io.gravitee.repository.analytics.AnalyticsException) HashMap(java.util.HashMap) Map(java.util.Map) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 4 with AnalyticsException

use of io.gravitee.repository.analytics.AnalyticsException in project gravitee-management-rest-api by gravitee-io.

the class LogsServiceImpl method findByApi.

@Override
public SearchLogResponse findByApi(String api, LogQuery query) {
    try {
        TabularResponse response = logRepository.query(QueryBuilders.tabular().page(query.getPage()).size(query.getSize()).query(query.getQuery()).timeRange(DateRangeBuilder.between(query.getFrom(), query.getTo()), IntervalBuilder.interval(query.getInterval())).root("api", api).build());
        SearchLogResponse<ApiRequestItem> logResponse = new SearchLogResponse<>(response.getSize());
        // Transform repository logs
        logResponse.setLogs(response.getLogs().stream().map(this::toApiRequestItem).collect(Collectors.toList()));
        // Add metadata (only if they are results)
        if (response.getSize() > 0) {
            Map<String, Map<String, String>> metadata = new HashMap<>();
            logResponse.getLogs().forEach(logItem -> {
                String application = logItem.getApplication();
                String plan = logItem.getPlan();
                if (application != null) {
                    metadata.computeIfAbsent(application, getApplicationMetadata(application));
                }
                if (plan != null) {
                    metadata.computeIfAbsent(plan, getPlanMetadata(plan));
                }
            });
            logResponse.setMetadata(metadata);
        }
        return logResponse;
    } catch (AnalyticsException ae) {
        logger.error("Unable to retrieve logs: ", ae);
        throw new TechnicalManagementException("Unable to retrieve logs", ae);
    }
}
Also used : TabularResponse(io.gravitee.repository.analytics.query.tabular.TabularResponse) HashMap(java.util.HashMap) AnalyticsException(io.gravitee.repository.analytics.AnalyticsException) HashMap(java.util.HashMap) Map(java.util.Map) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Aggregations

AnalyticsException (io.gravitee.repository.analytics.AnalyticsException)4 TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)2 TabularResponse (io.gravitee.repository.analytics.query.tabular.TabularResponse)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 GroupByQuery (io.gravitee.management.model.analytics.query.GroupByQuery)1 GroupByQueryBuilder (io.gravitee.repository.analytics.query.groupby.GroupByQueryBuilder)1 GroupByResponse (io.gravitee.repository.analytics.query.groupby.GroupByResponse)1 DateHistogramResponse (io.gravitee.repository.analytics.query.response.histogram.DateHistogramResponse)1