<commit_before>/*
 * Copyright 2017 Crown Copyright
 *
 * 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 stroom.resources.query.v1;

import com.codahale.metrics.annotation.Timed;
import com.codahale.metrics.health.HealthCheck;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import stroom.datasource.api.v1.DataSource;
import stroom.query.api.v1.DocRef;
import stroom.query.api.v1.QueryKey;
import stroom.query.api.v1.SearchRequest;
import stroom.query.api.v1.SearchResponse;
import stroom.resources.ResourcePaths;
import stroom.statistics.server.sql.StatisticsQueryService;
import stroom.util.json.JsonUtil;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path(ResourcePaths.SQL_STATISTICS + ResourcePaths.V1)
@Produces(MediaType.APPLICATION_JSON)
public class SqlStatisticsQueryResource implements QueryResource {

    private static final Logger LOGGER = LoggerFactory.getLogger(SqlStatisticsQueryResource.class);

    private StatisticsQueryService statisticsQueryService;

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path(QueryResource.DATA_SOURCE_ENDPOINT)
    @Timed
    public DataSource getDataSource(final DocRef docRef) {
        return statisticsQueryService.getDataSource(docRef);
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path(QueryResource.SEARCH_ENDPOINT)
    @Timed
    public SearchResponse search(final SearchRequest request) {

        if (LOGGER.isDebugEnabled()) {
            String json = JsonUtil.writeValueAsString(request);
            LOGGER.debug("/search called with searchRequest:\n{}", json);
        }

        SearchResponse response = statisticsQueryService.search(request);
        return response;
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path(QueryResource.DESTROY_ENDPOINT)
    @Timed
    public Boolean destroy(final QueryKey queryKey) {
        return statisticsQueryService.destroy(queryKey);
    }

    public void setStatisticsQueryService(final StatisticsQueryService statisticsQueryService) {
        this.statisticsQueryService = statisticsQueryService;
    }

    public HealthCheck.Result getHealth() {
        if (statisticsQueryService == null) {
            StringBuilder errorMessageBuilder = new StringBuilder();
            errorMessageBuilder.append("Dependency error!");
            String statisticsQueryServiceMessage = " 'statisticsQueryService' has not been set!";
            errorMessageBuilder.append(statisticsQueryServiceMessage);
            return HealthCheck.Result.unhealthy(errorMessageBuilder.toString());
        } else {
            return HealthCheck.Result.healthy();
        }
    }

}<commit_msg>Add more logging to SqlStatisticsQueryResource<commit_after>/*
 * Copyright 2017 Crown Copyright
 *
 * 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 stroom.resources.query.v1;

import com.codahale.metrics.annotation.Timed;
import com.codahale.metrics.health.HealthCheck;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import stroom.datasource.api.v1.DataSource;
import stroom.query.api.v1.DocRef;
import stroom.query.api.v1.QueryKey;
import stroom.query.api.v1.SearchRequest;
import stroom.query.api.v1.SearchResponse;
import stroom.resources.ResourcePaths;
import stroom.statistics.server.sql.StatisticsQueryService;
import stroom.util.json.JsonUtil;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path(ResourcePaths.SQL_STATISTICS + ResourcePaths.V1)
@Produces(MediaType.APPLICATION_JSON)
public class SqlStatisticsQueryResource implements QueryResource {

    private static final Logger LOGGER = LoggerFactory.getLogger(SqlStatisticsQueryResource.class);

    private StatisticsQueryService statisticsQueryService;

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path(QueryResource.DATA_SOURCE_ENDPOINT)
    @Timed
    public DataSource getDataSource(final DocRef docRef) {

        if (LOGGER.isDebugEnabled()) {
            String json = JsonUtil.writeValueAsString(docRef);
            LOGGER.debug("/dataSource called with docRef:\n{}", json);
        }
        return statisticsQueryService.getDataSource(docRef);
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path(QueryResource.SEARCH_ENDPOINT)
    @Timed
    public SearchResponse search(final SearchRequest request) {

        if (LOGGER.isDebugEnabled()) {
            String json = JsonUtil.writeValueAsString(request);
            LOGGER.debug("/search called with searchRequest:\n{}", json);
        }

        SearchResponse response = statisticsQueryService.search(request);
        return response;
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path(QueryResource.DESTROY_ENDPOINT)
    @Timed
    public Boolean destroy(final QueryKey queryKey) {
        if (LOGGER.isDebugEnabled()) {
            String json = JsonUtil.writeValueAsString(queryKey);
            LOGGER.debug("/destroy called with queryKey:\n{}", json);
        }
        return statisticsQueryService.destroy(queryKey);
    }

    public void setStatisticsQueryService(final StatisticsQueryService statisticsQueryService) {
        this.statisticsQueryService = statisticsQueryService;
    }

    public HealthCheck.Result getHealth() {
        if (statisticsQueryService == null) {
            StringBuilder errorMessageBuilder = new StringBuilder();
            errorMessageBuilder.append("Dependency error!");
            String statisticsQueryServiceMessage = " 'statisticsQueryService' has not been set!";
            errorMessageBuilder.append(statisticsQueryServiceMessage);
            return HealthCheck.Result.unhealthy(errorMessageBuilder.toString());
        } else {
            return HealthCheck.Result.healthy();
        }
    }

}
{'fraction_non_alphanumeric': 0.08706571787358727, 'fraction_numerical': 0.004743965396958281, 'mean_word_length': 3.8497970230040597, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1377231', 'n_tokens_mistral': 1980, 'n_tokens_neox': 1949, 'n_words': 458}