<commit_before># -*- coding: utf-8 -*-

import logging
import influxdb
import sys



import requests


class InfluxDBWriter(object):

    def __init__(self, host, port, user, password, dbname, retention_policy=None, time_precision=None):
        """
        Initialize InfluxDB writer
        """
        self.retention_policy = retention_policy
        self.time_precision = time_precision
        self.dbname = dbname
        self.headers = {
            'Content-type': 'application/octet-stream',
            'Accept': 'text/plain'
        }
        self.params = { 'db': self.dbname}
        if time_precision is not None:
            self.params['precision'] = time_precision
        if retention_policy is not None:
            self.params['rp'] = retention_policy
        logging.info("Connecting to InfluxDB at %s:%s...", host, port)
        self.client = influxdb.InfluxDBClient(host, port, user, password, dbname)
        logging.info("Creating database %s if not exists", dbname)

    def create_database(self, dbname):
        """ Initialize the given database """
        self.client.create_database(dbname)


    def write(self, msg, params=None, expected_response_code=204):
        """
        Write messages to InfluxDB database.
        Expects messages in line protocol format.
        See https://influxdb.com/docs/v0.9/write_protocols/line.html
        """
        try:
            self.client.request(url='write',
                                method='POST',
                                params=self.params,
                                data="\n".join(m.encode('utf-8') for m in msg),
                                expected_response_code=expected_response_code,
                                headers=self.headers
                                )
        except Exception, e:
            logging.warning("Cannot write data points: %s", e)
            return False
        return True

    def write_custom(self, msg, params=None, expected_response_code=204):
        """
        Write messages to InfluxDB database.
        Expects messages in line protocol format.
        See https://influxdb.com/docs/v0.9/write_protocols/line.html
        """
        try:
            self.client.request(url='write',
                                method='POST',
                                params=self.params,
                                data="\n".join(m.encode('utf-8') for m in msg),
                                expected_response_code=expected_response_code,
                                headers=self.headers
                                )
        except Exception, e:
            logging.warning("Cannot write data points: %s", e)
            return False
        return True

    def write08(self):
        """
        TODO: Write in InfluxDB legacy 08 format:
         data = [
                    {
                        "name": "cpu_load_short",<commit_msg>Remove unused write_custom method<commit_after># -*- coding: utf-8 -*-

import logging
import influxdb
import sys



import requests


class InfluxDBWriter(object):

    def __init__(self, host, port, user, password, dbname, retention_policy=None, time_precision=None):
        """
        Initialize InfluxDB writer
        """
        self.retention_policy = retention_policy
        self.time_precision = time_precision
        self.dbname = dbname
        self.headers = {
            'Content-type': 'application/octet-stream',
            'Accept': 'text/plain'
        }
        self.params = { 'db': self.dbname}
        if time_precision is not None:
            self.params['precision'] = time_precision
        if retention_policy is not None:
            self.params['rp'] = retention_policy
        logging.info("Connecting to InfluxDB at %s:%s...", host, port)
        self.client = influxdb.InfluxDBClient(host, port, user, password, dbname)
        logging.info("Creating database %s if not exists", dbname)

    def create_database(self, dbname):
        """ Initialize the given database """
        self.client.create_database(dbname)


    def write(self, msg, params=None, expected_response_code=204):
        """
        Write messages to InfluxDB database.
        Expects messages in line protocol format.
        See https://influxdb.com/docs/v0.9/write_protocols/line.html
        """
        try:
            self.client.request(url='write',
                                method='POST',
                                params=self.params,
                                data="\n".join(m.encode('utf-8') for m in msg),
                                expected_response_code=expected_response_code,
                                headers=self.headers
                                )
        except Exception, e:
            logging.warning("Cannot write data points: %s", e)
            return False
        return True

    def write08(self):
        """
        TODO: Write in InfluxDB legacy 08 format:
         data = [
                    {
                        "name": "cpu_load_short",
{'fraction_non_alphanumeric': 0.09731876861966236, 'fraction_numerical': 0.005561072492552135, 'mean_word_length': 1.647739221871714, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 3, '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': '1973011', 'n_tokens_mistral': 1362, 'n_tokens_neox': 1272, 'n_words': 314}