<commit_before>/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gravitee.management.rest.resource;

import io.gravitee.common.http.MediaType;
import io.gravitee.management.model.analytics.Analytics;
import io.gravitee.management.model.analytics.query.*;
import io.gravitee.management.model.api.ApiEntity;
import io.gravitee.management.model.application.ApplicationListItem;
import io.gravitee.management.rest.resource.param.Aggregation;
import io.gravitee.management.rest.resource.param.AnalyticsParam;
import io.gravitee.management.rest.resource.param.Range;
import io.gravitee.management.rest.security.Permission;
import io.gravitee.management.rest.security.Permissions;
import io.gravitee.management.service.AnalyticsService;
import io.gravitee.management.service.ApiService;
import io.gravitee.management.service.ApplicationService;
import io.gravitee.management.service.PermissionService;
import io.swagger.annotations.Api;

import javax.inject.Inject;
import javax.ws.rs.BeanParam;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import static io.gravitee.management.model.permissions.RolePermission.*;
import static io.gravitee.management.model.permissions.RolePermissionAction.READ;

/**
 * @author <NAME> (titouan.compiegne at graviteesource.com)
 * @author <NAME> (nicolas.geraud at graviteesource.com)
 * @author GraviteeSource Team
 */
@Api(tags = {"Gateway"})
public class PlatformAnalyticsResource extends AbstractResource  {

    @Inject
    private AnalyticsService analyticsService;

    @Inject
    ApiService apiService;

    @Inject
    PermissionService permissionService;

    @Inject
    ApplicationService applicationService;
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Permissions({
            @Permission(value = MANAGEMENT_PLATFORM, acls = READ)
    })
    public Response platformAnalytics(@BeanParam AnalyticsParam analyticsParam) {

        analyticsParam.validate();

        Analytics analytics = null;

        // add filter by Apis or Applications
        String extraFilter = null;
        if (!isAdmin()) {
            String fieldName;
            List<String> ids;
            if ("application".equals(analyticsParam.getField())) {
                fieldName = "application";
                ids = applicationService.findByUser(getAuthenticatedUser())
                        .stream()
                        .filter(app -> permissionService.hasPermission(APPLICATION_ANALYTICS, app.getId(), READ))
                        .map(ApplicationListItem::getId)
                        .collect(Collectors.toList());
            } else {
                fieldName = "api";
                ids = apiService.findByUser(getAuthenticatedUser(), null)
                        .stream()
                        .filter(api -> permissionService.hasPermission(API_ANALYTICS, api.getId(), READ))
                        .map(ApiEntity::getId)
                        .collect(Collectors.toList());
            }

            if (ids.isEmpty()) {
                return Response.noContent().build();
            }
            extraFilter = getExtraFilter(fieldName, ids);
        }

        switch (analyticsParam.getTypeParam().getValue()) {
            case DATE_HISTO:
                analytics = executeDateHisto(analyticsParam, extraFilter);
                break;
            case GROUP_BY:
                analytics = executeGroupBy(analyticsParam, extraFilter);
                break;
            case COUNT:
                analytics = executeCount(analyticsParam, extraFilter);
                break;
            case STATS:
                analytics = executeStats(analyticsParam, extraFilter);
                break;
        }

        return Response.ok(analytics).build();
    }

    private Analytics executeStats(AnalyticsParam analyticsParam, String extraFilter) {
        final StatsQuery query = new StatsQuery();
        query.setFrom(analyticsParam.getFrom());
        query.setTo(analyticsParam.getTo());
        query.setInterval(analyticsParam.getInterval());
        query.setQuery(analyticsParam.getQuery());<commit_msg>fix: Log unknown path for unknown APIs<commit_after>/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gravitee.management.rest.resource;

import io.gravitee.common.http.MediaType;
import io.gravitee.management.model.analytics.Analytics;
import io.gravitee.management.model.analytics.query.*;
import io.gravitee.management.model.api.ApiEntity;
import io.gravitee.management.model.application.ApplicationListItem;
import io.gravitee.management.rest.resource.param.Aggregation;
import io.gravitee.management.rest.resource.param.AnalyticsParam;
import io.gravitee.management.rest.resource.param.Range;
import io.gravitee.management.rest.security.Permission;
import io.gravitee.management.rest.security.Permissions;
import io.gravitee.management.service.AnalyticsService;
import io.gravitee.management.service.ApiService;
import io.gravitee.management.service.ApplicationService;
import io.gravitee.management.service.PermissionService;
import io.swagger.annotations.Api;

import javax.inject.Inject;
import javax.ws.rs.BeanParam;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import static io.gravitee.management.model.permissions.RolePermission.*;
import static io.gravitee.management.model.permissions.RolePermissionAction.READ;

/**
 * @author <NAME> (titouan.compiegne at graviteesource.com)
 * @author <NAME> (nicolas.geraud at graviteesource.com)
 * @author GraviteeSource Team
 */
@Api(tags = {"Gateway"})
public class PlatformAnalyticsResource extends AbstractResource  {

    @Inject
    private AnalyticsService analyticsService;

    @Inject
    ApiService apiService;

    @Inject
    PermissionService permissionService;

    @Inject
    ApplicationService applicationService;
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Permissions({
            @Permission(value = MANAGEMENT_PLATFORM, acls = READ)
    })
    public Response platformAnalytics(@BeanParam AnalyticsParam analyticsParam) {

        analyticsParam.validate();

        Analytics analytics = null;

        // add filter by Apis or Applications
        String extraFilter = null;
        if (!isAdmin()) {
            String fieldName;
            List<String> ids;
            if ("application".equals(analyticsParam.getField())) {
                fieldName = "application";
                ids = applicationService.findByUser(getAuthenticatedUser())
                        .stream()
                        .filter(app -> permissionService.hasPermission(APPLICATION_ANALYTICS, app.getId(), READ))
                        .map(ApplicationListItem::getId)
                        .collect(Collectors.toList());
            } else {
                fieldName = "api";
                ids = apiService.findByUser(getAuthenticatedUser(), null)
                        .stream()
                        .filter(api -> permissionService.hasPermission(API_ANALYTICS, api.getId(), READ))
                        .map(ApiEntity::getId)
                        .collect(Collectors.toList());
            }

            if (ids.isEmpty()) {
                return Response.noContent().build();
            }
            extraFilter = getExtraFilter(fieldName, ids);
        }

        if (analyticsParam.getQuery() != null) {
            analyticsParam.setQuery(analyticsParam.getQuery().replaceAll("\\?", "1"));
        }

        switch (analyticsParam.getTypeParam().getValue()) {
            case DATE_HISTO:
                analytics = executeDateHisto(analyticsParam, extraFilter);
                break;
            case GROUP_BY:
                analytics = executeGroupBy(analyticsParam, extraFilter);
                break;
            case COUNT:
                analytics = executeCount(analyticsParam, extraFilter);
                break;
            case STATS:
                analytics = executeStats(analyticsParam, extraFilter);
                break;
        }

        return Response.ok(analytics).build();
    }

    private Analytics executeStats(AnalyticsParam analyticsParam, String extraFilter) {
        final StatsQuery query = new StatsQuery();
        query.setFrom(analyticsParam.getFrom());
        query.setTo(analyticsParam.getTo());
        query.setInterval(analyticsParam.getInterval());
        query.setQuery(analyticsParam.getQuery());
{'fraction_non_alphanumeric': 0.0980272523896685, 'fraction_numerical': 0.0017286963595688429, 'mean_word_length': 2.773983115886416, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1897060', 'n_tokens_mistral': 2638, 'n_tokens_neox': 2565, 'n_words': 574}