<commit_before>/*
 *
 *  Copyright 2013 Netflix, Inc.
 *
 *     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 com.netflix.genie.server.services.impl;

import com.netflix.client.http.HttpRequest.Verb;
import com.netflix.genie.common.exceptions.CloudServiceException;
import com.netflix.genie.common.messages.ClusterConfigRequest;
import com.netflix.genie.common.messages.ClusterConfigResponse;
import com.netflix.genie.common.model.ClusterConfig;
import com.netflix.genie.common.model.ClusterCriteria;
import com.netflix.genie.common.model.CommandConfig;
import com.netflix.genie.common.model.Types;
import com.netflix.genie.common.model.Types.ClusterStatus;
import com.netflix.genie.server.persistence.ClauseBuilder;
import com.netflix.genie.server.persistence.PersistenceManager;
import com.netflix.genie.server.persistence.QueryBuilder;
import com.netflix.genie.server.services.ClusterConfigService;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.persistence.EntityExistsException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.RollbackException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Implementation of the PersistentClusterConfig interface.
 *
 * @author skrishnan
 * @author amsharma
 */
public class PersistentClusterConfigImpl implements ClusterConfigService {

    private static final Logger LOG = LoggerFactory
            .getLogger(PersistentClusterConfigImpl.class);

    private final PersistenceManager<ClusterConfig> pm;

    /**
     * Default constructor - initialize all required dependencies.
     *
     * @throws CloudServiceException
     */
    public PersistentClusterConfigImpl() throws CloudServiceException {
        // instantiate PersistenceManager
        this.pm = new PersistenceManager<ClusterConfig>();
    }

    @Override
    public ClusterConfigResponse getClusterConfig(String id) {
        LOG.info("called");

        ClusterConfigResponse ccr;
        ClusterConfig cce;
        try {
            cce = pm.getEntity(id, ClusterConfig.class);
        } catch (Exception e) {
            LOG.error("Failed to get cluster config: ", e);
            ccr = new ClusterConfigResponse(new CloudServiceException(
                    HttpURLConnection.HTTP_INTERNAL_ERROR, e.getMessage()));
            return ccr;
        }

        if (cce == null) {
            String msg = "Cluster config not found: " + id;
            LOG.error(msg);
            ccr = new ClusterConfigResponse(new CloudServiceException(
                    HttpURLConnection.HTTP_NOT_FOUND, msg));
            return ccr;
        } else {
            ccr = new ClusterConfigResponse();
            ccr.setClusterConfigs(new ClusterConfig[]{cce});
            ccr.setMessage("Returning cluster config for: " + id);
            return ccr;
        }
    }

    @Override
    public ClusterConfigResponse getClusterConfig(String id, String name,
            String commandId, List<String> tags, ClusterStatus status) {

        List<String> statusList = Arrays.asList(status.name());
        return getClusterConfig(id, name, statusList, tags,
                null, null, null, null);
    }

