this.metricFindQuery = function()

in ambari-metrics-grafana/ambari-metrics/datasource.js [799:1171]


      this.metricFindQuery = function (query) {
        var interpolated;
        try {
          interpolated = templateSrv.replace(query);
        } catch (err) {
          return $q.reject(err);
        }
        var templatedClusters = templateSrv.variables.filter(function (o) {
          return o.name === "cluster"
        });
        var templatedCluster = (_.isEmpty(templatedClusters)) ? '' : templatedClusters[0].options.filter(function (cluster) {
          return cluster.selected;
        }).map(function (clusterName) {
          return clusterName.value;
        });

        var tComponents = _.isEmpty(templateSrv.variables) ? '' : templateSrv.variables.filter(function (variable) {
          return variable.name === "components"
        });
        var tComponent = _.isEmpty(tComponents) ? '' : tComponents[0].current.value;


        // Templated Variable for HBase Users
        // It will search the cluster and populate the HBase Users.
        if (interpolated === "hbase-users") {
          return this.initMetricAppidMapping()
            .then(function () {
              var hbaseUsers = getMetrics(allMetrics, "hbase");;
              var extractUsers = hbaseUsers.filter(/./.test.bind(new RegExp("regionserver.Users.", 'g')));
              var removeUser = "regionserver.Users.numUsers";
              var i = extractUsers.indexOf(removeUser);
              if (i !== -1) {
                extractUsers.splice(i, 1);
              }
              var userPrefix = "regionserver.Users.";
              var users = _.map(extractUsers, function (user) {
                return user.substring(userPrefix.length);
              });
              users = _.map(users, function (userName) {
                return userName.substring(0, userName.lastIndexOf("_metric"));
              });
              users = _.sortBy(_.uniq(users));
              return _.map(users, function (users) {
                return {
                  text: users
                };
              });
            });
        }
        // Templated Variable for HBase Tables.
        // It will search the cluster and populate the hbase-tables.
        if (interpolated === "hbase-tables") {
          return this.initMetricAppidMapping()
            .then(function () {
              var hbaseTables = getMetrics(allMetrics, "hbase");;
              var extractTables = hbaseTables.filter(/./.test.bind(new RegExp("regionserver.Tables.", 'g')));
              var removeTable = "regionserver.Tables.numTables";
              var i = extractTables.indexOf(removeTable);
              if (i != -1) {
                extractTables.splice(i, 1);
              }
              var tablePrefix = "regionserver.Tables.";
              var tables = _.map(extractTables, function (user) {
                return user.substring(tablePrefix.length);
              });
              tables = _.map(tables, function (userName) {
                return userName.substring(0, userName.lastIndexOf("_metric"));
              });
              tables = _.sortBy(_.uniq(tables));
              return _.map(tables, function (tables) {
                return {
                  text: tables
                };
              });
            });
        }
        // Templated Variable for Kafka Topics.
        // It will search the cluster and populate the topics.
        if (interpolated === "kafka-topics") {
          return this.initMetricAppidMapping()
            .then(function () {
              // patterns to check possible kafka topics
              var kafkaTopicPatterns = ['^kafka\\.server.*\\.topic\\.(.*)\\.[\\d\\w]*$',
                '^kafka\\.log\\.Log\\..*.topic\\.(.*)$',
                '^kafka\\.cluster.*\\.topic\\.(.*)$'];

              var kafkaMetrics = getMetrics(allMetrics, "kafka_broker");
              var topics = []

              // filter metrics that can contain topic name
              var topicMetrics = kafkaMetrics.filter(function(metric) {
                return metric.indexOf(".topic.") > 0;
              });

              _.forEach(kafkaTopicPatterns, function(topicPattern) {
                _.forEach(topicMetrics, function(checkTopic) {
                    var topicMatch = checkTopic.match(RegExp(topicPattern));
                    var topicName = topicMatch ? topicMatch[1] : null;
                    if (topicName && topicName != "ambari_kafka_service_check" && topics.indexOf(topicName) < 0) {
                      topics.push(topicName);
                    }
                })
              })
              return _.map(topics, function (topics) {
                return {
                  text: topics
                };
              });
            });
        }

        //Templated Variables for Call Queue Metrics
        if (interpolated === "callers") {
          return this.initMetricAppidMapping()
            .then(function () {
              var nnCallers = getMetrics(allMetrics, "namenode");
              var extractCallers = nnCallers.filter(/./.test.bind(new
              RegExp("ipc.client.org.apache.hadoop.ipc.DecayRpcScheduler.Caller", 'g')));
              var callers = _.sortBy(_.uniq(_.map(extractCallers, function (caller) {
                return caller.substring(caller.indexOf('(') + 1, caller.indexOf(')'))
              })));
              return _.map(callers, function (callers) {
                return {
                  text: callers
                };
              });
            });
        }

        var cores = [];
        //Templated Variables for Infra Solr Cores
        if (interpolated === "infra_solr_core") {
           return this.initMetricAppidMapping()
             .then(function () {
               var solrMetrics = getMetrics(allMetrics, "ambari-infra-solr");
               var extractCores = solrMetrics.filter(/./.test.bind(new
               RegExp("^infra.solr.core.", 'g')));
               _.map(extractCores, function (core) {
                 // Core naming convention is infra.solr.core.<collection_name>.<shard>.<replica>.<metric_name>
                 // coreName should be <collection_name>.<shard>.<replica>
                 core = core.split('.');
                 var coreName = core.slice(3,6).join(".");
                 if (cores.indexOf(coreName) < 0) {
                   cores.push(coreName);
                 }
               });
               return _.map(cores, function (cores) {
                       return {
                         text: cores
                       };
                     });
             });
         }

        var collections = [];
        //Templated Variables for Infra Solr Collections
        if (interpolated === "infra_solr_collection") {
           return this.initMetricAppidMapping()
             .then(function () {
               var solrMetrics = getMetrics(allMetrics, "ambari-infra-solr");
               var extractCollections = solrMetrics.filter(/./.test.bind(new
               RegExp("^infra.solr.core.", 'g')));
               _.map(extractCollections, function (core) {
                 // Core naming convention is infra.solr.core.<collection_name>.<shard>.<replica>.<metric_name>
                 core = core.split('.');
                 var collection = core[3];
                 if (collections.indexOf(collection) < 0) {
                   collections.push(collection);
                 }
               });
               return _.map(collections, function (collections) {
                       return {
                         text: collections
                       };
                     });
             });
         }

        var topologies = {};
        //Templated Variables for Storm Topologies
        if (interpolated === "topologies") {
          return this.initMetricAppidMapping()
            .then(function () {
              var storm = getMetrics(allMetrics, "nimbus");
              var extractTopologies = storm.filter(/./.test.bind(new
              RegExp("^topology.", 'g')));
              _.map(extractTopologies, function (topology) {
                // Topology naming convention is topology.<topology-name>.component.
                topology = topology.split('.').slice(0, 3);
                if (topologies[topology[1]]) {
                  topologies[topology[1]].push(topology[2]);
                } else {
                  topologies[topology[1]] = [topology[2]];
                }
              });
              return _.map(Object.keys(topologies), function (topologyNames) {
                return {
                  text: topologyNames
                };
              });
            });
        }
        //Templated Variables for Storm Components per Topology
        if (interpolated.indexOf("stormComponent") >= 0) {
          var componentName = interpolated.substring(0, interpolated.indexOf('.'));
          return this.initMetricAppidMapping()
            .then(function () {
              var storm = getMetrics(allMetrics, "nimbus");
              var extractTopologies = storm.filter(/./.test.bind(new
              RegExp("^topology.", 'g')));
              _.map(extractTopologies, function (topology) {
                topology = topology.split('.').slice(0, 3);
                if (topologies[topology[1]]) {
                  topologies[topology[1]].push(topology[2]);
                } else {
                  topologies[topology[1]] = [topology[2]];
                }
              });
              // Retrieve unique component names from the list.
              var compName = _.uniq(topologies[componentName]);
              // Remove "kafka-topic" from the list of components.
              var remove = compName.indexOf('kafka-topic');
              if (remove > -1) {
                compName.splice(remove, 1);
              }
              return _.map(compName, function (components) {
                return {
                  text: components
                };
              });
            });
        }
        var stormEntities = {};
        this.getStormEntities = function () {
          return this.initMetricAppidMapping()
            .then(function () {
              var storm = getMetrics(allMetrics, "nimbus");
              var extractTopologies = storm.filter(/./.test.bind(new
              RegExp("partition", 'g')));
              _.map(extractTopologies, function (topology) {
                topology = topology.split('.').slice(0, 5);
                var topologyName = topologyN = topology[1]; // Topology
                var topologyTopicName = topicN = topology[3]; // Topic
                var topologyTopicPartitionName = topology[4]; // Partition
                if (stormEntities[topologyName]) {
                  if (stormEntities[topologyName][topologyTopicName]) {
                    stormEntities[topologyName][topologyTopicName].push(topologyTopicPartitionName);
                  } else {
                    stormEntities[topologyName][topologyTopicName] = [topologyTopicPartitionName];
                  }
                } else {
                  stormEntities[topologyName] = {};
                  stormEntities[topologyName][topologyTopicName] = [topologyTopicPartitionName];
                }
              });
            });
        };
        //Templated Variables for Storm Topics per Topology
        if (interpolated.indexOf("stormTopic") >= 0) {
          var topicName = interpolated.substring(0, interpolated.indexOf('.'));
          return this.getStormEntities().then(function () {
            if(!_.isEmpty(stormEntities) && !_.isEmpty(stormEntities[topicName])) {
              var topicNames = Object.keys(stormEntities[topicName]);
              return _.map(topicNames, function(names){
                return {
                  text: names
                };
              });
            } else return[];
          });
        }
        //Templated Variables for Storm Partitions per Topic
        if (interpolated.indexOf("stormPartition") >= 0) {
          var topicN, topologyN;
          return this.getStormEntities().then(function () {
              if(!_.isEmpty(stormEntities) && !_.isEmpty(stormEntities[topologyN]) && !_.isEmpty(stormEntities[topologyN][topicN])) {
                 var partitionNames = _.uniq(stormEntities[topologyN][topicN]);
                 return _.map(partitionNames, function(names){
                   return {
                     text: names
                   };
                 });
              } else return [];
          });
        }
        // Templated Variable for YARN Queues.
        // It will search the cluster and populate the queues.
        if (interpolated === "yarnqueues") {
          return this.initMetricAppidMapping()
            .then(function () {
              var yarnqueues = getMetrics(allMetrics, "resourcemanager");
              var extractQueues = yarnqueues.filter(/./.test.bind(new RegExp(".=root", 'g')));
              var queues = _.map(extractQueues, function (metric) {
                return metric.substring("yarn.QueueMetrics.Queue=".length);
              });
              queues = _.map(queues, function (metricName) {
                return metricName.substring(metricName.lastIndexOf("."), 0);
              });
              queues = _.sortBy(_.uniq(queues));
              return _.map(queues, function (queues) {
                return {
                  text: queues
                };
              });
            });
        }

        // Templated Variable for DruidServices.
        // It will search the cluster and populate the druid service names.
        if (interpolated === "druidServices") {
          return this.initMetricAppidMapping()
            .then(function () {
              var druidMetrics = getMetrics(allMetrics, "druid");
              // Assumption: each node always emits jvm metrics
              var extractNodeTypes = druidMetrics.filter(/./.test.bind(new RegExp("jvm/gc/time", 'g')));
              var nodeTypes = _.map(extractNodeTypes, function (metricName) {
                return metricName.substring(0, metricName.indexOf("."));
              });
              nodeTypes = _.sortBy(_.uniq(nodeTypes));
              return _.map(nodeTypes, function (nodeType) {
                return {
                  text: nodeType
                };
              });
            });
        }

        // Templated Variable for Druid datasources.
        // It will search the cluster and populate the druid datasources.
        if (interpolated === "druidDataSources") {
          return this.initMetricAppidMapping()
            .then(function () {
              var druidMetrics = getMetrics(allMetrics, "druid");
              // Assumption: query/time is emitted for each datasource
              var extractDataSources = druidMetrics.filter(/./.test.bind(new RegExp("query/time", 'g')));
              var dataSources = _.map(extractDataSources, function (metricName) {
                return metricName.split('.')[1]
              });
              dataSources = _.sortBy(_.uniq(dataSources));
              return _.map(dataSources, function (dataSource) {
                return {
                  text: dataSource
                };
              });
            });
        }

        // Templated Variable for Druid query type.
        // It will search the cluster and populate the druid query types.
        if (interpolated === "druidQueryTypes") {
          return this.initMetricAppidMapping()
            .then(function () {
              var druidMetrics = getMetrics(allMetrics, "druid");
              // Assumption: query/time is emitted for each query type.
              var extractQueryTypes = druidMetrics.filter(/./.test.bind(new RegExp("query/time", 'g')));
              var queryTypes = _.map(extractQueryTypes, function (metricName) {
                return metricName.split('.')[2]
              });
              queryTypes = _.sortBy(_.uniq(queryTypes));
              return _.map(queryTypes, function (queryType) {
                return {
                  text: queryType
                };
              });
            });
        }

        if (interpolated.indexOf("hosts") >= 0) {
          return this.suggestHosts(tComponent, templatedCluster);
        } else if (interpolated == 'cluster') {
          return this.suggestClusters(tComponent)
        }
      };