import logging
import requests


def get_metrics_data(client_api, metrics):
    """Gets all data for specific metrics.

    :param client_api: API client, that handles API requests
    :type client_api: client.APIClient
    :param metrics: list of metrics to retrieve data
    :type metrics: list
    :return: list of metrics data
    :rtype: list[dict]
    """

    logging.info("Start fetching metrics from Prometheus.")
    metrics_data_list = []
    for metric in metrics:
        params = {'query': metric}
        try:
            data = client_api.get_request('query', params)
        except requests.exceptions.RequestException as e:
            logging.error("Received error: {}".format(e), exc_info=True)
            raise
        # Prometheus returns false-positive result for non-existent metrics.
        # We have to skip non-existent metrics, i.e. those with empty data
        if not data['data']['result']:
            logging.warning("Metric '{0}' not found.".format(metric))
            continue
        metrics_data_list.append(data)
    logging.info("{0} out of {1} metrics were successfully fetched from "
                 "Prometheus.".format(len(metrics_data_list), len(metrics)))
    return metrics_data_list