    @Override
    public ClusterConfigResponse getClusterConfig(String id, String name,
            List<String> status, List<String> tags, Long minUpdateTime,
            Long maxUpdateTime, Integer limit, Integer page) {

        LOG.info("called");

        ClusterConfigResponse ccr;
        try {
            LOG.info("GENIE: Returning configs for specified params");

            ccr = new ClusterConfigResponse();
            Object[] results;

            // construct query
            ClauseBuilder criteria = new ClauseBuilder(ClauseBuilder.AND);
            if ((id != null) && (!id.isEmpty())) {
                criteria.append("id like '" + id + "'");
            }
            if ((name != null) && (!name.isEmpty())) {
                criteria.append("name like '" + name + "'");
            }
            if (minUpdateTime != null) {
                criteria.append("updateTime >= " + minUpdateTime);
            }
            if (maxUpdateTime != null) {
                criteria.append("updateTime <= " + maxUpdateTime);
            }

            if ((status != null) && !status.isEmpty()) {
                int count = 0;
                ClauseBuilder statusCriteria = new ClauseBuilder(ClauseBuilder.OR);<commit_msg>Adding logic to use tags while fetching clusters.<commit_after>/*
 *
 *  Copyright 2013 <NAME>.
 *
 *     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 com.netflix.genie.server.services.impl;

import com.netflix.client.http.HttpRequest.Verb;
import com.netflix.genie.common.exceptions.CloudServiceException;
import com.netflix.genie.common.messages.ClusterConfigRequest;
import com.netflix.genie.common.messages.ClusterConfigResponse;
import com.netflix.genie.common.model.ClusterConfig;
import com.netflix.genie.common.model.ClusterCriteria;
import com.netflix.genie.common.model.CommandConfig;
import com.netflix.genie.common.model.Types;
import com.netflix.genie.common.model.Types.ClusterStatus;
import com.netflix.genie.server.persistence.ClauseBuilder;
import com.netflix.genie.server.persistence.PersistenceManager;
import com.netflix.genie.server.persistence.QueryBuilder;
import com.netflix.genie.server.services.ClusterConfigService;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.persistence.EntityExistsException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.RollbackException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Implementation of the PersistentClusterConfig interface.
 *
 * @author skrishnan
 * @author amsharma
 */
public class PersistentClusterConfigImpl implements ClusterConfigService {

    private static final Logger LOG = LoggerFactory
            .getLogger(PersistentClusterConfigImpl.class);

    private final PersistenceManager<ClusterConfig> pm;

    /**
     * Default constructor - initialize all required dependencies.
     *
     * @throws CloudServiceException
     */
    public PersistentClusterConfigImpl() throws CloudServiceException {
        // instantiate PersistenceManager
        this.pm = new PersistenceManager<ClusterConfig>();
    }

    @Override
    public ClusterConfigResponse getClusterConfig(String id) {
        LOG.info("called");

        ClusterConfigResponse ccr;
        ClusterConfig cce;
        try {
            cce = pm.getEntity(id, ClusterConfig.class);
        } catch (Exception e) {
            LOG.error("Failed to get cluster config: ", e);
            ccr = new ClusterConfigResponse(new CloudServiceException(
                    HttpURLConnection.HTTP_INTERNAL_ERROR, e.getMessage()));
            return ccr;
        }

        if (cce == null) {
            String msg = "Cluster config not found: " + id;
            LOG.error(msg);
            ccr = new ClusterConfigResponse(new CloudServiceException(
                    HttpURLConnection.HTTP_NOT_FOUND, msg));
            return ccr;
        } else {
            ccr = new ClusterConfigResponse();
            ccr.setClusterConfigs(new ClusterConfig[]{cce});
            ccr.setMessage("Returning cluster config for: " + id);
            return ccr;
        }
    }

    @Override
    public ClusterConfigResponse getClusterConfig(String id, String name,
            String commandId, List<String> tags, ClusterStatus status) {

        List<String> statusList = Arrays.asList(status.name());
        return getClusterConfig(id, name, statusList, tags,
                null, null, null, null);
    }

    @Override
    public ClusterConfigResponse getClusterConfig(String id, String name,
            List<String> status, List<String> tags, Long minUpdateTime,
            Long maxUpdateTime, Integer limit, Integer page) {

        LOG.info("called");

        ClusterConfigResponse ccr;
        try {
            LOG.info("GENIE: Returning configs for specified params");

            ccr = new ClusterConfigResponse();
            Object[] results;

            // construct query
            ClauseBuilder criteria = new ClauseBuilder(ClauseBuilder.AND);
            if ((id != null) && (!id.isEmpty())) {
                criteria.append("id like '" + id + "'");
            }
            if ((name != null) && (!name.isEmpty())) {
                criteria.append("name like '" + name + "'");
            }
            if (minUpdateTime != null) {
                criteria.append("updateTime >= " + minUpdateTime);
            }
            if (maxUpdateTime != null) {
                criteria.append("updateTime <= " + maxUpdateTime);
            }

            if(tags != null) {
                for (String tag : tags) {
                    criteria.append("\"" + tag + "\" member of T.tags", false);
                }
            }
            
            if ((status != null) && !status.isEmpty()) {
                int count = 0;
                ClauseBuilder statusCriteria = new ClauseBuilder(ClauseBuilder.OR);
{'fraction_non_alphanumeric': 0.09674096272710245, 'fraction_numerical': 0.002060310919647874, 'mean_word_length': 2.6849551414768804, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, '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': '5222489', 'n_tokens_mistral': 2823, 'n_tokens_neox': 2767, 'n_words': 713}