<commit_before>package yanagishima.service;

import com.github.wyukawa.elasticsearch.unofficial.jdbc.driver.ElasticsearchTranslateClient;
import io.airlift.units.DataSize;
import io.airlift.units.Duration;
import me.geso.tinyorm.TinyORM;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.komamitsu.fluency.Fluency;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import yanagishima.config.YanagishimaConfig;
import yanagishima.exception.ElasticsearchQueryErrorException;
import yanagishima.result.ElasticsearchQueryResult;

import javax.inject.Inject;
import java.io.BufferedWriter;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.*;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static yanagishima.util.DbUtil.insertQueryHistory;
import static yanagishima.util.DbUtil.storeError;
import static yanagishima.util.PathUtil.getResultFilePath;
import static yanagishima.util.TimeoutUtil.checkTimeout;

public class ElasticsearchServiceImpl implements ElasticsearchService {

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

    private YanagishimaConfig yanagishimaConfig;

    private Fluency fluency;

    @Inject
    private TinyORM db;

    @Inject
    public ElasticsearchServiceImpl(YanagishimaConfig yanagishimaConfig) {
        this.yanagishimaConfig = yanagishimaConfig;
        if (yanagishimaConfig.getFluentdExecutedTag().isPresent() || yanagishimaConfig.getFluentdFaliedTag().isPresent()) {
            String fluentdHost = yanagishimaConfig.getFluentdHost().orElse("localhost");
            int fluentdPort = Integer.parseInt(yanagishimaConfig.getFluentdPort().orElse("24224"));
            try {
                fluency = Fluency.defaultFluency(fluentdHost, fluentdPort);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public ElasticsearchQueryResult doQuery(String datasource, String query, String userName, boolean storeFlag, int limit) throws ElasticsearchQueryErrorException {
        String queryId = generateQueryId(datasource, query, "elasticsearch");
        return getElasticsearchQueryResult(queryId, datasource, query, storeFlag, limit, userName);
    }

    @Override
    public ElasticsearchQueryResult doTranslate(String datasource, String query, String userName, boolean storeFlag, int limit) throws ElasticsearchQueryErrorException {
        String queryId = generateQueryId(datasource, query, "elasticsearch");
        String jdbcUrl = yanagishimaConfig.getElasticsearchJdbcUrl(datasource);
        String httpUrl = "http://" + jdbcUrl.substring("jdbc:es:".length());
        ElasticsearchTranslateClient translateClient = new ElasticsearchTranslateClient(httpUrl);
        try {
            long start = System.currentTimeMillis();
            ElasticsearchQueryResult elasticsearchQueryResult = new ElasticsearchQueryResult();
            elasticsearchQueryResult.setQueryId(queryId);
            List<String> columnNameList = new ArrayList<>();
            columnNameList.add("lucene_query");
            Path dst = getResultFilePath(datasource, queryId, false);
            int lineNumber = 0;
            int maxResultFileByteSize = yanagishimaConfig.getElasticsearchMaxResultFileByteSize();
            int resultBytes = 0;
            try (BufferedWriter bw = Files.newBufferedWriter(dst, StandardCharsets.UTF_8);
                 CSVPrinter csvPrinter = new CSVPrinter(bw, CSVFormat.EXCEL.withDelimiter('\t').withNullString("\\N").withRecordSeparator(System.getProperty("line.separator")));) {
                csvPrinter.printRecord(columnNameList);
                lineNumber++;
                elasticsearchQueryResult.setColumns(columnNameList);
                List<List<String>> rowDataList = new ArrayList<>();
                List<String> columnDataList = new ArrayList<>();
                String luceneQuery = translateClient.translate(query);
                columnDataList.add(luceneQuery);
                csvPrinter.printRecord(columnDataList);
                lineNumber++;<commit_msg>use constant<commit_after>package yanagishima.service;

import com.github.wyukawa.elasticsearch.unofficial.jdbc.driver.ElasticsearchTranslateClient;
import io.airlift.units.DataSize;
import io.airlift.units.Duration;
import me.geso.tinyorm.TinyORM;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.komamitsu.fluency.Fluency;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import yanagishima.config.YanagishimaConfig;
import yanagishima.exception.ElasticsearchQueryErrorException;
import yanagishima.result.ElasticsearchQueryResult;

import javax.inject.Inject;
import java.io.BufferedWriter;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.*;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.github.wyukawa.elasticsearch.unofficial.jdbc.driver.ElasticsearchDriver.DRIVER_URL_START;
import static yanagishima.util.DbUtil.insertQueryHistory;
import static yanagishima.util.DbUtil.storeError;
import static yanagishima.util.PathUtil.getResultFilePath;
import static yanagishima.util.TimeoutUtil.checkTimeout;

public class ElasticsearchServiceImpl implements ElasticsearchService {

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

    private YanagishimaConfig yanagishimaConfig;

    private Fluency fluency;

    @Inject
    private TinyORM db;

    @Inject
    public ElasticsearchServiceImpl(YanagishimaConfig yanagishimaConfig) {
        this.yanagishimaConfig = yanagishimaConfig;
        if (yanagishimaConfig.getFluentdExecutedTag().isPresent() || yanagishimaConfig.getFluentdFaliedTag().isPresent()) {
            String fluentdHost = yanagishimaConfig.getFluentdHost().orElse("localhost");
            int fluentdPort = Integer.parseInt(yanagishimaConfig.getFluentdPort().orElse("24224"));
            try {
                fluency = Fluency.defaultFluency(fluentdHost, fluentdPort);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public ElasticsearchQueryResult doQuery(String datasource, String query, String userName, boolean storeFlag, int limit) throws ElasticsearchQueryErrorException {
        String queryId = generateQueryId(datasource, query, "elasticsearch");
        return getElasticsearchQueryResult(queryId, datasource, query, storeFlag, limit, userName);
    }

    @Override
    public ElasticsearchQueryResult doTranslate(String datasource, String query, String userName, boolean storeFlag, int limit) throws ElasticsearchQueryErrorException {
        String queryId = generateQueryId(datasource, query, "elasticsearch");
        String jdbcUrl = yanagishimaConfig.getElasticsearchJdbcUrl(datasource);
        String httpUrl = "http://" + jdbcUrl.substring(DRIVER_URL_START.length());
        ElasticsearchTranslateClient translateClient = new ElasticsearchTranslateClient(httpUrl);
        try {
            long start = System.currentTimeMillis();
            ElasticsearchQueryResult elasticsearchQueryResult = new ElasticsearchQueryResult();
            elasticsearchQueryResult.setQueryId(queryId);
            List<String> columnNameList = new ArrayList<>();
            columnNameList.add("lucene_query");
            Path dst = getResultFilePath(datasource, queryId, false);
            int lineNumber = 0;
            int maxResultFileByteSize = yanagishimaConfig.getElasticsearchMaxResultFileByteSize();
            int resultBytes = 0;
            try (BufferedWriter bw = Files.newBufferedWriter(dst, StandardCharsets.UTF_8);
                 CSVPrinter csvPrinter = new CSVPrinter(bw, CSVFormat.EXCEL.withDelimiter('\t').withNullString("\\N").withRecordSeparator(System.getProperty("line.separator")));) {
                csvPrinter.printRecord(columnNameList);
                lineNumber++;
                elasticsearchQueryResult.setColumns(columnNameList);
                List<List<String>> rowDataList = new ArrayList<>();
                List<String> columnDataList = new ArrayList<>();
                String luceneQuery = translateClient.translate(query);
                columnDataList.add(luceneQuery);
                csvPrinter.printRecord(columnDataList);
                lineNumber++;
{'fraction_non_alphanumeric': 0.08827085852478839, 'fraction_numerical': 0.0021985269869187643, 'mean_word_length': 3.9472539423599784, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3314916', 'n_tokens_mistral': 2568, 'n_tokens_neox': 2528, 'n_words': 463}