# Path: collectors/lib/docker_engine/metric.py
# class Metric(object):
#     def __init__(self, name, etime, value, tags=None):
#         self.name = name
#         self.value = value
#         self.event_time = etime
#         if tags is None:
#             self.dims = set([])
#         else:
#             self.dims = set(tags)
#         self.dims.update(set(DEFAULT_DIMS))
# 
#     def add_dims(self, dims):
#         self.dims.update(set(dims))
# 
#     def get_metric_lines(self):
#         """ return in OpenTSDB format
#         <name> <time_epoch> <value> [key=val] [key1=val1]...
#         """
#         m = "%s %s %s" % (self.name, int(time.mktime(self.event_time)), self.value)
#         return "%s %s" % (m, " ".join(sorted(list(self.dims))))


import time
import requests
from prometheus_client.parser import text_string_to_metric_families
from collectors.lib.docker_engine.metric import Metric
# of the GNU Lesser General Public License along with this program.  If not,
# see <http://www.gnu.org/licenses/>.

from __future__ import print_function

class DockerMetrics(object):
    def __init__(self, url):
        self._url = url
        self.event_time = time.gmtime()

    def get_endpoint(self):
        """ Fetches the endpoint """
        ret = []
        r = requests.get(self._url)
        if r.status_code != 200:
            print("Error %s: %s" % (r.status_code, r.text))
        else:
            for line in r.iter_lines(decode_unicode=True):
                if not line.startswith('#'):
                    ret.extend(self.eval_prometheus_line(self.event_time, line))
        return ret

    @staticmethod
    def eval_prometheus_line(etime, line):
        ret = []
        for family in text_string_to_metric_families(line):
            for sample in family.samples:
                dims = []
                for kv in sample[1].items():
                    dims.append("%s=%s" % kv)

# Path: collectors/lib/docker_engine/metric.py
# class Metric(object):
#     def __init__(self, name, etime, value, tags=None):
#         self.name = name
#         self.value = value
#         self.event_time = etime
#         if tags is None:
#             self.dims = set([])
#         else:
#             self.dims = set(tags)
#         self.dims.update(set(DEFAULT_DIMS))
# 
#     def add_dims(self, dims):
#         self.dims.update(set(dims))
# 
#     def get_metric_lines(self):
#         """ return in OpenTSDB format
#         <name> <time_epoch> <value> [key=val] [key1=val1]...
#         """
#         m = "%s %s %s" % (self.name, int(time.mktime(self.event_time)), self.value)
#         return "%s %s" % (m, " ".join(sorted(list(self.dims))))


# Path: collectors/lib/docker_engine/docker_metrics.py
import time
import requests
from prometheus_client.parser import text_string_to_metric_families
from collectors.lib.docker_engine.metric import Metric

# of the GNU Lesser General Public License along with this program.  If not,
# see <http://www.gnu.org/licenses/>.

from __future__ import print_function

class DockerMetrics(object):
    def __init__(self, url):
        self._url = url
        self.event_time = time.gmtime()

    def get_endpoint(self):
        """ Fetches the endpoint """
        ret = []
        r = requests.get(self._url)
        if r.status_code != 200:
            print("Error %s: %s" % (r.status_code, r.text))
        else:
            for line in r.iter_lines(decode_unicode=True):
                if not line.startswith('#'):
                    ret.extend(self.eval_prometheus_line(self.event_time, line))
        return ret

    @staticmethod
    def eval_prometheus_line(etime, line):
        ret = []
        for family in text_string_to_metric_families(line):
            for sample in family.samples:
                dims = []
                for kv in sample[1].items():
                    dims.append("%s=%s" % kv)

                m = Metric("docker.{0}".format(*sample), etime, sample[2], dims)