/*
 * Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
 *
 * 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.hpcloud.mon.infrastructure.persistence;

import com.google.inject.Inject;
import com.hpcloud.mon.MonApiConfiguration;
import com.hpcloud.mon.common.model.metric.MetricDefinition;
import com.hpcloud.mon.domain.model.metric.MetricDefinitionRepository;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.Serie;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class MetricDefinitionInfluxDBRepositoryImpl implements MetricDefinitionRepository {
    private static final Logger logger = LoggerFactory.getLogger(AlarmStateHistoryInfluxDBRepositoryImpl.class);

    private final MonApiConfiguration config;
    private final InfluxDB influxDB;

    @Inject
    public MetricDefinitionInfluxDBRepositoryImpl(MonApiConfiguration config) {
        this.config = config;
        this.influxDB = InfluxDBFactory.connect(this.config.influxDB.getUrl(), this.config.influxDB.getUser(),
                this.config.influxDB.getPassword());
    }

    @Override
    public List<MetricDefinition> find(String tenantId, String name, Map<String, String> dimensions) {

        String dimWhereClause = "";
        String dimColNames = "";
        boolean first = true;
        if (dimensions != null) {
            for (String colName : dimensions.keySet()) {
                if (first) {
                    first = false;
                } else {
                    dimWhereClause += " and";
                    dimColNames += ",";
                }
                dimWhereClause += String.format(" %1$s = '%2$s'", colName, dimensions.get(colName));
                dimColNames += colName;

            }
            if (dimWhereClause.length() > 0) {
                dimWhereClause = String.format(" and %1$s", dimWhereClause);
            }
        }
        String query = String.format("select %1$s from /.*/ where tenant_id = '%2$s' %3$s", dimColNames, tenantId, dimWhereClause);

        logger.debug("Query string: {}", query);

        List<Serie> result = this.influxDB.Query(this.config.influxDB.getName(), query, TimeUnit.SECONDS);

        List<MetricDefinition> metricDefinitionList = new ArrayList<>();
        for (Serie serie : result) {

            MetricDefinition metricDefinition = new MetricDefinition();
            metricDefinition.name = serie.getName();
            metricDefinition.setDimensions(dimensions);
            metricDefinitionList.add(metricDefinition);
        }

        return metricDefinitionList;
    }
}

@@ -1066,24 +1066,50 @@
 .ArrayList;%0A
+import java.util.HashMap;%0A
 import java.
@@ -1115,24 +1115,24 @@
 .util.List;%0A
-
 import java.
@@ -1877,24 +1877,894 @@
 ensions) %7B%0A%0A
+        String dimWhereClause = buildDimWherePart(dimensions);%0A%0A        String query = String.format(%22select time from /.*/ where tenant_id = '%251$s' %252$s%22, tenantId, dimWhereClause);%0A%0A        logger.debug(%22Query string: %7B%7D%22, query);%0A%0A        List%3CSerie%3E result = this.influxDB.Query(this.config.influxDB.getName(), query, TimeUnit.SECONDS);%0A%0A        List%3CMetricDefinition%3E metricDefinitionList = new ArrayList%3C%3E();%0A        for (Serie serie : result) %7B%0A%0A            MetricDefinition metricDefinition = new MetricDefinition();%0A            metricDefinition.name = serie.getName();%0A            metricDefinition.setDimensions(dimensions == null ? new HashMap%3CString, String%3E() : dimensions);%0A            metricDefinitionList.add(metricDefinition);%0A        %7D%0A%0A        return metricDefinitionList;%0A    %7D%0A%0A    private String buildDimWherePart(Map%3CString, String%3E dimensions) %7B%0A%0A
         Stri
@@ -2791,41 +2791,8 @@
 %22%22;%0A
-        String dimColNames = %22%22;%0A
     
@@ -3047,48 +3047,8 @@
 d%22;%0A
-                    dimColNames += %22,%22;%0A
     
@@ -3165,48 +3165,8 @@
 e));
-%0A                dimColNames += colName;
 %0A%0A  
@@ -3337,685 +3337,29 @@
     
-String query = String.format(%22select %251$s from /.*/ where tenant_id = '%252$s' %253$s%22, dimColNames, tenantId, dimWhereClause);%0A%0A        logger.debug(%22Query string: %7B%7D%22, query);%0A%0A        List%3CSerie%3E result = this.influxDB.Query(this.config.influxDB.getName(), query, TimeUnit.SECONDS);%0A%0A        List%3CMetricDefinition%3E metricDefinitionList = new ArrayList%3C%3E();%0A        for (Serie serie : result) %7B%0A%0A            MetricDefinition metricDefinition = new MetricDefinition();%0A            metricDefinition.name = serie.getName();%0A            metricDefinition.setDimensions(dimensions);%0A            metricDefinitionList.add(metricDefinition);%0A        %7D%0A%0A        return metricDefinitionList
+return dimWhereClause
 ;%0A  

Allow queries with no dimensions specified.