<commit_before># coding: utf-8
import requests
import simplejson as json
import pygal
from pygal.util import compute_scale
from pygal.style import LightStyle
import base64

from django.utils.formats import date_format
from datetime import datetime


TIMEOUT = 10
chart_config = pygal.Config()
chart_config.legend_at_bottom = True
chart_config.x_label_rotation = 30
chart_config.human_readable = False
chart_config.show_dots = False
chart_config.include_x_axis = True
chart_config.legend_box_size = 10
chart_config.legend_font_size = 12
chart_config.style = LightStyle


class XY_Timestamps(pygal.XY):
    """
    XY Line graph with timestamps in X-axis.
    """

    def _display_date(self, timestamp):
        return date_format(datetime.fromtimestamp(timestamp),
            "SHORT_DATETIME_FORMAT")

    @property
    def _format_x(self):
        return self._display_date

    def _compute(self):
        """
        This method was taken from Pygal.XY class, with my minor changes. It's
        licensed under GPL v3 or greater.
        """
        for serie in self.series:
            for metadata in serie.metadata:
                if not hasattr(metadata.value, '__iter__'):
                    metadata.value = (metadata.value, self.zero)

        xvals = [val[0]
                 for serie in self.series
                 for val in serie.values
                 if val[0] is not None]
        yvals = [val[1]
                 for serie in self.series
                 for val in serie.values
                 if val[1] is not None]
        xmin = min(xvals)
        xmax = max(xvals)
        rng = (xmax - xmin)

        for serie in self.series:
            serie.points = serie.values
            if self.interpolate:
                vals = zip(*sorted(serie.points, key=lambda x: x[0]))
                serie.interpolated = self._interpolate(
                    vals[1], vals[0], xy=True, xy_xmin=xmin, xy_rng=rng)
                if not serie.interpolated:
                    serie.interpolated = serie.values

        if self.interpolate:
            xvals = [val[0]
                     for serie in self.series
                     for val in serie.interpolated]
            yvals = [val[1]
                     for serie in self.series
                     for val in serie.interpolated]

        self._box.xmin, self._box.xmax = min(xvals), max(xvals)
        self._box.ymin, self._box.ymax = min(yvals), max(yvals)
        x_pos = compute_scale(self._box.xmin, self._box.xmax, self.logarithmic,
            self.order_min)
        y_pos = compute_scale(self._box.ymin, self._box.ymax, self.logarithmic,
            self.order_min)

        self._x_labels = zip(map(self._format_x, x_pos), x_pos)
        self._y_labels = zip(map(self._format, y_pos), y_pos)


def metrics_tree(host):
    """
    Return tree structure of collectd's data directory.
    """
    tree = requests.get(host + "/list_tree", timeout=TIMEOUT)
    return tree


def arbitrary_metrics(host, paths, start=None, end=None):
    """
    Return metrics for given set of full paths.
    :param paths: list of paths for which metrics are shown. Example:
        ["host1/plugin1/type1.rrd", "host1/plugin1/type2.rrd"]<commit_msg>API += metrics_hosts<commit_after># coding: utf-8
import requests
import simplejson as json
import pygal
from pygal.util import compute_scale
from pygal.style import LightStyle
import base64

from django.utils.formats import date_format
from datetime import datetime


TIMEOUT = 10
chart_config = pygal.Config()
chart_config.legend_at_bottom = True
chart_config.x_label_rotation = 30
chart_config.human_readable = False
chart_config.show_dots = False
chart_config.include_x_axis = True
chart_config.legend_box_size = 10
chart_config.legend_font_size = 12
chart_config.style = LightStyle


class XY_Timestamps(pygal.XY):
    """
    XY Line graph with timestamps in X-axis.
    """

    def _display_date(self, timestamp):
        return date_format(datetime.fromtimestamp(timestamp),
            "SHORT_DATETIME_FORMAT")

    @property
    def _format_x(self):
        return self._display_date

    def _compute(self):
        """
        This method was taken from Pygal.XY class, with my minor changes. It's
        licensed under GPL v3 or greater.
        """
        for serie in self.series:
            for metadata in serie.metadata:
                if not hasattr(metadata.value, '__iter__'):
                    metadata.value = (metadata.value, self.zero)

        xvals = [val[0]
                 for serie in self.series
                 for val in serie.values
                 if val[0] is not None]
        yvals = [val[1]
                 for serie in self.series
                 for val in serie.values
                 if val[1] is not None]
        xmin = min(xvals)
        xmax = max(xvals)
        rng = (xmax - xmin)

        for serie in self.series:
            serie.points = serie.values
            if self.interpolate:
                vals = zip(*sorted(serie.points, key=lambda x: x[0]))
                serie.interpolated = self._interpolate(
                    vals[1], vals[0], xy=True, xy_xmin=xmin, xy_rng=rng)
                if not serie.interpolated:
                    serie.interpolated = serie.values

        if self.interpolate:
            xvals = [val[0]
                     for serie in self.series
                     for val in serie.interpolated]
            yvals = [val[1]
                     for serie in self.series
                     for val in serie.interpolated]

        self._box.xmin, self._box.xmax = min(xvals), max(xvals)
        self._box.ymin, self._box.ymax = min(yvals), max(yvals)
        x_pos = compute_scale(self._box.xmin, self._box.xmax, self.logarithmic,
            self.order_min)
        y_pos = compute_scale(self._box.ymin, self._box.ymax, self.logarithmic,
            self.order_min)

        self._x_labels = zip(map(self._format_x, x_pos), x_pos)
        self._y_labels = zip(map(self._format, y_pos), y_pos)


def metrics_tree(host):
    """
    Return tree structure of collectd's data directory.
    """
    tree = requests.get(host + "/list_tree", timeout=TIMEOUT)
    return tree


def metrics_hosts(host):
    """
    Return list of hosts watched on specified metrics server.
    """
    hosts = requests.get(host + "/list_hosts", timeout=TIMEOUT)
    return hosts


def arbitrary_metrics(host, paths, start=None, end=None):
    """
    Return metrics for given set of full paths.
    :param paths: list of paths for which metrics are shown. Example:
        ["host1/plugin1/type1.rrd", "host1/plugin1/type2.rrd"]
{'fraction_non_alphanumeric': 0.0908130939809926, 'fraction_numerical': 0.008146025041484387, 'mean_word_length': 2.1556401713469775, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7528175', 'n_tokens_mistral': 2057, 'n_tokens_neox': 2007, 'n_words': 527}