<commit_before>import json
import maps
import os
import traceback

from requests import delete
from requests import get
from requests import post
from requests import put

from tendrl.monitoring_integration.grafana import constants
from tendrl.monitoring_integration.grafana import exceptions
from tendrl.monitoring_integration.grafana import utils


def _post_dashboard(dashboard_json, authorization_key=None):
    config = maps.NamedDict(NS.config.data)
    if utils.port_open(config.grafana_port, config.grafana_host):
        upload_str = json.dumps(dashboard_json)
        if authorization_key:
            new_header = constants.HEADERS
            new_header["Authorization"] = "Bearer " + str(authorization_key)
            response = post("http://{}/grafana/api/dashboards/"
                            "db".format(config.grafana_host),
                            headers=new_header,
                            data=upload_str)
        else:
            response = post("http://{}/grafana/api/dashboards/"
                            "db".format(config.grafana_host),
                            headers=constants.HEADERS,
                            auth=config.credentials,
                            data=upload_str)
        return response
    else:
        raise exceptions.ConnectionFailedException


def get_dashboard(dashboard_name):
    config = maps.NamedDict(NS.config.data)
    if utils.port_open(config.grafana_port, config.grafana_host):
        resp = get("http://{}/grafana/api/dashboards/"
                   "db/{}".format(config.grafana_host,
                                  dashboard_name),
                   auth=config.credentials)
    else:
        raise exceptions.ConnectionFailedException
    return resp.json()


def delete_dashboard(dashboard_name):
    config = maps.NamedDict(NS.config.data)
    if utils.port_open(config.grafana_port, config.grafana_host):
        resp = delete(
            "http://{}/grafana/api/dashboards/db/{}".format(
                config.grafana_host,
                dashboard_name
            ),
            auth=config.credentials
        )
    else:
        raise exceptions.ConnectionFailedException
    return resp.json()


def get_all_dashboards():
    config = maps.NamedDict(NS.config.data)
    if utils.port_open(config.grafana_port, config.grafana_host):
        resp = get("http://{}/grafana/api/search/"
                   .format(config.grafana_host),
                   auth=config.credentials)
    else:
        raise exceptions.ConnectionFailedException
    return resp.json()


def set_home_dashboard(dash_id):
    config = maps.NamedDict(NS.config.data)
    if utils.port_open(config.grafana_port, config.grafana_host):
        resp = put('http://{}/grafana/api/org/'
                   'preferences'.format(config.grafana_host),
                   headers=constants.HEADERS,
                   auth=config.credentials,
                   data=json.dumps({"name": constants.MAIN_ORG,
                                    "theme": "light",
                                    "homeDashboardId": dash_id}))
    else:
        raise exceptions.ConnectionFailedException
    return resp


def create_dashboard(dashboard_name, dashboard_dir=None):
    if not dashboard_dir:
        dashboard_dir = "/etc/tendrl/monitoring-integration/grafana/dashboards"
    dashboard_path = os.path.join(dashboard_dir,
                                  "{}.json".format(dashboard_name))

    if os.path.exists(dashboard_path):

        dashboard_data = utils.fread(dashboard_path)

        try:
            dashboard_json = json.loads(dashboard_data)
            response = _post_dashboard(dashboard_json)
            return response

        except exceptions.ConnectionFailedException:
            traceback.print_stack()
            raise exceptions.ConnectionFailedException

    else:
        raise exceptions.FileNotFoundException


def get_alert(alert_id):
    config = maps.NamedDict(NS.config.data)
    if utils.port_open(config.grafana_port, config.grafana_host):
        resp = get(
            "http://{0}/grafana/api/alerts/{2}".format(
                config["grafana_host"],
                alert_id
            ),
            auth=config["credentials"]
        )
    else:
        raise exceptions.ConnectionFailedException
    return resp<commit_msg>Utilization alerts are not working- 1656057, 1656064<commit_after>import json
import maps
import os
import traceback

from requests import delete
from requests import get
from requests import post
from requests import put

from tendrl.monitoring_integration.grafana import constants
from tendrl.monitoring_integration.grafana import exceptions
from tendrl.monitoring_integration.grafana import utils


def _post_dashboard(dashboard_json, authorization_key=None):
    config = maps.NamedDict(NS.config.data)
    if utils.port_open(config.grafana_port, config.grafana_host):
        upload_str = json.dumps(dashboard_json)
        if authorization_key:
            new_header = constants.HEADERS
            new_header["Authorization"] = "Bearer " + str(authorization_key)
            response = post("http://{}/grafana/api/dashboards/"
                            "db".format(config.grafana_host),
                            headers=new_header,
                            data=upload_str)
        else:
            response = post("http://{}/grafana/api/dashboards/"
                            "db".format(config.grafana_host),
                            headers=constants.HEADERS,
                            auth=config.credentials,
                            data=upload_str)
        return response
    else:
        raise exceptions.ConnectionFailedException


def get_dashboard(dashboard_name):
    config = maps.NamedDict(NS.config.data)
    if utils.port_open(config.grafana_port, config.grafana_host):
        resp = get("http://{}/grafana/api/dashboards/"
                   "db/{}".format(config.grafana_host,
                                  dashboard_name),
                   auth=config.credentials)
    else:
        raise exceptions.ConnectionFailedException
    return resp.json()


def delete_dashboard(dashboard_name):
    config = maps.NamedDict(NS.config.data)
    if utils.port_open(config.grafana_port, config.grafana_host):
        resp = delete(
            "http://{}/grafana/api/dashboards/db/{}".format(
                config.grafana_host,
                dashboard_name
            ),
            auth=config.credentials
        )
    else:
        raise exceptions.ConnectionFailedException
    return resp.json()


def get_all_dashboards():
    config = maps.NamedDict(NS.config.data)
    if utils.port_open(config.grafana_port, config.grafana_host):
        resp = get("http://{}/grafana/api/search/"
                   .format(config.grafana_host),
                   auth=config.credentials)
    else:
        raise exceptions.ConnectionFailedException
    return resp.json()


def set_home_dashboard(dash_id):
    config = maps.NamedDict(NS.config.data)
    if utils.port_open(config.grafana_port, config.grafana_host):
        resp = put('http://{}/grafana/api/org/'
                   'preferences'.format(config.grafana_host),
                   headers=constants.HEADERS,
                   auth=config.credentials,
                   data=json.dumps({"name": constants.MAIN_ORG,
                                    "theme": "light",
                                    "homeDashboardId": dash_id}))
    else:
        raise exceptions.ConnectionFailedException
    return resp


def create_dashboard(dashboard_name, dashboard_dir=None):
    if not dashboard_dir:
        dashboard_dir = "/etc/tendrl/monitoring-integration/grafana/dashboards"
    dashboard_path = os.path.join(dashboard_dir,
                                  "{}.json".format(dashboard_name))

    if os.path.exists(dashboard_path):

        dashboard_data = utils.fread(dashboard_path)

        try:
            dashboard_json = json.loads(dashboard_data)
            response = _post_dashboard(dashboard_json)
            return response

        except exceptions.ConnectionFailedException:
            traceback.print_stack()
            raise exceptions.ConnectionFailedException

    else:
        raise exceptions.FileNotFoundException


def get_alert(alert_id):
    config = maps.NamedDict(NS.config.data)
    if utils.port_open(config.grafana_port, config.grafana_host):
        resp = get(
            "http://{0}/grafana/api/alerts/{1}".format(
                config["grafana_host"],
                alert_id
            ),
            auth=config["credentials"]
        )
    else:
        raise exceptions.ConnectionFailedException
    return resp
{'fraction_non_alphanumeric': 0.09254793891376736, 'fraction_numerical': 0.0020668274199104374, 'mean_word_length': 2.1196275071633237, 'pattern_counts': {'":': 6, '<': 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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7656957', 'n_tokens_mistral': 2431, 'n_tokens_neox': 2407, 'n_words': 389}